예제 #1
0
    def test_blob_goal_4(self, board_2x2_2) -> None:
        correct_scores = [(COLOUR_LIST[0], 0), (COLOUR_LIST[1], 8),
                          (COLOUR_LIST[2], 8), (COLOUR_LIST[3], 0)]

        for colour, expected in correct_scores:
            goal = BlobGoal(colour)
            assert goal.score(board_2x2_2) == expected
예제 #2
0
    def test_blob_goal_2(self, board_4x4_3) -> None:
        correct_scores = [(COLOUR_LIST[0], 12), (COLOUR_LIST[1], 12),
                          (COLOUR_LIST[2], 4), (COLOUR_LIST[3], 24)]

        for colour, expected in correct_scores:
            goal = BlobGoal(colour)
            assert goal.score(board_4x4_3) == expected
예제 #3
0
    def set_players(self, num_human, random_players, smart_players):
        """Generate a common goal with different target colours for each players

        Each player gets player_id from 0 to total number of players.
        Add all of players to self.players
        """
        rand_goal, num = random.randint(0, 1), 0
        for _ in range(num_human):
            if rand_goal == 1:
                goal = BlobGoal(COLOUR_LIST[random.randint(0, 3)])
            else:
                goal = PerimeterGoal(COLOUR_LIST[random.randint(0, 3)])
            new_player = HumanPlayer(self.renderer, num, goal)
            self.renderer.display_goal(new_player)
            self.players.append(new_player)
            num += 1
        for _ in range(random_players):
            if rand_goal == 1:
                goal = BlobGoal(COLOUR_LIST[random.randint(0, 3)])
            else:
                goal = PerimeterGoal(COLOUR_LIST[random.randint(0, 3)])
            self.players.append(RandomPlayer(self.renderer, num, goal))
            num += 1
        for i in range(len(smart_players)):
            if rand_goal == 1:
                goal = BlobGoal(COLOUR_LIST[random.randint(0, 3)])
            else:
                goal = PerimeterGoal(COLOUR_LIST[random.randint(0, 3)])
            self.players.append(
                SmartPlayer(self.renderer, num, goal, smart_players[i]))
            num += 1
예제 #4
0
파일: test.py 프로젝트: Fenil-P/Blocky
 def test_BG_many_blob_not_connected(self) -> None:
     gol = BlobGoal(PACIFIC_POINT)
     borde = standard_borde()
     # print(borde)
     borde.children[2].children[0] = Block((100, 200), 100, OLD_OLIVE, 2, 3)
     # print(borde)
     assert gol.score(borde) == 20
     assert borde.children[2].children[0].smash() is True
예제 #5
0
    def test_blob_goal_unit(self, child_block) -> None:
        correct_scores = [(COLOUR_LIST[0], 1), (COLOUR_LIST[1], 0),
                          (COLOUR_LIST[2], 0), (COLOUR_LIST[3], 0)]

        # Set up a goal for each colour and check the results
        for colour, expected in correct_scores:
            goal = BlobGoal(colour)
            assert goal.score(child_block) == expected
    def test_blob_goal(self, board_16x16) -> None:
        correct_scores = [(COLOUR_LIST[0], 1), (COLOUR_LIST[1], 4),
                          (COLOUR_LIST[2], 4), (COLOUR_LIST[3], 5)]

        # Set up a goal for each colour and check the results
        for colour, expected in correct_scores:
            goal = BlobGoal(colour)
            assert goal.score(board_16x16) == expected
