def key_press(self, event): valid_game = True key = repr(event.char) if key == AI_PLAY_KEY: move_count = 0 while valid_game: self.matrix, valid_game = game_ai.ai_move(self.matrix, 40, 30) if valid_game: self.matrix = game_functions.add_new_tile(self.matrix) self.draw_grid_cells() move_count += 1 if key == AI_KEY: self.matrix, move_made = game_ai.ai_move(self.matrix, 20, 30) if move_made: self.matrix = game_functions.add_new_tile(self.matrix) self.draw_grid_cells() move_made = False elif key in self.commands: self.matrix, move_made, _ = self.commands[repr(event.char)]( self.matrix) if move_made: self.matrix = game_functions.add_new_tile(self.matrix) self.draw_grid_cells() move_made = False
def ai_move(board, searches_per_move, search_length): possible_first_moves = [move_left, move_up, move_down, move_right] first_move_scores = np.zeros(NUMBER_OF_MOVES) for first_move_index in range(NUMBER_OF_MOVES): first_move_function = possible_first_moves[first_move_index] board_with_first_move, first_move_made, first_move_score = first_move_function(board) if first_move_made: board_with_first_move = add_new_tile(board_with_first_move) first_move_scores[first_move_index] += first_move_score else: continue for _ in range(searches_per_move): move_number = 1 search_board = np.copy(board_with_first_move) game_valid = True while game_valid and move_number < search_length: search_board, game_valid, score = random_move(search_board) if game_valid: search_board = add_new_tile(search_board) first_move_scores[first_move_index] += score move_number += 1 best_move_index = np.argmax(first_move_scores) best_move = possible_first_moves[best_move_index] search_board, game_valid, score = best_move(board) return search_board, game_valid
def key_press(self, event): valid_game = True key = repr(event.char) #START AI ai_flag = False if key == AI_PLAY_KEY: move_count = 0 ai_flag = ~ai_flag try: while valid_game: self.matrix, valid_game = game_ai.ai_move( self.matrix, 30, 20) # 40 30 if valid_game: self.matrix = game_functions.add_new_tile(self.matrix) self.draw_grid_cells() move_count += 1 except KeyboardInterrupt: pass #HINT KEY if key == AI_KEY: self.matrix, move_made = game_ai.ai_move(self.matrix, 20, 30) if move_made: self.matrix = game_functions.add_new_tile(self.matrix) self.draw_grid_cells() move_made = False #UP LEFT DOWN RIGHT elif key in self.commands: self.matrix, move_made, _ = self.commands[repr(event.char)](self.matrix) if move_made: self.matrix = game_functions.add_new_tile(self.matrix) self.draw_grid_cells() move_made = False
def key_press(self, event): if self.ingame: valid_game = True key = repr(event.char) if key in self.commands: self.matrix, move_made, _ = self.commands[repr(event.char)]( self.matrix) if move_made: self.matrix = game_functions.add_new_tile(self.matrix) self.score = np.sum(self.matrix) self.draw_grid_cells() move_made = False if game_functions.check_for_win(self.matrix) == True: self.input_name("You Win") _, movable = game_functions.fixed_move(self.matrix) if movable == False: self.input_name("You Lost")
def grav_mov(self): self.matrix, move_made, _ = self.commands[DOWN_KEY](self.matrix) if move_made: self.matrix = game_functions.add_new_tile(self.matrix) self.draw_grid_cells() move_made = False