def _cell_display(self) -> cartesian.RectangleState: ''' Creates a bunch of rectangles to be displayed as game cells on the canvas. ''' gameboard_rectangles = cartesian.RectangleState() for row_index in range(self._game.no_of_rows()): for col_index in range(self._game.no_of_col()): # Two opposite corner points of the rectangle we are about # to create. p1 = cartesian.Point( (1 / self._game.no_of_col()) * col_index * self._gameboard_display.winfo_width(), (1 / self._game.no_of_rows()) * row_index * self._gameboard_display.winfo_height(), self._gameboard_display.winfo_width(), self._gameboard_display.winfo_height()) p2 = cartesian.Point( (1 / self._game.no_of_col()) * (col_index + 1) * self._gameboard_display.winfo_width(), (1 / self._game.no_of_rows()) * (row_index + 1) * self._gameboard_display.winfo_height(), self._gameboard_display.winfo_width(), self._gameboard_display.winfo_height()) gameboard_rectangles.append_rect(cartesian.Rectangle(p1, p2)) return gameboard_rectangles
def test_point_distance(self): """ Ensures the distance is correctly calculated """ self.assertEqual( cartesian.Point(3, 4).distance(cartesian.Point(0, 0)), 5) self.assertEqual( cartesian.Point(1, 2).distance(cartesian.Point(4, 3)), pow(10, 0.5))
def test_point_distance_default(self): """ Ensures the distance is correctly calculated between the origin """ # Real distance is 1.41... (the square root of 2) self.assertEqual(cartesian.Point(1, 1).distance(), pow(2, 0.5)) self.assertEqual(cartesian.Point(-1, 1).distance(), pow(2, 0.5)) self.assertEqual(cartesian.Point(1, -1).distance(), pow(2, 0.5)) self.assertEqual(cartesian.Point(-1, -1).distance(), pow(2, 0.5))
def test_point_quadrant_none(self): """ Ensures that a point lying on an axis has no quadrant """ points = [ cartesian.Point(1, 0), cartesian.Point(0, 1), cartesian.Point(-1, 0), cartesian.Point(0, -1), cartesian.Point(0, 0) ] for point in points: self.assertEqual(point.quadrant(), None)
def _on_mouse_click(self, event: tkinter.Event): ''' The response when the user clicks on the gameboard display. ''' click_point = cartesian.Point(event.x, event.y, self._gameboard_display.winfo_width(), self._gameboard_display.winfo_height()) for rect in self._gameboard_rectangles.all_rects(): if rect.contains(click_point): grid_coord = cartesian.index_to_grid_coord(self._gameboard_rectangles.all_rects().index(rect), \ self._game.no_of_rows(), self._game.no_of_col()) try: self._game.player_take_turn(grid_coord[1], grid_coord[0]) except game_classes.InvalidMoveError: # If the move fails, do not change the # state of the rectangles and circles. self.error_msg.set("Invalid Move") else: self._gameboard_discs = self._disc_display( ) # Update the displayed discs. self.error_msg.set("") self._game.next_player_turn() self._refresh_game_window(event)
def test_point_midpoint_default(self): """ Ensures the midpoint is correctly calculated between the origin """ self.assertEqual( cartesian.Point(1, 1).midpoint(), cartesian.Point(0.5, 0.5)) self.assertEqual( cartesian.Point(-1, 1).midpoint(), cartesian.Point(-0.5, 0.5)) self.assertEqual( cartesian.Point(1, -1).midpoint(), cartesian.Point(0.5, -0.5)) self.assertEqual( cartesian.Point(-1, -1).midpoint(), cartesian.Point(-0.5, -0.5))
def test_point_distance_none(self): """ Ensures that a lack of distance returns zero """ self.assertEqual( cartesian.Point(1, 1).distance(cartesian.Point(1, 1)), 0) self.assertEqual( cartesian.Point(-1, 1).distance(cartesian.Point(-1, 1)), 0) self.assertEqual( cartesian.Point(1, -1).distance(cartesian.Point(1, -1)), 0) self.assertEqual( cartesian.Point(-1, -1).distance(cartesian.Point(-1, -1)), 0)
def test_point_negative(self): """ Ensures the distance is correctly calculated with negatives """ self.assertEqual( cartesian.Point(-1, 2).distance(cartesian.Point(4, 3)), pow(26, 0.5)) self.assertEqual( cartesian.Point(1, -2).distance(cartesian.Point(4, 3)), pow(34, 0.5)) self.assertEqual( cartesian.Point(-1, -2).distance(cartesian.Point(4, 3)), pow(50, 0.5))
def test_point_quadrant_first(self): """ Ensures that the first quadrant is correctly identified """ self.assertEqual(cartesian.Point(1, 1).quadrant(), 1)
def test_point_eq(self): """ Ensures the __eq__ dunder method works correctly """ self.assertEqual(cartesian.Point(1, 1), cartesian.Point(1, 1)) self.assertEqual(cartesian.Point(-1, 1), cartesian.Point(-1, 1)) self.assertEqual(cartesian.Point(1, -1), cartesian.Point(1, -1)) self.assertEqual(cartesian.Point(-1, -1), cartesian.Point(-1, -1)) self.assertNotEqual(cartesian.Point(1, 1), cartesian.Point(-1, -1)) self.assertNotEqual(cartesian.Point(-1, 1), cartesian.Point(1, -1)) self.assertNotEqual(cartesian.Point(1, -1), cartesian.Point(-1, 1)) self.assertNotEqual(cartesian.Point(-1, -1), cartesian.Point(1, 1))
def test_create_point(self): """ Ensures that a point can be created """ point = cartesian.Point() self.assertIsNotNone(point)
def test_point_quadrant_fourth(self): """ Ensures that the fourth quadrant is correctly identified """ self.assertEqual(cartesian.Point(1, -1).quadrant(), 4)
def test_create_point_with_coords(self): """ Ensures providied coordinates are correctly assigned """ point = cartesian.Point(-1, 2) self.assertEqual(point.x, -1) self.assertEqual(point.y, 2)
def test_point_quadrant_third(self): """ Ensures that the third quadrant is correctly identified """ self.assertEqual(cartesian.Point(-1, -1).quadrant(), 3)
def test_point_quadrant_second(self): """ Ensures that the second quadrant is correctly identified """ self.assertEqual(cartesian.Point(-1, 1).quadrant(), 2)
def test_create_point_default_coords(self): """ Ensures that the default coordinates are (0, 0) """ point = cartesian.Point() self.assertEqual(point.x, 0) self.assertEqual(point.y, 0)
def test_point_repr(self): """ Ensures that repr() returns the correct result """ point = cartesian.Point(-1, 2) self.assertEqual(point.__repr__(), "Point(-1, 2)") self.assertEqual(repr(point), "Point(-1, 2)")
def test_point_str(self): """ Ensures that str() returns the correct result """ point = cartesian.Point(-1, 2) self.assertEqual(point.__str__(), "Point at x=-1 and y=2") self.assertEqual(str(point), "Point at x=-1 and y=2")