-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGnCalculate.py
187 lines (170 loc) · 7.23 KB
/
GnCalculate.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
class GnCalculate(object):
def __init__(self, siz = 19):
self.siz = siz
self.ston = [[0 for x in range(26)] for y in range(26)]
self.tmpbord = [[0 for x in range(26)] for y in range(26)]
self.conflict = 0
self.confmove = 0
def findStoneBlock(self, colour, tmpcolour, pos, tmpsiz):
movStack = []
movStack.append(pos)
breath = 0
stonnum = 1
while len(movStack):
tmpmov = movStack.pop()
tmpx = tmpmov / 100
tmpy = tmpmov % 100
if tmpx > 0 and self.tmpbord[tmpx - 1][tmpy] != tmpcolour:
if self.ston[tmpx - 1][tmpy] == colour:
movStack.append(tmpmov - 100)
self.tmpbord[tmpx - 1][tmpy] = tmpcolour
stonnum += 1
elif self.ston[tmpx - 1][tmpy] == 0:
breath += 1
if tmpx < tmpsiz and self.tmpbord[tmpx + 1][tmpy] != tmpcolour:
if self.ston[tmpx + 1][tmpy] == colour:
movStack.append(tmpmov + 100)
self.tmpbord[tmpx + 1][tmpy] = tmpcolour
stonnum += 1
elif self.ston[tmpx + 1][tmpy] == 0:
breath += 1
if tmpx > 0 and self.tmpbord[tmpx][tmpy - 1] != tmpcolour:
if self.ston[tmpx][tmpy - 1] == colour:
movStack.append(tmpmov - 1)
self.tmpbord[tmpx][tmpy - 1] = tmpcolour
stonnum += 1
elif self.ston[tmpx][tmpy - 1] == 0:
breath += 1
if tmpx < tmpsiz and self.tmpbord[tmpx][tmpy + 1] != tmpcolour:
if self.ston[tmpx][tmpy + 1] == colour:
movStack.append(tmpmov + 1)
self.tmpbord[tmpx][tmpy + 1] = tmpcolour
stonnum += 1
elif self.ston[tmpx][tmpy + 1] == 0:
breath += 1
return (breath, stonnum) # finished findStoneBlock
def configDropStone(self, colour, mov):
tmpx = mov / 100
tmpy = mov % 100
if mov < 0:
return 0
if self.ston[tmpx][tmpy] > 0:
return 0
if self.conflict == 1 and mov == self.confmove:
return 0
tmpsiz = self.siz - 1
if colour == 1:
opcolour = 2
else:
opcolour = 1
self.tmpbord = [[0 for x in range(26)] for y in range(26)]
self.tmpbord[tmpx][tmpy] = 3
self.ston[tmpx][tmpy] = colour
lift = 0
if tmpx > 0 and self.ston[tmpx - 1][tmpy] == opcolour: # up
self.tmpbord[tmpx - 1][tmpy] = 4
tmpfind = self.findStoneBlock(opcolour, 4, mov - 100, tmpsiz)
if tmpfind[0] == 0:
lift += tmpfind[1]
for tmpi in range(self.siz):
for tmpj in range(self.siz):
if self.tmpbord[tmpi][tmpj] == 4:
self.tmpbord[tmpi][tmpj] = 5
else:
for tmpi in range(self.siz):
for tmpj in range(self.siz):
if self.tmpbord[tmpi][tmpj] == 4:
self.tmpbord[tmpi][tmpj] = 6
if tmpx < tmpsiz and self.ston[tmpx - 1][tmpy] == opcolour and self.tmpbord[tmpx + 1][tmpy] != 5 \
and self.tmpbord[tmpx + 1][tmpy] != 6: # down
self.tmpbord[tmpx + 1][tmpy] = 4
tmpfind = self.findStoneBlock(opcolour, 4, mov + 100, tmpsiz)
if tmpfind[0] == 0:
lift += tmpfind[1]
for tmpi in range(self.siz):
for tmpj in range(self.siz):
if self.tmpbord[tmpi][tmpj] == 4:
self.tmpbord[tmpi][tmpj] = 5
else:
for tmpi in range(self.siz):
for tmpj in range(self.siz):
if self.tmpbord[tmpi][tmpj] == 4:
self.tmpbord[tmpi][tmpj] = 6
if tmpy > 0 and self.ston[tmpx][tmpy - 1] == opcolour and self.tmpbord[tmpx][tmpy - 1] != 5 \
and self.tmpbord[tmpx][tmpy - 1] != 6: # left
self.tmpbord[tmpx][tmpy - 1] = 4
tmpfind = self.findStoneBlock(opcolour, 4, mov - 1, tmpsiz)
if tmpfind[0] == 0:
lift += tmpfind[1]
for tmpi in range(self.siz):
for tmpj in range(self.siz):
if self.tmpbord[tmpi][tmpj] == 4:
self.tmpbord[tmpi][tmpj] = 5
else:
for tmpi in range(self.siz):
for tmpj in range(self.siz):
if self.tmpbord[tmpi][tmpj] == 4:
self.tmpbord[tmpi][tmpj] = 6
if tmpy < tmpsiz and self.ston[tmpx][tmpy + 1] == opcolour and self.tmpbord[tmpx][tmpy + 1] != 5 \
and self.tmpbord[tmpx][tmpy + 1] != 6: # right
self.tmpbord[tmpx][tmpy + 1] = 4
tmpfind = self.findStoneBlock(opcolour, 4, mov + 1, tmpsiz)
if tmpfind[0] == 0:
lift += tmpfind[1]
for tmpi in range(self.siz):
for tmpj in range(self.siz):
if self.tmpbord[tmpi][tmpj] == 4:
self.tmpbord[tmpi][tmpj] = 5
else:
for tmpi in range(self.siz):
for tmpj in range(self.siz):
if self.tmpbord[tmpi][tmpj] == 4:
self.tmpbord[tmpi][tmpj] = 6
tmpfind = self.findStoneBlock(colour, 3, mov, tmpsiz)
if tmpfind[0] == 0 and lift == 0:
self.ston[tmpx][tmpy] = 0
return 0
if lift == 1 and tmpfind[1] == 1:
if tmpx > 0 and self.tmpbord[tmpx - 1][tmpy] == 5:
self.ston[tmpx - 1][tmpy] = 0
self.confmove = (tmpx - 1) * 100 + tmpy
if tmpx < tmpsiz and self.tmpbord[tmpx + 1][tmpy] == 5:
self.ston[tmpx + 1][tmpy] = 0
self.confmove = (tmpx + 1) * 100 + tmpy
if tmpy > 0 and self.tmpbord[tmpx][tmpy - 1] == 5:
self.ston[tmpx][tmpy - 1] = 0
self.confmove = tmpx * 100 + tmpy - 1
if tmpy < tmpsiz and self.tmpbord[tmpx][tmpy + 1] == 5:
self.ston[tmpx][tmpy + 1] = 0
self.confmove = tmpx * 100 + tmpy + 1
self.conflict = 1
return 1
if lift > 0:
self.conflict = 0
for tmpi in range(0, self.siz):
for tmpj in range(0, self.siz):
if self.tmpbord[tmpi][tmpj] == 5:
self.ston[tmpi][tmpj] = 0
return 2
self.conflict = 0
return 3
# finished configDropStone
def printbord(self):
for x in range(self.siz):
for y in range(self.siz):
print self.ston[x][y],
print
if __name__ == "__main__":
colour = 1
App = GnCalculate(19)
App.printbord()
while True:
if colour == 1:
move = input("Black: ")
else:
move = input("White: ")
App.printbord()
if colour == 1:
colour = 2
else:
colour = 1