-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgo.py
388 lines (350 loc) · 13.9 KB
/
go.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
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
import pygame
import pygame.gfxdraw
import sys
from lib import color
# 棋盘大小
size = 760
# 棋盘留白
margin = 40
# 棋子半径
csize = 16
# 棋盘线间隔
interval = 38
class Go():
def __init__(self):
sys.setrecursionlimit(361 * 361)
pygame.init()
# 棋子列表
self.__chesses = {}
# 最后落子
self.__later = -1
# 当前步数
self.__steps = 0
# 打劫
self.__ko = ""
self.__font = pygame.font.Font(
"./static/font/SourceHanSansCN-Regular.otf", 14
)
self.__move = pygame.mixer.Sound("./static/voice/move.wav")
self.__take = pygame.mixer.Sound("./static/voice/take.wav")
self.__take_multi = pygame.mixer.Sound("./static/voice/take_multi.wav")
def run(self):
screen = pygame.display.set_mode((size + 300, size))
pygame.display.set_caption("Go")
background = pygame.image.load("./static/img/bg.jpg")
screen_right = pygame.Surface((300, size))
screen_right.fill(color.bg)
while True:
screen.blit(background, (0, 0))
screen.blit(screen_right, (size, 0))
self.chessboard(screen)
self.panel(screen)
self.getChesses(screen)
for e in pygame.event.get():
if e.type == pygame.QUIT:
sys.exit()
elif e.type == pygame.MOUSEMOTION:
self.chessMove(screen)
elif e.type == pygame.MOUSEBUTTONDOWN:
self.mouse(screen)
self.chessMove(screen)
pygame.display.flip()
def chessboard(self, screen):
width = interval * 19
for i in range(19):
x = i * interval + margin
# 网格线
pygame.draw.line(screen, color.black,
(x, margin), (x, width + 1), 1)
pygame.draw.line(screen, color.black,
(margin, x), (width + 1, x), 1)
# 棋盘坐标
text = self.__font.render(str(i + 1), True, color.black)
textRect = text.get_rect()
textRect.center = (margin / 2, i * interval + margin)
screen.blit(text, textRect)
text = self.__font.render(chr(i + 65), True, color.black)
textRect = text.get_rect()
textRect.center = (i * interval + margin, margin / 2)
screen.blit(text, textRect)
# 添加星位点
for x in [4, 10, 16]:
piont_x = x * interval
for y in [4, 10, 16]:
# 圆滑星位点
pygame.gfxdraw.aacircle(
screen, piont_x + 2, y * interval + 2, 4, color.black
)
pygame.gfxdraw.aacircle(
screen, piont_x + 2, y * interval + 2, 3, color.black
)
pygame.draw.circle(
screen, color.black, (piont_x + 2, y * interval + 2), 4
)
def panel(self, screen):
self.btnNew(screen)
self.viewSteps(screen)
self.btnAction(screen)
def btnNew(self, screen):
new_surface = pygame.Surface((260, 50))
new_surface.fill(color.btn_bg)
self.__btn_new = pygame.Rect(size + 20, 20, 260, 50)
screen.blit(new_surface, self.__btn_new)
pygame.draw.rect(screen, color.white, (size + 20, 20, 260, 50), 1)
font = pygame.font.Font(
"./static/font/SourceHanSansCN-Regular.otf", 18
)
text = font.render("重 新 开 始", True, color.white)
textRect = text.get_rect()
textRect.center = (size + 150, 45)
screen.blit(text, textRect)
def viewSteps(self, screen):
pygame.draw.rect(screen, color.btn_bg, (size + 20, 90, 150, 70))
font = pygame.font.Font(
"./static/font/SourceHanSansCN-Regular.otf", 26
)
text = font.render(str(self.__steps), True, color.white)
textRect = text.get_rect()
textRect.center = (size + 95, 125)
screen.blit(text, textRect)
def btnAction(self, screen):
back_surface = pygame.Surface((90, 32))
back_surface.fill(color.btn_bg)
self.__btn_back = pygame.Rect(size + 190, 90, 90, 32)
screen.blit(back_surface, self.__btn_back)
text = self.__font.render("悔 棋", True, color.white)
textRect = text.get_rect()
textRect.center = (size + 235, 105)
screen.blit(text, textRect)
pygame.draw.rect(screen, color.white, (size + 190, 90, 90, 32), 1)
pass_surface = pygame.Surface((90, 32))
pass_surface.fill(color.btn_bg)
self.__btn_pass = pygame.Rect(size + 190, 127, 90, 32)
screen.blit(pass_surface, self.__btn_pass)
text = self.__font.render("停 一 手", True, color.white)
textRect = text.get_rect()
textRect.center = (size + 235, 142)
screen.blit(text, textRect)
pygame.draw.rect(screen, color.white, (size + 190, 127, 90, 32), 1)
def mouse(self, screen):
p = pygame.mouse.get_pos()
new = pygame.Rect.collidepoint(self.__btn_new, p)
back = pygame.Rect.collidepoint(self.__btn_back, p)
btn_pass = pygame.Rect.collidepoint(self.__btn_pass, p)
if new:
self.__chesses = {}
self.__later = -1
self.__steps = 0
elif back:
if self.__steps > 0:
self.__later *= -1
self.__steps -= 1
chess_len = len(self.__chesses)
if chess_len == 0:
return
key = list(self.__chesses.keys())[-1]
del(self.__chesses[key])
elif btn_pass:
self.__later *= -1
self.__steps += 1
key = "pass," + str(self.__steps)
self.__chesses[key] = [(0, 0), self.__later, self.__steps]
else:
self.chessMove(screen, True)
def getPosition(self):
p = pygame.mouse.get_pos()
x = p[0] - margin
y = p[1] - margin
x = round(x / interval) * interval + margin
y = round(y / interval) * interval + margin
return (x, y)
def getPos(self):
p = self.getPosition()
return (
int((p[0] - margin) / interval + 1),
int((p[1] - margin) / interval + 1)
)
def getKey(self, p):
return self.makeKey((
int((p[0] - margin) / interval + 1),
int((p[1] - margin) / interval + 1)
))
def makeKey(self, p):
return ",".join((
str(p[0]),
str(p[1])
))
def chessMove(self, screen, down=False):
p = self.getPosition()
bottom = size - margin + 6
bg = color.white if self.__later == -1 else color.black
chess_color = color.black if self.__later == -1 else color.white
if p[0] >= margin and p[1] >= margin and p[0] <= bottom and p[1] <= bottom:
if down:
return self.chessDown(p)
key = self.getKey(p)
if key in self.__chesses:
return
screen_chess = pygame.Surface((100, 100))
screen_chess.set_colorkey(bg)
screen_chess.set_alpha(128)
pygame.gfxdraw.aacircle(screen, p[0], p[1], csize, chess_color)
# aacircle 在右侧有个微小的缺口,画一个像素弥补这个缺口
pygame.gfxdraw.pixel(screen, p[0] + csize, p[1], chess_color)
pygame.draw.circle(screen_chess, chess_color, (50, 50), csize)
# 填补因为锯齿带来的空隙
pygame.draw.circle(screen_chess, chess_color, (51, 51), csize)
screen.blit(screen_chess, (p[0] - 50, p[1] - 50))
def chessDown(self, p):
key = self.getKey(p)
if key in self.__chesses:
return
# 打劫判断
if key == self.__ko:
return
else:
self.__ko = ""
self.__later *= -1
self.__steps += 1
self.__chesses[key] = [p, self.__later, self.__steps]
liberties = self.getLiberties(key, self.__later)
remove = self.removeFromBoard(self.__later, {})
if liberties == 0 and not remove > 0:
self.__later *= -1
self.__steps -= 1
del(self.__chesses[key])
return
self.__move.play()
if remove > 0:
if remove < 4:
self.__take.play()
else:
self.__take_multi.play()
# 获取已落子棋子并渲染
def getChesses(self, screen):
for item in self.__chesses.values():
if item[0][0] + item[0][1] == 0:
continue
chess_color = color.black if item[1] == 1 else color.chess_white
pygame.gfxdraw.aacircle(
screen, item[0][0], item[0][1], csize, chess_color)
# 填补因为锯齿带来的空隙
pygame.gfxdraw.aacircle(
screen, item[0][0], item[0][1], csize - 1, chess_color
)
# aacircle 在右侧有个微小的缺口,画一个像素弥补这个缺口
pygame.gfxdraw.pixel(
screen, item[0][0] + csize, item[0][1], chess_color)
pygame.draw.circle(screen, chess_color, item[0], csize)
highlight = (color.gray, color.black) if item[1] == 1 else (
color.white, color.chess_white
)
self.makeHighlight(
screen,
highlight[0],
highlight[1],
pygame.Rect(item[0][0] - 11, item[0][1] - 11, 5, 3)
)
fcolor = (255, 0, 0) if item[2] == self.__steps else (
0, 0, 0) if item[2] % 2 == 0 else color.white
text = self.__font.render(str(item[2]), True, fcolor)
textRect = text.get_rect()
textRect.center = (item[0][0], item[0][1] - 1)
screen.blit(text, textRect)
def makeHighlight(self, screen, left_color, right_color, target_rect):
color_rect = pygame.Surface((2, 2))
pygame.draw.line(color_rect, left_color, (0, 0), (0, 1))
pygame.draw.line(color_rect, right_color, (1, 0), (1, 1))
color_rect = pygame.transform.rotozoom(color_rect, 330, 2)
screen.blit(color_rect, target_rect)
def getLiberties(self, key, chess):
result = self.setLiberties(self.getPos(), chess, {}, {})
liberties = len(result[1])
if liberties == 1 and key in result[1]:
return 0
return liberties
"""
数气
Args:
key: 落子坐标
chess: 落子(黑子/白子)
chess_checked: 落子已计算气数
territory_checked: 空已计算气数
"""
def setLiberties(self, p, chess, chess_checked={}, territory_checked={}):
key = self.makeKey(p)
chess_checked[key] = 1
for mx in [p[0] - 1, p[0] + 1]:
if mx < 1 or mx > 19:
continue
k = self.makeKey((mx, p[1]))
# 该坐标无子且未算气
if k not in self.__chesses and k not in territory_checked:
territory_checked[k] = 1
elif k in self.__chesses:
# 坐标有同色子且此子未算气
if self.__chesses[k][1] == chess and k not in chess_checked:
result = self.setLiberties(
(mx, p[1]), chess, chess_checked, territory_checked
)
chess_checked = {**chess_checked, **result[0]}
territory_checked = {**territory_checked, **result[1]}
for my in [p[1] - 1, p[1] + 1]:
if my < 1 or my > 19:
continue
k = self.makeKey((p[0], my))
# 该坐标无子且未算气
if k not in self.__chesses and k not in territory_checked:
territory_checked[k] = 1
elif k in self.__chesses:
# 坐标有同色子且此子未算气
if self.__chesses[k][1] == chess and k not in chess_checked:
result = self.setLiberties(
(p[0], my), chess, chess_checked, territory_checked
)
chess_checked = {**chess_checked, **result[0]}
territory_checked = {**territory_checked, **result[1]}
return (chess_checked, territory_checked)
def removeFromBoard(self, chess, chess_checked={}):
p = self.getPos()
waitRemove = {}
key = self.makeKey(p)
chess_checked[key] = 1
for mx in [p[0] - 1, p[0] + 1]:
if mx < 1 or mx > 19:
continue
k = self.makeKey((mx, p[1]))
if k in self.__chesses and k not in chess_checked:
if self.__chesses[k][1] != chess:
result = self.getDeadStone(
(mx, p[1]), self.__chesses[k][1]
)
if self.isRemove(p, result):
waitRemove = {**waitRemove, **result[0]}
for my in [p[1] - 1, p[1] + 1]:
if my < 1 or my > 19:
continue
k = self.makeKey((p[0], my))
if k in self.__chesses and k not in chess_checked:
if self.__chesses[k][1] != chess:
result = self.getDeadStone(
(p[0], my), self.__chesses[k][1]
)
if self.isRemove(p, result):
waitRemove = {**waitRemove, **result[0]}
for k in waitRemove:
del(self.__chesses[k])
dead = len(waitRemove)
if dead == 1:
self.__ko = list(waitRemove.keys())[0]
return dead
def getDeadStone(self, p, chess):
result = self.setLiberties(p, chess, {}, {})
return result
def isRemove(self, p, result):
if len(result[0]) > 0:
if len(result[1]) == 0:
return True
return False
go = Go()
go.run()