示例#1
0
class PlayerTestCase(unittest.TestCase):
    def setUp(self):
        self.player1 = PlayerData(Color.RED, 5)
        self.player2 = PlayerData(Color.WHITE, 10)

    def test_invalid_color(self):
        self.assertRaisesRegex(ValueError, "Invalid color", PlayerData, 5)

    def test_get_penguins(self):
        self.assertEqual(self.player1.get_penguins(), [])
        self.player1.add_penguin((0, 0))
        self.assertEqual(self.player1.get_penguins(), [(0, 0)])

    def test_copy(self):
        new_player = self.player1.copy()
        self.assertEqual(self.player1.get_age(), new_player.get_age())
        self.assertEqual(self.player1.get_color(), new_player.get_color())
        self.assertEqual(self.player1.get_penguins(),
                         new_player.get_penguins())
        self.assertFalse(self.player1 is new_player)

    def test_add_penguin(self):
        self.player1.add_penguin((0, 0))
        self.player1.add_penguin((1, 0))
        self.assertEqual(self.player1.get_penguins(), [(0, 0), (1, 0)])

    def get_color(self):
        self.assertEqual(self.player1.get_color(), Color.RED)
        self.assertEqual(self.player2.get_color(), Color.WHITE)

    def get_age(self):
        self.assertEqual(self.player1.get_age(), 5)
        self.assertEqual(self.player2.get_age(), 10)

    def move_penguin_success(self):
        self.player1.add_penguin((0, 0))
        self.assertEqual(self.player1.get_penguins(), [(0, 0)])
        self.player1.move_penguin((0, 0), (3, 0))
        self.assertEqual(self.player1.get_penguins(), [(3, 0)])

    def move_penguin_no_penguin(self):
        self.player1.add_penguin((0, 0))
        self.assertEqual(self.player1.get_penguins(), [(0, 0)])
        self.player1.move_penguin((3, 1), (3, 0))
        self.assertEqual(self.player1.get_penguins(), [(0, 0)])
示例#2
0
 def test_attempt_move_last_move(self):
     mini_board_array = [[1, 5], [2, 1], [1, 5], [1, 5], [0, 5]]
     player1 = PlayerData(Color.RED)
     player2 = PlayerData(Color.WHITE)
     player1.add_penguin((0, 0))
     player1.add_penguin((1, 0))
     player1.add_penguin((2, 0))
     player1.add_penguin((3, 0))
     player2.add_penguin((0, 1))
     player2.add_penguin((1, 1))
     player2.add_penguin((2, 1))
     player2.add_penguin((3, 1))
     players = [player1, player2]
     board = Board(5, 2)
     board.create_board_from_json(mini_board_array)
     state = State(players, board)
     game_tree = GameTree(state)
     action = Move((3, 0), (4, 1))
     self.assertEqual(type(game_tree.attempt_move(action)), GameEnded)
