/
shogi.py
391 lines (369 loc) · 15.8 KB
/
shogi.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
389
390
391
from board import * # board method
from goma import * # goma method
fourdirection = [(0, 1), (1, 0), (0, -1), (-1, 0)]
fourdignodirection = [(1, 1), (1, -1), (-1, 1), (-1, -1)]
class shogi:
def __init__(self):
self.board = initialBoard
self.chesser = '先手'
self.moveRecorder = list()
self.possibleMoveList = list()
self.round = 1
self.cameraAngle = '先手'
self.stimulateFlag = False
def posIsUnderAttack(self, pos):
suji, dan = pos
chesserDierction = -1 if self.chesser == '後手' else 1
another = '後手' if self.chesser == '先手' else '先手'
if legalPos((suji-1, dan+2*chesserDierction)):
if self.board[(suji-1, dan+2*chesserDierction)] == (another, '桂'):
return True
if legalPos((suji+1, dan+2*chesserDierction)):
if self.board[(suji+1, dan+2*chesserDierction)] == (another, '桂'):
return True
if legalPos((suji, dan+1*chesserDierction)):
if self.board[(suji, dan+1*chesserDierction)] in ((another, '步'), (another, '金'), (another, '銀'), (another, '圭'), (another, '杏'), (another, '全'), (another, 'と')):
return True
if legalPos((suji-1, dan+1*chesserDierction)):
if self.board[(suji-1, dan+1*chesserDierction)] in ((another, '金'), (another, '銀'), (another, '圭'), (another, '杏'), (another, '全'), (another, 'と')):
return True
if legalPos((suji+1, dan+1*chesserDierction)):
if self.board[(suji+1, dan+1*chesserDierction)] in ((another, '金'), (another, '銀'), (another, '圭'), (another, '杏'), (another, '全'), (another, 'と')):
return True
if legalPos((suji-1, dan)):
if self.board[(suji-1, dan)] in ((another, '金'), (another, '圭'), (another, '杏'), (another, '全'), (another, 'と')):
return True
if legalPos((suji+1, dan)):
if self.board[(suji+1, dan)] in ((another, '金'), (another, '圭'), (another, '杏'), (another, '全'), (another, 'と')):
return True
if legalPos((suji-1, dan-1*chesserDierction)):
if self.board[(suji-1, dan-1*chesserDierction)] == (another, '銀'):
return True
if legalPos((suji+1, dan-1*chesserDierction)):
if self.board[(suji+1, dan-1*chesserDierction)] == (another, '銀'):
return True
if legalPos((suji, dan-1*chesserDierction)):
if self.board[(suji, dan-1*chesserDierction)] in ((another, '金'), (another, '圭'), (another, '杏'), (another, '全'), (another, 'と')):
return True
for i in range(1, 10, 1):
if not legalPos((suji, dan+i*chesserDierction)): break
if self.board[(suji, dan+i*chesserDierction)] == (another, '香'): return True
if self.board[(suji, dan+i*chesserDierction)][0] != '': break
for d in fourdirection:
for i in range(1, 10, 1):
checksuji = suji+d[0]*i
checkdan = dan+d[1]*i
if not legalPos((checksuji, checkdan)): break
if self.board[(checksuji, checkdan)] == (another, '飛'): return True
if self.board[(checksuji, checkdan)] == (another, '竜'): return True
if i == 1:
if self.board[(checksuji, checkdan)] == (another, '馬'): return True
if self.board[(checksuji, checkdan)] == (another, '王'): return True
if self.board[(checksuji, checkdan)][0] != '': break
for d in fourdignodirection:
for i in range(1, 10, 1):
checksuji = suji+d[0]*i
checkdan = dan+d[1]*i
if not legalPos((checksuji, checkdan)): break
if self.board[(checksuji, checkdan)][0] == self.chesser: break
if self.board[(checksuji, checkdan)] == (another, '角'): return True
if self.board[(checksuji, checkdan)] == (another, '馬'): return True
if i == 1:
if self.board[(checksuji, checkdan)] == (another, '竜'): return True
if self.board[(checksuji, checkdan)] == (another, '王'): return True
if self.board[(checksuji, checkdan)][0] != '': break
return False
def posIsUnderDefense(self, pos):
suji, dan = pos
chesserDierction = 1 if self.chesser == '後手' else -1
if legalPos((suji-1, dan+2*chesserDierction)):
if self.board[(suji-1, dan+2*chesserDierction)] == (self.chesser, '桂'):
return True
if legalPos((suji+1, dan+2*chesserDierction)):
if self.board[(suji+1, dan+2*chesserDierction)] == (self.chesser, '桂'):
return True
if legalPos((suji, dan+1*chesserDierction)):
if self.board[(suji, dan+1*chesserDierction)] in ((self.chesser, '步'), (self.chesser, '金'), (self.chesser, '銀'), (self.chesser, '圭'), (self.chesser, '杏'), (self.chesser, '全'), (self.chesser, 'と')):
return True
if legalPos((suji-1, dan+1*chesserDierction)):
if self.board[(suji-1, dan+1*chesserDierction)] in ((self.chesser, '金'), (self.chesser, '銀'), (self.chesser, '圭'), (self.chesser, '杏'), (self.chesser, '全'), (self.chesser, 'と')):
return True
if legalPos((suji+1, dan+1*chesserDierction)):
if self.board[(suji+1, dan+1*chesserDierction)] in ((self.chesser, '金'), (self.chesser, '銀'), (self.chesser, '圭'), (self.chesser, '杏'), (self.chesser, '全'), (self.chesser, 'と')):
return True
if legalPos((suji-1, dan)):
if self.board[(suji-1, dan)] in ((self.chesser, '金'), (self.chesser, '圭'), (self.chesser, '杏'), (self.chesser, '全'), (self.chesser, 'と')):
return True
if legalPos((suji+1, dan)):
if self.board[(suji+1, dan)] in ((self.chesser, '金'), (self.chesser, '圭'), (self.chesser, '杏'), (self.chesser, '全'), (self.chesser, 'と')):
return True
if legalPos((suji-1, dan-1*chesserDierction)):
if self.board[(suji-1, dan-1*chesserDierction)] == (self.chesser, '銀'):
return True
if legalPos((suji+1, dan-1*chesserDierction)):
if self.board[(suji+1, dan-1*chesserDierction)] == (self.chesser, '銀'):
return True
if legalPos((suji, dan-1*chesserDierction)):
if self.board[(suji, dan-1*chesserDierction)] in ((self.chesser, '金'), (self.chesser, '圭'), (self.chesser, '杏'), (self.chesser, '全'), (self.chesser, 'と')):
return True
for i in range(1, 10, 1):
if not legalPos((suji, dan+i*chesserDierction)): break
if self.board[(suji, dan+i*chesserDierction)] == (self.chesser, '香'): return True
if self.board[(suji, dan+i*chesserDierction)][0] != '': break
for d in fourdirection:
for i in range(1, 10, 1):
checksuji = suji+d[0]*i
checkdan = dan+d[1]*i
if not legalPos((checksuji, checkdan)): break
if self.board[(checksuji, checkdan)] == (self.chesser, '飛'): return True
if self.board[(checksuji, checkdan)] == (self.chesser, '竜'): return True
if i == 1:
if self.board[(checksuji, checkdan)] == (self.chesser, '馬'): return True
if self.board[(checksuji, checkdan)] == (self.chesser, '王'): return True
if self.board[(checksuji, checkdan)][0] != '': break
for d in fourdignodirection:
for i in range(1, 10, 1):
checksuji = suji+d[0]*i
checkdan = dan+d[1]*i
if not legalPos((checksuji, checkdan)): break
if self.board[(checksuji, checkdan)][0] == self.chesser: break
if self.board[(checksuji, checkdan)] == (self.chesser, '角'): return True
if self.board[(checksuji, checkdan)] == (self.chesser, '馬'): return True
if i == 1:
if self.board[(checksuji, checkdan)] == (self.chesser, '竜'): return True
if self.board[(checksuji, checkdan)] == (self.chesser, '王'): return True
if self.board[(checksuji, checkdan)][0] != '': break
return False
def isUnderOte(self):
suji = 1
dan = 1
for i in range(1, 10):
for j in range(1, 10):
if self.board[(i, j)] == (self.chesser, '王'):
suji = i
dan = j
break
return self.posIsUnderAttack((suji, dan))
def isLegalMove(self, move):
underOteFlag = self.isUnderOte()
prePiecePos, newPiecePos, nari = move
if newPiecePos[0] < 0 or newPiecePos[0] > 9 or newPiecePos[1] < 0 or newPiecePos[1] > 9:
return False
if self.board[newPiecePos][0] == self.chesser:
return False
if prePiecePos[0] == 0:
if self.board[sentegomaT][prePiecePos[1]][1] == '步' or self.board[sentegomaT][prePiecePos[1]][1] == '香':
if newPiecePos[1] == 9: return False
if self.board[sentegomaT][prePiecePos[1]][1] == '桂':
if newPiecePos[1] >= 8: return False
if self.board[sentegomaT][prePiecePos[1]][1] == '步':
for dan in range(1, 10):
if self.board[(newPiecePos[0], dan)] == ('先手', '步'): return False
if underOteFlag:
self.board[newPiecePos] = self.board[sentegomaT][prePiecePos[1]]
if self.isUnderOte():
self.board[newPiecePos] = ('', ' ')
return False
else:
self.board[newPiecePos] = ('', ' ')
if self.board[sentegomaT][prePiecePos[1]][1] == '步':
self.board[newPiecePos] = ('先手', '步')
self.chesser = '後手'
if self.isUnderOte():
self.stimulateFlag = True
if self.getPML() == False:
self.chesser = '先手'
self.board[newPiecePos] = ('', ' ')
self.stimulateFlag = False
return False
else:
self.chesser = '先手'
self.board[newPiecePos] = ('', ' ')
self.stimulateFlag = False
self.possibleMoveList.pop()
else:
self.chesser = '先手'
self.board[newPiecePos] = ('', ' ')
self.stimulateFlag = False
return True
if prePiecePos[0] == 10:
if self.board[gotegomaT][prePiecePos[1]][1] == '步' or self.board[gotegomaT][prePiecePos[1]][1] == '香':
if newPiecePos[1] == 1: return False
if self.board[gotegomaT][prePiecePos[1]][1] == '桂':
if newPiecePos[1] <= 2: return False
if self.board[gotegomaT][prePiecePos[1]][1] == '步':
for dan in range(1, 10):
if self.board[(newPiecePos[0], dan)] == ('後手', '步'): return False
if underOteFlag:
self.board[newPiecePos] = self.board[gotegomaT][prePiecePos[1]]
if self.isUnderOte():
self.board[newPiecePos] = ('', ' ')
return False
else:
self.board[newPiecePos] = ('', ' ')
if self.board[gotegomaT][prePiecePos[1]][1] == '步':
self.board[newPiecePos] = ('後手', '步')
self.chesser = '先手'
if self.isUnderOte():
self.stimulateFlag = True
if self.getPML() == False:
self.chesser = '後手'
self.board[newPiecePos] = ('', ' ')
self.stimulateFlag = False
return False
else:
self.chesser = '後手'
self.board[newPiecePos] = ('', ' ')
self.stimulateFlag = False
self.possibleMoveList.pop()
else:
self.chesser = '後手'
self.board[newPiecePos] = ('', ' ')
self.stimulateFlag = False
return True
if prePiecePos[0] < 0 or prePiecePos[0] > 9 or prePiecePos[1] < 0 or prePiecePos[1] > 9:
return False
if not legalPos(newPiecePos):
return False
if self.board[prePiecePos][0] != self.chesser:
return False
if (self.board[prePiecePos][1] == '香' or self.board[prePiecePos][1] == '步') and newPiecePos[1] == 9 and self.chesser == '先手' and nari == False:
return False
if (self.board[prePiecePos][1] == '香' or self.board[prePiecePos][1] == '步') and newPiecePos[1] == 1 and self.chesser == '後手' and nari == False:
return False
if self.board[prePiecePos][1] == '桂' and newPiecePos[1] <= 2 and self.chesser == '後手' and nari == False:
return False
if self.board[prePiecePos][1] == '桂' and newPiecePos[1] >= 8 and self.chesser == '先手' and nari == False:
return False
if prePiecePos[0] >= 1 and prePiecePos[0] <= 9:
tmpPiecePre = self.board[prePiecePos]
tmpPieceNew = self.board[newPiecePos]
self.board[newPiecePos] = self.board[prePiecePos]
self.board[prePiecePos] = ('', ' ')
if self.isUnderOte():
self.board[newPiecePos] = tmpPieceNew
self.board[prePiecePos] = tmpPiecePre
return False
self.board[newPiecePos] = tmpPieceNew
self.board[prePiecePos] = tmpPiecePre
return True
def getPML(self):
underOteFlag = self.isUnderOte()
if not self.stimulateFlag:
self.possibleMoveList.clear()
chesserDierction = -1 if self.chesser == '後手' else 1
gomaT = sentegomaT if self.chesser == '先手' else gotegomaT
pregoma = ''
for mochigoma in self.board[gomaT]:
if pregoma == mochigoma:
continue
else:
pregoma = mochigoma
for suji in range(1, 10, 1):
for dan in range(1, 10, 1):
if self.board[(suji, dan)][0] == '':
move = ((gomaT[0], self.board[gomaT].index(mochigoma)), (suji, dan), False)
if self.isLegalMove(move):
self.possibleMoveList.append(move)
if self.stimulateFlag:
return True
for pos in self.board.keys():
suji, dan = pos
returnvalue = getGomaPossibleMove(pos, self.board, self.board[pos])
if returnvalue is False:
continue
for move in returnvalue:
if self.isLegalMove(move):
self.possibleMoveList.append(move)
if self.stimulateFlag:
return True
if self.stimulateFlag:
return False
if len(self.possibleMoveList) == 0:
self.moveRecorder.append(str(self.round)+'手まで、'+self.chesser+'詰み\n')
return False
else:
return True
def makeMove(self, move):
if move not in self.possibleMoveList:
return False
prePiecePos, newPiecePos, nari = move
if prePiecePos[0] == 0 or prePiecePos[0] == 10:
self.board[newPiecePos] = self.board[(prePiecePos[0], 0)][prePiecePos[1]]
thelen = len(self.board[(prePiecePos[0], 0)]) - 1
self.board[(prePiecePos[0], 0)][prePiecePos[1]] = self.board[(prePiecePos[0], 0)][thelen]
self.board[(prePiecePos[0], 0)].pop()
self.chesser = '後手' if self.chesser == '先手' else '先手'
self.moveRecorder.append(move)
self.board[(prePiecePos[0], 0)].sort(key=lambda val: gomaOrder[val[1]])
return True
else:
if self.board[newPiecePos][0] == '先手':
self.board[gotegomaT].append(('後手', gomaDePromote[self.board[newPiecePos][1]]))
self.board[gotegomaT].sort(key=lambda val: gomaOrder[val[1]])
if self.board[newPiecePos][0] == '後手':
self.board[sentegomaT].append(('先手', gomaDePromote[self.board[newPiecePos][1]]))
self.board[sentegomaT].sort(key=lambda val: gomaOrder[val[1]])
if nari is True:
self.board[newPiecePos] = (self.board[prePiecePos][0], gomaPromote[self.board[prePiecePos][1]])
else:
self.board[newPiecePos] = self.board[prePiecePos]
self.board[prePiecePos] = ('', ' ')
self.round += 1
self.chesser = '後手' if self.chesser == '先手' else '先手'
self.moveRecorder.append(move)
return True
def easyBoardPrint(self):
if self.cameraAngle == '先手':
for dan in range(9, 0, -1):
if dan == 9:
print(' ', end='')
for suji in range(1, 10, 1):
print(suji, end=' ')
print('')
print(dan, ':', end='')
for suji in range(1, 10, 1):
pos = (suji, dan)
if self.board[pos][0] == '先手':
print(self.board[pos][1], end=' ')
elif self.board[pos][0] == '後手':
print(self.board[pos][1], end='|')
else:
print('. ', end=' ')
print('')
else:
for dan in range(1, 10, 1):
if dan == 1:
print(' ', end='')
for suji in range(9, 0, -1):
print(suji, end=' ')
print('')
print(dan, ':', end='')
for suji in range(9, 0, -1):
pos = (suji, dan)
if self.board[pos][0] == '先手':
print(self.board[pos][1], end='|')
elif self.board[pos][0] == '後手':
print(self.board[pos][1], end=' ')
else:
print('. ', end=' ')
print('')
print('先手持駒:', end='')
for goma in self.board[sentegomaT]:
print(goma[1], end=',')
print('')
print('後手持駒:', end='')
for goma in self.board[gotegomaT]:
print(goma[1], end=',')
print('')
def printDefenseState(self):
for dan in range(1, 10):
for suji in range(9, 0, -1):
pos = (suji, dan)
if self.posIsUnderDefense(pos):
print('x', end='')
else:
print('.', end='')
print('')
print('')
# def revertMove(self): todo function