예제 #7
0
def test_blob_goal_score() -> None:
    """ Test the BlobGoal class' score method. """
    goal = BlobGoal((255, 255, 255))
    assert goal.score(Block((0, 0), 250, (255, 255, 255), 0, 0)) == 1

    board = Block((0, 0), 1000, None, 0, 2)
    board.children = [Block((500, 0), 500, (0, 255, 0), 1, 2),
                      Block((0, 0), 500, (0, 0, 0), 1, 2),
                      Block((0, 500), 500, (255, 255, 255), 1, 2),
                      Block((500, 500), 500, (0, 255, 0), 1, 2)]
    assert goal.score(board) == 4

    board.children[3].children = [Block((750, 500), 250, (0, 0, 0), 2, 2),
                                  Block((500, 500), 250, (0, 0, 0), 2, 2),
                                  Block((500, 750), 250, (55, 0, 0), 2, 2),
                                  Block((750, 750), 250, (0, 0, 0), 2, 2)]
    board.children[3].colour = None

    assert goal.score(board) == 4

    goal2 = BlobGoal((0, 0, 0))

    assert goal2.score(board) == 4

    board.children[0].colour = (0, 0, 0)

    assert goal2.score(board) == 11
예제 #8
0
    def __init__(self, max_depth: int, num_human: int, random_players: int,
                 smart_players: List[int]) -> None:
        """Initialize this game, as described in the Assignment 2 handout.

        Precondition:
            2 <= max_depth <= 5
        """
        self.max_depth = max_depth
        self.num_human = num_human
        self.random_players = random_players
        self.smart_players = smart_players
        self.num_players = num_human + random_players + len(smart_players)

        # 1 create a Renderer for this game
        self.renderer = Renderer(self.num_players)

        # 2 Generate a random goal typr, for all players to share
        potential_goal = [
            BlobGoal(COLOUR_LIST[0]),
            PerimeterGoal(COLOUR_LIST[0])
        ]
        self.goal = potential_goal[random.randint(0, 1)]

        # 3 Generate a random board, with the given maximum depth
        self.board = random_init(0, max_depth)
        self.board.update_block_locations((0, 0), 750)

        # 4 Generate the right number of human players, random players,
        # and smart players
        self.players = []
        for human_id in range(num_human):
            self.players.append(HumanPlayer(self.renderer, human_id,
                                            self.goal))
        for ran_id in range(num_human, num_human + random_players):
            self.players.append(RandomPlayer(self.renderer, ran_id, self.goal))
        for smart_id in range(num_human + random_players, self.num_players):
            difficulty_level = smart_players[smart_id - num_human -
                                             random_players]
            self.players.append(
                SmartPlayer(self.renderer, smart_id, self.goal,
                            difficulty_level))

        for player in self.players:
            player.goal.colour = COLOUR_LIST[random.randint(0, 3)]
            if isinstance(self.goal, BlobGoal):
                player.goal = BlobGoal(player.goal.colour)
            else:
                player.goal = PerimeterGoal(player.goal.colour)
            if isinstance(player, HumanPlayer):
                self.renderer.display_goal(player)

        # 5 before returning, draw the board
        for player_id in range(len(self.players)):
            self.renderer.draw(self.board, player_id)
예제 #9
0
def test_JEN_smart_player(board_16x16) -> None:
    goal = BlobGoal((138, 151, 71))
    player = SmartPlayer(2, goal, 2)
    player._proceed = True
    score1 = goal.score(board_16x16)
    move = player.generate_move(board_16x16)
    while move is SMASH:
        move.generate_move()
    score2 = goal.score(board_16x16)
    assert move is not None
    assert score2 >= score1
예제 #10
0
def test_undiscovered_blob_size() -> None:
    board = [[(199, 44, 58), (199, 44, 58), (199, 44, 58), (255, 211, 92)],
             [(199, 44, 58), (199, 44, 58), (1, 128, 181), (199, 44, 58)],
             [(1, 128, 181), (1, 128, 181), (199, 44, 58), (255, 211, 92)],
             [(1, 128, 181), (1, 128, 181), (255, 211, 92), (199, 44, 58)]]
    visited = [[-1, -1, -1, -1], [-1, -1, -1, -1], [-1, -1, -1, -1],
               [-1, -1, -1, -1]]
    pos = (0, 2)
    goal1 = BlobGoal((199, 44, 58))
    result = goal1._undiscovered_blob_size(pos, board, visited)
    assert result == 5
    assert visited[1][1] == 1
