def _populate_board_with_tiles(cls, fish_board: FishBoardModel,
                                   board_canvas: Canvas) -> Canvas:
        """
        Purpose: Populate an empty canvas with renderings of tiles
        Signature: FishBoardModel Canvas -> Canvas
        :param fish_board: Fish board that contains information about tiles
        :param board_canvas: Board canvas that is to be drawn on
        :return: The board canvas with the tiles drawn onto it
        """
        # x offset is 2 * size for each hexagon
        x_offset_func = lambda x: 2 * FishTileView.SIZE_MULTIPLIER * x
        # y offset is the height of one hexagon times the double height
        # else on odd rows it goes down 1 size instead of 2 * size
        y_offset_func = lambda y: FishTileView.HEX_HEIGHT * (
            y / 2) if y % 2 == 0 else (FishTileView.HEX_HEIGHT / 2) * y

        for x, y in fish_board.get_tile_coords():
            tile = fish_board.get_tile_at_coord(x, y)
            x_offset = x_offset_func(x)
            y_offset = y_offset_func(y)
            # TODO use tag in controller
            FishTileView.add_fish_tile_to_canvas(board_canvas, tile,
                                                 '{}{}'.format(x, y), x_offset,
                                                 y_offset)
        return board_canvas
 def test_basic_placement(self):
     """
     Test getting basic placement from this player.
     """
     test_board = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
     test_state = FishGameStateFactory.create_place_penguins_state(test_board,
                                                                   [(PlayerColor.RED, []),
                                                                   (PlayerColor.WHITE, [])])
     player = BasicPlayer()
     placement = player.player_place_penguin(test_state)
     self.assertEqual((0, 0), placement)
示例#3
0
 def test_create_end_state_end(self):
     """
     Purpose: Test creating a penguin end state
     Signature: Void -> Void
     """
     small_board = FishBoardModel.create_with_same_fish_amount(4, 2, 3)
     game_state = FishGameStateFactory.create_end_game_state(
         small_board,
         [(PlayerColor.RED, CreateStateTest.test_board_valid_col1, 0),
          (PlayerColor.BROWN, CreateStateTest.test_board_valid_col2, 5)])
     self.assertEqual(GamePhase.END_GAME, game_state.get_game_phase())
示例#4
0
 def test_create_initial_state(self):
     """
     Purpose:Test creating an initial game state where only the ref can remove
             penguins (no players yet)
     Signature: Void -> Void
     """
     board = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
     game_state = FishGameState(
         board, player_colors=[PlayerColor.RED, PlayerColor.BROWN])
     self.assertEqual([PlayerColor.RED, PlayerColor.BROWN],
                      game_state.get_player_order())
     self.assertEqual(PlayerColor.RED, game_state.get_current_turn())
     self.assertEqual(GamePhase.PLACE_PENGUINS, game_state.get_game_phase())
 def test_placing_penguin_valid(self):
     """
     Purpose: Test what happens when you place a penguin in a valid tile on the board.
     Signature: Void -> Void
     """
     test_board_hole = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
     test_board_hole.remove_tile(0, 0)
     game_state = FishGameStateFactory.create_place_penguins_state(
         test_board_hole, [(PlayerColor.BLACK, []),
                           (PlayerColor.BROWN, [])])
     game_state.place_penguin(PlayerColor.BLACK, (1, 1))
     game_state.place_penguin(PlayerColor.BROWN, (2, 2))
     game_state.place_penguin(PlayerColor.BLACK, (3, 3))
 def test_get_fish(self):
     """
     Purpose: Test getting a player's fish before and after they move on a board
     Signature: Void -> Void
     """
     small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
     game_state = FishGameStateFactory.create_move_penguins_state(
         small_board,
         [(PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 5),
          (PlayerColor.BLACK, GameStateTest.test_board_valid_col1, 0)])
     self.assertEqual(5, game_state.get_fish_for_player(PlayerColor.BROWN))
     # Valid move from penguin at 3,3 to 4,2
     game_state.move_penguin(PlayerColor.BROWN, (3, 3), (4, 2))
     self.assertEqual(8, game_state.get_fish_for_player(PlayerColor.BROWN))
