-
Notifications
You must be signed in to change notification settings - Fork 2
/
graph_tools_games.py
174 lines (159 loc) · 6.28 KB
/
graph_tools_games.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
from graph_tools_game import Graph_game
from graph_board_game import Board_game
from util import findfivers, findsquares, remove_useless_wsn
from graph_tool.all import *
import json
from collections import defaultdict
import os,sys
base_path = os.path.abspath(os.path.dirname(__file__))
class Json_game(Graph_game):
def __init__(self,json_path):
super().__init__()
with open(json_path,"r") as f:
self.config = json.load(f)
self.board = Json_board(self.config)
self.board.game = self
self.graph_from_board()
self.name = self.config["name"]
class Json_board(Board_game):
def __init__(self,config):
super().__init__()
self.squares = config["squares"]
self.position = ["f" for _ in range(self.squares)]
self.winsquarenums = set(frozenset(x) for x in config["winsquarenums"])
remove_useless_wsn(self.winsquarenums)
with open(os.path.join(base_path,f"rulesets/{config['name']}.json"),"r") as f:
self.rulesets = json.load(f)
class Qango6x6(Graph_game):
def __init__(self):
super().__init__()
self.board = Qango6x6_board()
self.board.game = self
self.graph_from_board()
self.name = "qango6x6"
class Qango6x6_board(Board_game):
def __init__(self):
super().__init__()
self.squares = 36
self.position = ["f" for _ in range(self.squares)]
self.winsquarenums = {
frozenset({0,1,6}),frozenset({4,5,11}),frozenset({24,30,31}),frozenset({29,34,35}),
frozenset({2,7,12}),frozenset({3,10,17}),frozenset({18,25,32}),frozenset({23,28,33}),
frozenset({8,13,14}),frozenset({9,15,16}),frozenset({19,20,26}),frozenset({21,22,27})
}
self.winsquarenums.update(findsquares(self.squares))
self.winsquarenums.update(findfivers(self.squares))
remove_useless_wsn(self.winsquarenums)
with open(os.path.join(base_path,"rulesets/qango6x6.json"),"r") as f:
self.rulesets = json.load(f)
class Tic_tac_toe(Graph_game):
def __init__(self):
super().__init__()
self.board = Tic_tac_toe_board()
self.board.game = self
self.graph_from_board()
self.name = "tic_tac_toe"
class Tic_tac_toe_board(Board_game):
def __init__(self):
super().__init__()
self.squares = 9
self.position = ["f" for _ in range(self.squares)]
self.winsquarenums = {frozenset({0,1,2}),frozenset({3,4,5}),frozenset({6,7,8}),
frozenset({0,3,6}),frozenset({1,4,7}),frozenset({2,5,8}),
frozenset({0,4,8}),frozenset({2,4,6})}
with open(os.path.join(base_path,"rulesets/tic_tac_toe.json"),"r") as f:
self.rulesets = json.load(f)
class Qango7x7(Graph_game):
def __init__(self):
super().__init__()
self.board = Qango7x7_board()
self.board.game = self
self.graph_from_board()
self.name = "qango7x7"
class Qango7x7_board(Board_game):
def __init__(self):
super().__init__()
self.squares = 49
self.position = ["f" for _ in range(self.squares)]
self.winsquarenums = {
frozenset({0,1,7}),frozenset({5,6,13}),frozenset({35,42,43}),
frozenset({41,47,48}),frozenset({2,8,14}),frozenset({4,12,20}),
frozenset({28,36,44}),frozenset({34,40,46}),frozenset({3,9,10}),
frozenset({26,27,33}),frozenset({29,30,37}),frozenset({11,18,19}),
frozenset({15,21,22}),frozenset({38,39,45}),frozenset({16,17,23}),
frozenset({25,31,32})
}
self.winsquarenums.update(findsquares(self.squares))
self.winsquarenums.update(findfivers(self.squares))
remove_useless_wsn(self.winsquarenums)
with open(os.path.join(base_path,"rulesets/qango7x7.json"),"r") as f:
self.rulesets = json.load(f)
class Qango7x7_plus(Graph_game):
def __init__(self):
super().__init__()
self.board = Qango7x7_plus_board()
self.board.game = self
self.graph_from_board()
self.name = "qango7x7_plus"
class Qango7x7_plus_board(Board_game):
def __init__(self):
super().__init__()
self.squares = 37
self.position = ["f" for _ in range(self.squares)]
self.winsquarenums = {
frozenset({2,8,14}),frozenset({4,12,20}),
frozenset({28,36,44}),frozenset({34,40,46}),frozenset({3,9,10}),
frozenset({26,27,33}),frozenset({29,30,37}),frozenset({11,18,19}),
frozenset({15,21,22}),frozenset({38,39,45}),frozenset({16,17,23}),
frozenset({25,31,32})
}
self.winsquarenums.update(findsquares(49))
self.winsquarenums.update(findfivers(49))
remove_useless_wsn(self.winsquarenums)
self.change_wsn()
with open(os.path.join(base_path,"rulesets/qango7x7_plus.json"),"r") as f:
self.rulesets = json.load(f)
def change_wsn(self):
removals = [0,1,5,6,7,13,35,42,43,41,47,48]
new_wsn = set()
for wsn in self.winsquarenums:
new = set()
for ws in wsn:
if ws in removals:
break
for i,r in enumerate(removals):
if r>ws:
break
unders = i
new.add(ws-unders)
else:
new_wsn.add(frozenset(new))
self.winsquarenums = new_wsn
def draw_me(self,pos=None):
row_starts = [2,1,0,0,0,1,2]
row_ends = [5,6,7,7,7,6,5]
out = "#"*(7+2)+"\n"
count = 0
pos = self.position if pos is None else pos
for rs,re in zip(row_starts,row_ends):
out+=" "*rs+"#"
for _ in range(rs,re):
out+=pos[count]
count+=1
out+="#"+" "*re+"\n"
out += "#"*(7+2)
print(out)
return out
def instanz_by_name(game_name) -> Graph_game:
if "qango6x6_static"==game_name:
game = Qango6x6()
elif "qango7x7_static"==game_name:
game = Qango7x7()
elif game_name == "qango7x7_plus_static":
game = Qango7x7_plus()
else:
game = Json_game(os.path.join(base_path,"json_games",game_name+".json"))
return game
if __name__ == "__main__":
q = Qango7x7_board()
print(q.winsquarenums)