예제 #11
0
    def __init__(self, max_depth: int, num_human: int, random_players: int,
                 smart_players: List[int]) -> None:
        """Initialize this game, as described in the Assignment 2 handout.

        Precondition:
            2 <= max_depth <= 5
        """
        self.board = random_init(0, max_depth)
        self.board.update_block_locations((0, 0), BOARD_WIDTH)
        num_players = num_human + random_players + len(smart_players)
        self.renderer = Renderer(num_players)
        self.players = []
        if random.random() < 0.5:
            for i in range(num_human):
                target = \
                    COLOUR_LIST[int(random.random() * len(COLOUR_LIST))]
                self.players.append(HumanPlayer(self.renderer, \
                                                len(self.players), \
                                                BlobGoal(target)))
            for i in range(random_players):
                target = \
                    COLOUR_LIST[int(random.random() * len(COLOUR_LIST))]
                self.players.append(RandomPlayer(self.renderer, \
                                                 len(self.players), \
                                                 BlobGoal(target)))
            for i in smart_players:
                target = \
                    COLOUR_LIST[int(random.random() * len(COLOUR_LIST))]
                self.players.append(SmartPlayer(self.renderer, \
                                                len(self.players), \
                                                BlobGoal(target), \
                                                i))
        else:
            for i in range(num_human):
                target = \
                    COLOUR_LIST[int(random.random() * len(COLOUR_LIST))]
                self.players.append(HumanPlayer(self.renderer, \
                                                len(self.players), \
                                                PerimeterGoal(target)))
            for i in range(random_players):
                target = \
                    COLOUR_LIST[int(random.random() * len(COLOUR_LIST))]
                self.players.append(RandomPlayer(self.renderer, \
                                                 len(self.players), \
                                                 PerimeterGoal(target)))
            for i in smart_players:
                target = \
                    COLOUR_LIST[int(random.random() * len(COLOUR_LIST))]
                self.players.append(SmartPlayer(self.renderer, \
                                                len(self.players), \
                                                PerimeterGoal(target), \
                                                i))
        self.renderer.draw(self.board, 0)
예제 #12
0
def test_blob_goal_single_board(single_board) -> None:
    correct_scores = [
        (COLOUR_LIST[0], 1),
        (COLOUR_LIST[1], 0),
        (COLOUR_LIST[2], 0),
        (COLOUR_LIST[3], 0)
    ]

    # Set up a goal for each colour and check the results
    for colour, expected in correct_scores:
        goal = BlobGoal(colour)
        assert goal.score(single_board) == expected
예제 #13
0
def test_blob_goal():
    """Test the blob goal for the given board
    """
    board, _ = construct_board()

    # On the hand-constructed board, these are the correct scores for
    # each colour.
    correct_scores = [(COLOUR_LIST[0], 1), (COLOUR_LIST[1], 4),
                      (COLOUR_LIST[2], 4), (COLOUR_LIST[3], 5)]

    # Set up a goal for each colour and check results.
    for colour, score in correct_scores:
        goal = BlobGoal(colour)
        assert goal.score(board) == score
예제 #14
0
    def __init__(self, max_depth: int, num_human: int, random_players: int,
                 smart_players: List[int]) -> None:
        """Initialize this game, as described in the Assignment 2 handout.

        Each player has a random target colour, and all players share the
        same goal(Players might have same target colour.)

        Precondition:
            2 <= max_depth <= 5
        """
        self.board = random_init(0, max_depth)
        self.board.update_block_locations((0, 0), BOARD_WIDTH)
        num_player = num_human + random_players + len(smart_players)
        self.renderer = Renderer(num_player)
        self.players = []

        random_num = random.randint(0, 1)
        for i in range(num_player):
            if random_num == 0:
                goal = BlobGoal(COLOUR_LIST[random.randint(0, 3)])
            else:
                goal = PerimeterGoal(COLOUR_LIST[random.randint(0, 3)])
            if i < num_human:
                self.players.append(HumanPlayer(self.renderer, i, goal))
            elif num_human <= i < num_human + random_players:
                self.players.append(RandomPlayer(self.renderer, i, goal))
            else:
                self.players.append(
                    SmartPlayer(
                        self.renderer, i, goal,
                        smart_players[i - (num_human + random_players)]))
        # Display each player's goal before starting game.
        for i in range(num_player):
            self.renderer.display_goal(self.players[i])
