コード例 #1
0
    def test_initialize_board_invalid_tile_type(self):
        # Test failure to initialize board due to the board containing an invalid type

        # Invalid json board representation
        invalid_json = json.loads("[[1, 0, 0], [\"one\", 0, 0]]")

        with self.assertRaises(TypeError):
            result_board = xboard.initialize_board(invalid_json)
コード例 #2
0
    def test_initialize_board_invalid_tile_no(self):
        # Test failure to initialize board due to a board containing a number outside the acceptable range

        # Invalid json board representation
        invalid_json = json.loads("[[1, 0, 0], [1, 8, 0]]")

        with self.assertRaises(ValueError):
            result_board = xboard.initialize_board(invalid_json)
コード例 #3
0
    def test_initialize_board_padding2(self):
        # Test initializing board with the need for padding less readily apparent

        # Invalid json board representation
        valid_json = json.loads(
            "[[1, 3, 4], [1, 2], [1, 0, 3, 5, 4], [0, 2, 3]]")

        # Board obtained from the initialize_board function
        result_board = xboard.initialize_board(valid_json)

        # Test properties of the board
        self.assertTrue(isinstance(result_board, Board))
        self.assertEquals(result_board.rows, 4)
        self.assertEquals(result_board.cols, 5)
        self.assertEquals(result_board.tile_no, 20)

        # Expected tile dict
        expected_tiles = \
        {
            Position(0, 0): Tile(1),
            Position(0, 1): Tile(3),
            Position(0, 2): Tile(4),
            Position(0, 3): Hole(),
            Position(0, 4): Hole(),
            Position(1, 0): Tile(1),
            Position(1, 1): Tile(2),
            Position(1, 2): Hole(),
            Position(1, 3): Hole(),
            Position(1, 4): Hole(),
            Position(2, 0): Tile(1),
            Position(2, 1): Hole(),
            Position(2, 2): Tile(3),
            Position(2, 3): Tile(5),
            Position(2, 4): Tile(4),
            Position(3, 0): Hole(),
            Position(3, 1): Tile(2),
            Position(3, 2): Tile(3),
            Position(3, 3): Hole(),
            Position(3, 4): Hole()
        }

        # Verify all the positions in the board are the same as expected
        self.assertCountEqual(expected_tiles.keys(), result_board.tiles.keys())

        # Verify that all tiles have the correct number of fish
        for key in expected_tiles:
            tile = expected_tiles[key]

            # Tile on initialized board
            actual_tile = result_board.get_tile(key)

            # Assert both tiles are the same type
            self.assertEqual(tile.is_tile, actual_tile.is_tile)

            # If the tiles are both tiles, assert that the ones on the board
            # have the expected number of fish
            if tile.is_tile:
                self.assertEqual(tile.fish_no, actual_tile.fish_no)
コード例 #4
0
    def test_initialize_board_success(self):
        # Test successful validation of a board object from valid board json

        # Create json board object based on the provided representation
        valid_json = json.loads("[[1, 0], [3, 5], [2, 4]]")

        # Board obtained from the initialize_board function
        result_board = xboard.initialize_board(valid_json)

        # Test properties of the board
        self.assertTrue(isinstance(result_board, Board))
        self.assertEquals(result_board.rows, 3)
        self.assertEquals(result_board.cols, 2)
        self.assertEquals(result_board.tile_no, 6)

        # Expected tile dict
        expected_tiles = \
        {
            Position(0, 0): Tile(1),
            Position(0, 1): Hole(),
            Position(1, 0): Tile(3),
            Position(1, 1): Tile(5),
            Position(2, 0): Tile(2),
            Position(2, 1): Tile(4)
        }

        # Verify all the positions in the board are the same as expected
        self.assertCountEqual(expected_tiles.keys(), result_board.tiles.keys())

        # Verify that all tiles have the correct number of fish
        for key in expected_tiles:
            tile = expected_tiles[key]

            # Tile on initialized board
            actual_tile = result_board.get_tile(key)

            # Assert both tiles are the same type
            self.assertEqual(tile.is_tile, actual_tile.is_tile)

            # If the tiles are both tiles, assert that the ones on the board
            # have the expected number of fish
            if tile.is_tile:
                self.assertEqual(tile.fish_no, actual_tile.fish_no)
コード例 #5
0
def initialize_state(json_obj: dict) -> State:
    """
    Initializes a State object from the given json representation of a state.

    :param json_obj: json object (dictionary) containing player list and board
    :return: the state described by the json obj as a State object
    """
    if not isinstance(json_obj, dict):
        raise TypeError('Expected dict for json_obj!')

    if 'players' not in json_obj.keys():
        raise ValueError('Expected players in object!')

    if 'board' not in json_obj.keys():
        raise ValueError('Expected board in object!')

    # Retrieve player list
    player_list = json_obj['players']
    board_json = json_obj['board']

    # Get board from json
    board = initialize_board(board_json)

    # Initialize empty collection to hold players
    players = []

    # Initialize collection to hold player ids and their
    # avatar placements
    player_placements = {}

    # Cycle over each json object in the json list
    for player in player_list:
        # Make up Player object
        new_player = PlayerEntity("", _str_to_color(player['color']))

        # Update player score to whatever the current score is in the state
        new_player.score = player['score']

        # Add player object to players list
        players.append(new_player)

        # Insert placement
        player_placements.update(
            {_str_to_color(player['color']): player['places']})

    # Make up state with board and players
    state = State(board, players)

    # Determine the maximum number of avatars for a single player
    avatars_per_player_no = max(
        map(lambda x: len(x), player_placements.values()))

    # For each i in the number of avatars per player
    for i in range(avatars_per_player_no):
        # For each player, place i-th avatar if possible
        for p_color, p_placements in player_placements.items():
            # Retrieve current player's i-th avatar
            try:
                placement = player_placements[p_color][i]

                # Convert to Position object
                position_to_place = Position(placement[0], placement[1])

                # Place current player's avatar at position
                state.place_avatar(p_color, position_to_place)
            except IndexError:
                continue

    return state