def simulate(lineup, games, print_outcomes=True): winlog = [] P1 = lineup[0] P2 = lineup[1] for _ in range(games): game = Game(P1, P2) game.play(print_board=False, record_history=False) winlog.append([P1.name(), P2.name(), game.winner.nr]) winlog = pd.DataFrame(winlog, columns=['P1', 'P2', 'winner']) wins = round(100*len(winlog[winlog['winner']==1])/games, 1) lost = round(100*len(winlog[winlog['winner']==2])/games, 1) draw = round(100*len(winlog[winlog['winner']==0])/games, 1) if print_outcomes: print(f'P1 W: {wins} | D: {draw} | L: {lost}') return wins, draw, lost
(HumanPlayer(), BotPlayer(model=P2model)), # (BotPlayer(model=P1model), HumanPlayer()) ] games = 3 winlog = [] for match in lineup: P1 = match[0] P2 = match[1] print(P1.name() + ' vs ' + P2.name()) for _ in range(games): game = Game(P1, P2) game.play(print_board=True, record_history=False) winlog.append([P1.name(), P2.name(), game.winner.nr]) winlog = pd.DataFrame(winlog, columns=['P1', 'P2', 'winner']) results = winlog.groupby( by=['P1', 'P2', 'winner'])['winner'].count().to_frame('pc') / games results = results.pivot_table(index=['P1', 'P2'], columns='winner', values='pc').reset_index() results = results.rename(columns={0: 'Draw', 1: 'Win', 2: 'Lost'}) print(results)
message = "Do you want to be Crosses or Noughts [X/O]: " human_is_cross = get_input(message, lambda x: x in ['x', 'o']) == 'x' message = "Human starts or computer starts? [H/C]: " human_move = get_input(message, lambda x: x in ['h', 'c']) == 'h' game = Game(player1_first=human_move, player1_is_cross=human_is_cross) while True: if (game.player1_move): game.print_boards() move_raw = get_input("Enter move seperated by spaces (board x y) ", game.check_valid_move_string) move = list(map(int, move_raw.split())) board, x, y = move result = game.play(move) else: translate = mf(np.array(game.boards).flatten()) moves = mlp.predict([translate])[0] moves = [z[1] for z in sorted([(x, i) for i, x in enumerate(moves)])] #print("Wat",moves) for move in moves: board = move // 9 y = (move % 9) // 3 x = move % 3 move = (board, x, y) #print("Player2: Trying move", move) result = game.play(move) if (result != -2):
def evaluate_game(mlp1, mlp2): mf = np.vectorize(lambda x: 0.5 if x is None else float(x)) game = Game() result = None depth = 0 mistakes = 0 while (result not in [-1,0,1]): #print(result) #game.print_boards() depth += 1 if (game.player1_move): translate = mf(np.array(game.boards).flatten()) moves = mlp1.predict([translate])[0] moves = [z[1] for z in sorted([(x,i) for i,x in enumerate(moves)])] for move in moves: board = move // 9 y = (move % 9) // 3 x = move % 3 move = (board,x,y) #print("Player1: Trying move", move) result = game.play(move) if (result != -2): #print("Move made") break mistakes += 1 else: translate = mf(np.array(game.boards).flatten()) moves = mlp2.predict([translate])[0] moves = [z[1] for z in sorted([(x,i) for i,x in enumerate(moves)])] #print("Wat",moves) for move in moves: board = move // 9 y = (move % 9) // 3 x = move % 3 move = (board,x,y) #print("Player2: Trying move", move) result = game.play(move) if (result != -2): #print("Move made") break if (result == 1): return (10,depth,mistakes) elif (result == -1): return (-10,depth,mistakes) elif (result == 0): return (20,depth,mistakes) else: print("This should never happen") return None if (result == 1): average += (10.0) #+depth*3)/mistakes elif (result == -1): average += (-10.0) #+depth*3)/mistakes elif (result == 0): average += 1000.0 else: print("This should never happen") return None return (average/10.0,)
def evaluate_game(mlp1): mf = np.vectorize(lambda x: 0.5 if x is None else float(x)) game = Game() result = None depth = 0 mistakes = 0 average = 0.0 average_depth = 0.0 average_mistakes = 0.0 for _ in range(10): while (result not in [-1, 0, 1]): #print(result) #game.print_boards() depth += 1 if (game.player1_move): translate = mf(np.array(game.boards).flatten()) moves = mlp1.predict([translate])[0] moves = [ z[1] for z in sorted([(x, i) for i, x in enumerate(moves)]) ] for move in moves: board = move // 9 y = (move % 9) // 3 x = move % 3 move = (board, x, y) #print("Player1: Trying move", move) result = game.play(move) if (result != -2): #print("Move made") break mistakes += 1 else: move = game.get_computer_move() result = game.play(move) #game.print_boards() if (mistakes == 0): mistakes = 1 if (result == 1): average += (10.0) average_depth += depth average_mistakes += mistakes elif (result == -1): average += (-10.0) average_depth += depth average_mistakes += mistakes elif (result == 0): average += 1000.0 else: print("This should never happen") return None return (average / 10.0, average_depth / 10.0, average_mistakes / 10.0)