def auto_play(self): import game self.matrix = game.init_matrix() while not game.is_over(self.matrix): print('') game.print_matrix(self.matrix) move, _score = self.find_best_move() print("best move:", move, 'with score: ', _score) _matrix = move(self.matrix) if _matrix == self.matrix: print("invalid move, nothing changed.") else: self.matrix = game.random_insert(_matrix)
def ai_play(): step = 0 matrix = game.init_matrix() moves = [game.move_up, game.move_down, game.move_left, game.move_right] while not game.is_over(matrix): game.print_matrix(matrix) best_move, best_score = find_best_move(matrix) if best_move == -1: print('cannot find best move, quit game.') break matrix = moves[best_move](matrix) print('step %4d the best move is %4s, the score %5d' % \ (step, ['up', 'down', 'left', 'right'][best_move], best_score)) matrix = game.random_insert(matrix) step += 1 return