-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
146 lines (115 loc) · 3.74 KB
/
main.py
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import timeit
import game
import Minimax
import MCTS
def readBoard(file):
count = 0
board = []
with open(file, 'r') as f:
for line in f:
board.append([int(x) for x in line.split()])
count += 1
if count == 5: break
return board
def printBoard(board):
for i in range(4, -1, -1):
for j in range(5):
e = ''
if j == 4: e = '\n'
if board[i][j] == -1:
print(' X ', end = e)
elif board[i][j] == 1:
print(' O ', end = e)
else:
print(' - ', end = e)
print('')
def saveBoard(board, file):
with open(file, 'w') as f:
for i in range(5):
for j in range(5):
if board[i][j] != -1:
f.write(' ' + str(board[i][j]) + ' ')
else:
f.write(str(board[i][j]) + ' ')
f.write('\n')
def restart(file):
with open(file, 'w') as f:
f.write(' 1 1 1 1 1\n')
f.write(' 1 0 0 0 1\n')
f.write('-1 0 0 0 1\n')
f.write('-1 0 0 0 -1\n')
f.write('-1 -1 -1 -1 -1')
def move(prev_board, board, player, remain_time_x, remain_time_o):
start = timeit.default_timer()
# Use depth = 2 when fighting online with 'random move' bot
# Use depth >= 4 when fighting offline with another team's bot
depth = 4
# Using Minimax
"""
solver = Minimax.Solver(depth, prev_board, board, player)
result = solver.solv()
"""
# Using MCTS
threshold = 15
solver = MCTS.Solver(prev_board, board, player, threshold)
result = solver.solv()
stop = timeit.default_timer()
time_step = stop - start
if player == 1:
remain_time_o -= time_step
else:
remain_time_x -= time_step
return result
restart('input.txt')
restart('output.txt')
cg = game.CoGanh()
bot = input('--> Bot(X/O): ')
if bot == 'x' or bot == 'X': player = 'O'
else: player = 'X'
print('\n--> Player(X/O): ' + player)
inp = input('\n--> First(X/O): ')
remain_time_x = 100
remain_time_o = 100
board = None
prev_board = None
while True:
print('\n================================================\n- TURN: ' + inp)
if inp == 'o' or inp == 'O':
board = readBoard('input.txt')
printBoard(board)
if bot == 'o' or bot == 'O':
step = move(prev_board, board, 1, remain_time_x, remain_time_o)
print(step)
start, end = (step[0][0], step[0][1]), (step[1][0], step[1][1])
prev_board = board
else:
pos = input('POSITION: ')
tmp = [int(x) for x in pos]
start, end = (tmp[0], tmp[1]), (tmp[2], tmp[3])
prev_board = None
cg.simple_move(board, start, end)
saveBoard(board, 'output.txt')
if cg.end_game(board):
break
inp = 'X'
elif inp == 'x' or inp == 'X':
board = readBoard('output.txt')
printBoard(board)
if bot == 'x' or bot == 'X':
step = move(prev_board, board, -1, remain_time_x, remain_time_o)
print(step)
start, end = (step[0][0], step[0][1]), (step[1][0], step[1][1])
prev_board = board
else:
pos = input('POSITION: ')
tmp = [int(x) for x in pos]
start, end = (tmp[0], tmp[1]), (tmp[2], tmp[3])
prev_board = None
cg.simple_move(board, start, end)
saveBoard(board, 'input.txt')
if cg.end_game(board):
break
inp = 'O'
else:
print("\nEND PROGRAM")
break