def key_down(self, key): """ # 0: left, 1: up, 2: right, 3: down """ self.matrix, moved, score = logic.move(self.matrix, key) if moved: self.matrix = logic.add_two_or_four(self.matrix)
def take_action(self, event=None, action=None): generated_new = False state, action, state_after, reward, terminal = (None, action, None, None, False) if event is None: key = action elif action is None: key = repr(event.char) if key in self.commands: state = self.matrix[:] self.matrix, done, score_increase = self.commands[key](self.matrix) action = key[:] reward = score_increase self.score += score_increase if [0 for row in self.matrix if 0 in row]: generated_new = True self.matrix = add_two_or_four(self.matrix) state_after = self.matrix[:] if game_state(self.matrix) == 'lose' or (state_after == state and generated_new == False): reward -= 100 terminal = True print(f"This EP Score: {self.score}") self.reset_episode() five_tup = (state_after, reward, terminal) [print(row) for row in state] print(action) return five_tup
def get_score(board, first_move): sboard = board.copy() sboard, moved, score = logic.move(sboard, first_move) if not moved: return -1 total_score = 0 for i in range(100): game_score = score simulation_board = sboard.copy() simulation_board = logic.add_two_or_four(simulation_board) while logic.game_state(simulation_board) != "lose": simulation_board, moved, move_score = logic.move( simulation_board, randint(0, 3)) if moved: simulation_board = logic.add_two_or_four(simulation_board) game_score += move_score total_score += game_score return total_score / 100
def auto_solve(self): while True: input = get_best_input(self.matrix) self.matrix, moved, score = logic.move(self.matrix, input) if moved: self.matrix = logic.add_two_or_four(self.matrix) self.update_grid_cells() moved = False self.update()
def get_score(board, first_move): """ Given a board and a first_move, get a score by playing a lot of random games """ sboard = board.copy() sboard, moved, score = logic.move(sboard, first_move) if not moved: return -1 total_score = 0 for i in range(SIMULATION_NUMBER): game_score = score simulation_board = sboard.copy() simulation_board = logic.add_two_or_four(simulation_board) while logic.game_state(simulation_board) != "lose": simulation_board, moved, move_score = logic.move(simulation_board, random.randint(0, 3)) if moved: simulation_board = logic.add_two_or_four(simulation_board) game_score += move_score total_score += game_score return total_score / SIMULATION_NUMBER
def __init__(self): self.matrix = np.zeros((4, 4)) self.matrix = logic.add_two_or_four(self.matrix) self.matrix = logic.add_two_or_four(self.matrix)
def init_matrix(self): self.matrix = new_game(GRID_LEN) self.matrix = add_two_or_four(self.matrix) self.matrix = add_two_or_four(self.matrix)