示例#1
0
def play(board, save_normalized_matrix=True):
    """
    Parameters
    ----------
    board : numpy.array
    save_normalized_matrix : bool
        Whether to save normalized (log2 transformed) or original matrix.

    Returns
    -------
    collections.namedtuple
        Game with recorded steps.
    """

    steps = []
    render_board(board)

    while True:
        for event in pygame.event.get():
            if event.type == QUIT:
                pygame.quit()
                sys.exit()
            if event.type == pygame.KEYDOWN:
                if event.key in POSSIBLE_ACTIONS:
                    matrix = board.normalized_matrix if save_normalized_matrix else board.matrix
                    action = POSSIBLE_ACTIONS[event.key]
                    moved = board.move(action)

                    if moved:
                        print()
                        print(board.matrix)
                        print("SCORE:", board.score, "\tSTEP:",
                              board.n_steps_valid, "\tHIGHEST VALUE:",
                              board.highest_value)
                        steps.append(
                            Step(matrix=matrix,
                                 action=action,
                                 action_encoded=encode_action(action)))
                        render_board(board)

                        if board.is_gameover():
                            print("GAME OVER!")
                            return Game(steps=steps,
                                        score=board.score,
                                        random_seed=board.random_seed,
                                        is_gameover=True)
                    else:
                        print("\nCannot move to this direction!")
                elif event.key == pygame.K_q:
                    screen.fill(BLACK)
                    return Game(steps=steps,
                                random_seed=board.random_seed,
                                is_gameover=False)
                elif event.key == pygame.K_p:
                    screen.fill(BLACK)
                    return "quit"

        clock.tick(60)
        pygame.display.flip()
示例#2
0
def gen_games(n_games,
              job_id,
              n_jobs,
              weights=None,
              min_score=0,
              min_tile_value=0,
              verbose=0):
    games = []
    i = 0

    if not weights:
        weights = [0.25, 0.25, 0.25, 0.25]

    play_for_score = False
    play_for_tile = False
    play_for_both = False

    if min_score and not min_tile_value:
        play_for_score = True
    if min_tile_value and not min_score:
        play_for_tile = True
    if min_score and min_tile_value:
        play_for_both = True
    if not min_score and not min_tile_value:
        raise ValueError("min_score or min_tile_value or both must be > 0!")

    try:
        while len(games) != n_games:
            env = GymBoard()
            r = np.random.RandomState()
            steps = []

            while True:
                matrix = env.matrix
                action = r.choice(actions, p=weights)
                moved = env.move(action)

                if moved:
                    steps.append(
                        Step(matrix=matrix,
                             action=action,
                             action_encoded=encode_action(action)))

                if env.is_gameover():
                    if (play_for_score and env.score >= min_score) or \
                       (play_for_tile and np.any(env.matrix >= min_tile_value)) or \
                       (play_for_both and env.score >= min_score and np.any(env.matrix >= min_tile_value)):
                        if verbose:
                            print("\tchunk {}/{}...game {}/{}".format(
                                job_id, n_jobs, i + 1, n_games))
                        games.append(
                            Game(steps=steps,
                                 score=env.score,
                                 random_seed=env.random_seed,
                                 is_gameover=True))
                        i += 1
                    break
    except KeyboardInterrupt:
        pass

    return games
示例#3
0
        board = g.ret_board()
        print(board)
        row, col = P1.play(board, free)
        g.update_spot(row, col, P1sym)
        board = g.ret_board()
        print(board)
        if (g.check_victory(P1) == P1sym):
            AI_1.AI_update_state_record('loss')
            break
        free = g.get_play_spots()
        if len(free) == 0:
            print("Game Drawn")
            AI_1.AI_update_state_record('draw')
            break
        bcop = np.copy(board)
        row, col = AI_1.play(bcop, free)
        g.update_spot(row, col, AIsym)
        if (g.check_victory(AI_1) == AIsym):
            AI_1.AI_update_state_record('win')
            break


g = Game()
print("game1")
start_game(g)
g.reset_board()
print("game2")
start_game(g)
print("printing State Record after 2 games")
print(Player.state_record)