class StateTestCase(unittest.TestCase):

    def setUp(self):
        self.board_full = Board(4, 3)
        self.board_full.create_board_without_holes(4)
        self.board_holes = Board(4, 3)
        self.board_holes.create_board_with_holes([(0, 0), (1, 1)], 3)

        self.player1 = PlayerData(Color.RED, 5)
        self.player2 = PlayerData(Color.WHITE, 10)
        self.player3 = PlayerData(Color.BROWN)
        self.player4 = PlayerData(Color.BLACK)
        self.player5 = PlayerData(Color.BLACK)
        self.players = [self.player1, self.player2]

        self.state_full = State(self.players, self.board_full)
        self.state_holes = State(self.players, self.board_holes)

    def test_duplicate_player_colors(self):
        duplicate_colors = [self.player1, self.player3, self.player4, self.player5]
        self.assertRaisesRegex(ValueError, "Players cannot have duplicate colors",
                               State, duplicate_colors, self.board_full)

    def test_place_penguin_for_player_success(self):
        self.assertEqual(self.state_full.get_all_penguins(), [])
        new_state = self.state_full.place_penguin_for_player(Color.RED, (1, 2))
        self.assertEqual(new_state.get_all_penguins(), [(1, 2)])

    def test_place_penguin_for_player_out_of_bounds(self):
        self.assertEqual(self.state_full.get_all_penguins(), [])
        self.assertFalse(self.state_full.place_penguin_for_player(Color.RED, (4, 3)))

    def test_place_penguin_for_player_hole(self):
        self.assertEqual(self.state_holes.get_all_penguins(), [])
        self.assertFalse(self.state_holes.place_penguin_for_player(Color.RED, (0, 0)))

    def test_place_penguin_for_player_no_player(self):
        self.assertEqual(self.state_full.get_all_penguins(), [])
        self.assertFalse(self.state_full.place_penguin_for_player(Color.BROWN, (1, 2)))

    def test_get_all_penguins(self):
        self.assertEqual(self.state_full.get_all_penguins(), [])
        self.player1.get_penguins = MagicMock(return_value=[])
        self.player2.get_penguins = MagicMock(return_value=[(0, 0), (3, 2)])
        self.assertEqual(self.state_full.get_all_penguins(), [(0, 0), (3, 2)])

    def test_get_player_success(self):
        self.assertEqual(self.state_full.get_player(Color.RED), self.player1)
        self.assertEqual(self.state_full.get_player(Color.WHITE), self.player2)

    def test_get_player_no_player(self):
        self.assertFalse(self.state_full.get_player(Color.BROWN))

    def test_is_tile_available_true(self):
        self.assertTrue(self.state_full.is_tile_available((1, 1)))
        self.assertTrue(self.state_holes.is_tile_available((2, 2)))

    def test_is_tile_available_holes(self):
        self.assertFalse(self.state_holes.is_tile_available((0, 0)))
        self.assertFalse(self.state_holes.is_tile_available((1, 1)))

    def test_is_tile_available_out_of_bounds(self):
        self.assertFalse(self.state_full.is_tile_available((4, 3)))
        self.assertFalse(self.state_full.is_tile_available((-2, 3)))

    def test_is_tile_available_occupied(self):
        new_state = self.state_full.place_penguin_for_player(Color.RED, (1, 2))
        self.assertFalse(new_state.is_tile_available((1, 2)))

    def test_valid_move_success(self):
        self.state_full.get_all_penguins = MagicMock(return_value=[(0, 0), (1, 0), (3, 2)])
        self.assertTrue(self.state_full.valid_move((0, 0), (2, 0)))

    def test_valid_move_occupied(self):
        self.state_full.get_all_penguins = MagicMock(return_value=[(0, 0), (1, 0), (3, 2)])
        self.assertFalse(self.state_full.valid_move((0, 0), (1, 0)))

    def test_valid_move_hole(self):
        self.state_holes.get_all_penguins = MagicMock(return_value=[(0, 1), (1, 0), (3, 2)])
        self.assertFalse(self.state_holes.valid_move((0, 1), (1, 1)))

    def test_valid_move_no_penguin(self):
        self.state_holes.get_all_penguins = MagicMock(return_value=[(0, 1), (1, 0), (3, 2)])
        self.assertFalse(self.state_holes.valid_move((0, 0), (2, 0)))

    def test_valid_move_not_reachable(self):
        self.state_holes.get_all_penguins = MagicMock(return_value=[(0, 1), (1, 0), (3, 2)])
        self.assertFalse(self.state_holes.valid_move((0, 0), (3, 0)))

    def test_get_player_from_penguin_exists(self):
        self.player1.get_penguins = MagicMock(return_value=[(0, 0), (1, 0)])
        self.player2.get_penguins = MagicMock(return_value=[(3, 2)])
        self.assertEqual(self.state_full.get_player_from_penguin((3, 2)), self.player2)

    def test_get_player_from_penguin_no_player(self):
        self.player1.get_penguins = MagicMock(return_value=[(0, 0), (1, 0)])
        self.assertFalse(self.state_full.get_player_from_penguin((3, 2)))

    def test_move_penguin_success(self):
        self.player1.add_penguin((0, 0))

        self.assertTrue((0, 0) in self.state_full.get_all_penguins())
        self.assertFalse((2, 0) in self.state_full.get_all_penguins())
        self.assertEqual(self.state_full.get_current_player_color(), Color.RED)
        new_state = self.state_full.move_penguin((0, 0), (2, 0))
        self.assertTrue((2, 0) in new_state.get_all_penguins())
        self.assertFalse((0, 0) in new_state.get_all_penguins())
        self.assertEqual(new_state.get_current_player_color(), Color.WHITE)

    def test_move_penguin_occupied(self):
        self.player1.add_penguin((0, 0))
        self.player2.add_penguin((2, 0))
        self.assertEqual(self.state_full.get_all_penguins(), [(0, 0), (2, 0)])
        self.assertEqual(self.state_full.get_current_player_color(), Color.RED)
        self.assertFalse(self.state_full.move_penguin((0, 0), (2, 0)))
        self.assertEqual(self.state_full.get_all_penguins(), [(0, 0), (2, 0)])
        self.assertEqual(self.state_full.get_current_player_color(), Color.RED)

    def test_any_remaining_moves_true(self):
        self.player1.add_penguin((0, 0))
        self.player2.add_penguin((2, 0))
        self.assertTrue(self.state_full.any_remaining_moves())

    def test_any_remaining_moves_no_penguins(self):
        self.assertFalse(self.state_full.any_remaining_moves())

    def test_any_remaining_moves_no_moves(self):
        board = Board(4, 3)
        board.create_board_with_holes([(1, 0), (2, 0)], 0)
        state = State(self.players, board)
        self.player1.add_penguin((0, 0))
        self.assertFalse(state.any_remaining_moves())

    def test_get_possible_moves(self):
        self.player1.add_penguin((0, 0))

        actions = [Move((0, 0), (1, 0)), Move((0, 0), (2, 0)),
                   Move((0, 0), (2, 1)), Move((0, 0), (3, 1))]
        for action in self.state_full.get_possible_moves():
            self.assertTrue(action in actions)
        self.assertEqual(len(self.state_full.get_possible_moves()), len(actions))

    def test_get_possible_moves_another_penguin(self):
        self.player1.add_penguin((0, 0))
        self.player2.add_penguin((2, 1))
        actions = [Move((0, 0), (1, 0)), Move((0, 0), (2, 0))]
        for action in self.state_full.get_possible_moves():
            self.assertTrue(action in actions)
        self.assertEqual(len(self.state_full.get_possible_moves()), len(actions))

    def test_get_players_score_after_move(self):
        self.assertEqual(self.state_full.get_players_score(Color.RED), 0)
        self.player1.add_penguin((0, 0))
        new_state = self.state_full.move_penguin((0, 0), (1, 0))
        self.assertEqual(new_state.get_players_score(Color.RED), 4)
        self.assertEqual(self.state_full.get_players_score(Color.WHITE), 0)

    def test_get_players_score_mutate_player(self):
        self.assertEqual(self.state_full.get_players_score(Color.RED), 0)
        self.player1.add_to_score(10)
        self.assertEqual(self.state_full.get_players_score(Color.RED), 10)

    def test_from_json(self):
        players_json = [
            {
                'color': "red",
                'score': 0,
                'places': []
            },
            {
                'color': "black",
                'score': 123,
                'places': [[2, 3]]
            },
            {
                'color': "white",
                'score': 321,
                'places': [[0, 0], [4, 5]]
            },
            {
                'color': "brown",
                'score': 7,
                'places': [[0, 1], [321, 123]]
            },
        ]
        board_json = [
            [1,     2],
            [4,     0,      0,      5],
            [1,     1,      0,      1]
        ]
        state_json = {'players': players_json, 'board': board_json}
        players = [PlayerData.from_json(player) for player in players_json]
        board = Board.from_json(board_json)
        self.assertEqual(State(players, board), State.from_json(state_json))
