Beispiel #1
0
def train(n):
    player = BarasinggaQlearning()

    for i in range(n):
        print(f"training {i+1}")

        game = Barasingga()

        # Keep track of last move made by either player
        last = {
            1: {
                "state": None,
                "action": None
            },
            2: {
                "state": None,
                "action": None
            }
        }

        # Game loop
        while True:

            # Keep track of current state and action
            state = (tuple_board(game.board), game.player)
            action = player.choose_action(state)

            # Keep track of last state and action
            last[game.player]["state"] = state
            last[game.player]["action"] = action

            # Make move
            game.move(action)
            new_state = (tuple_board(game.board), game.player)

            # When game is over, update Q values with rewards
            if game.winner is not None:
                player.update(state, action, new_state, -1)
                player.update(last[game.player]["state"],
                              last[game.player]["action"], new_state, 1)
                break

            elif game.over:
                player.update(state, action, new_state, -1)
                player.update(last[game.player]["state"],
                              last[game.player]["action"], new_state, 1)
                break
            # If game is continuing, no rewards yet
            elif last[game.player]["state"] is not None:
                player.update(last[game.player]["state"],
                              last[game.player]["action"], new_state, 0)

    print("Done training")

    # Return the trained AI
    return player
Beispiel #2
0
        starty = (i * scale + padding, padding)
        endy = (i * scale + padding, board_size + padding)

        # drawing the diamond
        diamond = [(padding, board_size / 2 + padding),
                   (board_size / 2 + padding, padding),
                   (board_size + padding, board_size / 2 + padding),
                   (board_size / 2 + padding, board_size + padding)]
        pygame.draw.line(screen, (GREEN), startx, endx, line_width)
        pygame.draw.line(screen, (GREEN), starty, endy, line_width)
        pygame.draw.polygon(screen, (GREEN), diamond, line_width)

    if game.player == 2:
        m = bai.best_move(game.board)
        time.sleep(0.1)
        game.move(m)

    if len(clicks) == 2:
        initial_mouse = clicks[0]
        final_mouse = clicks[1]
        initial = None
        final = None
        for i in range(5):
            for j in range(5):
                if pieces[j][i].collidepoint(initial_mouse):
                    initial = (i, j)
                if pieces[j][i].collidepoint(final_mouse):
                    final = (i, j)
        action = (initial, final)
        if action in game.available_actions(game.board, game.player):
            game.move(action)