Example #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
Example #2
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)