def take_action(self, action_number): if action_number in self.commands: self.matrix, done = self.commands[action_number](self.matrix) if done: self.matrix = logic.add_tile(self.matrix) # record last move self.history_matrixs.append(self.matrix)
def _process_move(self, move): try: new_state, done = self.commands[move](self.matrix) if done: # done = not a fully filled game matrix self.matrix = logic.add_tile(self.matrix) self.matrix = new_state except KeyError: raise Exception( "Move is not one of Left, Up, Right, Down but is {}".format( str(move)))
def process_move(self, key): if logic.game_state(self.game_matrix) == logic.STATE_PROGRESS: if key not in self.commands: return self.game_matrix, done = self.commands[key](self.game_matrix) if done: # done = not a fully filled game matrix self.game_matrix = logic.add_tile(self.game_matrix) self.print_game() if logic.game_state(self.game_matrix) == logic.STATE_WIN: print('You have won! Press <r> to reset, <q> to close.\n') if logic.game_state(self.game_matrix) == logic.STATE_LOSE: print('You lost! Press <r> to reset, <q> to close.\n')
def play_game(self, player): self.new_game() loop_detected = False while not loop_detected: previous_score = logic.total_value(self.matrix) move = player.play(self.matrix) self._process_move(move) self.matrix = logic.add_tile(self.matrix) new_score = logic.total_value(self.matrix) loop_detected = new_score == previous_score # If there is no increase in the score, then the move did nothing return logic.total_value( self.matrix), self.highest_tile(), self.fitness()
def key_down(self, event): key = repr(event.char) if key == c.KEY_BACK and len(self.history_matrixs) > 0: self.matrix = self.history_matrixs.pop() self.update_grid_cells() print('back on step total step:', len(self.history_matrixs)) elif key in self.commands: self.matrix, done = self.commands[repr(event.char)](self.matrix) if done: self.matrix = logic.add_tile(self.matrix) # record last move self.history_matrixs.append(self.matrix) self.update_grid_cells() done = False if logic.game_state(self.matrix) == 'win': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Win!", bg=c.BACKGROUND_COLOR_CELL_EMPTY) if logic.game_state(self.matrix) == 'lose': self.grid_cells[1][1].configure( text="You", bg=c.BACKGROUND_COLOR_CELL_EMPTY) self.grid_cells[1][2].configure( text="Lose!", bg=c.BACKGROUND_COLOR_CELL_EMPTY)
def init_matrix(self): self.matrix = logic.new_game(4) self.history_matrixs = list() self.matrix = logic.add_tile(self.matrix) self.matrix = logic.add_tile(self.matrix)
def new_game(self): self.game_matrix = logic.new_game(GRID_LEN) self.game_matrix = logic.add_tile(self.game_matrix) self.game_matrix = logic.add_tile(self.game_matrix) self.print_game()
def new_game(self): self.matrix = logic.new_game(GRID_LEN) self.matrix = logic.add_tile(self.matrix) self.matrix = logic.add_tile(self.matrix)