-
Notifications
You must be signed in to change notification settings - Fork 0
/
3_in_arow.py
363 lines (306 loc) · 13.2 KB
/
3_in_arow.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
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
352
353
354
355
356
357
358
359
360
361
362
363
import g2d_pygame as g2d
from time import time
W, H = 40, 40
LONG_PRESS = 0.5
class BoardGame:
def play_at(self, x: int, y: int): abstract()
def flag_at(self, x: int, y: int): abstract()
def value_at(self, x: int, y: int) -> str: abstract()
def col(self) -> int: abstract()
def row(self) -> int: abstract()
def finished(self) -> bool: abstract()
def message(self) -> str: abstract()
def abstract():
raise NotImplementedError("Abstract method")
def gui_play(game: BoardGame):
g2d.init_canvas((game.col() * W, game.row() * H))
ui = BoardGameGui(game)
g2d.main_loop(ui.tick)
def start() -> str:
control = g2d.prompt("Please insert the matrix's dimension [4;6;8;10;12]: ")
diz_k_v = {"4":"conf4.txt","6":"conf6.txt","8":"conf8.txt","10":"conf10.txt","12":"conf12.txt"}
return diz_k_v[control]
class Tinr(BoardGame):
def __init__(self,file:str):
self._board = []
self._row = 0
self._forb = [] # In this list there are the indices of the "forbidden" cells
with open (file,"r") as f:
for line in f:
conf_line = line.strip()
conf_line = conf_line.split(",")
if conf_line[0] == "#":
conf_line.remove("#")
for element in conf_line:
self._board.append(int(element))
elif conf_line[0] == "@":
conf_line.remove("@")
for element in conf_line:
self._forb.append(int(element))
elif conf_line[0] == "r":
conf_line.remove("r")
self._row = int(conf_line[0])
self._col = len(self._board) // self._row
def row(self) -> int :
return self._row
def col(self) -> int:
return self._col
def play_at(self,x,y):
pos = y*self._col+x
if (x >= 0 and x < self._col) and (y >= 0 and y < self._row) and pos not in self._forb :
if self._board[pos] == 0:
self._board[pos] = 1
elif self._board[pos] == 1:
self._board[pos] = -1
elif self._board[pos] == -1:
self._board[pos] = 0
def value_at(self,x,y) -> int:
pos = y*self._col+x
if (x >= 0 and x < self._col) and (y >= 0 and y < self._row) :
return self._board[pos]
def finished(self) -> bool :
control_1 = 0 #This variable represents the number of not gray cells
control_2 = 0 #this variable represents the number of "not contiguos group of three cells"
control_3 = 0 #This variable represents the number of rows and cols with the same amount of cells of the same color
wcounter = 0
bcounter = 0
for elem in self._board:
if elem != 0:
control_1 += 1
for y in range(self._row):
for x in range(self._col-2):
pos = y*self._col+x
if (self._board[pos] != self._board[pos+1]) or (self._board[pos] != self._board[pos+2]):
control_2 += 1
for x in range(self._col):
for y in range(self._row-2):
pos = y*self._col+x
if (self._board[pos] != self._board[pos+self._col]) or (self._board[pos] != self._board[pos+(2*self._col)]):
control_2 += 1
for y in range(self._row):
for x in range(self._col):
pos = y*self._col+x
if self._board[pos] == 1:
wcounter += 1
elif self._board[pos] == -1:
bcounter += 1
if bcounter == wcounter :
control_3 += 1
wcounter = 0
bcounter = 0
for x in range(self._col):
for y in range(self._row):
pos = y*self._col+x
if self._board[pos] == 1:
wcounter += 1
elif self._board[pos] == -1:
bcounter += 1
if bcounter == wcounter :
control_3 += 1
wcounter = 0
bcounter = 0
if control_1 == (self._col*self._row) and control_2 == (2*(self._row-2)*(self._col)) and control_3 == self._col + self._row :
return True
else:
return False
def message(self) -> str :
return ("YOU WON")
def unsolvable(self) -> bool:
bcounter = 0
wcounter = 0
for y in range(self._row):
for x in range(self._col):
pos = y*self._col + x
if self._board[pos] == 1:
wcounter += 1
elif self._board[pos] == -1:
bcounter += 1
if bcounter > (self._col / 2) or wcounter > (self._col / 2) :
return True
bcounter = 0
wcounter = 0
for x in range(self._col):
for y in range(self._row):
pos = y*self._col + x
if self._board[pos] == 1:
wcounter += 1
elif self._board[pos] == -1:
bcounter += 1
if bcounter > (self._row / 2) or wcounter > (self._row / 2) :
return True
bcounter = 0
wcounter = 0
for y in range(self._row):
for x in range(self._col - 2):
pos = y*self._col+x
if (self._board[pos] == self._board[pos+1] and self._board[pos] == self._board[pos + 2] and self._board[pos] != 0):
return True
for x in range(self._col):
for y in range(self._row - 2):
pos = y*self._col+x
if (self._board[pos] == self._board[pos+self._col] and self._board[pos] == self._board[pos + 2*self._col] and self._board[pos] != 0):
return True
return False
def robothelp(self):
bcounter = 0
wcounter = 0
#Here the algorithm checks if the are cols or rows with half cells of the same color and apply a correction
for y in range(self._row):
for x in range(self._col):
pos = y*self._col+ x
if self._board[pos] == 1:
wcounter += 1
elif self._board[pos] == -1:
bcounter += 1
if bcounter == (self._col / 2) :
for x in range(self._col):
pos = y*self._col+x
if self._board[pos] != -1 and pos not in self._forb:
self._board[pos] = 1
elif wcounter == (self._col / 2):
for x in range(self._col):
pos = y*self._col+x
if self._board[pos] != 1 and pos not in self._forb:
self._board[pos] = -1
bcounter = 0
wcounter = 0
for x in range(self._col):
for y in range(self._row):
pos = y*self._col + x
if self._board[pos] == 1:
wcounter += 1
elif self._board[pos] == -1:
bcounter += 1
if bcounter == (self._row / 2):
for y in range(self._row):
pos = y *self._col+x
if self._board[pos] != -1 and pos not in self._forb:
self._board[pos] = 1
elif wcounter == (self._row / 2):
for y in range(self._row):
pos = y*self._col+x
if self._board[pos] != 1 and pos not in self._forb:
self._board[pos] = -1
bcounter = 0
wcounter = 0
#Here the algorithm checks if the are two cells colored in the same way and apply every possible corrections
for y in range(self._row):
for x in range(self._col-2):
pos = y*self._col + x
if (self._board[pos] == self._board[pos + 1] and self._board[pos] != 0 and not pos+2 in self._forb):
self._board[pos+2] = -self._board[pos]
for x in range(self._col):
for y in range(self._row-2):
pos = y*self._col + x
if (self._board[pos] == self._board[pos+self._col] and self._board[pos] != 0 and not pos+(2*self._col) in self._forb):
self._board[pos+(2*self._col)] = -self._board[pos]
for y in range(self._row):
for x in range(2,self._col,1):
pos = y*self._col + x
if (self._board[pos] == self._board[pos - 1] and self._board[pos] != 0 and not pos-2 in self._forb):
self._board[pos-2] = -self._board[pos]
for x in range(self._col):
for y in range(2,self._row,1):
pos = y*self._col + x
if (self._board[pos] == self._board[pos-self._col] and self._board[pos] != 0 and not pos-(2*self._col) in self._forb):
self._board[pos-(2*self._col)] = -self._board[pos]
def black_ver(self,pos:int) -> bool:
formerboard = self._board[:]
self._board[pos] = 1
for teta in range(3):
self.robothelp()
if self.unsolvable():
self._board = formerboard
return True
self._board = formerboard
return False
def white_ver(self,pos:int) -> bool:
formerboard = self._board[:]
self._board[pos] = -1
for teta in range(3):
self.robothelp()
if self.unsolvable():
self._board = formerboard
return True
self._board = formerboard
return False
def no_rec_solver(self):
if not self.unsolvable():
formerboard = self._board[:]
for y in range(self._row):
for x in range(self._col):
pos = y*self._col+x
if self._board[pos] == 0:
if self.black_ver(pos):
self._board[pos] = -1
return
elif self.white_ver(pos):
self._board[pos] = 1
return
def solve_recursive(self,i:int) -> bool:
while i < len(self._board) and self._board[i] != 0 :
i += 1
if i < len(self._board):
saved = self._board[:] # save current status
for color in (1,-1):
self._board[i] = color
if self.solve_recursive(i + 1):
return True
self._board = saved[:] # backtracking
return self.finished()
class BoardGameGui:
def __init__(self, g: BoardGame):
self._game = g
self._downtime = 0
self.update_buttons()
def tick(self):
if g2d.key_pressed("LeftButton"):
self._downtime = time()
elif g2d.key_released("LeftButton"):
mouse = g2d.mouse_position()
x, y = mouse[0] // W, mouse[1] // H
if time() - self._downtime > LONG_PRESS:
pass
else:
self._game.play_at(x, y)
pass
elif g2d.key_pressed("h"):
self._game.no_rec_solver()
elif g2d.key_pressed("u"):
if self._game.unsolvable():
g2d.alert("Unsolvable")
else:
g2d.alert("Solvable")
elif g2d.key_pressed("a"):
self._game.robothelp()
elif g2d.key_pressed("s"):
self._game.solve_recursive(0)
self.update_buttons()
def update_buttons(self):
g2d.clear_canvas()
g2d.set_color((0, 0, 0))
col, row = self._game.col(), self._game.row()
for y in range(1, row):
g2d.draw_line((0, y * H), (col * W, y * H))
for x in range(1, col):
g2d.draw_line((x * W, 0), (x * W, row * H))
for y in range(row):
for x in range(col):
value = self._game.value_at(x, y)
xr,yr = (x*W, y*H)
if self._game.value_at(x,y) == 0:
g2d.set_color((150,160,140))
g2d.fill_rect((xr-1,yr-1,W-1,H-1))
elif self._game.value_at(x,y) == 1:
g2d.set_color((250,250,250))
g2d.fill_rect((xr-1,yr-1,W-1,H-1))
else:
g2d.set_color((0,0,0))
g2d.fill_rect((xr-1,yr-1,W-1,H-1))
g2d.update_canvas()
if self._game.finished():
g2d.alert(self._game.message())
g2d.close_canvas()
conf = start()
game = Tinr(conf)
gui_play(game)