-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame_types.py
More file actions
98 lines (96 loc) · 3.3 KB
/
Copy pathgame_types.py
File metadata and controls
98 lines (96 loc) · 3.3 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
from mcts import monte_simulation
from table import Board
from time import time
import copy
from statistics import median
from titles import *
def play_itself(table,simulations=1000,save_time="",verbose = 0):
board = Board(table)
is_game_over,winner = board.check_root_gameover()
time_tirals = [f"{save_time}\n"]
times_move = []
while is_game_over == False:
try:
start = time()
move = monte_simulation(board,simulations,verbose=verbose)
end = time()
time_tirals.append(f"move_time: {end-start}\n")
times_move.append(end-start)
board.table_update(move)
is_game_over, winner = board.check_root_gameover()
except Exception as e:
print(board.table)
board.table_print()
print('turn',board.root_turn)
print(e)
print(e.__traceback__)
raise
if verbose > 0:
ans = input("press 4 to finish game, 5 to skip game: ")
if ans == "4":
verbose = 0
elif ans == "5":
is_game_over = True
winner = 0
print(board.table)
board.table_print()
if save_time != "":
time_tirals.append(f"median {median(times_move)}; mean {(sum(times_move))/len(times_move)}\n")
with open(f"time_trials/{save_time}.txt","a") as file:
file.writelines(time_tirals)
if winner == 1:
print('x wins!')
elif winner == -1:
print('o wins!')
else:
print('tie!')
def man_vs_machine(table,width,simulations = 100000,player_turn=True,verbose=0):
board = Board(table)
old_board = None
is_game_over,winner = board.check_root_gameover()
board.table_print()
lets_play = True
while is_game_over == False and lets_play == True:
if player_turn == True:
while 1:
move = input(f'input 1-{width}: ')
try:
move = int(move)-1
if board.root_possible_moves[move] != -1:
break
except:
if move == 'quit':
lets_play = False
break
if move == 'help':
print(f'enter legal move 1-{width}\nquit - exit game\nredo - go back one move')
if move == 'redo':
if old_board != None:
board = old_board
old_board = None
board.table_print()
else:
print('Sorry no more redos')
pass
else:
move = monte_simulation(board,simulations,verbose=verbose,print_eval=True)
if not lets_play:
break
if player_turn == True:
old_board = copy.deepcopy(board)
board.table_update(move)
print(board.table)
board.table_print()
is_game_over, winner = board.check_root_gameover()
if player_turn == True:
player_turn = False
else:
player_turn = True
if winner == 1:
print(x_wins_title)
elif winner == -1:
print(o_wins_title)
elif winner == 0:
print(tie_title)
else:
print(gaveup_title)