예제 #15
0
def test_random_player_generate_move() -> None:
    """ Test the RandomPlayer class' generate_move method. """
    goal = BlobGoal((0, 0, 0))
    player1 = RandomPlayer(1, goal)
    player1._proceed = True
    b1 = Block((0, 0), 1000, (0, 0, 0), 0, 2)

    move1 = player1.generate_move(b1)

    assert isinstance(move1, tuple) and isinstance(move1[0], str) and \
           (isinstance(move1[1], int) or move1[1] is None) and \
           isinstance(move1[2], Block)

    if move1[0:2] == ('rotate', 1):
        assert move1[2].rotate(1)
    elif move1[0:2] == ('rotate', 3):
        assert move1[2].rotate(3)
    elif move1[0:2] == ('swap', 0):
        assert move1[2].swap(0)
    elif move1[0:2] == ('swap', 1):
        assert move1[2].swap(1)
    elif move1[0:2] == ('smash', None):
        assert move1[2].smash()
    elif move1[0:2] == ('paint', None):
        assert move1[2].paint(self.goal.colour)
    elif move1[0:2] == ('combine', None):
        assert move1[2].combine()
    else:
        assert False
예제 #16
0
    def _generate_players(self, num_players: int, player_type: str,
                          goal_type: str, start_id: int) -> List[Player]:
        """Generate a list of players with the given number of players,
        of the given type, with the given <goal_type>, with ids incrementing
        from <start_id>.

        Preconditions:
            player_type == 'human', 'random', or 'smart'
            goal_type == 'blob', or 'perimeter'
        """
        player_list = []
        player_count = 0
        id_count = start_id

        while player_count < num_players:
            # Generate a blob goal or a perimeter goal with a random colour
            if goal_type == 'blob':
                goal = BlobGoal(COLOUR_LIST[random.randint(0, 3)])
            else:
                goal = PerimeterGoal(COLOUR_LIST[random.randint(0, 3)])

            # Generate a player of the given type, using goal
            if player_type == 'human':
                player = HumanPlayer(self.renderer, id_count, goal)
            elif player_type == 'random':
                player = RandomPlayer(self.renderer, id_count, goal)
            else:
                player = SmartPlayer(self.renderer, id_count, goal)

            # Add player to the player list, iterate counts by one
            player_list.append(player)
            player_count += 1
            id_count += 1

        return player_list
예제 #17
0
파일: test.py 프로젝트: jaspermai/Blocky
def test_blobgoal__undiscovered_blob_size() -> None:
    """Test BlobGoal._undiscovered_blob_size.
    - gives largest blob of the colour and position
    - updates visited
    """
    block = Block((0, 0), 100, (1, 128, 181), 0, 1)
    assert block.smash()
    block.children[0].colour = (1, 128, 181)
    block.children[1].colour = (1, 128, 181)
    block.children[2].colour = (0, 0, 0)
    block.children[3].colour = (0, 0, 0)
    goal = BlobGoal((1, 128, 181))
    flattened = _flatten(block)
    visited = [[-1, -1], [-1, -1]]
    assert goal._undiscovered_blob_size((1, 0), flattened, visited) == 2
    assert visited == [[1, 0], [1, 0]]
예제 #18
0
def test_JEN_random_player(board_16x16) -> None:
    goal = BlobGoal((138, 151, 71))
    player = SmartPlayer(2, goal, 2)
    player._proceed = True
    move = player.generate_move(board_16x16)
    assert move is PASS or SMASH or COMBINE or ROTATE_COUNTER_CLOCKWISE or \
    ROTATE_CLOCKWISE or SWAP_VERTICAL or SWAP_HORIZONTAL
