Exemplo n.º 1
0
 def _search(self, board, player, n, turn, alpha):
     """
         Depth tree search of the minimax algorithm
         """
     a = mvc.converv(board.tup)
     if n == self.depth:  #End of the exploration
         return 0
     if np.where(a == 2)[0].shape[0] == 0:  #Tie
         return 0
     if mvc._find_winner(board.tup, board.size) == player:  #Win
         return 1
     if mvc._find_winner(board.tup, board.size) == (not player):  #Loss
         return -1
     list_children = board.find_children()
     value = 0
     for i in list_children:
         new_value = self._search(i, player, n + 1, not turn, alpha)
         value = self._maxmin(value, new_value, turn)
         #Alpha beta pruning optimization
         if not turn and alpha < value:  #Alpha
             break
         if turn and alpha > value:  #Beta
             break
         alpha = value
         ########
     return value
Exemplo n.º 2
0
 def choose(self,board):
     """
     Choose the board to play
     """
     children_board = []
     list_children =board.find_children()
     player = board.turn
     #Check if I can win the next move
     for i in list_children:
         if mvc._find_winner(i.tup,i.size) == player:
             return i
     ideas = []
     #Check to avoid lose in two moves
     for i in list_children:
         list_children_2 = i.find_children()
         choose = True
         for j in list_children_2:
             if mvc._find_winner(j.tup,j.size) == (not player):
                 choose = False
         if choose:
             ideas.append(i)
     if len(ideas) > 0: #Choose a random play that dont make me lose in two moves
         return ideas[np.random.choice(np.arange(len(ideas)))]
     else: #Choose an random play already knowing I am gonna lose
         list_children = list(list_children)
         return list_children[np.random.choice(np.arange(len(list_children)))]
Exemplo n.º 3
0
def gameTerminal(size, train_steps, turnPlayer, agent, agent2):
    round = 0
    board = Model(tup=(None, ) * size**2,
                  turn=True,
                  winner=None,
                  size=size,
                  terminal=False)
    mvc.view(board.tup, size, round)
    while (True):  #Loop of the total game
        while (True):  #Loop of a turn of a player
            if turnPlayer.playerTurn(
            ) == player.typePlayer.IA_PLAYER:  #Agent turn
                for _ in range(train_steps):
                    agent.do_rollout(board)
                board = agent.choose(board)
                print("IA choose")
                mvc.view(board.tup, size, round)
                break
            if turnPlayer.playerTurn(
            ) == player.typePlayer.IA_PLAYER_2:  #Agent turn
                for _ in range(train_steps):
                    agent2.do_rollout(board)
                board = agent2.choose(board)
                print("IA choose")
                mvc.view(board.tup, size, round)
                break
            else:  #Player turn
                while (True):  #Player choose play
                    try:
                        row_col = input("enter row,col: ")
                        row, col = map(int, row_col.split(","))
                        index = size * (row - 1) + (col - 1)
                        if not (mvc.correctNumber(size, col)
                                and mvc.correctNumber(size, row)):
                            print("That position is out of the board")
                        elif np.array(board.tup)[index] != None:
                            print("That position is already taken.")
                        else:
                            break
                    except:
                        print("You must insert with this format row,col")
                try:  #Game change the state
                    board = board.make_move(index)
                    mvc.view(board.tup, size, round)
                    break
                except Exception as e:
                    print(e)
        round += 1
        turnPlayer.newTurn()
        player_won = mvc._find_winner(board.tup, size)
        if player_won != None:  #Game check if the player won
            if player_won:
                aux = turnPlayer.PLAYER1
            else:
                aux = turnPlayer.PLAYER2
            print("Congratulations, player", aux, "won")
            break
        if mvc._isTie(size, round):  #Game check if there is tie
            print("Game finish in a tie")
            break
Exemplo n.º 4
0
 def make_move(board, index):
     """
     Make a move in the game
     """
     tup = board.tup[:index] + (board.turn, ) + board.tup[index + 1:]
     turn = not board.turn
     size = board.size
     winner = mvc._find_winner(tup, size)
     is_terminal = (winner is not None) or not any(v is None for v in tup)
     return Model(tup, turn, winner, size, is_terminal)
