Пример #1
0
def find_mc_boards(number_of_boards, min_max_value):
    ''' Plays game until number_of_boards amount of plays with at least
        min_max_value have been found. E.g 100 games that reached 512.
        Returns list with the boards and direction and a list with the
        number of games needed to play.
    '''
    # for each found game this is the number of games needed to play.
    counting_total_games = []
    games = {}
    while len(counting_total_games) < number_of_boards:
        nmr_games = 1
        max_val = 0
        while max_val < min_max_value:
            # Create instance of 2048 game
            spel = Board()
            high_game_value, game_steps = play_one_game_with_montecarlo(
                spel, nmr_games, len(counting_total_games),
                conf["montecarlo_width"], conf["montecarlo_depth"])
            if high_game_value > max_val:
                max_val = high_game_value
            nmr_games += 1
        print(spel)
        #        print("Number of games: {}".format(nmr_games))
        #        print(game_steps[-1])
        games["game_" + str(len(counting_total_games))] = game_steps
        counting_total_games += [nmr_games]
        # for i in game_steps:
        #     print("{}: {}".format(i, game_steps[i]))
    # Clear games of moves after reaching highest values.
    games = remove_moves_after_highest(games)
    return games, counting_total_games
Пример #2
0
def find_boards(nn, number_of_boards=5, min_max_value=256):
    ''' Plays game until number_of_boards amount of plays with at least
        min_max_value have been found. E.g 100 games that reached 512.
        Returns list with the boards and direction and a list with the
        number of games needed to play.
    '''
    # for each found game this is the number of games needed to play.
    counting_total_games = []
    games = {}
    while len(counting_total_games) < number_of_boards:
        nmr_games = 0
        max_val = 0
        while max_val < min_max_value:
            # Create instance of 2048 game
            spel = Board()
            high_game_value, game_steps = play_one_game(spel, nn)
            if high_game_value > max_val:
                max_val = high_game_value
            nmr_games += 1
        print(spel)
        #        print(game_steps[-1])
        games["game_" + str(len(counting_total_games))] = game_steps
        counting_total_games += [nmr_games]
        # for i in game_steps:
        #     print("{}: {}".format(i, game_steps[i]))
    # TODO: clear games of movements after reaching highest values. Now the
    # game continues after that until board is no longer playable.
    return games, counting_total_games
Пример #3
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
Пример #4
0
#!/usr/bin/env python

from board2048 import Board
import copy
#import yaml   "pyyaml"

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")