Exemple #1
0
def play_one_game_with_montecarlo(new_game,
                                  nmr_games,
                                  found_games,
                                  mc_width=100,
                                  mc_depth=20):
    '''
    while moves are possible
        simulate all 4 directions
            each with mc_width games played for mc_depth moves.
        select direction with highest score
        play that direction
    return highest value on board and the played game
    '''
    count_moves = 0
    one_game = {}
    all_directions = [0, 1, 2, 3]  # up, down, left, right
    spel = new_game
    while spel.check_if_moves_possible() and count_moves < conf["max_moves"]:
        # While moves are possible and total number of moves below limit
        scores = [0, 0, 0, 0]
        for this_move in all_directions:
            # for each of the 4 directions
            this_game = Board()
            this_game.board = spel.board
            this_game.move_in_direction(this_move)
            if this_game.board_changed:
                # Only if that first move does anything
                sim_start_board = this_game.board
                for _ in range(mc_width):
                    # Simulate multiple games to get some sort af average
                    depth_count = 0
                    this_game.board = sim_start_board
                    # start every sim with same start board
                    while this_game.check_if_moves_possible() and \
                    depth_count < mc_depth:
                        this_game.move_in_direction(getrandbits(2))
                        if this_game.board_changed:
                            this_game.add_random()
                            depth_count += 1
                            scores[this_move] += this_game.move_score
        # Needs int() because json can't handle numpy int64.
        direction = int(np.argmax(scores))
        one_move = {}
        one_move["board"] = spel.board
        one_move["direction"] = direction
        spel.move_in_direction(direction)
        one_move["score"] = spel.move_score
        one_game["move_" + str(count_moves)] = one_move
        spel.add_random()
        spel.score += spel.move_score
        count_moves += 1
        print(spel)
        print("Scores: {}".format(scores))
        print("Number of moves: {}".format(count_moves))
        print("Number of Games played so far: {}".format(nmr_games))
        print("Found number of games: {}".format(found_games))
    return max(spel.board), one_game
Exemple #2
0
spel = Board()
print(spel)
rij = spel.move_row([0, 2, 0, 2])
print(rij)

test_rows = [[0, 2, 0, 2], [4, 4, 4, 4], [2, 2, 2, 0], [2, 0, 2, 4],
             [0, 2, 4, 4], [4, 2, 2, 8]]
for row in test_rows:
    rij = spel.move_row(row)
    print("{} => {} score: {}".format(row, rij, spel.score))

test_game = [[0, 2, 0, 64], [0, 0, 16, 64], [0, 2, 2, 256], [0, 2, 4, 128]]

#spel.board = copy.deepcopy(test_game)
spel.board = [copy.copy(i) for i in test_game]
for i in spel.board:
    print(i)
spel.flatten_board()
print("Moving up")
spel.move_up()

for i in spel.board:
    print(i)
if spel.board == test_game:
    print("the same")
else:
    print("different")
for i in test_game:
    print(i)
spel.board = [copy.copy(i) for i in test_game]
Exemple #3
0
    #    plt.hist(moves_needed, bins=50)
    #    plt.title('Aantal moves')
    #
    #    plt.show()
    #    plt.hist(counting_games, bins=50)
    #    plt.title('Aantal spellen')

    plt.show()
    print("start")
    for game in games:
        for move in games[game]:
            getch = input()
            if getch == "q":
                break
            else:
                spel.board = games[game][move]["board"]
                print(spel)
                print("Points this move: {}".format(
                    [games[game][move]["score"]][0]))
                print("Direction: {}".format(
                    directions[games[game][move]["direction"]]))
                print("Direction: {}".format([games[game][move]["direction"]
                                              ][0]))
                moves_list.append([games[game][move]["direction"]][0])
                print("Game: {}".format(games[game][move]["board"]))
                games_list.append(games[game][move]["board"])

#    while spel.check_if_moves_possible():
#        getch = input()[0]
#        if getch == ",":
#            spel.move_up()