Beispiel #1
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
Beispiel #2
0
def test_JEN_smart_player_not_smart_moves(board_16x16) -> None:
    goal = PerimeterGoal((199, 44, 58))
    player = SmartPlayer(3, goal, 10)
    player._proceed = True
    move = player.generate_move(board_16x16)
    assert move is not COMBINE
    assert move is not ROTATE_CLOCKWISE
    assert move is not ROTATE_COUNTER_CLOCKWISE
    assert move is not SWAP_HORIZONTAL
    assert move is not SWAP_VERTICAL
    assert move is PASS or SMASH
Beispiel #3
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)
Beispiel #4
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
Beispiel #5
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)
Beispiel #6
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)
Beispiel #7
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
 def run_game(self, num_h, num_r, num_s):
     t = 0
     l = []
     for i in range(num_h):
         l.append(HumanPlayer(self.b))
         t += 2
     for i in range(num_r):
         l.append(RandomPlayer(self.b))
         t += 2
     for i in range(num_s):
         l.append(SmartPlayer(self.b, -1 + t))
         t += 2
     while not self.game_over():
         for playr in l:
             self.draw_board()
             plm = playr.move()
             self.make_move(plm[0], plm[1])
             print(
                 '\n' + str(playr) + ' ' + str(l.index(playr)) +
                 ' made move (' + str(plm[0]) + ', ' + str(plm[1]) +
                 ')\n\n---------------------------------------------------\n'
             )
             if self.game_over():
                 print('\n\n' + str(playr) + ' ' + str(l.index(playr)) +
                       ' Lost')
                 return l.index(playr)
Beispiel #9
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])
Beispiel #10
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
 def test_smart_player(self, board_16x16) -> None:
     goal_smart = PerimeterGoal(COLOUR_LIST[1])
     player_smart = SmartPlayer(1, goal_smart, 1)
     print(player_smart.difficulty)
     goal_random = PerimeterGoal(COLOUR_LIST[0])
     player_random = RandomPlayer(10, goal_random)
     random_move = player_random.generate_move(board_16x16)
     #player_smart._proceed = True
     #x = player_smart.generate_move(board_16x16)
     assert 1
Beispiel #12
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)
Beispiel #13
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
        """
        # Create the board
        self.board = random_init(0, max_depth)
        self.board.update_block_locations((0, 0), BOARD_WIDTH)
        # Generate a goal
        goals = [BlobGoal, PerimeterGoal]
        index = random.randrange(0, 2)
        goal = goals[index]

        # Create render
        num_players = num_human + random_players + len(smart_players)
        self.renderer = Renderer(num_players)

        # Create players
        self.players = []
        val = 0
        for id_ in range(num_human):
            # Create a colour for this player
            index = random.randrange(0, 4)
            colour = COLOUR_LIST[index]
            self.players.append(HumanPlayer(self.renderer, id_, goal(colour)))
            val += 1

        for id_ in range(random_players):
            # Create a colour for this player
            index = random.randrange(0, 4)
            colour = COLOUR_LIST[index]
            self.players.append(RandomPlayer(self.renderer, val, goal(colour)))
            val += 1

        # A SmartPlayer has a difficulty level
        if len(smart_players) > 0:
            for id_ in range(len(smart_players)):
                # Create a colour for this player
                index = random.randrange(0, 4)
                colour = COLOUR_LIST[index]
                self.players.append(SmartPlayer(self.renderer, val,
                                                goal(colour),
                                                smart_players[id_]))
                val += 1
        # Display the goal and draw the board
        for i in self.players:
            self.renderer.display_goal(i)
            self.renderer.draw(self.board, i.id)
Beispiel #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.

        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)
Beispiel #15
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.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)
Beispiel #16
0
    def _create_player(self, num_human: int, random_players: int,
                       smart_players: List[int]) -> None:
        """Generate a random goal type, for all players to share. Generate the
        right number of human players, random players, and smart players (with
        the given difficulty levels), in that order. Give the players
        consecutive player numbers, starting at 0. Assign each of them a random
        target colour and display their goal to them.

        """
        # Create a list of goal for use in randomizing goals
        goal = [BlobGoal, PerimeterGoal]

        # Generate a random goal for all player to share
        rand_goal = random.choice(goal)

        self.players = []
        # Store the current player id
        player_id = 0

        # Generate HumanPLayer(s) and add it to the list of players
        for _ in range(num_human):
            # Randomize the target colours
            rand_num = random.randint(0, 3)
            human = HumanPlayer(self.renderer, player_id,
                                rand_goal(COLOUR_LIST[rand_num]))
            self.players.append(human)
            self.renderer.display_goal(human)
            player_id += 1

        # Generate RandomPlayer(s) and add it to the list of players
        for _ in range(random_players):
            # Randomize the target colours
            rand_num = random.randint(0, 3)
            rand_player = RandomPlayer(self.renderer, player_id,
                                       rand_goal(COLOUR_LIST[rand_num]))
            self.players.append(rand_player)
            self.renderer.display_goal(rand_player)
            player_id += 1

        # Generate SmartPlayer(s) and add it to the list of players
        for difficulty in smart_players:
            # Randomize the target colours
            rand_num = random.randint(0, 3)
            smart_player = SmartPlayer(self.renderer, player_id,
                                       rand_goal(COLOUR_LIST[rand_num]),
                                       difficulty)
            self.players.append(smart_player)
            self.renderer.display_goal(smart_player)
            player_id += 1
Beispiel #17
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
Beispiel #18
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
        """

        goals = (BlobGoal, PerimeterGoal)

        total_num = num_human + random_players + len(smart_players)
        # Initialize renderer
        self.renderer = Renderer(total_num)

        # Generate and update board
        self.board = random_init(0, max_depth)
        self.board.update_block_locations((0, 0), BOARD_WIDTH)

        # Generate a random goal for all players
        goal = random.choice(goals)

        self.players = []
        # Generate and add some HumanPlayers
        id_offset = 0
        human_list: List[HumanPlayer] = [
            HumanPlayer(
                self.renderer,
                i + id_offset,
                goal(random.choice(COLOUR_LIST)))
            # iterate <num_human> times
            for i in range(num_human)
        ]
        self.players.extend(human_list)

        # Generate and add some RandomPlayers
        id_offset += num_human
        random_list: List[RandomPlayer] = [
            RandomPlayer(
                self.renderer,
                i + id_offset,
                goal(random.choice(COLOUR_LIST)))
            # iterate <random_players> times
            for i in range(random_players)
        ]
        self.players.extend(random_list)

        # Generate and add some SmartPlayers according to
        # their corresponding difficulty levels
        id_offset += random_players
        smart_list: List[SmartPlayer] = [
            SmartPlayer(
                self.renderer,
                i + id_offset,
                goal(random.choice(COLOUR_LIST)), difficulty)
            # iterates <len(smart_players)> times,
            # capturing difficulty levels
            for i, difficulty in enumerate(smart_players)
        ]
        self.players.extend(smart_list)
