Example #1
0
    def test_is_equal(self):
        board1 = FishBoard(4, 3)
        board1 = board1.create_hole(2, 0)
        board1 = board1.create_hole(3, 0)
        board1 = board1.create_hole(2, 1)
        board1 = board1.create_hole(0, 1)

        board2 = FishBoard(4, 3)
        board2 = board2.create_hole(2, 0)
        board2 = board2.create_hole(3, 0)
        board2 = board2.create_hole(2, 1)
        board2 = board2.create_hole(0, 1)

        factory1 = FishGameState(board=board1,
                                 num_players=3,
                                 players=TestGameState.player_list,
                                 phase=GameStatePhase.INITIAL,
                                 current_player=TestGameState.player_list[0])
        factory1 = factory1.add_penguin(0, 0, "red")
        factory1 = factory1.add_penguin(1, 0, "white")

        factory2 = FishGameState(board=board2,
                                 num_players=3,
                                 players=TestGameState.player_list,
                                 phase=GameStatePhase.INITIAL,
                                 current_player=TestGameState.player_list[0])
        factory2 = factory2.add_penguin(0, 0, "red")
        factory2 = factory2.add_penguin(1, 0, "white")

        state1 = factory1.finalize()
        state2 = factory2.finalize()

        self.assertTrue(state1.is_equal(state2))
Example #2
0
 def test_validate_input_true(self):
     board = FishBoard(4, 3)
     game_state = FishGameState(board,
                                3,
                                TestGameState.player_list,
                                current_player=TestGameState.player_list[0],
                                phase=GameStatePhase.FINAL)
     is_valid, error_msg = game_state.validate_input("red", 0, 0)
     self.assertTrue(is_valid)
     self.assertEqual(error_msg, "")
Example #3
0
 def test_add_penguin_success(self):
     board = FishBoard(4, 3)
     factory = FishGameState(board=board,
                             num_players=2,
                             players=TestGameState.player_list,
                             phase=GameStatePhase.INITIAL,
                             current_player=TestGameState.player_list[0])
     factory = factory.add_penguin(0, 0, "red")
     expected_penguins = [Coordinate(0, 0)]
     self.assertEqual(factory.players["red"].penguins, expected_penguins)
Example #4
0
 def test_validate_input_invalid_player_id_upperbound(self):
     board = FishBoard(4, 3)
     game_state = FishGameState(board,
                                3,
                                TestGameState.player_list,
                                current_player=TestGameState.player_list[0],
                                phase=GameStatePhase.FINAL)
     is_valid, error_msg = game_state.validate_input("", 0, 0)
     self.assertFalse(is_valid)
     self.assertEqual(error_msg, "Player color does not exist")
Example #5
0
 def test_has_penguin(self):
     board = FishBoard(4, 3)
     factory = FishGameState(board=board,
                             num_players=3,
                             players=TestGameState.player_list,
                             phase=GameStatePhase.INITIAL,
                             current_player=TestGameState.player_list[0])
     factory = factory.add_penguin(0, 0, "red")
     self.assertTrue(factory.has_penguin(0, 0))
     self.assertFalse(factory.has_penguin(1, 1))
Example #6
0
 def test_any_player_can_move_success(self):
     board = FishBoard(4, 3)
     factory = FishGameState(board=board,
                             num_players=3,
                             players=TestGameState.player_list,
                             phase=GameStatePhase.INITIAL,
                             current_player=TestGameState.player_list[0])
     factory = factory.add_penguin(0, 0, "red")
     factory = factory.add_penguin(1, 1, "white")
     game_state = factory.finalize()
     self.assertTrue(game_state.check_any_player_can_move())
Example #7
0
 def test_validate_input_invalid_row_and_col_out_of_bounds(self):
     board = FishBoard(4, 3)
     game_state = FishGameState(board,
                                3,
                                TestGameState.player_list,
                                current_player=TestGameState.player_list[0],
                                phase=GameStatePhase.FINAL)
     is_valid, error_msg = game_state.validate_input("red", 6, 6)
     self.assertFalse(is_valid)
     self.assertEqual(error_msg,
                      "Row and column do not exist in this board")