示例#7
0
 def create_game_state_with_dimensions(cls, row: int, col: int,
                                       player_colors: List[PlayerColor]) -> FishGameState:
     """
     Purpose: Convenience method for creating a board with a certain number of rows and columns
              and a random amount of fish on each tile
     Signature: Int Int -> FishGameState
     :param row: Amount of rows for the board
     :param col: Amount of columns for the board
     :param player_colors: Colors of the players in the order that they play. Initializes a state
                           where no penguins have been placed.
     :return: Game state ready for ref to remove tiles with random amount of fish on each tile
     """
     game_state = FishGameState(FishBoardModel.create_with_holes(row, col, set(), 0), player_colors)
     return game_state
示例#8
0
 def test_create_move_state_valid(self):
     """
     Purpose: Test creating a penguin movement state with correct amount of penguins
     Signature: Void -> Void
     """
     small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
     game_state = FishGameStateFactory.create_move_penguins_state(
         small_board,
         [(PlayerColor.RED, CreateStateTest.test_board_valid_col1, 0),
          (PlayerColor.BROWN, CreateStateTest.test_board_valid_col2, 5)])
     self.assertEqual(
         4, len(game_state.get_penguins_for_player(PlayerColor.RED)))
     self.assertEqual(
         4, len(game_state.get_penguins_for_player(PlayerColor.BROWN)))
 def test_add_fish(self):
     """
     Purpose: Test getting a player's fish before and after adding fish to a player
     Signature: Void -> Void
     """
     small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
     game_state = FishGameStateFactory.create_move_penguins_state(
         small_board,
         [(PlayerColor.BLACK, GameStateTest.test_board_valid_col1, 0),
          (PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 5)])
     self.assertEqual(5, game_state.get_fish_for_player(PlayerColor.BROWN))
     game_state.add_fish_to_player(PlayerColor.BROWN, 6)
     self.assertEqual(11, game_state.get_fish_for_player(PlayerColor.BROWN))
     with self.assertRaises(ValueError):
         game_state.add_fish_to_player(PlayerColor.BROWN, -10)
