示例#1
0
    def test_is_vertical_wall_undo_correct(self):
        board = Board(BoardTestCase.std_dim)
        self.assertRaises(ValueError, board.place_wall, False,
                          Coordinate(1, 0))  # Bad coord except check

        for i in range(board.dimensions):
            for j in range(board.dimensions):
                coord = Coordinate(i, j)

                if not board._compute_static_valid_wall(False, coord):
                    continue

                left1 = board.board[coord.row][coord.column - 1]
                left2 = board.board[coord.row + 1][coord.column - 1]
                right1 = board.board[coord.row][coord.column]
                right2 = board.board[coord.row + 1][coord.column]

                neighbors_left1 = copy.deepcopy(left1.neighbors)
                neighbors_left2 = copy.deepcopy(left2.neighbors)
                neighbors_right1 = copy.deepcopy(right1.neighbors)
                neighbors_right2 = copy.deepcopy(right2.neighbors)

                board.place_wall(False, coord)
                board.undo_wall(False, coord)

                self.assertTrue(
                    left1.neighbors[Ti.WEST] == neighbors_left1[Ti.WEST])
                self.assertTrue(
                    left2.neighbors[Ti.WEST] == neighbors_left2[Ti.WEST])
                self.assertTrue(
                    right1.neighbors[Ti.EAST] == neighbors_right1[Ti.EAST])
                self.assertTrue(
                    right2.neighbors[Ti.EAST] == neighbors_right2[Ti.EAST])
