forked from shiluying/dachuang-factory
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathamazons.py
More file actions
358 lines (294 loc) · 9.91 KB
/
amazons.py
File metadata and controls
358 lines (294 loc) · 9.91 KB
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import numpy as np
import copy
'''
我们规定 0 为没有棋子
我们规定 1 为先手方 A
我们规定 2 为后手方 B
我们规定 3 为先手方障碍 A
我们规定 4 为后手方障碍 B
'''
def direction(origin, move):
y = move[0] - origin[0]
x = move[1] - origin[1]
if y == 0:
if x > 0:
return 3, x - 1
elif x < 0:
return 2, abs(x) - 1
elif y > 0:
if x == 0:
return 1, y - 1
elif abs(y) != abs(x):
return -1, -1
elif x > 0:
return 4, y - 1
elif x < 0:
return 5, y - 1
elif y < 0:
if x == 0:
return 0, abs(y) - 1
elif abs(y) != abs(x):
return -1, -1
elif x > 0:
return 6, x - 1
elif x < 0:
return 7, abs(y) - 1
vectors = [(-1, 0), (1, 0), (0, -1), (0, 1),
(1, 1), (1, -1), (-1, 1), (-1, -1)]
class Amazons(object):
def __init__(self):
board = np.zeros((10, 10), dtype=int)
# 初始化先手方棋子位置
board[0, 3] = 1
board[0, 6] = 1
board[3, 0] = 1
board[3, 9] = 1
# 初始化后手方棋子位置
board[6, 0] = 2
board[9, 3] = 2
board[9, 6] = 2
board[6, 9] = 2
self.__globalBoard = board
self.__width = 10
self.__height = 10
self.playerState = {'A': [(0, 3), (0, 6), (3, 0), (3, 9)],
'B': [(9, 3), (9, 6), (6, 0), (6, 9)]}
self.lastMove = []
# 获取棋盘情况
def getBoard(self):
return self.__globalBoard
def getPlayerLocation(self, player):
return self.playerState[player]
def rollback(self, board):
self.__globalBoard = board
# 下棋操作
def fire(self, player, location, itemtype, save=False):
# 我们规定itemtype=0 为棋子
# 我们规定itemtype=1 为障碍
# reshapeBoard = self.__globalBoard.reshape(self.__height, self.__width)
reshapeBoard = self.__globalBoard
# 将 from to 搞出来
itemfromY = location['from'][0]
itemfromX = location['from'][1]
itemtoY = location['to'][0]
itemtoX = location['to'][1]
# Start 下棋规则
# todo 检查相应位置是否有棋子,是否是对应用户的棋子
'''
说明:
chessX 是 行坐标
chessY 是 列坐标
enableLocation* 在八个方向可以下棋的位置
enabelLocation 是八个方向汇总在一起可以下的位置
flag* 是分别在八个方向有阻挡棋子的开始和结束位置
count* 是统计出当前每个enabelLocation*的成员数量
'''
chessX = itemfromX
chessY = itemfromY
countX, countY, countP, countN = 0, 0, 0, 0
'''
首先需要用两个for循环,遍历出当前棋子位置,在八个方向上允许下棋的所有位置
包括中间有格挡棋子的情况
'''
# 最后将整个可以放置为位置整合在一个list中
enableLocation = []
idx, _ = direction(location['from'], location['to'])
if idx == -1:
raise RuntimeError
else:
vector = vectors[idx]
enableLocation += self.enabledLocation((chessY, chessX), vector)
# End 棋盘规则
# 然后判断是否可以放在这个地方
if (itemtoY, itemtoX) not in enableLocation:
raise RuntimeError
if player == 'A' and itemtype == 0:
reshapeBoard[itemfromY, itemfromX] = 0
reshapeBoard[itemtoY, itemtoX] = 1
stateA = self.playerState['A']
stateA[stateA.index((itemfromY, itemfromX))] = (itemtoY, itemtoX)
elif player == 'A' and itemtype == 1:
# reshapeBoard[itemfromH, itemfromV] = 0
reshapeBoard[itemtoY, itemtoX] = 3
elif player == 'B' and itemtype == 0:
reshapeBoard[itemfromY, itemfromX] = 0
reshapeBoard[itemtoY, itemtoX] = 2
stateB = self.playerState['B']
stateB[stateB.index((itemfromY, itemfromX))] = (itemtoY, itemtoX)
elif player == 'B' and itemtype == 1:
# reshapeBoard[itemfromH, itemfromV] = 0
reshapeBoard[itemtoY, itemtoX] = 4
else:
pass
if save:
if itemtype == 0:
self.lastMove = [(itemfromY, itemfromX), (itemtoY, itemtoX)]
else:
self.lastMove.append((itemtoY, itemtoX))
# changedBoard = np.asarray(reshapeBoard, dtype=int)
# self.__globalBoard = reshapeBoard
# 然后进行胜负的判断
playerResult = self.gameStatus()
# return changedBoard, playerResult
return reshapeBoard, playerResult
def enabledLocation(self, loc, vec):
stack = [loc]
while self._isAvailable((stack[-1][0] + vec[0], stack[-1][1] + vec[1])):
stack.append((stack[-1][0] + vec[0], stack[-1][1] + vec[1]))
return stack[1:]
def allEnabledLocation(self, player):
pos = {}
for i in self.playerState[player]:
for vec in vectors:
if pos.get(i) == None:
pos[i] = self.enabledLocation(i, vec)
else:
pos[i] += self.enabledLocation(i, vec)
return pos
def _isAvailable(self, loc):
"""
判断这个位置是否超出边界,或者有棋子
:param loc: tuple: 位置
:return: 如果能下,返回真,否则返回假
:rtype: bool
"""
if self.isOutOfBound(loc):
return False
elif self.__globalBoard[loc] != 0:
return False
else:
return True
def isOutOfBound(self, loc):
if loc[0] < 0 or loc[1] < 0:
return True
elif loc[0] > 9 or loc[1] > 9:
return True
else:
return False
# 判断胜负
def gameStatus(self):
def setStatus(status, playerState):
for i, (y, x) in enumerate(playerState):
empty = -1
for vec in vectors:
pos = (y + vec[0], x + vec[1])
if not self.isOutOfBound(pos):
if board[pos] == 0:
empty = 1
break
if empty == -1:
status[i] = False
playerAState = self.playerState['A']
playerBState = self.playerState['B']
statusA = [True, True, True, True]
statusB = [True, True, True, True]
board = self.__globalBoard
setStatus(statusA, playerAState)
setStatus(statusB, playerBState)
playerA = False
playerB = False
try:
statusA.index(True)
except ValueError:
playerB = True
try:
statusB.index(True)
except ValueError:
playerA = True
if playerA == True and playerB == False:
return 'A'
elif playerA == False and playerB == True:
return 'B'
elif playerA == True and playerB == True:
return 'TIE'
elif playerA == False and playerB == False:
return None
def getBarrier(self):
result = np.where(self.__globalBoard >= 3)
return list(zip(result[0], result[1]))
def setBoard(self, board):
self.__globalBoard = board
def showBoard(self):
print(' ', end='')
for i in range(97, 107):
print(chr(i), end=' ')
print()
print(' ', end='')
for i in range(10):
print('-', end=' ')
print()
for i in range(10):
if i == 0:
print('10', end='|')
else:
print(' ' + str(10 - i), end='|')
for j in range(10):
print(self.__globalBoard[i, j], end=' ')
print()
def getBinaryBoard(self, player, first='A', winner=None):
binary = []
board = self.__globalBoard
if player == 'A':
binary.append((board == 1).astype(int))
binary.append((board == 2).astype(int))
else:
binary.append((board == 2).astype(int))
binary.append((board == 1).astype(int))
binary.append((board >= 3).astype(int))
if len(self.lastMove) != 0:
move = []
for i in self.lastMove:
tmp = np.zeros((10, 10))
tmp[i] = 1
move.append(tmp)
if len(move) != 3:
for i in range(3 - len(move)):
move.append(np.zeros((10, 10)))
binary.extend(move)
else:
for i in range(3):
binary.append(np.zeros((10, 10)))
if player == first:
binary.append((board >= 0).astype(int))
else:
binary.append((board < 0).astype(int))
return binary
def alphaToPos(pos):
alpha = {
'a': 0,
'b': 1,
'c': 2,
'd': 3,
'e': 4,
'f': 5,
'g': 6,
'h': 7,
'i': 8,
'j': 9,
}
return [10 - int(pos[1]), alpha[pos[0]]]
def main():
board = Amazons()
moves = [
"A a 7 c 7 c 1",
"A c 7 c 6 c 2",
"A c 6 d 6 d 2",
"A d 6 e 6 e 1",
"A e 6 e 7 e 2",
]
for i in moves:
move = i.split(" ")
player = move[0]
move = move[1:]
cheeseFrom = alphaToPos([move[0], move[1]])
cheeseTo = alphaToPos([move[2], move[3]])
itemTo = alphaToPos([move[4], move[5]])
board.fire(
player, {'from': cheeseFrom, 'to': cheeseTo}, 0)
a = board.fire(
player, {'from': cheeseTo, 'to': itemTo}, 1)
board.showBoard()
if __name__ == '__main__':
main()