Exemplo n.º 1
0
def benchmark(game, episode_num, grid_size):
    winTotal = 0.0
    drawTotal = 0.0
    for i in range(episode_num):
        if (i % 100 == 0): print('hundered episodes completed:', i)

        game = dotsnboxes(grid_size)
        firstTurn = random.randint(0, 1)
        # print(firstTurn)
        if firstTurn == 1:
            aiCharacter = 2
            userLetter = 1
        else:
            aiCharacter = 1
            userLetter = 2

        aiAgent = deepai(game, grid_size, 1, 0.1, 0.95, 1.0, 0.01, 100)
        aiAgent.loadFromCsv()
        turn = firstTurn
        gameOn = 1
        while gameOn == 1:
            ## 1 means AI turn
            if game.player == aiCharacter:
                aiAgent.getMoveVsHuman()
                if game.isBoardFull():
                    # game.drawBoard()
                    gameOn = 0

            # user turn
            else:
                # game.drawBoard()
                # print(game.availMove())
                # print('Its player ' + str(game.player) + ' turn')
                i, j = random.choice(
                    game.availMove()
                )  #raw_input('Enter the moves from the above listed moves:  ').split()
                # print('User entered move')
                game.makeMove([], int(i), int(j))
                if game.isBoardFull():
                    # game.drawBoard()
                    gameOn = 0

        # print('winner is ' + str(game.winner()))
        if game.winner() == aiCharacter:
            winTotal = winTotal + 1
        elif game.winner() == 0:
            drawTotal = drawTotal + 1
        else:
            continue
    print('Total Episode :', episode_num)
    print('Win percentage is: ', winTotal / episode_num)
    print('Draw percentage is: ', drawTotal / episode_num)
Exemplo n.º 2
0
def play(grid_size):
    game = dotsnboxes(grid_size)
    firstTurn = random.randint(0, 1)
    print(firstTurn)
    if firstTurn == 1:
        aiCharacter = 2
        userLetter = 1
    else:
        aiCharacter = 1
        userLetter = 2

    aiAgent = deepai(game, grid_size, 1, 0.1, 0.95, 1.0, 0.01, 100)
    aiAgent.loadFromCsv()
    turn = firstTurn
    gameOn = 1
    while gameOn == 1:
        ## 1 means AI turn
        if game.player == aiCharacter:
            aiAgent.getMoveVsHuman()
            if game.isBoardFull():
                game.drawBoard()
                gameOn = 0

        # user turn
        else:
            game.drawBoard()
            print(game.availMove())
            print('Its player ' + str(game.player) + ' turn')
            i, j = raw_input(
                'Enter the moves from the above listed moves:  ').split()
            # print('User entered move')
            game.makeMove([], int(i), int(j))
            if game.isBoardFull():
                game.drawBoard()
                gameOn = 0

    print('winner is ' + str(game.winner()))
Exemplo n.º 3
0
def trainAI(grid_size, episode_num):
    # ticGame = tictactoe('X', ' '*9)
    aiAgent = deepai(dotsnboxes, grid_size, 1, 0.1, 0.95, 1.0, 0.01,
                     episode_num)
    aiAgent.trainFromEpisode()
    aiAgent.write2csv()