Example #8
0
 def test_add_penguin_failure(self):
     board = FishBoard(4, 3)
     factory = FishGameState(board=board,
                             num_players=2,
                             players=TestGameState.player_list,
                             phase=GameStatePhase.INITIAL,
                             current_player=TestGameState.player_list[0])
     factory = factory.add_penguin(0, 0, "red")
     factory = factory.add_penguin(0, 1, "white")
     with self.assertRaises(ValueError):
         factory = factory.add_penguin(0, 2, "white")
Example #9
0
 def test_validate_input_invalid_row_and_col_has_penguin(self):
     board = FishBoard(4, 3)
     factory = FishGameState(board=board,
                             num_players=2,
                             players=TestGameState.player_list,
                             phase=GameStatePhase.INITIAL,
                             current_player=TestGameState.player_list[0])
     factory = factory.add_penguin(2, 2, "red")
     is_valid, error_msg = factory.validate_input("white", 2, 2)
     self.assertFalse(is_valid)
     self.assertEqual(error_msg, "This tile already has a penguin")
Example #10
0
 def test_validate_input_invalid_row_and_col_has_hole(self):
     board = FishBoard(4, 3)
     board = board.create_hole(1, 1)
     game_state = FishGameState(board,
                                3,
                                TestGameState.player_list,
                                current_player=TestGameState.player_list[0],
                                phase=GameStatePhase.FINAL)
     is_valid, error_msg = game_state.validate_input("red", 1, 1)
     self.assertFalse(is_valid)
     self.assertEqual(error_msg, "Row and column is a hole")
Example #11
0
 def test_move_penguin_failure(self):
     board = FishBoard(4, 3)
     factory = FishGameState(board=board,
                             num_players=3,
                             players=TestGameState.player_list,
                             phase=GameStatePhase.INITIAL,
                             current_player=TestGameState.player_list[0])
     factory = factory.add_penguin(row=0, col=0, color="red")
     factory = factory.add_penguin(row=0, col=1, color="white")
     factory = factory.add_penguin(row=0, col=2, color="brown")
     game_state = factory.finalize()
     with self.assertRaises(ValueError):
         game_state.move_penguin("red", 0, 0, 0, 2)
        def random_choice(s: FishGameState) -> GameStateTree:
            if s.current_player != self.current_player:
                raise ValueError("Not current players turn...")

            selected_action = choice(s.get_player_actions_from(s.current_player))
            return GameStateTree(
                state=s.move_penguin(
                    color=self.current_player.color,
                    start_row=selected_action.start.row,
                    start_col=selected_action.start.col,
                    end_row=selected_action.end.row,
                    end_col=selected_action.end.col)
            )
Example #13
0
 def test_any_player_can_move_failure(self):
     board = FishBoard(4, 3)
     board = board.create_hole(2, 0)
     board = board.create_hole(3, 0)
     board = board.create_hole(2, 1)
     board = board.create_hole(0, 1)
     factory = FishGameState(board=board,
                             num_players=3,
                             players=TestGameState.player_list,
                             phase=GameStatePhase.INITIAL,
                             current_player=TestGameState.player_list[0])
     factory = factory.add_penguin(0, 0, "red")
     factory = factory.add_penguin(1, 0, "white")
     game_state = factory.finalize()
     self.assertFalse(game_state.check_any_player_can_move())
Example #14
0
    def test_move_penguin(self):
        board = FishBoard(4, 3)
        factory = FishGameState(board=board,
                                num_players=3,
                                players=TestGameState.player_list,
                                phase=GameStatePhase.INITIAL,
                                current_player=TestGameState.player_list[0])
        factory = factory.add_penguin(row=0, col=1, color="red")
        factory = factory.add_penguin(row=0, col=0, color="white")
        factory = factory.add_penguin(row=3, col=0, color="brown")

        game_state = factory.finalize()
        game_state = game_state.move_penguin("red", 0, 1, 2, 1)
        expected_penguins = [Coordinate(row=2, col=1)]
        self.assertEqual(game_state.players["red"].penguins, expected_penguins)
