def test_incorrect_number(self): invalid_input_1 = "Almost" invalid_input_2 = ["this is so wrong."] invalid_input_3 = {0: "And also this."} invalid_input_4 = None self.assertRaises(ValueError, lambda: functions.is_number(invalid_input_1)) self.assertRaises(ValueError, lambda: functions.is_number(invalid_input_2)) self.assertRaises(ValueError, lambda: functions.is_number(invalid_input_3)) self.assertRaises(ValueError, lambda: functions.is_number(invalid_input_4))
def _process_change_field(self, num): if functions.is_string(num) and functions.is_number(int(num)): opponent_name = self.player.opponent.name if int(num) == ResponseCode.Ok.value: # opponent accept print("changed field size") self.player.board.gui.set_field_size(self.new_field_size) num = self.player.board.gui.playing_field.restart() self.player.board.playing_field.initialize_field(num) elif int( num ) == ResponseCode.Not_Ok.value: # opponent is not cool with a field change print("didn't change size") true_size = int( math.sqrt( self.player.board.playing_field.get_number_of_cells())) self.player.board.gui.set_field_size(true_size) messenger.SendToUser.show_info( "Message", opponent_name + " declined your request for changing field size to " + str(self.new_field_size)) elif 3 <= int(num) <= 5: # field size can only be from 3 to 5 agree = messenger.SendToUser.ask_for_field_change( opponent_name + " want to change field size to " + str(num) + ".\nDo you agree?") if agree: print("I agree to change size") mailman.ChangeFieldSizeRequest( self.sock, ResponseCode.Ok.value).send() self.new_field_size = int(num) else: print("I disagree to change size") mailman.ChangeFieldSizeRequest( self.sock, ResponseCode.Not_Ok.value).send()
def _add_label(self, text, color="black", font=0): if functions.is_number(font) and functions.is_string( text) and functions.is_string(color): return tkinter.Label(self, text=text, font=(variables.TEXT_FONT, variables.LABEL_TEXT_SIZE + font), fg=color)
def draw_info_cross(canvas, parent, shrink=0): if functions.is_number(shrink): tkinter.Tk.update(parent) width = height = canvas.winfo_reqheight() canvas.delete(["all"]) ShapeDrawer.draw_cross(canvas, shrink, shrink, width - shrink, height - shrink) return True
def _win(cells, columns): if isinstance(cells, list) and functions.is_number(columns): if len(cells) > 0: first_one = cells[0] for one_cell in cells: if one_cell.status != first_one.status or one_cell.status == CellStatus.Empty: return False return len(cells) == columns raise ValueError(str(cells) + " is not a list!")
def _check_decreasing_diagonal(playing_field, columns): if functions.is_number(columns): cells = [] first_cell = playing_field.get_cell_by_id(0) for x in range(int(columns)): cell = playing_field.get_cell_by_x_y(x, x) if cell.status != first_cell.status or cell.status == CellStatus.Empty: return [] cells.append(cell) return cells
def restart(self): try: num = int(self.parent.right_frame.field_size.get()) if functions.is_number(num) and 3 <= num <= 5: self._draw_grid(num) return num except ValueError: self.parent.parent.handle_wrong_input() self.parent.right_frame.field_size.set( variables.DEFAULT_FIELD_SIZE) return self.restart()
def _process_change_size(self, client_sock, msg_rec): if functions.is_string(msg_rec) and functions.is_number(int(msg_rec)): print("Trying to change field size") other_sock = self._get_other_sock(client_sock) if int(msg_rec) == ResponseCode.Ok.value: mailman.ChangeFieldSizeResponse(other_sock, str(ResponseCode.Ok.value)).send() mailman.ChangeFieldSizeResponse(client_sock, str(ResponseCode.Ok.value)).send() elif int(msg_rec) == ResponseCode.Not_Ok.value: mailman.ChangeFieldSizeResponse(other_sock, str(ResponseCode.Not_Ok.value)).send() else: mailman.ChangeFieldSizeResponse(other_sock, msg_rec).send()
def change_cell_type(self, previous_id, new_type): if functions.is_number(previous_id) and PlayingField._correct_input( new_type): old_cell = self.field_cells[previous_id] x = old_cell.x y = old_cell.y # we are not interested if new_type is typeof EmptyCell new_cell = cell.CrossCell(old_cell.x1, old_cell.y1, old_cell.x2, old_cell.y2, previous_id, x, y) if new_type == variables.CellStatus.Circle.value: new_cell = cell.CircleCell(old_cell.x1, old_cell.y1, old_cell.x2, old_cell.y2, previous_id, x, y) self.field_cells[previous_id] = new_cell return new_cell return False
def _draw_grid(self, num): self.delete("all") # clear canvas if functions.is_number(num): # we already know that height and width of the field is the same self._draw_outer_lines() for x in range(num): padding = x * (self.height / num) self.create_line( padding, 0, padding, self.height, fill=variables.BACKGROUND_COLOR) # vertical line self.create_line( 0, padding, self.width, padding, fill=variables.BACKGROUND_COLOR) # horizontal line
def initialize_field(self, num): """ Initialize/reset the playing field. :param num: number of columns/lines (default 3) :return: """ if functions.is_number(num): self.field_cells = {} id_counter = 0 padding = (variables.PLAYING_FIELD_WIDTH / num) for y in range(num): for x in range(num): # height and width are the same x1 = x * padding y1 = y * padding x2 = x1 + padding y2 = y1 + padding self.field_cells[id_counter] = cell.EmptyCell( x1, y1, x2, y2, id_counter, x, y) # cells are empty id_counter += 1
def _calculate_center_of_the_screen(self, width, height): if functions.is_number(width) and functions.is_number(height): position_right = int(width / 2 - self.width / 2) position_down = int(height / 2 - self.height / 2 - 50) return [position_right, position_down] return False
def _process_click(self, msg): if functions.is_number(int(msg)): print("My opponent clicked cell with id", msg) cell_id = int(msg) self.player.opponent.click(cell_id)
def send_clicked_cell(self, cell_id): if functions.is_number(cell_id): print("I clicked cell number", cell_id) mailman.ClickRequest(self.sock, str(cell_id)).send()
def send_change_field_request(self): num = self.player.board.gui.get_field_size() if functions.is_number(num): print("I want to change field size to " + str(num)) self.new_field_size = num mailman.ChangeFieldSizeRequest(self.sock, str(num)).send()
def _process_click(self, sock, msg): if functions.is_number(int(msg)): print("player", self.clients[sock], "clicked cell with id", msg) mailman.ClickResponse(self._get_other_sock(sock), msg).send()
def get_cell_by_id(self, id_cell): if functions.is_number(id_cell) and id_cell in self.field_cells: return self.field_cells[id_cell] return None
def _add_editbox(self, text, width=5): if functions.is_number(width) and functions.is_string(text.get()): return tkinter.Entry(self, text=text, justify="center", width=width)
def calculate_center_of_the_screen(width, height): if functions.is_number(width) and functions.is_number(height): position_right = int(width / 2 - variables.BOARD_WIDTH / 2) position_down = int(height / 2 - variables.BOARD_HEIGHT / 2 - 50) return [position_right, position_down] return False
def set_field_size(self, val): if functions.is_number(val): self.right_frame.field_size.set(str(val)) return val
def test_correct_number(self): valid_input_1 = 1 valid_input_2 = 1.0 expected_output = True self.assertEqual(expected_output, functions.is_number(valid_input_1)) self.assertEqual(expected_output, functions.is_number(valid_input_2))
def click(self, cell_id): if functions.is_number(cell_id) and self.click_manager is not None: cell = self.board.playing_field.get_cell_by_id(cell_id) self.click_manager.click(cell.x1 + 5, cell.y1 + 5)