示例#2
0
 def _compute_player_homes(self):
     return [
         Coordinate(0, self.dimensions // 2),  # P1 top
         Coordinate(self.dimensions - 1, self.dimensions // 2),  # P2 bottom
         Coordinate(self.dimensions // 2, 0),  # P3 left
         Coordinate(self.dimensions // 2, self.dimensions - 1)  # P4 right
     ]
示例#3
0
    def test_board_equality(self):
        board1 = Board(10)
        board2 = Board(10)

        self.assertTrue(board1 == board2)

        board2._dimensions = 3
        self.assertFalse(board1 == board2)
        board2._dimensions = board1._dimensions
        self.assertTrue(board1 == board2)

        board2._homes[0] = None
        self.assertFalse(board1 == board2)
        board2._homes[0] = board1._homes[0]
        self.assertTrue(board1 == board2)

        board2._players[0] = None
        self.assertFalse(board1 == board2)
        board2._players[0] = board1._players[0]
        self.assertTrue(board1 == board2)

        board2.place_wall(True, Coordinate(5, 5))
        self.assertFalse(board1 == board2)
        board2.undo_wall(True, Coordinate(5, 5))
        self.assertTrue(board1 == board2)
示例#4
0
    def test_is_horizontal_wall_place_correct(self):
        board = Board(BoardTestCase.std_dim)
        self.assertRaises(ValueError, board.place_wall, True,
                          Coordinate(0, 2))  # Bad coord except check

        for i in range(board.dimensions):
            for j in range(board.dimensions):
                coord = Coordinate(i, j)

                if not board._compute_static_valid_wall(True, coord):
                    continue

                self.assertTrue(board._compute_dynamic_valid_wall(True, coord))

                top1 = board.board[coord.row - 1][coord.column]
                top2 = board.board[coord.row - 1][coord.column + 1]
                bottom1 = board.board[coord.row][coord.column]
                bottom2 = board.board[coord.row][coord.column + 1]

                neighbors_top1 = copy.deepcopy(top1.neighbors)
                neighbors_top2 = copy.deepcopy(top2.neighbors)
                neighbors_bottom1 = copy.deepcopy(bottom1.neighbors)
                neighbors_bottom2 = copy.deepcopy(bottom2.neighbors)

                board.place_wall(True, coord)

                self.assertTrue(top1.neighbors[Ti.SOUTH] is None)
                self.assertTrue(
                    top1.neighbors[Ti.NORTH] == neighbors_top1[Ti.NORTH])
                self.assertTrue(
                    top1.neighbors[Ti.EAST] == neighbors_top1[Ti.EAST])
                self.assertTrue(
                    top1.neighbors[Ti.WEST] == neighbors_top1[Ti.WEST])

                self.assertTrue(top2.neighbors[Ti.SOUTH] is None)
                self.assertTrue(
                    top2.neighbors[Ti.NORTH] == neighbors_top2[Ti.NORTH])
                self.assertTrue(
                    top2.neighbors[Ti.EAST] == neighbors_top2[Ti.EAST])
                self.assertTrue(
                    top2.neighbors[Ti.WEST] == neighbors_top2[Ti.WEST])

                self.assertTrue(bottom1.neighbors[Ti.NORTH] is None)
                self.assertTrue(
                    bottom1.neighbors[Ti.SOUTH] == neighbors_bottom1[Ti.SOUTH])
                self.assertTrue(
                    bottom1.neighbors[Ti.EAST] == neighbors_bottom1[Ti.EAST])
                self.assertTrue(
                    bottom1.neighbors[Ti.WEST] == neighbors_bottom1[Ti.WEST])

                self.assertTrue(bottom2.neighbors[Ti.NORTH] is None)
                self.assertTrue(
                    bottom2.neighbors[Ti.SOUTH] == neighbors_bottom2[Ti.SOUTH])
                self.assertTrue(
                    bottom2.neighbors[Ti.EAST] == neighbors_bottom2[Ti.EAST])
                self.assertTrue(
                    bottom2.neighbors[Ti.WEST] == neighbors_bottom2[Ti.WEST])

                board.undo_wall(True, coord)
示例#5
0
 def test_is_players_home_correct(self):
     board = Board(BoardTestCase.std_dim)
     homes = [
         Coordinate(0, BoardTestCase.std_dim // 2),
         Coordinate(BoardTestCase.std_dim - 1, BoardTestCase.std_dim // 2),
         Coordinate(BoardTestCase.std_dim // 2, 0),
         Coordinate(BoardTestCase.std_dim // 2, BoardTestCase.std_dim - 1)
     ]
     self.assertTrue(homes == board.players)
示例#6
0
 def test_is_eq_correct(self):
     neighbors1 = [Coordinate(1, 2), None, None, None]
     tile1 = Tile(coordinate=Coordinate(1, 2))
     tile2 = Tile(coordinate=Coordinate(1, 2))
     tile3 = Tile(coordinate=Coordinate(1, 3))
     tile4 = Tile(coordinate=Coordinate(1, 2), neighbors=neighbors1)
     self.assertTrue(tile1 == tile2)
     self.assertFalse(tile1 == tile3)
     self.assertFalse(tile3 == tile4)
示例#7
0
 def test_is_homes_correct(self):
     board = Board(BoardTestCase.std_dim)
     homes = board._compute_player_homes()
     check = [
         Coordinate(0, BoardTestCase.std_dim // 2),
         Coordinate(BoardTestCase.std_dim - 1, BoardTestCase.std_dim // 2),
         Coordinate(BoardTestCase.std_dim // 2, 0),
         Coordinate(BoardTestCase.std_dim // 2, BoardTestCase.std_dim - 1)
     ]
     self.assertTrue(homes == check)
示例#8
0
    def test_is_compute_coordinate_diff_direction_correct(self):
        std_coord = Coordinate(5, 5)

        self.assertRaises(ValueError, Moves._compute_coordinate_diff_direction,
                          std_coord, Coordinate(5, 7))
        self.assertRaises(ValueError, Moves._compute_coordinate_diff_direction,
                          std_coord, Coordinate(7, 5))
        self.assertRaises(ValueError, Moves._compute_coordinate_diff_direction,
                          Coordinate(5, 7), std_coord)
        self.assertRaises(ValueError, Moves._compute_coordinate_diff_direction,
                          Coordinate(7, 5), std_coord)

        north = Moves._compute_coordinate_diff_direction(
            std_coord, Coordinate(4, 5))
        south = Moves._compute_coordinate_diff_direction(
            std_coord, Coordinate(6, 5))
        east = Moves._compute_coordinate_diff_direction(
            std_coord, Coordinate(5, 4))
        west = Moves._compute_coordinate_diff_direction(
            std_coord, Coordinate(5, 6))

        self.assertTrue(north == Ti.NORTH)
        self.assertTrue(south == Ti.SOUTH)
        self.assertTrue(east == Ti.EAST)
        self.assertTrue(west == Ti.WEST)
示例#9
0
    def test_is_eq_correct(self):
        # Test trivial eq
        move1 = PlayerMove(Coordinate(5, 5), 1, True)
        move2 = PlayerMove(Coordinate(5, 5), 1, True)
        self.assertTrue(move1 == move2)

        # Test walls not equal
        move3 = PlayerMove(Coordinate(5, 5), 1, False)
        move4 = PlayerMove(Coordinate(5, 5), 1)
        self.assertFalse(move1 == move3)
        self.assertFalse(move1 == move4)
        self.assertFalse(move3 == move4)

        # Test coordinates not equal
        move5 = PlayerMove(Coordinate(5, 6), 1, True)
        move6 = PlayerMove(Coordinate(6, 5), 1, True)
        move7 = PlayerMove(Coordinate(4, 4), 1, True)
        self.assertFalse(move1 == move5)
        self.assertFalse(move1 == move6)
        self.assertFalse(move1 == move7)

        # Test pid not equal
        move8 = PlayerMove(Coordinate(5, 5), 2, True)
        self.assertFalse(move1 == move8)

        # Test all not equal
        move9 = PlayerMove(Coordinate(4, 4), 2)
        self.assertFalse(move1 == move9)
示例#10
0
 def __init_board(self):
     for i in range(0, self.dimensions):
         board_lst = []
         for j in range(0, self.dimensions):
             tile = self.__init_tile(Coordinate(i, j))
             board_lst.append(tile)
         self._board.append(board_lst)
示例#11
0
 def test_is_tile_coord_correct(self):
     board = Board(BoardTestCase.std_dim)
     coord = Coordinate(1, 2)
     tile12 = board.tile_at_coord(1, 2)
     self.assertTrue(isinstance(tile12, Tile))
     self.assertTrue(coord.row == tile12.coordinate.row
                     and coord.column == tile12.coordinate.column)
示例#12
0
    def __init_tile(self, coord):
        tile = Tile(coord)
        tile.init_draw(self.dimensions)

        row = coord.row
        col = coord.column

        if row - 1 >= 0:
            Tile.add_neighbor(tile, NORTH, Coordinate(row - 1, col))
        if row + 1 < self._dimensions:
            Tile.add_neighbor(tile, SOUTH, Coordinate(row + 1, col))
        if col - 1 >= 0:
            Tile.add_neighbor(tile, EAST, Coordinate(row, col - 1))
        if col + 1 < self._dimensions:
            Tile.add_neighbor(tile, WEST, Coordinate(row, col + 1))

        return tile
示例#13
0
    def _compute_player_goals(self):
        goal0 = [
            Coordinate(self.homes[0].row, row_col)
            for row_col in range(0, self.dimensions)
        ]
        goal1 = [
            Coordinate(self.homes[1].row, row_col)
            for row_col in range(0, self.dimensions)
        ]
        goal2 = [
            Coordinate(row_col, self.homes[2].column)
            for row_col in range(0, self.dimensions)
        ]
        goal3 = [
            Coordinate(row_col, self.homes[3].column)
            for row_col in range(0, self.dimensions)
        ]

        return [goal0, goal1, goal2, goal3]
示例#14
0
    def test_pid_setter(self):
        move = PlayerMove(Coordinate(5, 5))
        self.assertTrue(move.player_id == -1)

        move.player_id = 0
        self.assertTrue(move.player_id == -1)
        move.player_id = 1
        self.assertTrue(move.player_id == 1)
        move.player_id = 4
        self.assertTrue(move.player_id == 4)
        move.player_id = 5
        self.assertTrue(move.player_id == 4)
示例#15
0
    def compute_all_valid_vertical_walls(self):
        valid_walls = []
        for i in range(0, self.dimensions):
            for j in range(0, self.dimensions):
                coord = Coordinate(i, j)

                if self._compute_static_valid_wall(
                        False, coord) and self._compute_dynamic_valid_wall(
                            False, coord):
                    valid_walls.append(coord)

        return valid_walls
示例#16
0
 def test_is_remove_neighbors_correct():
     coord = Coordinate(1, 2)
     neighbors1 = [coord, None, None, None]
     tile1 = Tile(coordinate=coord, neighbors=neighbors1)
     Tile.remove_neighbor(tile1, 0)
示例#17
0
    def test_is_init_params_correct(self):
        player1 = Player(1, 10,
                         [Coordinate(0, 5), Coordinate(9, 5)],
                         [Coordinate(9, 4), Coordinate(0, 4)])

        self.assertTrue(player1.pid == 1)
        self.assertTrue(player1.walls == 10)
        self.assertTrue(player1.location == Coordinate(0, 5))
        self.assertTrue(player1.goal == Coordinate(9, 4))

        player3 = Player(3, 10, [
            Coordinate(0, 5),
            Coordinate(9, 5),
            Coordinate(5, 0),
            Coordinate(5, 9)
        ], [
            Coordinate(9, 4),
            Coordinate(0, 4),
            Coordinate(4, 9),
            Coordinate(4, 0)
        ])

        self.assertTrue(player3.pid == 3)
        self.assertTrue(player3.walls == 10)
        self.assertTrue(player3.location == Coordinate(5, 0))
        self.assertTrue(player3.goal == Coordinate(4, 9))

        player4 = Player(4, 10, [
            Coordinate(0, 5),
            Coordinate(9, 5),
            Coordinate(5, 0),
            Coordinate(5, 9)
        ], [
            Coordinate(9, 4),
            Coordinate(0, 4),
            Coordinate(4, 9), [Coordinate(4, 0),
                               Coordinate(6, 0)]
        ])

        self.assertTrue(player4.pid == 4)
        self.assertTrue(player4.walls == 10)
        self.assertTrue(player4.location == Coordinate(5, 9))
        self.assertTrue(player4.goal == [Coordinate(4, 0), Coordinate(6, 0)])
示例#18
0
 def test_is_xy_correct_input1(self):
     coord = Coordinate(1, 2)
     self.assertEqual(1, coord.row)
     self.assertEqual(2, coord.column)
示例#19
0
 def test_is_xy_correct_input2(self):
     coord1 = Coordinate(1, 2)
     coord2 = Coordinate(coordinate=coord1)
     self.assertTrue(coord1 == coord2)
示例#20
0
 def test_is_horizontal_correct(self):
     coord1 = Coordinate(1, 2)
     coordh = Coordinate(1, 4)
     coordv = Coordinate(3, 2)
     self.assertTrue(Coordinate.is_horizontal(coord1, coordh))
     self.assertFalse(Coordinate.is_horizontal(coord1, coordv))
示例#21
0
 def test_is_neighbors_correct_input1(self):
     coord = Coordinate(1, 2)
     neighbors = [None, None, None, None]
     tile1 = Tile(coordinate=coord)
     self.assertTrue(tile1.neighbors == neighbors)
     self.assertTrue(tile1.coordinate == coord)
示例#22
0
 def test_is_str_correct(self):
     coord = Coordinate(1, 2)
     msg = '(1, 2)'
     self.assertEqual(str(coord), msg)
示例#23
0
 def test_is_occupant_correct(self):
     coord = Coordinate(1, 2)
     tile1 = Tile(coordinate=coord)
     self.assertTrue(tile1.occupant == -1)
示例#24
0
 def test_is_coordinate_correct(self):
     coord = Coordinate(1, 2)
     tile1 = Tile(coordinate=coord)
     self.assertTrue(tile1.coordinate == coord)
示例#25
0
    def test_is_static_valid_walls_correct(self):
        board = Board(BoardTestCase.std_dim)

        # Horizontal checks
        self.assertFalse(
            board._compute_static_valid_wall(True,
                                             Coordinate(0,
                                                        2)))  # Bad lower row
        self.assertFalse(
            board._compute_static_valid_wall(True, Coordinate(
                2, -1)))  # Bad lower column
        self.assertFalse(
            board._compute_static_valid_wall(
                True, Coordinate(BoardTestCase.std_dim - 1,
                                 2)))  # Bad upper row
        self.assertFalse(
            board._compute_static_valid_wall(
                True,
                Coordinate(2, BoardTestCase.std_dim - 2)))  # Bad upper column

        self.assertTrue(
            board._compute_static_valid_wall(True,
                                             Coordinate(1,
                                                        2)))  # Good lower row
        self.assertTrue(
            board._compute_static_valid_wall(True, Coordinate(
                2, 0)))  # Good lower column
        self.assertTrue(
            board._compute_static_valid_wall(
                True, Coordinate(BoardTestCase.std_dim - 2,
                                 2)))  # Good upper row
        self.assertTrue(
            board._compute_static_valid_wall(
                True,
                Coordinate(2, BoardTestCase.std_dim - 3)))  # Good upper column

        # Vertical checks
        self.assertFalse(
            board._compute_static_valid_wall(False,
                                             Coordinate(-1,
                                                        1)))  # Bad lower row
        self.assertFalse(
            board._compute_static_valid_wall(False, Coordinate(
                1, 0)))  # Bad lower column
        self.assertFalse(
            board._compute_static_valid_wall(
                False, Coordinate(BoardTestCase.std_dim - 2,
                                  3)))  # Bad upper row
        self.assertFalse(
            board._compute_static_valid_wall(
                False,
                Coordinate(3, BoardTestCase.std_dim - 1)))  # Bad upper column

        self.assertTrue(
            board._compute_static_valid_wall(False,
                                             Coordinate(0,
                                                        1)))  # Good lower row
        self.assertTrue(
            board._compute_static_valid_wall(False, Coordinate(
                1, 1)))  # Good lower column
        self.assertTrue(
            board._compute_static_valid_wall(
                False, Coordinate(BoardTestCase.std_dim - 3,
                                  3)))  # Good upper row
        self.assertTrue(
            board._compute_static_valid_wall(
                False,
                Coordinate(3, BoardTestCase.std_dim - 2)))  # Good upper column
示例#26
0
 def test_is_neighbors_correct_input2(self):
     coord = Coordinate(1, 2)
     neighbors2 = [coord, None, None, None]
     tile1 = Tile(coordinate=coord)
     tile2 = Tile(coordinate=coord, neighbors=neighbors2)
     self.assertTrue(tile2.neighbors[0] == tile1.coordinate)
示例#27
0
 def test_is_xy_correct_input3(self):
     coord1 = Coordinate()
     self.assertEqual(-1, coord1.row)
     self.assertEqual(-1, coord1.column)
示例#28
0
 def test_is_end_occupancy_correct(self):
     coord = Coordinate(1, 2)
     tile1 = Tile(coordinate=coord)
     tile1.occupant = 1
     Tile.end_occupancy(tile1)
     self.assertTrue(tile1.occupant == -1)
示例#29
0
    def test_is_dynamic_valid_walls_correct(self):
        board = Board(BoardTestCase.std_dim)

        self.assertTrue(
            board._compute_dynamic_valid_wall(True, Coordinate(2, 2)))
        self.assertTrue(
            board._compute_dynamic_valid_wall(False, Coordinate(5, 5)))

        board.place_wall(True, Coordinate(2, 2))
        self.assertFalse(
            board._compute_dynamic_valid_wall(True, Coordinate(2, 2)))
        self.assertFalse(
            board._compute_dynamic_valid_wall(True, Coordinate(2, 3)))
        self.assertFalse(
            board._compute_dynamic_valid_wall(True, Coordinate(2, 1)))
        self.assertTrue(
            board._compute_dynamic_valid_wall(True, Coordinate(2, 0)))
        self.assertTrue(
            board._compute_dynamic_valid_wall(True, Coordinate(2, 4)))

        board.place_wall(False, Coordinate(5, 5))
        self.assertFalse(
            board._compute_dynamic_valid_wall(False, Coordinate(5, 5)))
        self.assertFalse(
            board._compute_dynamic_valid_wall(False, Coordinate(6, 5)))
        self.assertFalse(
            board._compute_dynamic_valid_wall(False, Coordinate(4, 5)))
        self.assertTrue(
            board._compute_dynamic_valid_wall(False, Coordinate(3, 5)))
        self.assertTrue(
            board._compute_dynamic_valid_wall(False, Coordinate(7, 5)))