Example #15
0
def Demo3(holes: List[Coordinate]) -> FishGameController:
    """
    Given the list of holes, render a board with holes at those
    cooridates and 1 fish at every non-hole tile.
    :param holes: List of hole Coordinates
    :return: A controller to be execute in main.
    """

    hexgrid = HexGridView()
    board = FishBoard(num_rows=HEXES_ROWS, num_cols=HEXES_COLUMNS)

    for hole in holes:
        board = board.create_hole(row=hole.row, col=hole.col)

    for row in range(board.num_rows):
        for col in range(board.num_cols):
            if board.retrieve_num_fish(
                    row=row, col=col) == 0 and board.is_tile(row=row, col=col):
                board = board.add_fish(row=row, col=col)

    player_1 = Player("red", 0, 0, [])
    state = FishGameState(board=board,
                          num_players=1,
                          players=[player_1],
                          current_player=player_1)
    behavior = FishGameBehavior(state=state, view=hexgrid)

    controller = FishGameController(behavior=behavior,
                                    view=hexgrid,
                                    state=state)

    return controller
Example #16
0
def Demo1() -> FishGameController:
    """
    "Randomly Generate number of holes, hole locations, number of fish, and
    fish location.

    :return: A controller to be execute in main.
    """

    hexgrid = HexGridView()
    board = FishBoard(num_rows=HEXES_ROWS, num_cols=HEXES_COLUMNS)

    board = RandomlyGenerateHole(board)
    board = RandomlyGenerateFish(board)

    player_1 = Player("red", 0, 0, [])
    state = FishGameState(board=board,
                          num_players=1,
                          players=[player_1],
                          current_player=player_1)
    behavior = FishGameBehavior(state=state, view=hexgrid)

    controller = FishGameController(behavior=behavior,
                                    view=hexgrid,
                                    state=state)

    return controller
Example #17
0
    def test_is_equal_false_board_only(self):
        board1 = FishBoard(5, 3)
        board2 = FishBoard(4, 3)

        state1 = FishGameState(board=board1,
                               num_players=3,
                               players=TestGameState.player_list[0:2],
                               phase=GameStatePhase.INITIAL,
                               current_player=TestGameState.player_list[0])
        state2 = FishGameState(board=board2,
                               num_players=3,
                               players=TestGameState.player_list,
                               phase=GameStatePhase.INITIAL,
                               current_player=TestGameState.player_list[0])

        self.assertFalse(state1.is_equal(state2))
Example #18
0
def Demo2(n_fish_per_tile=1) -> FishGameController:
    """
    Randomly Generated Holes with 1 Fish on all tiles...
    :return: A controller to be execute in main.
    """

    hexgrid = HexGridView()
    board = FishBoard(num_rows=HEXES_ROWS, num_cols=HEXES_COLUMNS)

    for row in range(board.num_rows):
        for col in range(board.num_cols):
            if board.retrieve_num_fish(
                    row=row, col=col) == 0 and board.is_tile(row=row, col=col):
                for i in range(n_fish_per_tile):
                    board = board.add_fish(row=row, col=col)
    player_1 = Player("red", 0, 0, [])
    state = FishGameState(board=board,
                          num_players=1,
                          players=[player_1],
                          current_player=player_1)
    behavior = FishGameBehavior(state=state, view=hexgrid)

    controller = FishGameController(behavior=behavior,
                                    view=hexgrid,
                                    state=state)

    return controller
