def __init__(self,player1=randomAI.RandomAI(1,"black","randomAIOne"),player2=randomAI.RandomAI(1,"white","randomAITwo")): self.player1 = player1 self.player2 = player2 self.__gameBoard = GomokuBoard() self.isRecord = False self.resultHistory = np.zeros((1,1,4)) self.gameHistory = None
def play(printGame=True): board = chess.Board() if printGame: print board print "Let's start with this game of chess" numMoves = 0 #player1 = human.Human("Player 1") player1 = randomAI.RandomAI("Player 1") player2 = randomAI.RandomAI("Player 2") while (not board.is_game_over()): if printGame: print '\n', player1.description, 'will now play' player1Move = player1.move(board) board.push(player1Move) if printGame: print board if printGame: print '\n', player2.description, 'will now play' player2Move = player2.move(board) if player2Move == -1: return board, numMoves board.push(player2Move) if printGame: print board numMoves += 1 if printGame: print '\nResult:', board.result() print 'Number of moves:', numMoves return board, numMoves
def __init__(self, c, rm, nnet): self.c = c self.rm = rm self.ai1 = snnAI.SnnAI(c, nnet, add_noise=True) self.ai2 = randomAI.RandomAI() # The length of this set is the number of distinct games # generated in the current iteration. self.game_codes = set() # The list of the GenGameState objets representing the games # played in parallel. self.states1 = [] self.states2 = []
def __init__(self, evaluator_ai, num_games): if evaluator_ai == 'random': self.evaluator_ai = randomAI.RandomAI() else: self.evaluator_ai = evaluator_ai self.num_games = num_games self.num_wins = 0 self.num_draws = 0 self.num_moves = 0 self.duration = 0 self.win_perc = -1 self.draw_perc = -1 self.win_no_draws_perc = -1 self.avg_game_length = -1 self.duration = -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 if __name__ == "__main__": ai = randomAI.RandomAI() c = Configuration() ai = snnAI.SnnAI(c) org = GameOrganizer(ai) # org.usi_engine("SNN AI") org.ai_vs_ai(512, print_moves=True, print_outcome=True) # org.ai_vs_player() # org.player_vs_player()
elif self.board[1, x, y]: res += 'O' + blank else: res += '.' + blank if x != 2: res += '\n\n' return res if __name__ == '__main__': from encoding import * from configuration import Configuration c = Configuration() randAI = randomAI.RandomAI() nnAI = snnAI.SnnAI(c) nnAI_plays = True human_plays = True s = GameState() while not s.finished: if human_plays and not nnAI_plays: move_str = input("\nChoose your move: ") move = tuple(int(x) for x in move_str.split(',')) elif nnAI_plays: v = nnAI.nnet(encode_game_state(s).unsqueeze(0).cuda()) # pylint: disable=E v = v.squeeze(0).detach().cpu().numpy()
self.win_perc = 100 * self.num_wins / self.num_games self.win_black_perc = 100 * self.num_wins_black / self.num_games self.win_white_perc = 100 * self.num_wins_white / self.num_games self.draw_perc = 100 * self.num_draws / self.num_games self.win_no_draws_perc = -1 self.avg_game_length = -1 if self.num_games != self.num_draws: self.win_no_draws_perc = 100 * self.num_wins / (self.num_games - self.num_draws) self.avg_game_length = self.num_moves / (self.num_games - self.num_draws) if __name__ == '__main__': evaluated_ai = randomAI.RandomAI() num_games = 1000 ev = Evaluator('random', num_games, 512) ev.evaluate_ai(evaluated_ai) str_eval_duration = str(datetime.timedelta(seconds=round(ev.duration))) print(f"wins: {round(ev.win_perc, 2)} % | " f"draws: {round(ev.draw_perc, 2)} % | " f"wins no draws: {round(ev.win_no_draws_perc, 2)} %" f"\nwins as black: {round(ev.win_black_perc, 2)} % | " f"wins as white: {round(ev.win_white_perc, 2)} %" f"\naverage length: {round(ev.avg_game_length)} | " f"eval duration: {str_eval_duration}")