def generate_training_data(self, filename=TRAINING_DATA_FILENAME): """ Generate training data sets. Play the 2048 game using A*-GAC, and write every move made to supplied file :param filename: Filename to save the results to """ while True: logging.info('Starting AI run') twenty_forty_eight = TwentyFortyEight() moves = twenty_forty_eight.run(twenty_forty_eight.make_player_move, True) logging.info('AI run completed. Saving to file') with open(filename, 'a') as f: for move in moves: game_board = self.flatten_list(move[0]) move_made = move[1] f.write('[%s] %d\n' % (','.join([str(element) for element in game_board]), move_made)) logging.info('Moves successfully saved to file %s' % filename)
def run(self): twenty_forty_eight = TwentyFortyEight(ui=self.ui) twenty_forty_eight.run(twenty_forty_eight.make_player_move) try: input('Done. Press return to continue') except SyntaxError: pass
def run(self): twenty_forty_eight = TwentyFortyEight(ui=self.ui) twenty_forty_eight.run(twenty_forty_eight.make_player_move) try: input('Done. Press return to continue') except SyntaxError: pass
def play_intelligently(self): """ Play 2048 using the trained neural network :return: Highest tile achieved """ logging.info('Playing using the neural network') ui = Ui() twenty_forty_eight = TwentyFortyEight(ui=ui) is_game_over = not twenty_forty_eight.make_computer_move() # Makes moves as long as the game isn't lost yet while not is_game_over: # Makes the move board_state = self.flatten_list( twenty_forty_eight.game_board.get_cell_values()) next_moves = self.ann.testing_function_list([board_state])[0] moved = False illegal_moves = 0 while not moved: next_move_index = next_moves.argmax() moved = twenty_forty_eight.game_board.make_player_move( next_move_index) if not moved: next_moves[next_move_index] = -1 illegal_moves += 1 if illegal_moves == 4: break # Updates UI, if any if not moved and ui: ui.update_ui(twenty_forty_eight.game_board.state) # Spawns new value is_game_over = not twenty_forty_eight.make_computer_move() result = 2**twenty_forty_eight.game_board.get_max_value() logging.info('Result: %d' % result) # Returns final score return result
def play_randomly(): """ Play 2048 using only random moves :return: Highest tile achieved """ twenty_forty_eight = TwentyFortyEight() logging.info('Playing using random moves') result = twenty_forty_eight.run(twenty_forty_eight.make_random_move) logging.info('Result: %d' % result) return result
def play_randomly(): """ Play 2048 using only random moves :return: Highest tile achieved """ twenty_forty_eight = TwentyFortyEight() logging.info('Playing using random moves') result = twenty_forty_eight.run(twenty_forty_eight.make_random_move) logging.info('Result: %d' % result) return result
def play_intelligently(self): """ Play 2048 using the trained neural network :return: Highest tile achieved """ logging.info('Playing using the neural network') ui = Ui() twenty_forty_eight = TwentyFortyEight(ui=ui) is_game_over = not twenty_forty_eight.make_computer_move() # Makes moves as long as the game isn't lost yet while not is_game_over: # Makes the move board_state = self.flatten_list(twenty_forty_eight.game_board.get_cell_values()) next_moves = self.ann.testing_function_list([board_state])[0] moved = False illegal_moves = 0 while not moved: next_move_index = next_moves.argmax() moved = twenty_forty_eight.game_board.make_player_move(next_move_index) if not moved: next_moves[next_move_index] = -1 illegal_moves += 1 if illegal_moves == 4: break # Updates UI, if any if not moved and ui: ui.update_ui(twenty_forty_eight.game_board.state) # Spawns new value is_game_over = not twenty_forty_eight.make_computer_move() result = 2 ** twenty_forty_eight.game_board.get_max_value() logging.info('Result: %d' % result) # Returns final score return result
def generate_training_data(self, filename=TRAINING_DATA_FILENAME): """ Generate training data sets. Play the 2048 game using A*-GAC, and write every move made to supplied file :param filename: Filename to save the results to """ while True: logging.info('Starting AI run') twenty_forty_eight = TwentyFortyEight() moves = twenty_forty_eight.run(twenty_forty_eight.make_player_move, True) logging.info('AI run completed. Saving to file') with open(filename, 'a') as f: for move in moves: game_board = self.flatten_list(move[0]) move_made = move[1] f.write('[%s] %d\n' % (','.join([str(element) for element in game_board]), move_made)) logging.info('Moves successfully saved to file %s' % filename)