Example #19
0
    def test_get_player_id_success(self):
        board = FishBoard(4, 3)
        board = board.create_hole(2, 0)
        board = board.create_hole(3, 0)
        board = board.create_hole(2, 1)
        board = board.create_hole(0, 1)
        factory = FishGameState(board=board,
                                num_players=3,
                                players=TestGameState.player_list,
                                phase=GameStatePhase.INITIAL,
                                current_player=TestGameState.player_list[0])
        factory = factory.add_penguin(0, 0, "red")
        factory = factory.add_penguin(1, 0, "white")

        game_state = factory.finalize()
        self.assertEqual(game_state.get_player_color(0, 0), "red")
        self.assertEqual(game_state.get_player_color(3, 2), "")
Example #20
0
    def setup():
        test_board = FishBoard(4, 3)
        player_1 = Player("red", 0, 0, [])
        player_2 = Player("white", 0, 0, [])
        test_players = [player_1, player_2]
        state = FishGameState(board=test_board,
                              num_players=len(test_players),
                              players=test_players,
                              current_player=player_1,
                              phase=GameStatePhase.INITIAL)
        state = state.add_penguin(0, 0, "red") \
            .add_penguin(0, 2, "white") \
            .add_penguin(0, 1, "red") \
            .add_penguin(1, 2, "white") \
            .add_penguin(2, 0, "red") \
            .add_penguin(2, 2, "white") \
            .add_penguin(3, 0, "red") \
            .add_penguin(3, 2, "white")

        return state.finalize(), state.board, state.players
    def place_penguin(state: FishGameState) -> Coordinate:
        """
        Place a penguin in the next available place in the given state's board according to a zig-zag pattern that begins
        in the top left corner.
        :param state: The state that will have penguins added to it.
        :return: The updated state after adding the penguin in the correct place.
        """
        for r, row in enumerate(state.board.board):
            for c, col in enumerate(row):
                if state.validate_input(state.current_player.color, r, c)[0]:
                    return Coordinate(r, c)

        raise ValueError("No available tiles for penguin placement.")
    def setup():
        test_board = FishBoard(4, 3)
        player_1 = Player("red", 0, 0, [])
        player_2 = Player("white", 0, 0, [])
        test_players = [player_1, player_2]

        state = FishGameState(board=test_board,
                              num_players=len(test_players),
                              players=test_players,
                              current_player=player_1,
                              phase=GameStatePhase.INITIAL)

        return state
Example #23
0
 def test_create_game_state(self):
     board = FishBoard(4, 3)
     game_state = FishGameState(board,
                                3,
                                TestGameState.player_list,
                                current_player=TestGameState.player_list[0],
                                phase=GameStatePhase.INITIAL)
     expected_players = {
         "red": Player(color="red", age=0, score=0, penguins=[]),
         "white": Player(color="white", age=0, score=0, penguins=[]),
         "brown": Player(color="brown", age=0, score=0, penguins=[])
     }
     self.assertEqual(game_state.players, expected_players)
     self.assertEqual(game_state.board, board)
     self.assertEqual(game_state.num_players, 3)
     self.assertEqual(game_state.num_penguins, 3)
 def simply_policy(s: FishGameState) -> int:
     return len(s.get_player_actions_from(s.current_player))
 def setup():
     test_board = FishBoard(4, 3)
     player_1 = Player("red", 0, 0, [])
     player_2 = Player("white", 0, 0, [])
     test_players = [player_1, player_2]
     factory = FishGameState(board=test_board,
                             players=test_players,
                             phase=GameStatePhase.INITIAL,
                             num_players=len(test_players),
                             current_player=test_players[0])
     factory = factory.add_penguin(0, 0, "red")
     factory = factory.add_penguin(0, 2, "white")
     factory = factory.add_penguin(1, 0, "red")
     factory = factory.add_penguin(1, 2, "white")
     factory = factory.add_penguin(2, 0, "red")
     factory = factory.add_penguin(2, 2, "white")
     factory = factory.add_penguin(3, 0, "red")
     factory = factory.add_penguin(3, 2, "white")
     return factory.finalize(), factory.board, factory.players