def evaluate_ai(self, evaluated_ai): start_time = time.time() self.reset() # The games are separated in half. # The first part is the games for which the evaluated AI is black # and the second one is those for which the evaluator is black. states1 = [egn.GameState() for _ in range(self.num_games // 2)] states2 = [ egn.GameState() for _ in range(self.num_games - len(states1)) ] # Whether it's the evaluator's turn in states1. evaluator_plays1 = False while states1 or states2: player1 = self.evaluator_ai if evaluator_plays1 else evaluated_ai player2 = evaluated_ai if evaluator_plays1 else self.evaluator_ai if states1: moves1 = player1.best_moves(states1) self.update_states(states1, moves1, evaluator_plays1) if states2: moves2 = player2.best_moves(states2) self.update_states(states2, moves2, not evaluator_plays1) evaluator_plays1 = not evaluator_plays1 # Compute results self.compute_results() self.duration = time.time() - start_time
def ai_vs_ai_step(self, nb_moves, print_moves=False, print_outcome=False): s = egn.GameState() if print_moves: print() print(s.string_board()) print('\n') for i in range(1, nb_moves+1): instruction = input('') if instruction == 's': break if instruction == 'c': s.undo() if print_moves: self.print_move(s, "cancel", i) continue best_move = self.AI.best_move() if best_move == "resign": if print_outcome: self.print_outcome(s, i) break s.update(best_move) if print_moves: self.print_move(s, best_move, i)
def usi_engine(self, usi_engine_name): s = egn.GameState() while True: # Catch the command sent by the GUI. usi_command = input() # The engine is being registered. if usi_command == "usi": print("id name", usi_engine_name) print("usiok") elif usi_command == "isready": print("readyok") # Save the opponent's last move and update the current game state. elif re.search("position startpos moves", usi_command): last_move = usi_command.split()[-1] s.update(last_move) # Make a move. elif re.search("go", usi_command): best_move = self.AI.best_move() s.update(best_move) print("bestmove", best_move) elif usi_command == "quit": break
def ai_vs_player(self): s = egn.GameState() print() player_side = input("Select your side (b or w) : ") print() print("Initial board position :") print(s.string_board()) print('\n') i = 1 while True: if s.playing_side == player_side: move = input(f"Move number {i} by {s.playing_side} : ") s.update(move) else: move = self.AI.best_move() s.update(move) print(f"Move number {i} by {s.playing_side} : {move}") if move == "quit": break elif move == "resign": print(f"{s.playing_side} resigns") break print("Board after move :") print(s.string_board()) print() i += 1
def ai_vs_ai(self, max_moves, print_moves=False, print_outcome=False): s = egn.GameState() if print_moves: print() print(s.string_board()) print('\n') for i in range(1, max_moves + 1): best_move = self.AI.best_move(s) if best_move == "resign": if print_outcome: self.print_outcome(s, i) break s.update(best_move) if print_moves: self.print_move(s, best_move, i)
def player_vs_player(self): s = egn.GameState() print() print(s.string_board()) print('\n') i = 1 while True: move = input(f"Move number {i} by {s.playing_side} : ") if move == "resign": print(f"{s.playing_side} resigns") break elif move == 'c': s.undo() else: s.update(move) print("Board after move :") print(s.string_board()) print() i += 1