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
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
def geniusGame(): board = Board() while True: board.printBoard() if board.inEndState(): print(" Game Over") with open('bestScores.txt','a') as f: f.write("10000: " + str(np.amax(board.board)) + "\n") return change = searchEM(board) if change != 0: board.addRandomTile()
#!/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")
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
#!/usr/bin/env python from board2048 import Board import json import pickle import matplotlib.pyplot as plt # Create instance of 2048 game spel = Board() directions = {0: "up", 1: "down", 2: "left", 3: "right"} games_list = [] moves_list = [] moves_needed = [] max_scores = [] file_list = ["sample.json"] def remove_moves_after_highest(games): ''' After reaching the highest value the game continues until the board is not playable anymore. These extra moves can be removed. ''' small_games = {} max_scores = [] for game in games: highest = 0 highest_move = "" # find highest value in this game for move in games[game]: this_max = max(games[game][move]["board"]) if this_max > highest: highest_move = move
#!/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)
#theBoard = np.array(aBoard) #addRandomTile(theBoard) #addRandomTile(theBoard) #heuristicTest(theBoard,0) #evaluateHeuristic() #board.testSnake() correctInput = False while not correctInput: userInput = raw_input('Would you like to play (enter "p") or watch the ai play (enter "ai")?\n') if userInput == "p": correctInput = True board = Board() board.newTurn() elif userInput == "ai": correctInput = True for i in range(5): geniusGame() """ with open('bestScores.txt','w') as f: f.write("Scores:\n") for i in range(25): aBoard = [[0 for x in range(4)] for x in range(4)] theBoard = np.array(aBoard) addRandomTile(theBoard) addRandomTile(theBoard) heuristicTest(theBoard,0) """