-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
630 lines (508 loc) · 20.1 KB
/
Copy pathrun.py
File metadata and controls
630 lines (508 loc) · 20.1 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
from bauhaus import Encoding, proposition, constraint, And
from bauhaus.utils import count_solutions, likelihood
from cards import *
import random
import subprocess
import sys
import json
# These two lines make sure a faster SAT solver is used.
from nnf import config
config.sat_backend = "kissat"
# Encoding that will store all of your constraints
E = Encoding()
class Hashable:
def __hash__(self):
return hash(str(self))
def __eq__(self, __value: object) -> bool:
return hash(self) == hash(__value)
def __repr__(self):
return str(self)
# Defines if a tile at (i, j) is occupied by anyone
@proposition(E)
class occupied(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Tile is occupied at ({self.i}, {self.j})"
# Defines if a tile at (i, j) is occupied by red, red(i, j)
@proposition(E)
class o_red(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Red tile at ({self.i}, {self.j})"
# Defines if a tile at (i, j) is occupied by green, green(i, j)
@proposition(E)
class o_green(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Green tile at ({self.i}, {self.j})"
# Defines if a tile at (i, j) is occupied by blue, blue(i, j)
@proposition(E)
class o_blue(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Blue tile at ({self.i}, {self.j})"
# Defines if someone has a card that can be played on this tile
@proposition(E)
class playable(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"At least one player can place a token at ({self.i}, {self.j})"
# Defines if red has a card that can be played on this tile
@proposition(E)
class playable_red(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Red can place a token at ({self.i}, {self.j})"
# Defines if green has a card that can be played on this tile
@proposition(E)
class playable_green(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Green can place a token at ({self.i}, {self.j})"
# Defines if blue has a card that can be played on this tile
@proposition(E)
class playable_blue(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Blue can place a token at ({self.i}, {self.j})"
occupied_list = []
not_occupied_list = []
red = []
green = []
blue = []
red_cards = []
green_cards = []
blue_cards = []
dealt_cards = []
blue_unified = []
red_unified = []
green_unified = []
def initialize_game_state():
for x in range(0, 3):
unique_pairs = set() # Use a set to ensure uniqueness
while len(unique_pairs) < 10:
i = random.randint(0, 9)
j = random.randint(0, 9)
new_ij = (i, j)
if new_ij not in unique_pairs:
unique_pairs.add(new_ij)
occupied_list.append(new_ij)
if x == 0:
# Red tiles
E.add_constraint(And(occupied(i, j), o_red(i, j)))
red.append(new_ij)
elif x == 1:
# Green tiles
E.add_constraint(And(occupied(i, j), o_green(i, j)))
green.append(new_ij)
else:
# Blue tiles
E.add_constraint(And(occupied(i, j), o_blue(i, j)))
blue.append(new_ij)
for x in range (0, 10):
for y in range(0, 10):
pair = (x, y)
if not occupied_list.__contains__(pair):
E.add_constraint(~occupied(x, y))
not_occupied_list.append(pair)
for x in range(0, 3):
unique_pairs = set()
while len(unique_pairs) < 7:
i = random.randint(0, 9)
j = random.randint(0, 9)
pair = (i, j)
if pair not in occupied_list and pair not in dealt_cards and pair not in unique_pairs:
dealt_cards.append(pair)
unique_pairs.add(pair)
if x == 0:
# Red cards
E.add_constraint(playable(i, j) & playable_red(i, j))
red_cards.append(pair)
elif x == 1:
# Green cards
E.add_constraint(playable(i, j) & playable_green(i, j))
green_cards.append(pair)
else:
# Blue cards
E.add_constraint(playable(i, j) & playable_blue(i, j))
blue_cards.append(pair)
blueE = Encoding()
@proposition(blueE)
class blue_horizontal(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Blue can win with tiles at ({self.i}, {self.j}), ({self.i + 1}, {self.j}), ({self.i + 2}, {self.j}), ({self.i + 3}, {self.j})."
@proposition(blueE)
class no_blue_horizontal(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Blue can't win with a horizontal sequence starting at ({self.i}, {self.j})."
@proposition(blueE)
class blue_vertical(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Blue can win with tiles at ({self.i}, {self.j}), ({self.i}, {self.j + 1}), ({self.i}, {self.j + 1}), ({self.i}, {self.j + 3})."
@proposition(blueE)
class no_blue_vertical(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"Blue can't win with a horizontal sequence starting at ({self.i}, {self.j})."
@proposition(blueE)
class blue_diagonal(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"blue can win with a diagonal sequence going up, starting at ({self.i}, {self.j}))."
@proposition(blueE)
class no_blue_diagonal_up(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"blue can't win with a diagonal sequence starting at ({self.i}, {self.j})."
@proposition(blueE)
class no_blue_diagonal_down(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"blue can't win with a diagonal sequence going down, starting at ({self.i}, {self.j})."
def check_blue_win():
for y in range(0, 10):
for x in range (0, 7):
p1 = (x, y)
p2 = (x + 1, y)
p3 = (x + 2, y)
p4 = (x + 3, y)
if p1 in blue_unified and p2 in blue_unified and p3 in blue_unified and p4 in blue_unified:
blueE.add_constraint(blue_horizontal(x, y))
flag = 1
print("Blue can win!")
else:
blueE.add_constraint(no_blue_horizontal(x, y))
for y in range(0, 7):
for x in range (0, 10):
p1 = (x, y)
p2 = (x, y + 1)
p3 = (x, y + 2)
p4 = (x, y + 3)
if p1 in blue_unified and p2 in blue_unified and p3 in blue_unified and p4 in blue_unified:
blueE.add_constraint(blue_vertical(x, y))
flag = 1
print("Blue can win!")
else:
blueE.add_constraint(no_blue_vertical(x, y))
for y in range(0, 7):
for x in range(0, 7):
p1 = (x, y)
p2 = (x + 1, y + 1)
p3 = (x + 2, y + 2)
p4 = (x + 3, y + 3)
if p1 in blue_unified and p2 in blue_unified and p3 in blue_unified and p4 in blue_unified:
blueE.add_constraint(blue_diagonal(x, y))
flag = 1
print("blue can win!")
else:
blueE.add_constraint(no_blue_diagonal_down(x, y))
for y in range(4, 10):
for x in range(0, 7):
p1 = (x, y)
p2 = (x + 1, y - 1)
p3 = (x + 2, y - 2)
p4 = (x + 3, y - 3)
if p1 in blue_unified and p2 in blue_unified and p3 in blue_unified and p4 in blue_unified:
blueE.add_constraint(blue_diagonal(x, y))
flag = 1
print("blue can win!")
else:
blueE.add_constraint(no_blue_diagonal_up(x, y))
redE = Encoding()
@proposition(redE)
class red_horizontal(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"red can win with tiles at ({self.i}, {self.j}), ({self.i + 1}, {self.j}), ({self.i + 2}, {self.j}), ({self.i + 3}, {self.j})."
@proposition(redE)
class no_red_horizontal(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"red can't win with a horizontal sequence starting at ({self.i}, {self.j})."
@proposition(redE)
class red_vertical(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"red can win with tiles at ({self.i}, {self.j}), ({self.i}, {self.j + 1}), ({self.i}, {self.j + 1}), ({self.i}, {self.j + 3})."
@proposition(redE)
class no_red_vertical(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"red can't win with a horizontal sequence starting at ({self.i}, {self.j})."
@proposition(redE)
class red_diagonal(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"red can win with a diagonal sequence going up, starting at ({self.i}, {self.j}))."
@proposition(redE)
class no_red_diagonal_up(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"red can't win with a diagonal sequence starting at ({self.i}, {self.j})."
@proposition(redE)
class no_red_diagonal_down(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"red can't win with a diagonal sequence going down, starting at ({self.i}, {self.j})."
def check_red_win():
for y in range(0, 10):
for x in range (0, 7):
p1 = (x, y)
p2 = (x + 1, y)
p3 = (x + 2, y)
p4 = (x + 3, y)
if p1 in red_unified and p2 in red_unified and p3 in red_unified and p4 in red_unified:
redE.add_constraint(red_horizontal(x, y))
flag = 1
print("red can win!")
else:
redE.add_constraint(no_red_horizontal(x, y))
for y in range(0, 7):
for x in range (0, 10):
p1 = (x, y)
p2 = (x, y + 1)
p3 = (x, y + 2)
p4 = (x, y + 3)
if p1 in red_unified and p2 in red_unified and p3 in red_unified and p4 in red_unified:
redE.add_constraint(red_vertical(x, y))
flag = 1
print("red can win!")
else:
redE.add_constraint(no_red_vertical(x, y))
for y in range(0, 7):
for x in range(0, 7):
p1 = (x, y)
p2 = (x + 1, y + 1)
p3 = (x + 2, y + 2)
p4 = (x + 3, y + 3)
if p1 in red_unified and p2 in red_unified and p3 in red_unified and p4 in red_unified:
redE.add_constraint(red_diagonal(x, y))
flag = 1
print("red can win!")
else:
redE.add_constraint(no_red_diagonal_down(x, y))
for y in range(4, 10):
for x in range(0, 7):
p1 = (x, y)
p2 = (x + 1, y - 1)
p3 = (x + 2, y - 2)
p4 = (x + 3, y - 3)
if p1 in red_unified and p2 in red_unified and p3 in red_unified and p4 in red_unified:
redE.add_constraint(red_diagonal(x, y))
flag = 1
print("red can win!")
else:
redE.add_constraint(no_red_diagonal_up(x, y))
greenE = Encoding()
@proposition(greenE)
class green_horizontal(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"green can win with tiles at ({self.i}, {self.j}), ({self.i + 1}, {self.j}), ({self.i + 2}, {self.j}), ({self.i + 3}, {self.j})."
@proposition(greenE)
class no_green_horizontal(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"green can't win with a horizontal sequence starting at ({self.i}, {self.j})."
@proposition(greenE)
class green_vertical(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"green can win with tiles at ({self.i}, {self.j}), ({self.i}, {self.j + 1}), ({self.i}, {self.j + 1}), ({self.i}, {self.j + 3})."
@proposition(greenE)
class no_green_vertical(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"green can't win with a horizontal sequence starting at ({self.i}, {self.j})."
@proposition(greenE)
class green_diagonal(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"green can win with a diagonal sequence going up, starting at ({self.i}, {self.j}))."
@proposition(greenE)
class no_green_diagonal_up(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"green can't win with a diagonal sequence starting at ({self.i}, {self.j})."
@proposition(greenE)
class no_green_diagonal_down(Hashable):
def __init__(self, i, j) -> None:
self.i = i
self.j = j
def __str__(self) -> str:
return f"green can't win with a diagonal sequence going down, starting at ({self.i}, {self.j})."
def check_green_win():
for y in range(0, 10):
for x in range (0, 7):
p1 = (x, y)
p2 = (x + 1, y)
p3 = (x + 2, y)
p4 = (x + 3, y)
if p1 in green_unified and p2 in green_unified and p3 in green_unified and p4 in green_unified:
greenE.add_constraint(green_horizontal(x, y))
flag = 1
print("green can win!")
else:
greenE.add_constraint(no_green_horizontal(x, y))
for y in range(0, 7):
for x in range(0, 7):
p1 = (x, y)
p2 = (x + 1, y + 1)
p3 = (x + 2, y + 2)
p4 = (x + 3, y + 3)
if p1 in green_unified and p2 in green_unified and p3 in green_unified and p4 in green_unified:
greenE.add_constraint(green_diagonal(x, y))
flag = 1
print("green can win!")
else:
greenE.add_constraint(no_green_diagonal_down(x, y))
for y in range(4, 10):
for x in range(0, 7):
p1 = (x, y)
p2 = (x + 1, y - 1)
p3 = (x + 2, y - 2)
p4 = (x + 3, y - 3)
if p1 in green_unified and p2 in green_unified and p3 in green_unified and p4 in green_unified:
greenE.add_constraint(green_diagonal(x, y))
flag = 1
print("green can win!")
else:
greenE.add_constraint(no_green_diagonal_up(x, y))
for y in range(0, 7):
for x in range (0, 10):
p1 = (x, y)
p2 = (x, y + 1)
p3 = (x, y + 2)
p4 = (x, y + 3)
if p1 in green_unified and p2 in green_unified and p3 in green_unified and p4 in green_unified:
greenE.add_constraint(green_vertical(x, y))
flag = 1
print("green can win!")
else:
greenE.add_constraint(no_green_vertical(x, y))
# Build an example full theory for your setting and return it.
#
# There should be at least 10 variables, and a sufficiently large formula to describe it (>50 operators).
# This restriction is fairly minimal, and if there is any concern, reach out to the teaching staff to clarify
# what the expectations are.
def example_theory():
# At most 2 sequences, the number of s_vertical + s_horizontal + s_diagonal_up + s_diagonal_down
# The size of the board is 10x10, each tile has coordinates (i,j). Top left tile is (0,0) and bottom right is (9,9)
# Sequences of the same kind can't overlap with each other, they will only be one sequence (any proposition starting with s_ is a sequence)
# Sequences and can sequence only consider blue
initialize_game_state()
print()
return E
def blue_win():
check_blue_win()
return blueE
def red_win():
check_red_win()
return redE
def green_win():
check_green_win()
return greenE
if __name__ == "__main__":
T = example_theory()
# Don't compile until you're finished adding all your constraints!
T = T.compile()
# After compilation (and only after), you can check some of the properties
# of your model:
print("\nSatisfiable: %s" % T.satisfiable())
print("# Solutions: %d" % count_solutions(T))
print(" Solution: %s" % T.solve())
#These loops create a shared list of where each team has tiles and where they can also place tiles
for x in range (0, 10):
blue_unified.append(blue[x])
red_unified.append(red[x])
green_unified.append(green[x])
for x in range (0, 7):
blue_unified.append(blue_cards[x])
red_unified.append(red_cards[x])
green_unified.append(green_cards[x])
B = blue_win()
# Don't compile until you're finished adding all your constraints!
B = B.compile()
# After compilation (and only after), you can check some of the properties
# of your model:
print("\nSatisfiable: %s" % B.satisfiable())
print("# Solutions: %d" % count_solutions(B))
print(" Solution: %s" % B.solve())
R = red_win()
# Don't compile until you're finished adding all your constraints!
R = R.compile()
print("\nSatisfiable: %s" % R.satisfiable())
print("# Solutions: %d" % count_solutions(R))
print(" Solution: %s" % R.solve())
G = green_win()
# Don't compile until you're finished adding all your constraints!
G = G.compile()
print("\nSatisfiable: %s" % G.satisfiable())
print("# Solutions: %d" % count_solutions(G))
print(" Solution: %s" % G.solve())
red_json = json.dumps(red)
blue_json = json.dumps(blue)
green_json = json.dumps(green)
red_cards_json = json.dumps(red_cards)
blue_cards_json = json.dumps(blue_cards)
green_cards_json = json.dumps(green_cards)
subprocess.run([sys.executable, 'app.py', '--red', red_json, '--blue', blue_json, '--green', green_json, '--red_cards', red_cards_json, '--blue_cards', blue_cards_json, '--green_cards', green_cards_json])