예제 #19
0
def test__undiscovered_blob_size() -> None:
    """ Test the BlobGoal class' _undiscovered_blob_size method. """
    goal1 = BlobGoal((0, 0, 0))
    pos1 = (0, 0)
    board1 = [[(0, 0, 0), (0, 0, 0)],
              [(0, 0, 0), (0, 0, 0)]]
    visited1 = [[-1, -1],
                [-1, -1]]

    assert goal1._undiscovered_blob_size(pos1, board1, visited1) == 4

    assert visited1 == [[1, 1], [1, 1]]

    assert goal1._undiscovered_blob_size(pos1, board1, visited1) == 0

    goal2 = BlobGoal((0, 255, 0))
    pos2 = (0, 0)
    board2 = [[(0, 255, 0), (0, 0, 0)],
              [(0, 0, 0), (0, 255, 0)]]
    visited2 = [[-1, -1],
                [-1, -1]]

    assert goal2._undiscovered_blob_size(pos2, board2, visited2) == 1

    assert visited2 == [[1, 0], [0, -1]]

    assert goal2._undiscovered_blob_size(pos2, board2, visited2) == 0

    goal3 = goal1  # (0, 0, 0)
    pos3 = (1, 1)
    board3 = [[(0, 255, 0), (0, 0, 0), (0, 255, 0)],
              [(0, 0, 0), (0, 255, 0), (0, 255, 0)],
              [(0, 255, 0), (0, 0, 0), (0, 255, 0)]]
    visited3 = [[-1, -1, -1],
                [-1, -1, -1],
                [-1, -1, -1]]

    assert goal3._undiscovered_blob_size(pos3, board3, visited3) == 0

    assert visited3 == [[-1, -1, -1], [-1, 0, -1], [-1, -1, -1]]

    assert goal3._undiscovered_blob_size(pos3, board3, visited3) == 0

    goal4 = goal1  # (0, 0, 0)
    pos4 = (0, 1)
    board4 = [[(0, 0, 0), (0, 0, 0), (0, 255, 0)],
              [(0, 0, 0), (0, 255, 0), (0, 255, 0)],
              [(0, 0, 0), (0, 0, 0), (0, 255, 0)]]
    visited4 = [[1, -1, -1],
                [-1, -1, -1],
                [-1, 1, -1]]

    assert goal4._undiscovered_blob_size(pos4, board4, visited4) == 1

    assert visited4 == [[1, 1, 0], [-1, 0, -1], [-1, 1, -1]]

    assert goal4._undiscovered_blob_size(pos4, board4, visited4) == 0
예제 #20
0
def test_depth_5_board(renderer):
    random.seed(1002)
    board = generate_board(5, BOARD_SIZE)
    renderer.draw_board(_block_to_squares(board))
    renderer.save_to_file('board_5_ref.png')
    renderer.clear()
    goal1 = BlobGoal(COLOUR_LIST[0])  # blue
    goal2 = BlobGoal(COLOUR_LIST[1])  # red
    player1 = RandomPlayer(1, goal1)
    player2 = RandomPlayer(2, goal2)
    player1._proceed = True
    move1 = player1.generate_move(board)
    move1_block = move1[2]
    to_do = _get_block(board, move1_block.position, move1_block.level)
    assert move1[0] == "swap" and move1[1] == 0
    assert to_do.swap(0)
    renderer.draw_board(_block_to_squares(board))
    renderer.save_to_file('board_5_move1.png')
    renderer.clear()
    afterfirst1 = goal1.score(board)
    afterfirst2 = goal2.score(board)
    player2._proceed = True
    move2 = player2.generate_move(board)
    move2_block = move2[2]
    to_do_2 = _get_block(board, move2_block.position, move2_block.level)
    assert move2[0] == "smash"
    assert to_do_2.smash()
    renderer.draw_board(_block_to_squares(board))
    renderer.save_to_file('board_5_move2.png')
    renderer.clear()
    aftersecond1 = goal1.score(board)
    aftersecond2 = goal2.score(board)
    player1._proceed = True
    move3 = player1.generate_move(board)
    move3_block = move3[2]
    to_do_3 = _get_block(board, move3_block.position, move3_block.level)
    assert move3[0] == "rotate" and move3[1] == 3
    assert to_do_3.rotate(3)
    renderer.draw_board(_block_to_squares(board))
    renderer.save_to_file('board_5_move3.png')
    renderer.clear()
    afterthird1 = goal1.score(board)
    afterthird2 = goal2.score(board)
