forked from Swkings/GObangV1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.py
320 lines (284 loc) · 8.31 KB
/
Board.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
import pygame
import random
pygame.init()
screen = pygame.display.set_mode((800, 800), 0, 32)
pygame.display.set_caption("五子棋")
font = pygame.font.Font("C:\Windows\Fonts\SimHei.ttf", 40)
w_success = font.render("白棋赢了!", True, (255, 255, 255))
b_success = font.render("黑棋赢了!", True, (0, 0, 0))
USER = 1
ROBOT = 2
PLAY = True # True:人下白棋 False:机下黑棋
# 棋子状态: 0:空置 1:white 2:black
list_board_status = [[] for i in range(15)]
for i in range(0, 15):
for j in range(0, 15):
list_board_status[i].append(0)
# 棋子坐标
list_board_pos = [[] for i in range(15)]
for i in range(0, 15):
posY = i * 40 + 120
for j in range(0, 15):
posX = j * 40 + 120
list_board_pos[i].append((posX, posY))
# print(list_board_pos[i][j])
def draw_board():
bgColor = (213, 176, 146)
screen.fill(bgColor)
lineColor = (0, 0, 0)
borderWidth = 4
lineWidth = 2
# startP = 120
# endP = 680
# 画边框
pygame.draw.line(screen, lineColor, (120, 120), (680, 120), borderWidth) # 上
pygame.draw.line(screen, lineColor, (120, 680), (680, 680), borderWidth) # 下
pygame.draw.line(screen, lineColor, (120, 120), (120, 680), borderWidth) # 左
pygame.draw.line(screen, lineColor, (680, 120), (680, 680), borderWidth) # 右
# 画棋盘
for i in range(1, 14):
y = 120 + i * 40
x = 120 + i * 40
startP_row = (120, y)
endP_row = (680, y)
startP_col = (x, 120)
endP_col = (x, 680)
pygame.draw.line(screen, lineColor, startP_row, endP_row, lineWidth) # 横线
pygame.draw.line(screen, lineColor, startP_col, endP_col, lineWidth) # 竖线
# 画标 5 个记点 (400, 400) (240, 240) (240, 560) (560, 240) (560, 560)
pygame.draw.circle(screen, lineColor, (400, 400), 4, 0)
pygame.draw.circle(screen, lineColor, (240, 240), 4, 0)
pygame.draw.circle(screen, lineColor, (240, 560), 4, 0)
pygame.draw.circle(screen, lineColor, (560, 240), 4, 0)
pygame.draw.circle(screen, lineColor, (560, 560), 4, 0)
class DrawChess:
def __init__(self, pos=None, chess=None):
self.pos = pos
self.chess = chess
self.white = (255, 255, 255)
self.black = (0, 0, 0)
# 棋子状态: 0:空置 1:white 2:black
if self.chess == 1:
self.draw_white()
elif self.chess == 2:
self.draw_black()
else:
pass
def draw_white(self):
pygame.draw.circle(screen, self.white, self.pos, 20, 0)
def draw_black(self):
pygame.draw.circle(screen, self.black, self.pos, 20, 0)
class AI:
def __init__(self, list_status=None):
self.list_status = list_status
def re(self):
return self.list_status
class Judge:
def __init__(self, list_status=None, play=None, chessindex=None):
self.list_status = list_status # 棋盘状态
self.PLAY = play # True人下白棋1, False机下黑棋2
self.indexX, self.indexY = chessindex # 当前放下棋子的坐标
self.chessNum = 1 # 连棋个数
self.searchAll = False # 是否全部搜索完毕
self.judge = False
if self.PLAY: # 现在在下棋的对象
self.role = USER
else:
self.role = ROBOT
self.main()
def main(self):
moveY = self.indexY
moveX = self.indexX
direction = True # 遍历方向 True往右 下, False往左 上
# 横向5个白棋 x坐标(行)不变
if not self.searchAll:
while moveY in range(0, 16):
if direction:
moveY += 1
else:
moveY -= 1
if self.list_status[self.indexX][moveY] == self.role:
self.chessNum += 1
else:
# 如果是搜索到左边,则搜索完毕
if direction is False:
if self.chessNum < 5:
self.chessNum = 1 # 搜索完横向,但没有获胜条件,将连棋置为初始状态
direction = True
moveY = self.indexY
moveX = self.indexX
break
else:
pass
# 只要有一个不匹配,改变方向,且moveX移至起点
direction = False
moveY = self.indexY
if self.chessNum >= 5:
self.judge = True
self.searchAll = True
break
else:
pass
# 竖向5个白棋 y 坐标(列)不变
if not self.searchAll:
while moveX in range(0, 16):
if direction:
moveX += 1
else:
moveX -= 1
if self.list_status[moveX][self.indexY] == self.role:
self.chessNum += 1
else:
# 如果是搜索到左边,则搜索完毕
if direction is False:
if self.chessNum < 5:
self.chessNum = 1 # 搜索完横向,但没有获胜条件,将连棋置为初始状态
direction = True
moveY = self.indexY
moveX = self.indexX
break
else:
pass
# 只要有一个不匹配,改变方向,且moveX移至起点
direction = False
moveX = self.indexX
if self.chessNum >= 5:
self.judge = True
self.searchAll = True
break
else:
pass
# 左上到右下斜角
if not self.searchAll:
while moveX in range(0, 16) and moveY in range(0, 16):
if direction:
moveX += 1
moveY += 1
else:
moveX -= 1
moveY -= 1
if self.list_status[moveX][moveY] == self.role:
self.chessNum += 1
else:
# 如果是搜索到左边,则搜索完毕
if direction is False:
if self.chessNum < 5:
self.chessNum = 1 # 搜索完横向,但没有获胜条件,将连棋置为初始状态
direction = True
moveY = self.indexY
moveX = self.indexX
break
else:
pass
# 只要有一个不匹配,改变方向,且moveX移至起点
direction = False
moveX = self.indexX
moveY = self.indexY
if self.chessNum >= 5:
self.judge = True
self.searchAll = True
break
else:
pass
# 左下到右上斜角
if not self.searchAll:
while moveX in range(0, 16) and moveY in range(0, 16):
if direction:
moveX += 1
moveY -= 1
else:
moveX -= 1
moveY += 1
if self.list_status[moveX][moveY] == self.role:
self.chessNum += 1
else:
# 如果是搜索到左边,则搜索完毕
if direction is False:
if self.chessNum < 5:
self.chessNum = 1 # 搜索完横向,但没有获胜条件,将连棋置为初始状态
direction = True
moveY = self.indexY
moveX = self.indexX
break
else:
pass
# 只要有一个不匹配,改变方向,且moveX移至起点
direction = False
moveX = self.indexX
moveY = self.indexY
if self.chessNum >= 5:
self.judge = True
self.searchAll = True
break
else:
pass
# 先画个棋盘,避免开始黑屏
draw_board()
while True:
for event in pygame.event.get():
if event.type == pygame.QUIT:
exit()
if event.type == pygame.MOUSEBUTTONDOWN:
draw_board()
# print(pygame.mouse.get_pos())
mouseX, mouseY = pygame.mouse.get_pos()
# 格子是40长宽的,棋子半径为20,
mouseX = mouseX - mouseX % 20
mouseY = mouseY - mouseY % 20
# 获得修正值,使得棋子落在网格上
offsetX = mouseX % 40
offsetY = mouseY % 40
# 修正坐标值
if offsetX != 0:
mouseX = mouseX + offsetX
else:
pass
if offsetY != 0:
mouseY = mouseY + offsetY
else:
pass
mousePos = (mouseX, mouseY)
# 找到点击时所在网格的索引值,用于修改棋盘的状态
for i in range(15):
try:
index = (i, list_board_pos[i].index(mousePos))
break
except ValueError:
pass
# print(index)
# 人机交替下棋,并修改棋盘状态
if list_board_status[index[0]][index[1]] == 0: # 当前位置没有棋子,才能放下棋子
if PLAY:
list_board_status[index[0]][index[1]] = USER
print(index)
judge = Judge(list_board_status, PLAY, (index[0], index[1]))
if judge.judge:
print("白棋赢了!")
screen.blit(w_success, (300, 40))
else:
pass
PLAY = False
else:
# 实现AI: 获取合理的 index[0] 和 index[1]
list_board_status[index[0]][index[1]] = ROBOT
judge = Judge(list_board_status, PLAY, (index[0], index[1]))
if judge.judge:
print("黑棋赢了!")
screen.blit(b_success, (300, 40))
else:
pass
PLAY = True
t = AI(list_board_status)
# print(t.re())
else:
pass
# 画棋子
for i in range(15):
for j in range(15):
if list_board_status[i][j] == USER:
DrawChess(list_board_pos[i][j], USER)
elif list_board_status[i][j] == ROBOT:
DrawChess(list_board_pos[i][j], ROBOT)
else:
pass
pygame.display.update()