-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtable.py
More file actions
352 lines (323 loc) · 13.7 KB
/
Copy pathtable.py
File metadata and controls
352 lines (323 loc) · 13.7 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import random
class Board:
def __init__(self,table):
# 1 root & 1 rollout. at the end of rollout copy all values of root to rollout to go again
self.table = table
self.height = len(table)
self.width = len(table[0])
self.lol = self.lol_creation()
# in a tradinonal 6x7 game there are 69 ways to get 4 in a row ;)
self.root_sec_list = [0 for _ in range(self.width*self.height*4)]
self.rollout_sec_list = [0 for _ in range(self.width*self.height*4)]
self.rollout_possible_moves = [0 for _ in range(self.width)]
#these two are for finding untried moves
self.rollout_find_move = [0 for _ in range(self.width)]
self.random_move_bucket = [0 for _ in range(self.width)]
self.root_turn = 0
self.rollout_turn = 0
self.root_total_moves = 0
self.rollout_total_moves = 0
self.rollout_turn = 0
self.root_hash = [0,0]
self.rollout_hash = [0,0]
self.root_setup()
# for print table highlighting
self.last_move = [None,None]
def lol_creation(self):
'''
I forget the acronymn but I am keeping it
for n in range(self.height):
for k in range(self.width):
self.lol[n+(self.height*k)]:
reads top down left to right
down is first, right is second, down right is third, up right is fourth
if you follow this order you only have to check these directions for each one
'''
lol = [[] for _ in range(self.height*self.width)]
down_constant = (self.height*self.width*0)
right_constant = (self.height*self.width*1)
dr_constant = (self.height*self.width*2)
ur_constant = (self.height*self.width*3)
for i in range(self.height):
for j in range(self.width):
# bottom
sec_list_position = (i + (j*self.height) + down_constant)
four_slots = []
for k in range(4):
if i+k < self.height:
four_slots.append((i+k+(self.height*j)))
if len(four_slots) == 4:
for n in four_slots:
lol[n].append(sec_list_position)
# right
sec_list_position = (i + (j*self.height) + right_constant)
four_slots = []
for k in range(4):
if j+k < self.width:
four_slots.append((i+(self.height*(j+k))))
if len(four_slots) == 4:
for n in four_slots:
lol[n].append(sec_list_position)
# dr
sec_list_position = (i + (j*self.height) + dr_constant)
four_slots = []
for k in range(4):
if j+k < self.width and i+k < self.height:
four_slots.append((i+k+(self.height*(j+k))))
if len(four_slots) == 4:
for n in four_slots:
lol[n].append(sec_list_position)
# ur
sec_list_position = (i + (j*self.height) + ur_constant)
four_slots = []
for k in range(4):
if j+k < self.width and i-k >= 0:
four_slots.append((i-k+(self.height*(j+k))))
if len(four_slots) == 4:
for n in four_slots:
lol[n].append(sec_list_position)
return lol
def random_rollout(self):
legal_moves = 0
for i in range(self.width):
if self.rollout_possible_moves[i] != -1:
self.random_move_bucket[legal_moves] = i
legal_moves +=1
legal_moves -= 1
while self.rollout_total_moves < (self.height*self.width):
assert(legal_moves != -1)
random_i = random.randint(0,legal_moves)
random_move = self.random_move_bucket[random_i]
if self.sec_int_3_rollout(random_move):
return self.rollout_turn
self.rollout_possible_moves[random_move] -= 1
if self.rollout_possible_moves[random_move] == -1:
# like swap but value going to the back doesn't matter
self.random_move_bucket[random_i] = self.random_move_bucket[legal_moves]
legal_moves -= 1
self.rollout_turn *= -1
self.rollout_total_moves +=1
return 0
def rollout_set_up(self):
'''
just resets all the rollout stuff before rollout
:return:
'''
for i in range(len(self.root_sec_list)):
self.rollout_sec_list[i] = self.root_sec_list[i]
for i in range(self.width):
self.rollout_find_move[i] = 0
self.rollout_possible_moves[i] = self.root_possible_moves[i]
self.rollout_turn = self.root_turn
self.rollout_total_moves = self.root_total_moves
self.rollout_hash[0] = self.root_hash[0]
self.rollout_hash[1] = self.root_hash[1]
def untried_moves(self,children_moves):
'''
:param child_nodes: children nodes of node
:return: a random legal move, and len(legal_moves) -1, that way can tell if need to mark fully expanded as True
'''
for i in range(self.width):
self.rollout_find_move[i] = 0
for i in range(self.width):
if self.rollout_possible_moves[i] == -1:
self.rollout_find_move[i] = -1
for child_move in children_moves:
#TODO replace child.justmoved
self.rollout_find_move[child_move] = -1
return self.random_legal_untried_move()
def random_legal_untried_move(self):
'''
so i take random move bucket and just add i's that aren't possible_move[i] == -1, then do random on number of moves i added
:return: a random legal i for possible_moves[i]
'''
legal_moves = 0
for i in range(self.width):
if self.rollout_find_move[i] != -1:
self.random_move_bucket[legal_moves] = i
legal_moves += 1
legal_moves -= 1
assert(legal_moves != -1)
# going to return legal moves, then can catch fully expanded right away
random_move = self.random_move_bucket[random.randint(0, legal_moves)]
return random_move
def root_setup(self):
''''''
# first whipe
for n in range(len(self.root_sec_list)):
self.root_sec_list[n] = 0
totalx = 0
totalo = 0
for n in range(self.height):
for k in range(self.width):
if self.table[n][k] == 'x':
totalx +=1
for value in self.lol[n+(self.height*k)]:
self.root_sec_list[value] += 1
self.toggle_bit_setup(n+(self.height*k),1)
elif self.table[n][k] == 'o':
totalo +=1
for value in self.lol[n+(self.height*k)]:
self.root_sec_list[value] -= 1
self.toggle_bit_setup(n + (self.height * k), -1)
if totalx > totalo:
self.root_turn = -1
else:
self.root_turn = 1
self.root_total_moves = totalo + totalx
self.root_possible_moves = self.possible_simple()
def sec_int_3(self,move,three):
lol_index = self.rollout_possible_moves[move] + move * self.height
for n in self.lol[lol_index]:
if self.rollout_sec_list[n] == three:
return True
return False
def sec_int_3_rollout(self,move):
'''
check if 3 but if not also adds, should save some time
:param move:
:return:
'''
if self.rollout_turn == 1:
three = 3
else:
three = -3
lol_index = self.rollout_possible_moves[move] + move * self.height
for n in self.lol[lol_index]:
if self.rollout_sec_list[n] == three:
return True
else:
self.rollout_sec_list[n] += self.rollout_turn
return False
def rollout_move(self,move):
self.rollout_total_moves += 1
lol_index = self.rollout_possible_moves[move] + move * self.height
for n in self.lol[lol_index]:
self.rollout_sec_list[n] += self.rollout_turn
self.toggle_bit_rollout((self.rollout_possible_moves[move] + move * self.height),self.rollout_turn)
self.rollout_turn *= -1
self.rollout_possible_moves[move] -= 1
def check_node_terminal(self):
'''
return terminal and anti_terminal; anti_terminal will be checked after terminal is determined false
I will include antiterminal here and I will check it after game over boolean because of tie logic
return:: terminal,result,move,anti_terminal,anti_terminal_move
if terminal, then anti_terminal is automcatically false
'''
if self.rollout_turn == 1:
three = 3
else:
three = -3
for i in range(self.width):
if self.rollout_possible_moves[i] != -1:
if self.sec_int_3(i,three):
return True, self.rollout_turn,i, False, None
if self.rollout_total_moves == ((self.height*self.width)):
return True, 0, None, False, None
if self.rollout_turn == 1:
three = -3
else:
three = 3
for i in range(self.width):
if self.rollout_possible_moves[i] != -1:
if self.sec_int_3(i,three):
return False, None, None, True, i
return False, None, None, False, None
def toggle_bit_setup(self,bit_position,turn):
if turn ==1:
mask = 1 << bit_position # Create a mask with a 1 at the desired bit position
self.root_hash[0] = self.root_hash[0] ^ mask # XOR the value with the mask to flip the bit
else:
mask = 1 << bit_position # Create a mask with a 1 at the desired bit position
self.root_hash[1] = self.root_hash[1] ^ mask
def toggle_bit_rollout(self, bit_position,turn):
if self.rollout_turn ==1:
mask = 1 << bit_position # Create a mask with a 1 at the desired bit position
self.rollout_hash[0] = self.rollout_hash[0] ^ mask # XOR the value with the mask to flip the bit
else:
mask = 1 << bit_position # Create a mask with a 1 at the desired bit position
self.rollout_hash[1] = self.rollout_hash[1] ^ mask
def possible_simple(self):
possible_moves = []
for k in range(self.width):
n = self.height-1
found = False
while n >= 0 and found == False:
if self.table[n][k] == ' ':
found = True
else:
n -= 1
possible_moves.append(n)
return possible_moves
def check_root_gameover(self):
'''
:return: bool is game over? , game winner == turn or 0 if tie
'''
for n in self.root_sec_list:
if n == 4:
return True,1
if n == -4:
return True,-1
if self.root_total_moves ==((self.height*self.width)):
return True,0
return False, -2
def table_update(self,move):
k = move
n = self.root_possible_moves[move]
if self.root_turn == 1:
char = 'x'
else:
char = 'o'
self.table[n][k] = char
self.last_move = [n,k]
self.root_setup()
def table_print(self):
sl = []
print_table_new = "\n\n\n\n\n\n\n\n\n\n "
for k in range(1,self.width+1):
print_table_new += f"{k} "
print_table_new += "\n-"
for k in range(1,self.width+1):
print_table_new += f"--------"
for n in range(self.height):
print_table_new += "\n|"
for k in range(self.width):
if self.table[n][k] == ' ':
print_table_new += ' |'
else:
if self.table[n][k] == 'x':
normal = "[31m"
highlight = "[41m"
bright = "[91m"
three = 3
four = 4
else:
normal = "[34m"
highlight = "[44m"
bright = "[96m"
three = -3
four = -4
if [n,k] == self.last_move:
print_table_new +=f' \033{highlight}{self.table[n][k]}\033[0m |'
else:
for value in self.lol[n+(self.height*k)]:
if self.root_sec_list[value] == four:
print_table_new +=f' \033{highlight}{self.table[n][k]}\033[0m |'
break
else:
for value in self.lol[n+(self.height*k)]:
if self.root_sec_list[value] == three:
print_table_new +=f' \033{bright}{self.table[n][k]}\033[0m |'
break
else:
print_table_new +=f' \033{normal}{self.table[n][k]}\033[0m |'
print_table_new += '\n-'
for k in range(1,self.width+1):
print_table_new += f"--------"
print(print_table_new)
def legal_moves(self):
legal_moves = 0
for n in self.rollout_possible_moves:
if n != -1:
legal_moves +=1
return legal_moves