Exemplo n.º 5
0
def gameWon(board):
    """
    Draw the line of the winning combination.
    """
    global grid, winner, model, size, height, width
    isWin, data = _find_winner_UI(model.tup, size)
    if isWin:  #Enter here if a player won the game
        if any(data[0]):  #Draw for winning row
            high = height / size
            for row in range(0, size):
                if data[0][row]:
                    winner = _find_winner(model.tup, size)
                    pygame.draw.line (board, (250,0,0), (0, (row + 1)*high - high/2), \
                                      (height, (row + 1)*high - high/2), 5)
                    break

        elif any(data[1]):  #Draw winning column
            large = width / size
            for col in range(0, size):
                if data[1][col]:
                    winner = _find_winner(model.tup, size)
                    pygame.draw.line (board, (250,0,0), ((col + 1)* large - large/2, 0), \
                                      ((col + 1)* large - large/2, width), 5)
                    break

        elif data[2]:  #Draw winning diagonal
            high = height / (2 * size)
            large = width / (2 * size)
            winner = _find_winner(model.tup, size)
            pygame.draw.line(board, (250, 0, 0), (high, large),
                             (height - high, width - large), 5)

        elif data[3]:  #Draw winning diagonal
            high = height / (2 * size)
            large = width / (2 * size)
            winner = _find_winner(model.tup, size)
            pygame.draw.line(board, (250, 0, 0), (height - high, high),
                             (large, width - large), 5)
Exemplo n.º 6
0
def gameStadistic(size,train_steps, turnPlayer,agent, agent2,train = 0 ,save_games = False,visualize = True):
    total_games = 1
    if train == 0:
        print("How many games:")
        while(True):
            try:
                total_games = input("Select number: ")
                total_games = int(total_games)
                assert total_games > 0, "Number must be positive."
                break
            except:
                print("You must choose a number")
    player1 = [0]
    player2 = [0]
    replay = []
    for j in range(total_games):
        round = 0
        model = Model(tup=(None,) * size**2, turn=True, winner=None,size = size ,terminal=False)
        if train == 0:
            print("Playing",j,"of",total_games)
        game = []
        while(True):
            #Check if a player won or tie
            if mvc._find_winner(model.tup,size) == True:
                player1.append(player1[-1] + 1)
                player2.append(player2[-1])
                break
            elif mvc._find_winner(model.tup,size) == False:
                player1.append(player1[-1])
                player2.append(player2[-1] +1)
                break
            elif mvc._isTie(size,round):
                player1.append(player1[-1])
                player2.append(player2[-1])
                break
            #Playing the game
            if turnPlayer.playerTurn() == player.typePlayer.IA_PLAYER:
                for _ in range(train_steps):
                    agent.do_rollout(model)
                model = agent.choose(model)
            elif turnPlayer.playerTurn() == player.typePlayer.IA_PLAYER_2:
                for _ in range(train_steps):
                    agent2.do_rollout(model)
                model = agent2.choose(model)
            round +=1
            if save_games:
                game.append(copy.copy(model))
            turnPlayer.newTurn()
        turnPlayer.reset()
        replay.append(game.copy())
    gamesPlayed = np.arange(total_games+1)

    #Create Report
    doc = Document('basic')
    doc.preamble.append(Command('title', 'Game report'))
    doc.preamble.append(Command('author', 'Tic Tac Toe'))
    doc.preamble.append(Command('date', NoEscape(r'\today')))
    doc.append(NoEscape(r'\maketitle'))

    with doc.create(Section('Introducction')):
            doc.append(bold('This document was autogenerate. '))
            doc.append('Here we present the result between two agents after ' + str(total_games) + 'games.')
            doc.append(' This document present graphs with the victories of the agents and the total points obtain.')
            doc.append(' The agent 1 is ' + agent.agent_name + ' and the agent 2 is ' + agent2.agent_name + '. ')
            date = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
            doc.append(bold('Document generate ' + date  + '.'))

    with doc.create(Section('Agent 1: ' + agent.agent_name)):
            doc.append(NoEscape(agent.agent_description))
    with doc.create(Section('Agent 2: ' + agent2.agent_name)):
            doc.append(NoEscape(agent2.agent_description))
    with doc.create(Section('Victories count')):
        doc.append('In this secction we can see the plot with the point counts of each agent.')
        with doc.create(Figure(position='htbp')) as plot:
            df = pd.DataFrame({
                "Games Played": gamesPlayed,
                "Player 1: "+ turnPlayer.PLAYER1: player1,
                "Player 2: "+ turnPlayer.PLAYER2: player2
            })
            ax = df.plot.area(x="Games Played",stacked=False)
            plot.add_plot()
            plot.add_caption('Points per agent.')
            #plt.close()
    if visualize:
        plt.show()

    doc.generate_pdf('Reports/game_report', clean_tex=True)


    if save_games:
        return replay