예제 #21
0
def test_random_player__init__() -> None:
    """ Test the __init__ method of the RandomPlayer class. """
    goal = BlobGoal((0, 0, 0))
    player = RandomPlayer(1, goal)

    assert player.id == 1

    assert player.goal is goal

    assert not player._proceed
예제 #22
0
def test_score_blob_goal_02() -> None:
    b = Block((0, 0), 500, None, 0, 2)
    b.children = [Block(b._children_positions()[0], 250, None, 1, 2),
                  Block(b._children_positions()[1], 250, COLOUR_LIST[1], 1, 2),
                  Block(b._children_positions()[2], 250, COLOUR_LIST[2], 1, 2),
                  Block(b._children_positions()[3], 250, COLOUR_LIST[3], 1, 2)]
    b.children[0].children = [Block(b.children[0]._children_positions()[0], 125,
                                    COLOUR_LIST[2], 2, 2),
                              Block(b.children[0]._children_positions()[1],
                                    125, COLOUR_LIST[0], 2, 2),
                              Block(b.children[0]._children_positions()[2], 125,
                                    COLOUR_LIST[2], 2, 2),
                              Block(b.children[0]._children_positions()[3], 125,
                                    COLOUR_LIST[3], 2, 2)]
    P = BlobGoal(COLOUR_LIST[3])
    assert P.score(b) == 5
    b.children[0].children[2].paint(COLOUR_LIST[3])
    assert P.score(b) == 6
    b.children[1].colour = COLOUR_LIST[3]
    assert P.score(b) == 10
예제 #23
0
def test_player(game_board2):
    random.seed(1)
    goal = BlobGoal(COLOUR_LIST[1])
    player = RandomPlayer(1, goal)
    player._proceed = True
    move = player.generate_move(game_board2)
    print(move)
    player2 = SmartPlayer(1, goal, 100)
    player2._proceed = True
    move2 = player2.generate_move(game_board2)
    print(move2)
예제 #24
0
def test_unit_board():
    random.seed(1)
    board = generate_board(1, BOARD_SIZE)
    goal = BlobGoal(COLOUR_LIST[1])
    player = RandomPlayer(1, goal)
    player._proceed = True
    move = player.generate_move(board)
    print(move)
    player2 = SmartPlayer(1, goal, 1)
    player2._proceed = True
    move2 = player2.generate_move(board)
    print(move2)
예제 #25
0
    def __init__(self, max_depth: int, num_human: int, random_players: int,
                 smart_players: List[int]) -> None:
        """Initialize this game, as described in the Assignment 2 handout.

        Precondition:
            2 <= max_depth <= 5
        """
        game_goal = []
        self.players = []
        num_players = num_human + random_players + len(smart_players)
        self.renderer = Renderer(num_players)

        # rand_num = random.randint(0, 1)
        # for i in range(num_players):
        #     game_goal.append([
        #                       PerimeterGoal(
        #                           COLOUR_LIST[random.randint(0, 3)]
        #                                     ),
        #                       BlobGoal(
        #                           COLOUR_LIST[random.randint(0, 3)]
        #                                 )
        #                       ][rand_num]
        #                      )

        if random.randint(0, 1) == 0:
            for i in range(num_players):
                game_goal.append(
                    PerimeterGoal(COLOUR_LIST[random.randint(0, 3)]))
        else:
            for i in range(num_players):
                game_goal.append(BlobGoal(COLOUR_LIST[random.randint(0, 3)]))

        self.board = random_init(0, max_depth)
        self.board.update_block_locations((0, 0), BOARD_WIDTH)
        for i in range(num_players):
            if i < num_human:
                self.players.append(HumanPlayer(self.renderer, i,
                                                game_goal[i]))
            elif i < random_players + num_human:
                self.players.append(
                    RandomPlayer(self.renderer, i, game_goal[i]))
            else:
                self.players.append(
                    SmartPlayer(self.renderer, i, game_goal[i],
                                smart_players[i - random_players - num_human]))
        for player in self.players:
            self.renderer.display_goal(player)
        # self.renderer.draw(self.board, 0)
        for i in range(num_players):
            self.renderer.draw(self.board, i)