示例#4
0
class GameTreeTestCase(unittest.TestCase):
    def setUp(self):
        self.board_full = Board(4, 3)
        self.board_full.create_board_without_holes(4)
        self.board_holes = Board(4, 3)
        self.board_holes.create_board_with_holes([(1, 0), (2, 0)], 3)

        self.player1 = PlayerData(Color.RED, 5)
        self.player2 = PlayerData(Color.WHITE, 10)
        self.players = [self.player1, self.player2]

        self.state_full = State(self.players, self.board_full)
        self.state_holes = State(self.players, self.board_holes)

        self.player1.add_penguin((0, 0))
        self.player2.add_penguin((0, 2))

        self.action1 = Move((0, 0), (1, 0))

        self.game_tree = GameTree(self.state_full)
        self.game_tree_holes = GameTree(self.state_holes)

        self.state1_0 = self.state_full.move_penguin((0, 0), (1, 0))
        self.state2_0 = self.state_full.move_penguin((0, 0), (2, 0))
        self.state2_1 = self.state_full.move_penguin((0, 0), (2, 1))
        self.state3_1 = self.state_full.move_penguin((0, 0), (3, 1))

    def test_attempt_move(self):
        self.assertEqual(type(self.game_tree.attempt_move(self.action1)),
                         GameTree)

    def test_attempt_move_last_move(self):
        mini_board_array = [[1, 5], [2, 1], [1, 5], [1, 5], [0, 5]]
        player1 = PlayerData(Color.RED)
        player2 = PlayerData(Color.WHITE)
        player1.add_penguin((0, 0))
        player1.add_penguin((1, 0))
        player1.add_penguin((2, 0))
        player1.add_penguin((3, 0))
        player2.add_penguin((0, 1))
        player2.add_penguin((1, 1))
        player2.add_penguin((2, 1))
        player2.add_penguin((3, 1))
        players = [player1, player2]
        board = Board(5, 2)
        board.create_board_from_json(mini_board_array)
        state = State(players, board)
        game_tree = GameTree(state)
        action = Move((3, 0), (4, 1))
        self.assertEqual(type(game_tree.attempt_move(action)), GameEnded)

    def test_attempt_move_invalid(self):
        self.assertFalse(self.game_tree.attempt_move(Move((0, 0), (2, 2))))

    def test_create_child_trees(self):
        trees = [
            GameTree(self.state1_0),
            GameTree(self.state2_0),
            GameTree(self.state2_1),
            GameTree(self.state3_1)
        ]
        children = self.game_tree.create_child_trees()
        for tree in trees:
            self.assertTrue(tree in children.values())
        self.assertEqual(len(children), len(trees))

    def test_create_child_trees_no_children(self):
        children = self.game_tree_holes.create_child_trees()
        child_trees = list(children.values())
        self.assertEqual(len(child_trees), 1)
        self.state_holes.set_next_players_turn()
        self.assertTrue(child_trees[0] in [GameTree(self.state_holes)])

    def test_apply_to_children(self):
        action = Move((0, 2), (1, 2))
        states = [self.state1_0, self.state2_0, self.state2_1, self.state3_1]
        new_states = self.game_tree.apply_to_children(action.apply_move)
        for state in states:
            move_state = state.move_penguin((0, 2), (1, 2))
            self.assertTrue(move_state in new_states)
        self.assertEqual(len(new_states), len(states))

    def test_apply_to_children_create_trees(self):
        child_trees = self.game_tree.apply_to_children(
            GameTree.create_child_trees)
        self.assertEqual(len(child_trees), 4)
        num_children = 0
        for c in child_trees:
            num_children += len(c)
            self.assertGreater(len(c), 0)
        self.assertEqual(num_children, 18)