def test_correct_numbers(self): valid_input_1 = [1, 2, 3] valid_input_2 = [1.0, 2.0, 3.0] valid_input_3 = [1, 2.0, 3] valid_input_4 = [] expected_output = True self.assertEqual(expected_output, functions.are_numbers(valid_input_1)) self.assertEqual(expected_output, functions.are_numbers(valid_input_2)) self.assertEqual(expected_output, functions.are_numbers(valid_input_3)) self.assertEqual(expected_output, functions.are_numbers(valid_input_4))
def test_incorrect_numbers(self): invalid_input_1 = "Almost" invalid_input_2 = ["this is so wrong."] invalid_input_3 = {0: "And also this."} invalid_input_4 = None invalid_input_5 = [1, "a", "b", "c", "d"] self.assertRaises(ValueError, lambda: functions.are_numbers(invalid_input_1)) self.assertRaises(ValueError, lambda: functions.are_numbers(invalid_input_2)) self.assertRaises(ValueError, lambda: functions.are_numbers(invalid_input_3)) self.assertRaises(ValueError, lambda: functions.are_numbers(invalid_input_4)) self.assertRaises(ValueError, lambda: functions.are_numbers(invalid_input_5))
def __init__(self, x1, y1, x2, y2, cell_id, x, y): if functions.are_numbers([x1, x2, y1, y2, x, y]): self.x1 = x1 self.x2 = x2 self.y1 = y1 self.y2 = y2 self.cell_id = cell_id self.x = x self.y = y self.status = None
def process_click(self, x, y): if functions.are_numbers([x, y]): clicked_cell = self.playing_field.get_clicked_cell(x, y) if clicked_cell.status == CellStatus.Empty: playing_player = self._get_playing_player() clicked_cell = self.playing_field.change_cell_type( clicked_cell.cell_id, playing_player.shape) playing_player.play(clicked_cell) return clicked_cell return None
def _check_column(playing_field, columns, column): if functions.are_numbers([columns, column]): cells = [] first_cell = playing_field.get_cell_by_x_y(column, 0) for y in range(int(columns)): cell = playing_field.get_cell_by_x_y(column, y) if cell.status != first_cell.status or cell.status == CellStatus.Empty: return [] cells.append(cell) return cells
def _check_line(playing_field, columns, line): if functions.are_numbers([columns, line]): cells = [] first_cell = playing_field.get_cell_by_x_y(0, line) for x in range(int(columns)): cell = playing_field.get_cell_by_x_y(x, line) if cell.status != first_cell.status or cell.status == CellStatus.Empty: return [] cells.append(cell) return cells
def draw_circle(canvas, x1, y1, x2, y2): if functions.are_numbers([x1, y1, x2, y2]): x1 += variables.CELL_PADDING y1 += variables.CELL_PADDING x2 -= variables.CELL_PADDING y2 -= variables.CELL_PADDING canvas.create_oval(x1, y1, x2, y2, fill=variables.FRAMES_BACKGROUND, outline=variables.CIRCLE_COLOR, width=variables.SHAPE_LINE_WIDTH) return True
def draw_cross(canvas, x1, y1, x2, y2): if functions.are_numbers([x1, y1, x2, y2]): x1 += variables.CELL_PADDING y1 += variables.CELL_PADDING x2 -= variables.CELL_PADDING y2 -= variables.CELL_PADDING canvas.create_line(x1, y1, x2, y2, fill=variables.CROSS_COLOR, width=variables.SHAPE_LINE_WIDTH) # LT to RB canvas.create_line(x1, y2, x2, y1, fill=variables.CROSS_COLOR, width=variables.SHAPE_LINE_WIDTH) # LB to RT return True
def is_click_inside(self, x, y): if functions.are_numbers([x, y]): return self.x1 <= x <= self.x2 and self.y1 <= y <= self.y2
def _detect_click(self, event): if functions.are_numbers([ event.x, event.y ]) and not isinstance(self._get_playing_player(), MultiPlayerOpponent): return self.process_click(event.x, event.y)
def __init__(self, master, x=0, y=0, width=100, height=100): if functions.are_numbers([x, y, width, height]): super().__init__(master=master, width=width, height=height) self.place(x=x, y=y)
def get_clicked_cell(self, x, y): if functions.are_numbers([x, y]): for one_cell in self.field_cells.values(): if one_cell.is_click_inside(x, y): return one_cell return None
def get_cell_by_x_y(self, x, y): if functions.are_numbers([x, y]): for cell in self.field_cells.values(): if cell.x == x and cell.y == y: return cell return None