예제 #26
0
파일: game.py 프로젝트: Sepeghh/Bocky-Game
    def __init__(self, max_depth: int, num_human: int, random_players: int,
                 smart_players: List[int]) -> None:
        """Initialize this game, as described in the Assignment 2 handout.

        Precondition:
            2 <= max_depth <= 5
        """
        self.players = []
        num_players = num_human + random_players + len(smart_players)
        self.renderer = Renderer(num_players)
        decider = random.randint(0, 1)
        # the random value ( 0 or 1 ) which we use it to
        # determine the goal of the game

        for i in range(num_players):
            c = COLOUR_LIST[random.randint(0, len(COLOUR_LIST) - 1)]
            # creating the random colour to assign to the player

            if decider == 0:
                # determining the random goal to the game
                goal = BlobGoal(c)
            else:
                goal = PerimeterGoal(c)

            if i < num_human:
                # first adding the human players
                self.players.append(HumanPlayer(self.renderer, i, goal))
                self.renderer.display_goal(self.players[i])
                # shows the goal of the player to him/her

            elif i < num_human + random_players:
                # adding the random players
                self.players.append(RandomPlayer(self.renderer, i, goal))
                self.renderer.display_goal(self.players[i])
                # shows the goal of the player

            else:

                level = smart_players[i - (num_human + random_players)]
                # it determines the difficulty level of the smartplayer which
                # is actualy the value of the smartplayer[index]

                # adding the smart players
                self.players.append(SmartPlayer(self.renderer, i, goal, level))
                self.renderer.display_goal(self.players[i])
                # shows the goal of the player

        self.board = random_init(0, max_depth)
        # make the board game with the given max_depth
        self.board.update_block_locations((0, 0), BOARD_WIDTH)
예제 #27
0
def test_smart_player_generate_move() -> None:
    """ Test the SmartPlayer class' generate_move method. """
    goal = BlobGoal((0, 0, 0))
    player1 = SmartPlayer(1, goal, 4)
    player1._proceed = True
    b1 = Block((0, 0), 1000, (0, 0, 0), 0, 2)

    b1_score = goal.score(b1)

    move1 = player1.generate_move(b1)

    assert isinstance(move1, tuple) and isinstance(move1[0], str) and \
           (isinstance(move1[1], int) or move1[1] is None) and \
           isinstance(move1[2], Block)

    if move1[0:2] == ('rotate', 1):
        assert move1[2].rotate(1)
        assert goal.score(b1) > b1_score
    elif move1[0:2] == ('rotate', 3):
        assert move1[2].rotate(3)
        assert goal.score(b1) > b1_score
    elif move1[0:2] == ('swap', 0):
        assert move1[2].swap(0)
        assert goal.score(b1) > b1_score
    elif move1[0:2] == ('swap', 1):
        assert move1[2].swap(1)
        assert goal.score(b1) > b1_score
    elif move1[0:2] == ('paint', None):
        assert move1[2].paint(self.goal.colour)
        assert goal.score(b1) > b1_score
    elif move1[0:2] == ('combine', None):
        assert move1[2].combine()
        assert goal.score(b1) > b1_score
    else:
        assert move1[0:2] == ('pass', None)
        assert goal.score(b1) == b1_score