示例#10
0
 def create_game_state_with_num_fish(cls, row: int, col: int, num_fish: int,
                                     player_colors: List[PlayerColor]) -> FishGameState:
     """
     Purpose: Convenience method for creating a board with a certain number of rows and columns
              and a certain number of fish on each row. no holes.
     Signature: Int Int -> FishGameState
     :param row: Amount of rows for the board
     :param col: Amount of columns for the board
     :param num_fish: Amount of fish to add to each tiles.
     :param player_colors: Colors of the players in the order that they play. Initializes a state
                           where no penguins have been placed.
     :return: Game state ready for ref to remove tiles with correct amount of fish on each tile
     """
     game_state = FishGameState(FishBoardModel.create_with_same_fish_amount(row, col, num_fish), player_colors)
     return game_state
 def test_moving_penguin_to_invalid_or_existing(self):
     """
     Purpose: Test what happens when you attempt to move a penguin to an invalid spot.
     Signature: Void -> Void
     """
     small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
     game_state = FishGameStateFactory.create_move_penguins_state(
         small_board,
         [(PlayerColor.RED, GameStateTest.test_board_valid_col1, 0),
          (PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 5)])
     # Moving to a valid spot with another penguin on it
     with self.assertRaises(ValueError):
         game_state.move_penguin(PlayerColor.BROWN, (2, 2), (3, 1))
     # Moving to an invalid spot
     with self.assertRaises(ValueError):
         game_state.move_penguin(PlayerColor.BROWN, (2, 2), (4, 0))
 def test_moving_valid_penguin(self):
     """
     Purpose: Test what happens when you attempt to move a penguin to a valid spot.
     Signature: Void -> Void
     """
     small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
     game_state = FishGameStateFactory.create_move_penguins_state(
         small_board,
         [(PlayerColor.RED, GameStateTest.test_board_valid_col1, 0),
          (PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 5)],
         turn=PlayerColor.BROWN)
     # Valid move from penguin at 3,3 to 4,2
     game_state.move_penguin(PlayerColor.BROWN, (3, 3), (4, 2))
     # Tile should be removed now, try going back
     with self.assertRaises(PenguinMovementError):
         game_state.set_turn(PlayerColor.BROWN)
         game_state.move_penguin(PlayerColor.BROWN, (4, 2), (3, 3))
 def _create_empty_canvas(cls, fish_board: FishBoardModel,
                          window: Tk) -> Canvas:
     """
     Purpose: Create an empty canvas that is the right size to accommodate all of the tiles for a certain board
     Signature: FishBoardModel TkWindow -> TkCanvas
     :param fish_board: Fish board that we are creating a canvas for
     :param window: Tkinter window that are creating the canvas in
     :return: Canvas that we can draw on that is the size of the board
     """
     rows, columns = fish_board.get_dimensions()
     # One column with 2 hexes is 5 width for first column + 4 width for the following columns
     board_width = (5 + ((columns - 1) * 4)) * FishTileView.SIZE_MULTIPLIER
     # Height of board is amount of rows + 1 since 4 * height for something stacked on one another and 3 * height
     # for being next to one another
     board_height = (rows + 1) * FishTileView.SIZE_MULTIPLIER
     canvas = Canvas(window, width=board_width, height=board_height)
     return canvas
 def parse_board(board_json):
     """
     Purpose: Parse the board representation into our internal representation
     Signature: JSONArray of JSONArrays -> FishBoardModel
     :param board_json: Parsed JSON array representation of a Fish game board
     :return: An instance of our board model
     """
     coords_to_holes = []
     for i, row in enumerate(board_json):
         for j, val in enumerate(row):
             y_coord = i
             x_coord = 2 * j if i % 2 == 0 else 1 + 2 * j
             coords_to_holes.append(((x_coord, y_coord), val))
     amount_rows = len(board_json)
     amount_cols = max([len(row) for row in board_json])
     board = FishBoardModel.create_with_coords_to_fish(
         amount_rows, amount_cols, coords_to_holes)
     return board
    def test_moving_valid_penguin_turns(self):
        """
        Purpose: Test what happens when you move penguins in turn order
        Signature: Void -> Void
        """
        small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
        game_state = FishGameStateFactory.create_move_penguins_state(
            small_board,
            [(PlayerColor.RED, GameStateTest.test_board_valid_col1, 0),
             (PlayerColor.BROWN, GameStateTest.test_board_valid_col3, 5)],
            turn=PlayerColor.BROWN)
        # Valid move from penguin at 3,3 to 4,2
        game_state.move_penguin(PlayerColor.BROWN, (4, 2), (3, 1))
        # Valid move for next turn
        game_state.move_penguin(PlayerColor.RED, (1, 1), (2, 0))

        # attempt to move out of order
        with self.assertRaises(ValueError):
            game_state.move_penguin(PlayerColor.RED, (2, 0), (2, 2))
 def test_moving_penguin_from_invalid_or_nonexisting(self):
     """
     Purpose: Test what happens when you attempt to move a penguin from an invalid spot
     (No existing penguin, no tile, not on board, or another player's penguin)
     Signature: Void -> Void
     """
     small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
     game_state = FishGameStateFactory.create_move_penguins_state(
         small_board,
         [(PlayerColor.BLACK, GameStateTest.test_board_valid_col1, 5),
          (PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 0)])
     # No penguin
     with self.assertRaises(PenguinMovementError):
         game_state.move_penguin(PlayerColor.BLACK, (1, 4), (2, 3))
     # Not on board
     with self.assertRaises(PenguinMovementError):
         game_state.move_penguin(PlayerColor.BLACK, (10, 10), (11, 9))
     # Other player's penguin
     with self.assertRaises(PenguinMovementError):
         game_state.move_penguin(PlayerColor.BLACK, (3, 3), (4, 2))
    def test_if_players_can_move(self):
        """
        Purpose: Test the state if a player can't move, if 1 player can move, and where more than one player can move.
        Signature: Void -> Void
        """
        test_board = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
        game_state = FishGameStateFactory.create_place_penguins_state(
            test_board, [(PlayerColor.BLACK, []), (PlayerColor.BROWN, [])])
        game_state.place_penguin(PlayerColor.BLACK, (0, 0))
        game_state.place_penguin(PlayerColor.BROWN, (1, 1))
        self.assertEqual(True, game_state.can_any_player_move())

        # Remove tiles
        test_board.remove_tile(2, 2)
        test_board.remove_tile(2, 0)
        test_board.remove_tile(0, 2)
        test_board.remove_tile(1, 3)
        game_state_holes = FishGameStateFactory.create_place_penguins_state(
            test_board, [(PlayerColor.BLACK, [(0, 0)]),
                         (PlayerColor.BROWN, [(1, 1)])])
        self.assertEqual(False, game_state_holes.can_any_player_move())
    def test_placing_penguin_invalid(self):
        """
        Purpose: Test what happens when you place a penguin where one was already placed, where a tile doesn't exist,
        or off of the board completely.
        Signature: Void -> Void
        """

        test_board_hole = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
        test_board_hole.remove_tile(0, 0)
        game_state = FishGameStateFactory.create_place_penguins_state(
            test_board_hole, [(PlayerColor.BLACK, []),
                              (PlayerColor.BROWN, [])])
        # placing at position off board
        with self.assertRaises(PenguinPlacementError):
            game_state.place_penguin(PlayerColor.BLACK, (-1, -1))
        with self.assertRaises(PenguinPlacementError):
            game_state.place_penguin(PlayerColor.BLACK, (10, 10))
        # placing where penguin already exists
        game_state.place_penguin(PlayerColor.BLACK, (1, 1))
        with self.assertRaises(PenguinPlacementError):
            game_state.place_penguin(PlayerColor.BROWN, (1, 1))
        # placing where no tile
        with self.assertRaises(PenguinPlacementError):
            game_state.place_penguin(PlayerColor.BROWN, (0, 0))
