コード例 #1
0
ファイル: 2048.py プロジェクト: pjotr145/2048game
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
コード例 #2
0
ファイル: test_2048.py プロジェクト: pjotr145/2048game
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]
spel.flatten_board()
spel.move_down()
for i in spel.board:
    print(i)
spel.board = test_game

end_games = [[[2, 16, 2, 4], [4, 2, 4, 32], [2, 16, 32, 64], [4, 2, 4, 2]],
             [[2, 16, 2, 4], [4, 0, 4, 32], [2, 16, 32, 64], [4, 2, 4, 2]],
             [[2, 16, 2, 4], [8, 8, 4, 32], [2, 16, 32, 64], [4, 2, 4, 2]],
             [[2, 16, 2, 4], [4, 2, 4, 32], [2, 8, 32, 64], [4, 8, 4, 2]]]

for game in end_games:
    spel.board = game
    spel.flatten_board()
    if (spel.check_if_moves_possible()):
        print("True")
    else:
        print("False")
#print(spel)
コード例 #3
0
#!/usr/bin/env python

from board2048 import Board

# Create instance of 2048 game
spel = Board()
print(spel)

if __name__ == "__main__":
    while spel.check_if_moves_possible():
        getch = input()[0]
        if getch == ",":
            spel.move_up()
            spel.add_random()
        elif getch == "o":
            spel.move_down()
            spel.add_random()
        elif getch == "a":
            spel.move_left()
            spel.add_random()
        elif getch == "e":
            spel.move_right()
            spel.add_random()
        elif getch == "q":
            break
        else:
            pass
        print(spel)