예제 #28
0
def test_undiscovered_blob_size2() -> None:
    blocky = Block((0, 0), 16, (199, 44, 58), 0, 1)
    by = _flatten(blocky)
    block = Block((0, 0), 16, None, 0, 1)
    b1 = Block((8, 0), 8, (1, 128, 181), 1, 1)
    b2 = Block((0, 0), 8, (199, 44, 58), 1, 1)
    b3 = Block((0, 8), 8, (1, 128, 181), 1, 1)
    b4 = Block((8, 8), 8, (255, 211, 92), 1, 1)
    block.children = [b1, b2, b3, b4]
    fb = _flatten(block)
    goal1 = BlobGoal((199, 44, 58))
    goal2 = BlobGoal((1, 128, 181))
    v = [[-1, -1], [-1, -1]]
    b = [[-1, -1], [-1, -1]]
    assert goal1._undiscovered_blob_size((0, 0), by, v) == 4
    assert goal2._undiscovered_blob_size((0, 1), fb, b) == 1
예제 #29
0
def goal_playground():
    from goal import generate_goals, _flatten, PerimeterGoal, BlobGoal
    goals = generate_goals(3)
    print([g.description() for g in goals])

    b = block_three_level1()
    print(block_graph(b))
    print(_flatten(b))

    pg = PerimeterGoal(COLOUR_LIST[0])
    print(pg.score(b))

    bg = BlobGoal(COLOUR_LIST[0])
    print(bg.score((b)))

    bg = BlobGoal(COLOUR_LIST[1])
    print(bg.score((b)))
def test_blob_depth_3() -> None:
    board = Block((0, 0), 100, None, 0, 3)
    # Level 1
    colours = [COLOUR_LIST[0], None, None, None]
    set_children(board, colours)
    # Level 2
    colours = [None, None, None, None]
    set_children(board.children[1], colours)
    colours = [COLOUR_LIST[2], COLOUR_LIST[1], COLOUR_LIST[3], COLOUR_LIST[0]]
    set_children(board.children[2], colours)
    colours = [None, None, None, None]
    set_children(board.children[3], colours)
    # Level 3
    colours = [COLOUR_LIST[3], COLOUR_LIST[0], COLOUR_LIST[2], COLOUR_LIST[2]]
    set_children(board.children[1].children[0], colours)
    colours = [COLOUR_LIST[1], COLOUR_LIST[0], COLOUR_LIST[2], COLOUR_LIST[3]]
    set_children(board.children[1].children[1], colours)
    colours = [COLOUR_LIST[2], COLOUR_LIST[2], COLOUR_LIST[3], COLOUR_LIST[0]]
    set_children(board.children[1].children[2], colours)
    colours = [COLOUR_LIST[1], COLOUR_LIST[2], COLOUR_LIST[3], COLOUR_LIST[2]]
    set_children(board.children[1].children[3], colours)

    colours = [COLOUR_LIST[1], COLOUR_LIST[2], COLOUR_LIST[3], COLOUR_LIST[1]]
    set_children(board.children[3].children[0], colours)
    colours = [COLOUR_LIST[2], COLOUR_LIST[1], COLOUR_LIST[2], COLOUR_LIST[3]]
    set_children(board.children[3].children[1], colours)
    colours = [COLOUR_LIST[3], COLOUR_LIST[1], COLOUR_LIST[1], COLOUR_LIST[0]]
    set_children(board.children[3].children[2], colours)
    colours = [COLOUR_LIST[2], COLOUR_LIST[3], COLOUR_LIST[2], COLOUR_LIST[2]]
    set_children(board.children[3].children[3], colours)

    goal = BlobGoal(COLOUR_LIST[0])
    assert goal.score(board) == 16
    goal = BlobGoal(COLOUR_LIST[1])
    assert goal.score(board) == 4
    goal = BlobGoal(COLOUR_LIST[2])
    assert goal.score(board) == 6
    goal = BlobGoal(COLOUR_LIST[3])
    assert goal.score(board) == 4