-
Notifications
You must be signed in to change notification settings - Fork 1
/
Board.py
324 lines (293 loc) · 12.4 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
321
322
323
324
from BoardCell import *
from Constants import *
from Piece import *
from Hand import *
class Board:
def __init__(self, imgs):
self.playing = "bot"
self.imgs = imgs
self.SELECTED = None
self.PROMOTING = None
self.matrix = [[BoardCell(x, y) for x in range(9)] for y in range(9)]
self.placePieces()
self.promotionData = None
self.botHand = Hand("bot", self.imgs)
self.topHand = Hand("top", self.imgs)
def changePlayer(self):
if self.playing == "bot":
self.playing = "top"
else:
self.playing = "bot"
def placePieces(self):
for x in range(9):
for y in range(9):
if y < 5:
side = "top"
else:
side = "bot"
if BOARD[y][x]!=None:
if BOARD[y][x]=="L":
p = Piece(side, "lance", x, y, self.imgs)
elif BOARD[y][x]=="N":
p = Piece(side, "knight", x, y, self.imgs)
elif BOARD[y][x]=="S":
p = Piece(side, "silver", x, y, self.imgs)
elif BOARD[y][x]=="G":
p = Piece(side, "gold", x, y, self.imgs)
elif BOARD[y][x]=="K":
p = Piece(side, "king", x, y, self.imgs)
elif BOARD[y][x]=="P":
p = Piece(side, "pawn", x, y, self.imgs)
elif BOARD[y][x]=="B":
p = Piece(side, "bishop", x, y, self.imgs)
elif BOARD[y][x]=="R":
p = Piece(side, "rook", x, y, self.imgs)
self.matrix[y][x].placePiece(p)
def show(self):
for x in range(9):
for y in range(9):
self.matrix[y][x].show()
self.topHand.show()
self.botHand.show()
def unmarkAll(self):
for x in range(9):
for y in range(9):
self.matrix[y][x].unmark()
def setAttackMove(self, x, y, friendly):
square = self.matrix[y][x]
if square.piece == None:
square.movable = True
else:
if square.piece.side != friendly:
square.attackable = True
def applyMovesMat(self, x, y, mat, friendly):
xDist = len(mat[0]) // 2
yDist = len(mat) // 2
for i in range(len(mat[0])):
for j in range(len(mat)):
xSquare = x+i-xDist
ySquare = y+j-yDist
if xSquare>=0 and xSquare<9 and ySquare>=0 and ySquare<9:
if mat[j][i] == "move":
self.setAttackMove(xSquare, ySquare, friendly)
def applyKnightJump(self, x, y, mat, friendly):
xDist = len(mat[0]) // 2
yDist = len(mat) // 2
for i in range(len(mat[0])):
for j in range(len(mat)):
xSquare = x+i-xDist
ySquare = y+j-yDist
if xSquare>=0 and xSquare<9 and ySquare>=0 and ySquare<9:
if mat[j][i] == "move":
self.setAttackMove(x+i-xDist, y+j-yDist, friendly)
def unvisitAll(self):
for x in range(9):
for y in range(9):
self.matrix[y][x].visited = False
def checkCellSight(self, x, y):
cell = self.matrix[y][x]
cell.visited = True
dirX = self.SELECTED.origPos[0] - x
dirY = self.SELECTED.origPos[1] - y
if dirX == 0 and dirY == 0:
return True
if dirX > 0:
newX = x + 1
elif dirX == 0:
newX = x
elif dirX < 0:
newX = x - 1
if dirY > 0:
newY = y + 1
elif dirY == 0:
newY = y
elif dirY < 0:
newY = y - 1
check = self.checkCellSight(newX, newY)
if not check:
cell.movable = False
cell.attackable = False
return False
elif cell.piece != None:
return False
else:
return True
def lineOfSight(self):
for x in range(9):
for y in range(9):
if not self.matrix[y][x].visited:
self.checkCellSight(x, y)
self.unvisitAll()
def markMoves(self, x, y, piece):
if piece.side == "bot":
if piece.piece == "pawn":
self.applyMovesMat(x, y, PAWNBOT, piece.side)
elif piece.piece == "lance":
self.applyMovesMat(x, y, LANCEBOT, piece.side)
elif piece.piece == "silver":
self.applyMovesMat(x, y, SILVERBOT, piece.side)
elif piece.piece in ["gold","tokin","lancePro","knightPro","silverPro"]:
self.applyMovesMat(x, y, GOLDBOT, piece.side)
elif piece.piece == "knight":
self.applyKnightJump(x, y, KNIGHTBOT, piece.side)
elif piece.side == "top":
if piece.piece == "pawn":
self.applyMovesMat(x, y, PAWNTOP, piece.side)
elif piece.piece == "lance":
self.applyMovesMat(x, y, LANCETOP, piece.side)
elif piece.piece == "silver":
self.applyMovesMat(x, y, SILVERTOP, piece.side)
elif piece.piece in ["gold","tokin","lancePro","knightPro","silverPro"]:
self.applyMovesMat(x, y, GOLDTOP, piece.side)
elif piece.piece == "knight":
self.applyKnightJump(x, y, KNIGHTTOP, piece.side)
if piece.piece == "king":
self.applyMovesMat(x, y, KING, piece.side)
elif piece.piece == "bishop":
self.applyMovesMat(x, y, BISHOP, piece.side)
elif piece.piece == "rook":
self.applyMovesMat(x, y, ROOK, piece.side)
elif piece.piece == "horse":
self.applyMovesMat(x, y, HORSE, piece.side)
elif piece.piece == "dragon":
self.applyMovesMat(x, y, DRAGON, piece.side)
if piece.piece != "knight":
self.lineOfSight()
def checkColNoPawns(self, x, side):
for y in range(9):
piece = self.matrix[y][x].piece
if piece != None and piece.piece == "pawn" and piece.side == side:
return False
return True
def markDrop(self, piece):
for x in range(9):
for y in range(9):
if self.matrix[y][x].piece == None:
if piece.piece == "pawn":
if piece.side == "bot":
if y > 0:
if self.checkColNoPawns(x, piece.side):
self.matrix[y][x].movable = True
else:
if y < 8:
if self.checkColNoPawns(x, piece.side):
self.matrix[y][x].movable = True
elif piece.piece == "lance":
if piece.side == "bot":
if y > 0:
self.matrix[y][x].movable = True
else:
if y < 8:
self.matrix[y][x].movable = True
elif piece.piece == "knight":
if piece.side == "bot":
if y > 1:
self.matrix[y][x].movable = True
else:
if y < 7:
self.matrix[y][x].movable = True
else:
self.matrix[y][x].movable = True
def checkSelect(self, x, y):
if x>MARGIN and x<CANVASSIZE-MARGIN and y>MARGIN and y<CANVASSIZE-MARGIN:
xSquare = (x-MARGIN) // CELLDIM
ySquare = (y-MARGIN) // CELLDIM
piece = self.matrix[ySquare][xSquare].piece
if piece != None and piece.side == self.playing:
self.matrix[ySquare][xSquare].selected = True
self.SELECTED = piece
piece.select()
self.markMoves(xSquare, ySquare, piece)
else:
if self.playing == "bot":
piece = self.botHand.checkClick(x, y)
elif self.playing == "top":
piece = self.topHand.checkClick(x, y)
if piece != None:
piece.dropping = True
self.SELECTED = piece
piece.select()
self.markDrop(piece)
def checkDeselect(self, x, y):
moved = False
self.SELECTED.deselect()
if x>MARGIN and x<CANVASSIZE-MARGIN and y>MARGIN and y<CANVASSIZE-MARGIN:
xSquare = (x-MARGIN) // CELLDIM
ySquare = (y-MARGIN) // CELLDIM
cell = self.matrix[ySquare][xSquare]
if cell.attackable or cell.movable:
moved = True
capPiece = cell.placePiece(self.SELECTED)
if capPiece != None:
if self.playing == "bot":
self.botHand.addPiece(capPiece)
elif self.playing == "top":
self.topHand.addPiece(capPiece)
if not self.SELECTED.dropping:
self.matrix[self.SELECTED.origPos[1]][self.SELECTED.origPos[0]].piece = None
self.changePlayer()
if not self.SELECTED.dropping:
self.checkPromote(self.SELECTED)
if self.SELECTED.dropping and not moved:
if self.SELECTED.side == "bot":
self.botHand.addPiece(self.SELECTED)
elif self.SELECTED.side == "top":
self.topHand.addPiece(self.SELECTED)
self.SELECTED.dropping = False
self.SELECTED = None
self.unmarkAll()
def checkPromote(self, piece):
if piece.side == "bot":
if piece.piece in ["pawn", "lance"]:
if piece.y == 0:
piece.promote()
elif piece.y < 3:
self.PROMOTING = piece
self.getPromotionData()
elif piece.piece == "knight":
if piece.y < 2:
piece.promote()
elif piece.y == 2:
self.PROMOTING = piece
self.getPromotionData()
elif piece.piece in ["bishop", "rook", "silver"]:
if piece.y < 3:
self.PROMOTING = piece
self.getPromotionData()
elif piece.side == "top":
if piece.piece in ["pawn", "lance"]:
if piece.y == 8:
piece.promote()
elif piece.y > 5:
self.PROMOTING = piece
self.getPromotionData()
elif piece.piece == "knight":
if piece.y > 6:
piece.promote()
elif piece.y == 6:
self.PROMOTING = piece
self.getPromotionData()
elif piece.piece in ["bishop", "rook", "silver"]:
if piece.y > 5:
self.PROMOTING = piece
self.getPromotionData()
def getPromotionData(self):
unPromoted = Piece(self.PROMOTING.side,self.PROMOTING.piece,0,0,self.imgs)
promoted = Piece(self.PROMOTING.side,self.PROMOTING.piece,0,0,self.imgs)
promoted.promote()
self.promotionData = (unPromoted, promoted)
def showPromote(self):
fill(*PROMOTION)
rect(width//2-BOXWIDTH//2,height//2-BOXHEIGHT//2,BOXWIDTH,BOXHEIGHT)
fill(*OPTIONCOLOR)
rect(Xpos1, Ypos, OPTIONDIM, OPTIONDIM)
rect(Xpos2, Ypos, OPTIONDIM, OPTIONDIM)
self.promotionData[0].showPromotionImage(Xpos1, Ypos)
self.promotionData[1].showPromotionImage(Xpos2, Ypos)
def checkPromotionSelection(self, x, y):
if x>Xpos1 and x<Xpos2+OPTIONDIM and y>Ypos and y<Ypos+OPTIONDIM:
if x<Xpos1+OPTIONDIM:
self.PROMOTING = None
elif x>Xpos2:
self.PROMOTING.promote()
self.PROMOTING = None