def play_one_time(): gamegrid = puzzle.GameGrid() train_times = 0 NN_data = {} model = obtain_model() i = 0 while (1): i += 1 # print("*"*25,"game_step: ", i, "*"*25) NN_data_temp, event = MCTS.mcts_process(gamegrid.matrix, model) if i == 1: NN_data = NN_data_temp else: data_merge(NN_data, NN_data_temp) gamegrid.action(event) for l in gamegrid.matrix: # print(l, event) pass if gamegrid.is_over: score_tem = gamegrid.max_value square_score = gamegrid.sum_square break return NN_data, score_tem, square_score
def explorer_game(): memory = [] prev_obs = [] choices = [] gamegrid = puzzle.GameGrid() for _ in range(conf.goal_steps): action, _ = explorer_move( inv_puzzle.Game(gamegrid.score, gamegrid.first, gamegrid.matrix), conf.options, 5) # Max depth == 5 done = gamegrid.move(action) choices.append(action) memory = [gamegrid.matrix, action] gamegrid.update_idletasks() gamegrid.update() if done: break time.sleep(0.01) gamegrid.destroy() time.sleep(1) return gamegrid.score, memory, choices
def visual_ia_game(model): memory = [] prev_obs = [] choices = [] gamegrid = puzzle.GameGrid() for _ in range(conf.goal_steps): prev_obs = np.concatenate(gamegrid.matrix, axis=0) prediction = sorted_prediction( model.predict(prev_obs.reshape(-1, len(prev_obs), 1))[0]) for i in range(len(prediction)): action = conf.options[prediction[i]] done = gamegrid.move(action) if not equals_grid(np.concatenate(gamegrid.matrix, axis=0), prev_obs): break choices.append(action) memory = [gamegrid.matrix, action] gamegrid.update_idletasks() gamegrid.update() if done: break time.sleep(0.01) gamegrid.destroy() time.sleep(1) return gamegrid.score, memory, choices
def manual_move(): gamegrid = puzzle.GameGrid() training_data = [] game_memory = [] prev_observation = [] scores = [] accepted_scores = [] try: training_data = np.load('saves/manual.npy').tolist() i = training_data[-1] training_data = training_data[:-1] print('Continuing manual training') except Exception as e: print('First manual training, good luck!') while not gamegrid.is_end_game: prev_observation = np.concatenate(gamegrid.matrix, axis=0) while not gamegrid.moved: gamegrid.update_idletasks() gamegrid.update() gamegrid.moved = False # Only if it is an active move, save it if not equals_grid(np.concatenate(gamegrid.matrix, axis=0), prev_observation): if not gamegrid.can_undo: # Redo this movement game_memory.pop() else: action = gamegrid.last_move game_memory.append([prev_observation, action]) if gamegrid.score >= conf.score_requirement: accepted_scores.append(gamegrid.score) for data in game_memory: if data[1] == conf.options[0]: output = [1, 0, 0, 0] elif data[1] == conf.options[1]: output = [0, 1, 0, 0] elif data[1] == conf.options[2]: output = [0, 0, 1, 0] elif data[1] == conf.options[3]: output = [0, 0, 0, 1] training_data.append([data[0], output]) scores.append(gamegrid.score) if training_data: training_data.append(i + 1) np.save('saves/manual.npy', np.array(training_data))
def playing(model): gamegrid = puzzle.GameGrid() i = 0 while (1): NN_data_temp, event = MCTS.mcts_process(gamegrid.matrix, model) gamegrid.action(event) print("step: ", i) i += 1 for l in gamegrid.matrix: print(l, event) if gamegrid.is_over: score_tem = gamegrid.max_value break return score_tem
def playing(cpuct, times): gamegrid = puzzle.GameGrid() i = 0 while (1): event = MCTS.mcts_process(gamegrid.matrix, cpuct=cpuct, update_times=times) gamegrid.action(event) # print("step: ", i) i += 1 for l in gamegrid.matrix: # print(l, event) pass if gamegrid.is_over: score_tem = gamegrid.max_value break return score_tem
assert direction in all_directions new_bits = { dir_up: 0, dir_down: 1, dir_left: 2, dir_right: 3, }[direction] self.random_state = ( (self.random_state << 2) | new_bits) & self.random_state_bitmask def rand_mod(self, mod): self.prng_step() return self.random_state % mod # takes a step in the pseudo random number generator def prng_step(self): # Does steps of of a fibonacci LFSR # https://en.wikipedia.org/wiki/Linear-feedback_shift_register#Fibonacci_LFSRs # This PRNG method was chosen due to it being simple to implement and not terrible. lfsr = self.random_state for i in range(self.random_state_bitwidth): bit = 1 & ((lfsr >> 0) ^ (lfsr >> 2) ^ (lfsr >> 3) ^ (lfsr >> 5)) lfsr = (lfsr >> 1) | (bit << (self.random_state_bitwidth - 1)) assert lfsr == lfsr & self.random_state_bitmask self.random_state = lfsr if __name__ == '__main__': import puzzle puzzle.GameGrid()
def initializeGame(seed = None, showGUI = True): return puzzle.GameGrid(is_ai_game=True, useSeed = seed, showGUI = showGUI)
def decimal(button4): global y import puzzle puzzle.GameGrid() tabla_puntaje(y)