Beispiel #19
0
    letter = 'X'

    while game.empty_squares():
        if letter =='O':
            square = o_player.get_move(game)
        else:
            square = x_player.get_move(game)
        if game.make_move(square,letter):

            print(letter + ' makes a move to square {}'.format(square))
            game.print_board()
            print('')
            
            # we check if there is a winner
            if game.winner != " ":
                print(letter+ ' HAS WON!')
                return  # we exit the game
            letter = 'O' if letter =='X' else 'X' # we switch the player turn
        time.sleep(.8)
    
    print('It\'s a tie!')
    
if __name__=='__main__':
    x_player = SmartPlayer('X')
    o_player = HumanPlayer('O')
    t=TicTacToe()
    play(t,x_player,o_player)

                

Beispiel #20
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
        """
        # Creates and stores a renderer
        self.renderer = \
            Renderer(num_human + random_players + len(smart_players))

        # Stores a random number that represents either:
        # 0. The blob goal. 1. The perimeter goal.
        goal_id = random.randint(0, 1)

        # Generates a random board.
        self.board = random_init(0, max_depth)

        # Sets the players as an empty list
        self.players = []

        # Stores the number of human AND random players (to save space later)
        num_human_rand = num_human + random_players

        for i in range(num_human_rand + len(smart_players)):
            # Loops once for every player, i represents the player ID.

            # Generates a random colour id (representing a colour)
            colour_id = random.randint(0, len(COLOUR_LIST) - 1)

            # If the goal id is 0, create a BlobGoal.
            # Otherwise, create a PerimeterGoal.
            if goal_id == 0:
                goal = BlobGoal(COLOUR_LIST[colour_id])
            else:
                goal = PerimeterGoal(COLOUR_LIST[colour_id])

            # Because players are created in the following order:
            # 1. Human 2. Random 3. Smart players.
            # We can tell which type of player it is based on the index <i>

            if i < num_human:
                # If the player is a human, store a human player
                self.players.append(HumanPlayer(self.renderer, i, goal))
            elif i < num_human_rand:
                # If the player is a random player, store a random player
                self.players.append(RandomPlayer(self.renderer, i, goal))
            else:
                # If the player is a smart player, store a smart player
                # with the specified difficulty
                self.players.append(
                    SmartPlayer(smart_players[i - num_human_rand],
                                self.renderer, i, goal))

            # Displays the goal to the player.
            self.renderer.display_goal(self.players[i])

        # Update the board locations, so everything shows up at the right place
        # and draw the board.
        self.board.update_block_locations((0, 0), BOARD_WIDTH)
        self.renderer.draw(self.board, 0)
Beispiel #21
0
 def test_SPGM_good_idea(self) -> None:
     borde = standard_borde()
     bobbito = SmartPlayer(1, BlobGoal(DAFFODIL_DELIGHT))
     bobbito._proceed = True
     assert bobbito.generate_move(borde) == 1 \
            or bobbito.generate_move(borde) == 2