import sys
import os

sys.path.insert(
    0,
    os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')))

from Fish.Common.representations.fish_board import FishBoardModel
from Fish.Common.views.pieces_view import FishBoardView

model = FishBoardModel.create_with_same_fish_amount(4, 4, 2)
FishBoardView.show_window(model)
示例#20
0
class CreateStateTest(unittest.TestCase):
    """
    Test cases for creating a game state
    """
    test_board = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
    test_board_valid_col1 = [(0, 0), (0, 2), (1, 1), (1, 3)]
    test_board_valid_col2 = [(2, 0), (2, 2), (3, 3), (3, 1)]

    def test_create_initial_state(self):
        """
        Purpose:Test creating an initial game state where only the ref can remove
                penguins (no players yet)
        Signature: Void -> Void
        """
        board = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
        game_state = FishGameState(
            board, player_colors=[PlayerColor.RED, PlayerColor.BROWN])
        self.assertEqual([PlayerColor.RED, PlayerColor.BROWN],
                         game_state.get_player_order())
        self.assertEqual(PlayerColor.RED, game_state.get_current_turn())
        self.assertEqual(GamePhase.PLACE_PENGUINS, game_state.get_game_phase())

    def test_create_initial_state_rows_cols(self):
        """
        Purpose: Test using convenience method to create board state
        Signature: Void -> Void
        """
        game_state = FishGameStateFactory.create_game_state_with_dimensions(
            4, 3, [PlayerColor.RED, PlayerColor.BROWN])
        self.assertEqual([PlayerColor.RED, PlayerColor.BROWN],
                         game_state.get_player_order())
        self.assertEqual(PlayerColor.RED, game_state.get_current_turn())
        self.assertEqual(GamePhase.PLACE_PENGUINS, game_state.get_game_phase())

    def test_create_placement_state_no_penguins(self):
        """
        Purpose: Test creating a penguin placement state with no penguins
        Signature: Void -> Void
        """
        game_state = FishGameStateFactory.create_place_penguins_state(
            CreateStateTest.test_board, [(PlayerColor.RED, []),
                                         (PlayerColor.BROWN, [])])
        self.assertEqual(GamePhase.PLACE_PENGUINS, game_state.get_game_phase())
        self.assertEqual(2, len(game_state.get_player_order()))

    def test_create_placement_state_same_penguins(self):
        """
        Purpose: Test creating a penguin placement state with same amount of penguins
        Signature: Void -> Void
        """
        game_state = FishGameStateFactory.create_place_penguins_state(
            CreateStateTest.test_board, [(PlayerColor.RED, [(1, 1)]),
                                         (PlayerColor.BROWN, [(0, 0)])])
        self.assertEqual(GamePhase.PLACE_PENGUINS, game_state.get_game_phase())
        self.assertEqual(2, len(game_state.get_player_order()))
        self.assertEqual([(0, 0)],
                         game_state.get_penguins_for_player(PlayerColor.BROWN))

    def test_create_placement_state_one_less(self):
        """
        Purpose: Test creating a penguin placement state with second penguin in order having one less penguin
        Signature: Void -> Void
        """
        game_state = FishGameStateFactory.create_place_penguins_state(
            CreateStateTest.test_board, [(PlayerColor.BROWN, [(0, 0)]),
                                         (PlayerColor.RED, [])])
        self.assertEqual(GamePhase.PLACE_PENGUINS, game_state.get_game_phase())
        self.assertEqual(2, len(game_state.get_player_order()))
        self.assertEqual([(0, 0)],
                         game_state.get_penguins_for_player(PlayerColor.BROWN))

    def test_create_placement_state_bad_penguin_amount(self):
        """
        Purpose: Test creating a penguin placement state with having the older player having penguins placed already
        Signature: Void -> Void
        """
        with self.assertRaises(ValueError):
            FishGameStateFactory.create_place_penguins_state(
                CreateStateTest.test_board, [(PlayerColor.RED, []),
                                             (PlayerColor.BROWN, [(0, 0)])])

    def test_create_state_too_little_players(self):
        """
        Purpose: Test creating a state with only 1 player
        """
        with self.assertRaises(ValueError):
            FishGameStateFactory.create_place_penguins_state(
                CreateStateTest.test_board, [(PlayerColor.RED, [])])

    def test_create_placement_state_bad_player_amount(self):
        """
        Purpose: Test creating a penguin placement state with too many players
        Signature: Void -> Void
        """
        with self.assertRaises(ValueError):
            FishGameStateFactory.create_place_penguins_state(
                CreateStateTest.test_board, [(PlayerColor.RED, []),
                                             (PlayerColor.BROWN, []),
                                             (PlayerColor.WHITE, []),
                                             (PlayerColor.BLACK, []),
                                             (PlayerColor.RED, [])])

    def test_create_move_state_valid(self):
        """
        Purpose: Test creating a penguin movement state with correct amount of penguins
        Signature: Void -> Void
        """
        small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
        game_state = FishGameStateFactory.create_move_penguins_state(
            small_board,
            [(PlayerColor.RED, CreateStateTest.test_board_valid_col1, 0),
             (PlayerColor.BROWN, CreateStateTest.test_board_valid_col2, 5)])
        self.assertEqual(
            4, len(game_state.get_penguins_for_player(PlayerColor.RED)))
        self.assertEqual(
            4, len(game_state.get_penguins_for_player(PlayerColor.BROWN)))

    def test_create_move_state_no_pengs(self):
        """
        Purpose: Test creating a penguin movement state with no penguins throws error
        Signature: Void -> Void
        """
        with self.assertRaises(ValueError):
            FishGameStateFactory.create_move_penguins_state(
                CreateStateTest.test_board, [(PlayerColor.RED, [], 0),
                                             (PlayerColor.BROWN, [], 0)])

    def test_create_move_state_same_place(self):
        """
        Purpose: Test creating a penguin movement state with trying to put penguins in the same place
        Signature: Void -> Void
        """
        with self.assertRaises(PenguinPlacementError):
            valid_posns = [(0, 0), (0, 2), (1, 1), (1, 3)]
            FishGameStateFactory.create_move_penguins_state(
                CreateStateTest.test_board,
                [(PlayerColor.RED, CreateStateTest.test_board_valid_col1, 0),
                 (PlayerColor.BROWN, CreateStateTest.test_board_valid_col1, 0)
                 ])

    def test_create_end_state_not_end(self):
        """
        Purpose: Test creating a penguin end state where it is not in an end state
        Signature: Void -> Void
        """
        with self.assertRaises(ValueError):
            FishGameStateFactory.create_end_game_state(
                CreateStateTest.test_board,
                [(PlayerColor.RED, CreateStateTest.test_board_valid_col1, 0),
                 (PlayerColor.BROWN, CreateStateTest.test_board_valid_col2, 0)
                 ])

    def test_create_end_state_end(self):
        """
        Purpose: Test creating a penguin end state
        Signature: Void -> Void
        """
        small_board = FishBoardModel.create_with_same_fish_amount(4, 2, 3)
        game_state = FishGameStateFactory.create_end_game_state(
            small_board,
            [(PlayerColor.RED, CreateStateTest.test_board_valid_col1, 0),
             (PlayerColor.BROWN, CreateStateTest.test_board_valid_col2, 5)])
        self.assertEqual(GamePhase.END_GAME, game_state.get_game_phase())
class GameStateTest(unittest.TestCase):
    """
    Test cases for creating a game state
    """
    test_board = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
    test_board_valid_col1 = [(0, 0), (0, 2), (1, 1), (1, 3)]
    test_board_valid_col2 = [(2, 0), (2, 2), (3, 3), (3, 1)]
    test_board_valid_col3 = [(4, 0), (4, 2), (5, 3), (5, 1)]

    def test_moving_valid_penguin(self):
        """
        Purpose: Test what happens when you attempt to move a penguin to a valid spot.
        Signature: Void -> Void
        """
        small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
        game_state = FishGameStateFactory.create_move_penguins_state(
            small_board,
            [(PlayerColor.RED, GameStateTest.test_board_valid_col1, 0),
             (PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 5)],
            turn=PlayerColor.BROWN)
        # Valid move from penguin at 3,3 to 4,2
        game_state.move_penguin(PlayerColor.BROWN, (3, 3), (4, 2))
        # Tile should be removed now, try going back
        with self.assertRaises(PenguinMovementError):
            game_state.set_turn(PlayerColor.BROWN)
            game_state.move_penguin(PlayerColor.BROWN, (4, 2), (3, 3))

    def test_moving_valid_penguin_turns(self):
        """
        Purpose: Test what happens when you move penguins in turn order
        Signature: Void -> Void
        """
        small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
        game_state = FishGameStateFactory.create_move_penguins_state(
            small_board,
            [(PlayerColor.RED, GameStateTest.test_board_valid_col1, 0),
             (PlayerColor.BROWN, GameStateTest.test_board_valid_col3, 5)],
            turn=PlayerColor.BROWN)
        # Valid move from penguin at 3,3 to 4,2
        game_state.move_penguin(PlayerColor.BROWN, (4, 2), (3, 1))
        # Valid move for next turn
        game_state.move_penguin(PlayerColor.RED, (1, 1), (2, 0))

        # attempt to move out of order
        with self.assertRaises(ValueError):
            game_state.move_penguin(PlayerColor.RED, (2, 0), (2, 2))

    def test_moving_penguin_to_invalid_or_existing(self):
        """
        Purpose: Test what happens when you attempt to move a penguin to an invalid spot.
        Signature: Void -> Void
        """
        small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
        game_state = FishGameStateFactory.create_move_penguins_state(
            small_board,
            [(PlayerColor.RED, GameStateTest.test_board_valid_col1, 0),
             (PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 5)])
        # Moving to a valid spot with another penguin on it
        with self.assertRaises(ValueError):
            game_state.move_penguin(PlayerColor.BROWN, (2, 2), (3, 1))
        # Moving to an invalid spot
        with self.assertRaises(ValueError):
            game_state.move_penguin(PlayerColor.BROWN, (2, 2), (4, 0))

    def test_moving_penguin_from_invalid_or_nonexisting(self):
        """
        Purpose: Test what happens when you attempt to move a penguin from an invalid spot
        (No existing penguin, no tile, not on board, or another player's penguin)
        Signature: Void -> Void
        """
        small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
        game_state = FishGameStateFactory.create_move_penguins_state(
            small_board,
            [(PlayerColor.BLACK, GameStateTest.test_board_valid_col1, 5),
             (PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 0)])
        # No penguin
        with self.assertRaises(PenguinMovementError):
            game_state.move_penguin(PlayerColor.BLACK, (1, 4), (2, 3))
        # Not on board
        with self.assertRaises(PenguinMovementError):
            game_state.move_penguin(PlayerColor.BLACK, (10, 10), (11, 9))
        # Other player's penguin
        with self.assertRaises(PenguinMovementError):
            game_state.move_penguin(PlayerColor.BLACK, (3, 3), (4, 2))

    def test_placing_penguin_invalid(self):
        """
        Purpose: Test what happens when you place a penguin where one was already placed, where a tile doesn't exist,
        or off of the board completely.
        Signature: Void -> Void
        """

        test_board_hole = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
        test_board_hole.remove_tile(0, 0)
        game_state = FishGameStateFactory.create_place_penguins_state(
            test_board_hole, [(PlayerColor.BLACK, []),
                              (PlayerColor.BROWN, [])])
        # placing at position off board
        with self.assertRaises(PenguinPlacementError):
            game_state.place_penguin(PlayerColor.BLACK, (-1, -1))
        with self.assertRaises(PenguinPlacementError):
            game_state.place_penguin(PlayerColor.BLACK, (10, 10))
        # placing where penguin already exists
        game_state.place_penguin(PlayerColor.BLACK, (1, 1))
        with self.assertRaises(PenguinPlacementError):
            game_state.place_penguin(PlayerColor.BROWN, (1, 1))
        # placing where no tile
        with self.assertRaises(PenguinPlacementError):
            game_state.place_penguin(PlayerColor.BROWN, (0, 0))

    def test_placing_penguin_valid(self):
        """
        Purpose: Test what happens when you place a penguin in a valid tile on the board.
        Signature: Void -> Void
        """
        test_board_hole = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
        test_board_hole.remove_tile(0, 0)
        game_state = FishGameStateFactory.create_place_penguins_state(
            test_board_hole, [(PlayerColor.BLACK, []),
                              (PlayerColor.BROWN, [])])
        game_state.place_penguin(PlayerColor.BLACK, (1, 1))
        game_state.place_penguin(PlayerColor.BROWN, (2, 2))
        game_state.place_penguin(PlayerColor.BLACK, (3, 3))

    def test_if_players_can_move(self):
        """
        Purpose: Test the state if a player can't move, if 1 player can move, and where more than one player can move.
        Signature: Void -> Void
        """
        test_board = FishBoardModel.create_with_same_fish_amount(4, 3, 3)
        game_state = FishGameStateFactory.create_place_penguins_state(
            test_board, [(PlayerColor.BLACK, []), (PlayerColor.BROWN, [])])
        game_state.place_penguin(PlayerColor.BLACK, (0, 0))
        game_state.place_penguin(PlayerColor.BROWN, (1, 1))
        self.assertEqual(True, game_state.can_any_player_move())

        # Remove tiles
        test_board.remove_tile(2, 2)
        test_board.remove_tile(2, 0)
        test_board.remove_tile(0, 2)
        test_board.remove_tile(1, 3)
        game_state_holes = FishGameStateFactory.create_place_penguins_state(
            test_board, [(PlayerColor.BLACK, [(0, 0)]),
                         (PlayerColor.BROWN, [(1, 1)])])
        self.assertEqual(False, game_state_holes.can_any_player_move())

    def test_get_fish(self):
        """
        Purpose: Test getting a player's fish before and after they move on a board
        Signature: Void -> Void
        """
        small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
        game_state = FishGameStateFactory.create_move_penguins_state(
            small_board,
            [(PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 5),
             (PlayerColor.BLACK, GameStateTest.test_board_valid_col1, 0)])
        self.assertEqual(5, game_state.get_fish_for_player(PlayerColor.BROWN))
        # Valid move from penguin at 3,3 to 4,2
        game_state.move_penguin(PlayerColor.BROWN, (3, 3), (4, 2))
        self.assertEqual(8, game_state.get_fish_for_player(PlayerColor.BROWN))

    def test_add_fish(self):
        """
        Purpose: Test getting a player's fish before and after adding fish to a player
        Signature: Void -> Void
        """
        small_board = FishBoardModel.create_with_same_fish_amount(4, 5, 3)
        game_state = FishGameStateFactory.create_move_penguins_state(
            small_board,
            [(PlayerColor.BLACK, GameStateTest.test_board_valid_col1, 0),
             (PlayerColor.BROWN, GameStateTest.test_board_valid_col2, 5)])
        self.assertEqual(5, game_state.get_fish_for_player(PlayerColor.BROWN))
        game_state.add_fish_to_player(PlayerColor.BROWN, 6)
        self.assertEqual(11, game_state.get_fish_for_player(PlayerColor.BROWN))
        with self.assertRaises(ValueError):
            game_state.add_fish_to_player(PlayerColor.BROWN, -10)

    def test_check_penguin_amount(self):
        """
        Purpose: Test the check penguin's amount function with valid and invalid arguments
        Signature: Void -> Void
        """
        game_state = FishGameStateFactory.create_place_penguins_state(
            GameStateTest.test_board, [(PlayerColor.BLACK, []),
                                       (PlayerColor.BROWN, [])])
        # Situation where not all penguins placed
        with self.assertRaises(ValueError):
            game_state.check_penguin_amount(
                2, {
                    PlayerColor.BLACK:
                    game_state.get_penguins_for_player(PlayerColor.BLACK),
                    PlayerColor.BROWN:
                    game_state.get_penguins_for_player(PlayerColor.BROWN)
                }, True)

        # Situation where not all penguins placed but not needed
        game_state.check_penguin_amount(
            2, {
                PlayerColor.BLACK:
                game_state.get_penguins_for_player(PlayerColor.BLACK),
                PlayerColor.BROWN:
                game_state.get_penguins_for_player(PlayerColor.BROWN)
            }, False)

        # Situation where all penguins placed
        game_state.set_turn(PlayerColor.BROWN)
        game_state.place_penguin(PlayerColor.BROWN, (0, 0),
                                 increase_turn=False)
        game_state.place_penguin(PlayerColor.BROWN, (1, 1),
                                 increase_turn=False)
        game_state.place_penguin(PlayerColor.BROWN, (0, 2),
                                 increase_turn=False)
        game_state.place_penguin(PlayerColor.BROWN, (2, 0),
                                 increase_turn=False)

        game_state.set_turn(PlayerColor.BLACK)
        game_state.place_penguin(PlayerColor.BLACK, (2, 2),
                                 increase_turn=False)
        game_state.place_penguin(PlayerColor.BLACK, (3, 3),
                                 increase_turn=False)
        game_state.place_penguin(PlayerColor.BLACK, (3, 1),
                                 increase_turn=False)
        game_state.place_penguin(PlayerColor.BLACK, (1, 3),
                                 increase_turn=False)
        game_state.check_penguin_amount(
            2, {
                PlayerColor.BLACK:
                game_state.get_penguins_for_player(PlayerColor.BLACK),
                PlayerColor.BROWN:
                game_state.get_penguins_for_player(PlayerColor.BROWN)
            }, True)

    def test_remove_invalid_color_from_game(self):
        """
        Purpose: Tests the remove invalid color from game function when an invalid color (not in the game) is removed.
        Signature: Void -> Void
        """
        game_state = FishGameStateFactory.create_place_penguins_state(
            GameStateTest.test_board, [(PlayerColor.BLACK, []),
                                       (PlayerColor.BROWN, [])])
        # check if color is not in game
        with self.assertRaises(ValueError):
            game_state.remove_color_from_game(PlayerColor.RED)

    def test_remove_color_from_game_player_info(self):
        """
        Purpose: Tests removing a color from the game, checks to see if the Player Color would be removed from the players
        in the state
        Signature: Void -> Void
        """
        game_state = FishGameStateFactory.create_place_penguins_state(
            GameStateTest.test_board, [(PlayerColor.BLACK, []),
                                       (PlayerColor.BROWN, [])])
        # check if removed from player info
        game_state.remove_color_from_game(PlayerColor.BROWN)
        brown_not_in_players = PlayerColor.BROWN in game_state._players.keys()
        black_in_players = PlayerColor.BLACK in game_state._players.keys()
        self.assertFalse(brown_not_in_players)
        self.assertTrue(black_in_players)

    def test_remove_color_from_game_next_player(self):
        """
        Purpose: Tests removing a color from the game, checks to see if the current turn will shift to be the next player's
        turn once the color is removed
        Signature: Void -> Void
        """
        game_state = FishGameStateFactory.create_place_penguins_state(
            GameStateTest.test_board, [(PlayerColor.BLACK, []),
                                       (PlayerColor.BROWN, [])])
        turn_before = game_state.get_current_turn()
        self.assertEqual(PlayerColor.BLACK, turn_before)
        game_state.remove_color_from_game(PlayerColor.BLACK)
        turn_after = game_state.get_current_turn()
        self.assertEqual(PlayerColor.BROWN, turn_after)

    def test_remove_color_from_game_order(self):
        """
        Purpose: Tests removing a color from a game, then checks if the state's game order has changed once a player color
        is removed from the game.
        Signature: Void -> Void
        """
        # check if removed from order
        game_state = FishGameStateFactory.create_place_penguins_state(
            GameStateTest.test_board, [(PlayerColor.BLACK, []),
                                       (PlayerColor.BROWN, [])])
        order_before = game_state.get_player_order()
        self.assertEqual([PlayerColor.BLACK, PlayerColor.BROWN], order_before)
        game_state.remove_color_from_game(PlayerColor.BLACK)
        order_after = game_state.get_player_order()
        self.assertEqual([PlayerColor.BROWN], order_after)
示例#22
0
import sys
import os

sys.path.insert(
    0,
    os.path.abspath(os.path.join(os.path.dirname(__file__), '..', '..', '..')))

from Fish.Common.representations.enumerations.player_color_enum import PlayerColor
from Fish.Common.representations.fish_board import FishBoardModel
from Fish.Common.representations.game_state import FishGameStateFactory
from Fish.Common.views.state_view import FishGameStateView

board = FishBoardModel.create_with_same_fish_amount(3, 3, 3)
placed_penguins = [(PlayerColor.RED, [(0, 0)]), (PlayerColor.WHITE, [(2, 2)]),
                   (PlayerColor.BROWN, [(1, 1)]),
                   (PlayerColor.BLACK, [(2, 0)])]
state = FishGameStateFactory.create_place_penguins_state(
    board, placed_penguins)
FishGameStateView.display_state(state)