-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamev4.py
373 lines (274 loc) · 8.72 KB
/
gamev4.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
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
# v7 revision
# this version is a working version of a search depth 1 pooling system
# although it has major bugs, this is actually a working prototype capable of creating world records
# v8 revision
# i found out why different boards gave the same hash
# when boards are parsed as string, they only give edge values
import itertools
from itertools import repeat
import numpy as np
import pprint
import heapq
import copy
X=6
Y=8
SIZE=X*Y
#test
CUT=40
MAX_BRANCH_POOL=20000
FEVER=10
MAX_INT=15
class r_board:
def __init__(self, board, count, lost, path=[]):
self.board=board
self.lost=lost
self.count=count
self.path=path
def __eq__(self, other):
return self.board.__hash__==other.board.__hash__
def __repr__(self):
i=0
outstr="\n"+"-"*(X*3+2)+"\n"
for yy in range(Y):
outstr+="|"
for xx in range(X):
xx,yy
outstr+=str(self.board[i]).rjust(2, " ")+" "
i+=1
outstr+="|\n"
outstr+="-"*(X*3+2)+f"\nlost: {self.lost}\n"
return outstr
def __hash__(self):
return hash(str(self.board))
def get_valid_moves(self):
if self.count==0:
return [[Move(0, None, None)]]
moves=[[] for x in repeat(None, MAX_INT)]
if self.count==1:
# only can be 15
moves=[[] for x in repeat(None, MAX_INT+1)]
for s in range(SIZE):
# print(s)
if self.board[s]!=0:
moves[self.board[s]].append(Move(0, s, s))
return moves
else:
moves=[[] for x in repeat(None, MAX_INT)]
count_minus_1=self.count-1
count_minus_2=self.count-2
if self.count <= FEVER:
# fever time
for select_index in range(SIZE-1):
if self.board[select_index] != 0:
for target_index in range(select_index+1, SIZE):
if self.board[target_index] != 0:
if self.board[select_index]==self.board[target_index]:
# dont flip
moves[0].append(Move(count_minus_2, select_index, target_index))
else:
moves[min(self.board[select_index], self.board[target_index])].extend([Move(count_minus_1, select_index, target_index), Move(count_minus_1, target_index, select_index)])
else:
one_index=[]
for row_idx in range(0, SIZE, X):
for select_index in range(row_idx, row_idx+X):
if self.board[select_index] != 0:
if self.board[select_index] == 1:
one_index.append(select_index)
for target_index in list(range(select_index+1, row_idx+X)):
if self.board[target_index] != 0:
if self.board[select_index]==self.board[target_index]:
moves[0].append(Move(count_minus_2, select_index, target_index))
else:
moves[min(self.board[select_index], self.board[target_index])].extend([Move(count_minus_1, select_index, target_index), Move(count_minus_1, target_index, select_index)])
break
for target_index in list(range(select_index+X, SIZE, X)):
if self.board[target_index] != 0:
if self.board[select_index]==self.board[target_index]:
if self.board[select_index] != 1:
moves[0].append(Move(count_minus_2, select_index, target_index))
else:
if self.board[select_index]==1:
moves[1].append(Move(count_minus_1, target_index, select_index))
else:
moves[min(self.board[select_index], self.board[target_index])].extend([Move(count_minus_1, select_index, target_index), Move(count_minus_1, target_index, select_index)])
break
# print("*"*100)
# pprint.pprint(moves)
for target_index, val in enumerate(self.board):
if val > 1:
for select_index in one_index:
moves[1].append(Move(count_minus_1, select_index, target_index))
# print("*"*100)
# pprint.pprint(moves)
for i, select_index in enumerate(one_index[:-1]):
for target_index in one_index[i+1:]:
moves[0].append(Move(count_minus_2, select_index, target_index))
return moves
# def return_move(self, move):
# # returns a new instance of self with new move
# data=self.board[:]
# # no need to calculate count
# lost=min(data[move.select], data[move.target])
# data[move.target]=abs(data[move.select]-data[move.target])
# data[move.select]=0
# return r_board(data, move.count, lost, self.path.append(move))
def do_move(self, move):
if move.select==None:
return self
if self.board[move.select]==self.board[move.target]:
if move.select == move.target:
lost=self.board[move.select]
else:
lost=0
else:
lost=min(self.board[move.select], self.board[move.target])
self.board[move.target]=abs(self.board[move.select]-self.board[move.target])
self.board[move.select]=0
self.count=move.count
self.path.append([move.select, move.target])
self.lost+=lost
return self
class Move:
def __init__(self, count, select, target):
self.count=count
self.select=select
self.target=target
def __repr__(self):
return f"<Move count:{repr(self.count)} select:{repr(self.select)} target:{repr(self.target)}>"
class Child_Move:
def __init__(self, move, id):
self.select=move.select
self.target=move.target
self.count=move.count
self.id=id
def __repr__(self):
return f"<Move count:{repr(self.count)} select:{repr(self.select)} target:{repr(self.target)} id:{repr(self.id)}>"
def __lt__(self, other):
return self.count < other.count
# log_file=open()
if __name__=="__main__":
# print("\n"*1000)
# log_file=open("./outlog.txt", "w")
# log_file.write("")
# log_file.close()
# log_file.
# game_board=r_board(np.array(
# [
# 3, 9, 12, 3, 9, 10,
# 15, 3, 12, 10, 5, 11,
# 6, 12, 7, 10, 1, 3,
# 8, 13, 5, 8, 2, 8,
# 15, 1, 7, 10, 8, 15,
# 3, 15, 14, 14, 15, 15,
# 6, 1, 4, 14, 15, 8,
# 7, 14, 4, 9, 3, 2
# ], dtype=np.byte), SIZE, 0)
# game_board=r_board(np.array(
# [
# 4, 7, 2, 9, 12, 15,
# 7, 4, 1, 5, 1, 4,
# 8, 13, 13, 10, 4, 13,
# 4, 15, 9, 4, 4, 12,
# 4, 2, 4, 14, 13, 6,
# 15, 13, 1, 4, 1, 10,
# 9, 4, 6, 9, 2, 11,
# 10, 11, 1, 9, 11, 11
# ], dtype=np.byte), SIZE, 0)
game_board=r_board(np.array(
[10, 15, 10, 5, 13, 11, 2, 4, 1, 11, 13, 7, 4, 1, 7, 5, 13,
5, 1, 7, 8, 7, 10, 15, 13, 6, 14, 3, 1, 10, 1, 7, 7, 2,
13, 10, 9, 15, 6, 2, 5, 6, 9, 2, 10, 4, 11, 10], dtype=np.byte), SIZE, 0)
# game_board=r_board(np.array(
# [
# 1,2,
# 3,4], dtype=np.byte), SIZE, 0)
# game_board=r_board(np.array(
# [
# 3,4,4,3], dtype=np.byte), SIZE, 0)
# print(game_board)
# pprint.pprint(game_board.get_valid_moves())
# s=0
# for thing in game_board.get_valid_moves():
# s+=len(thing)
# print(s)
# exit()
old_list=[game_board]
iterblock=0
new_set=set()
while True:
print(f"iterating {iterblock} times...")
# print("-"*100)
# print(old_list)
# until global pool doesnt have a count != 0
again=False
global_pool=[[] for x in repeat(None, CUT)]
for id, root_game in enumerate(old_list):
lost=root_game.lost
# print("ENUMERATE")
for lost_indexed_moves in root_game.get_valid_moves()[0:CUT-lost]:
# print("LOST_INDEXED")
# print(lost_indexed_moves)
for move in lost_indexed_moves:
if (again==False) and (move.count != 0):
again=True
# print(move)
global_pool[lost].append(Child_Move(move, id))
lost+=1
if lost >= CUT:
break
# pprint.pprint(global_pool)
# we have the global pool now
if again==False:
print("returning best")
for child_move_lists in global_pool:
if len(child_move_lists) > 0:
for stuff in child_move_lists:
final_board=copy.deepcopy(old_list[stuff.id])
final_board.do_move(stuff)
print("-"*100)
print(final_board.path)
print(final_board.lost)
print("-"*100)
exit()
print(":(")
remaining_branches=MAX_BRANCH_POOL
for moves in global_pool:
# print(remaining_branches)
if len(moves) > remaining_branches:
# print("iterating...")
# do stuff here
# pprint.pprint(heapq.nlargest(remaining_branches, moves))
for cmove in heapq.nlargest(remaining_branches, moves):
# we have time
# move is a Child_Move
if cmove.target == None:
new_set.add(old_list[cmove.id])
else:
new_set.add(copy.deepcopy(old_list[cmove.id]).do_move(cmove))
else:
# print("not iterating...")
# we have time
for cmove in moves:
# move is a Child_Move
if cmove.target == None:
new_set.add(old_list[cmove.id])
else:
new_set.add(copy.deepcopy(old_list[cmove.id]).do_move(cmove))
remaining_branches-=len(moves)
if remaining_branches<=0:
break
# input()
# print("newset vvvvvvvvvvvvvvvv")
# print(new_set)
# print("-"*100)
# print("setting old list to new set")
old_list=list(new_set)
if len(old_list) == 0:
print("No solution under given parameters")
break
new_set=set()
if iterblock > 100:
print("overloop")
break
iterblock+=1
# input()