-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtester.py
More file actions
62 lines (45 loc) · 2.04 KB
/
tester.py
File metadata and controls
62 lines (45 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import datetime
import multiprocessing
import time
import engine
def play_game(game_id, ai_plays_as):
x_turns = []
game_engine = engine.Engine()
game_engine.playing_as = ai_plays_as
old_engine = engine.OriginalEngine()
old_engine.playing_as = 'X' if ai_plays_as == 'O' else 'O'
while game_engine.won == '':
if game_engine.turn == ai_plays_as:
move = game_engine.ai_turn(True)
else:
move = old_engine.ai_turn(True)
# valid_moves = [i for i, space in enumerate(game_engine.board) if space == ' ']
# move = random.choice(valid_moves)
x_turns.append(move)
game_engine.board[move] = game_engine.turn
game_engine.turn = 'O' if game_engine.turn == 'X' else 'X'
game_engine.check_for_end()
old_engine.board[move] = game_engine.turn
old_engine.turn = 'O' if game_engine.turn == 'X' else 'X'
return game_engine.won, x_turns
def play_games(threads):
with multiprocessing.Pool() as pool:
games = pool.starmap(play_game, [(game_id, ai_plays_as) for game_id in range(threads)])
return games
if __name__ == '__main__':
# I have an AMD Ryzen 9 7900X with 12 cores and 24 threads and 64GB of RAM.
# LOWER THIS NUMBER SIGNIFICANTLY IF YOU HAVE LESS RESOURCES.
num_games = 10_000_000
ai_plays_as = 'X'
start_time = time.time()
results = play_games(num_games)
end_time = time.time()
winners = [result[0] for result in results]
print(
f'AI Won: {winners.count(ai_plays_as)} - Lost {winners.count("X" if ai_plays_as == "O" else "O")} - Tied {winners.count("Tie!")}')
if winners.count("X" if ai_plays_as == "O" else "O") > 0:
games_tester_won = [result[1] for result in results if result[0] == ("X" if ai_plays_as == "O" else "O")]
print(f'{"X" if ai_plays_as == "O" else "O"}\'s moves in games it won: {games_tester_won}')
else:
print('Seems pretty unbeatable!')
print(f'Tested {num_games} games in {datetime.timedelta(seconds=end_time - start_time)}')