forked from senbar/sigmaChess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboard.py
371 lines (332 loc) · 17.1 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
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
from pieces import *
import os.path
class BoardCOlours:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'
class ChessBoard:
def __init__(self):
self.boardArray = [[]]
self.boardArray = self.__spawnPieces()
self.whoMoved = factionColor.FACTION_BLACK
# lastMoves is a stack that stores tuples
# (movingPiece, startingPositionX, startingPositionY, targetPiece, targetPositionX, targetPositionY)
self.lastMoves = []
def __createEmptyArray(self):
hArray = []
for i in range(8):
hArray.append([])
for k in range(8):
hArray[i].append(None)
return hArray
def __spawnPieces(self):
Path = os.path.dirname(os.path.abspath(__file__))
boardArray = self.__createEmptyArray()
boardtemplate = open(Path+ "/boardtemplate.txt", "r")
for y in range(8):
line = boardtemplate.readline()
for x in range(8):
whatPiece = line[x]
if whatPiece == "O":
boardArray[x][y] = None
elif whatPiece == "P":
if y <= 3:
boardArray[x][y] = piecePawn(x, y, factionColor.FACTION_WHITE)
else:
boardArray[x][y] = piecePawn(x, y, factionColor.FACTION_BLACK)
elif whatPiece == "R":
if y <= 3:
boardArray[x][y] = pieceRook(x, y, factionColor.FACTION_WHITE)
else:
boardArray[x][y] = pieceRook(x, y, factionColor.FACTION_BLACK)
elif whatPiece == "N":
if y <= 3:
boardArray[x][y] = pieceKnight(x, y, factionColor.FACTION_WHITE)
else:
boardArray[x][y] = pieceKnight(x, y, factionColor.FACTION_BLACK)
elif whatPiece == "B":
if y <= 3:
boardArray[x][y] = pieceBishop(x, y, factionColor.FACTION_WHITE)
else:
boardArray[x][y] = pieceBishop(x, y, factionColor.FACTION_BLACK)
elif whatPiece == "Q":
if y <= 3:
boardArray[x][y] = pieceQueen(x, y, factionColor.FACTION_WHITE)
else:
boardArray[x][y] = pieceQueen(x, y, factionColor.FACTION_BLACK)
elif whatPiece == "K":
if y <= 3:
boardArray[x][y] = pieceKing(x, y, factionColor.FACTION_WHITE)
else:
boardArray[x][y] = pieceKing(x, y, factionColor.FACTION_BLACK)
return boardArray
def getAllMoves(self, faction):
'''
This function finds all possible moves for a given faction
:param faction: faction for which all moves are to be returned
:return: [coords, move] where coords is a tuple of (actualX, actualY) coordinates of piece and move is (newX, nexY) tuple
is a tuple (newX, newY) of coordinates where it can move
'''
legalMoves = []
for x in range(8):
for y in range(8):
if self.boardArray[x][y] is not None:
piece = self.getPiece(x, y)
# Pawn
if type(piece) == piecePawn and piece.faction == faction:
# Tuples of moves which may be possible for pawn
pawnPossibleMoves = [(0, 1), (0, -1), (0, 2), (0, -2)]
# Tuples of attacks which may be possible for pawn
pawnPossibleAttacks = [(1, 1), (-1, -1), (1, -1), (-1, 1)]
for move in pawnPossibleMoves:
if piece.checkMove(self.boardArray, coordHorizontal=(x + move[0]) % len(self.boardArray[0]),
coordVert=(y + move[1]) % len(self.boardArray[0])):
legalMove = ((x, y), ((x + move[0]) % 8, (y + move[1]) % 8))
legalMoves.append(legalMove)
for attack in pawnPossibleAttacks:
if piece.checkAttack(self.boardArray, coordHorizontal=(x + attack[0]) % len(self.boardArray[0]),
coordVert=(y + attack[1]) % len(self.boardArray[0])):
legalMove = ((x, y), ((x + attack[0]) % 8, (y + attack[1]) % 8))
legalMoves.append(legalMove)
continue
# Rook
if type(piece) == pieceRook and piece.faction == faction:
# Tuples of moves which may be possible for rook (works as attack as well)
rookPossibleMoves = [(1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0),
(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8)]
for move in rookPossibleMoves:
if piece.checkMove(self.boardArray, coordHorizontal=(x + move[0]) % len(self.boardArray[0]),
coordVert=(y + move[1]) % len(self.boardArray[0])):
legalMove = ((x, y), ((x + move[0]) % 8, (y + move[1]) % 8))
legalMoves.append(legalMove)
continue
# Knight
if type(piece) == pieceKnight and piece.faction == faction:
# Tuples of moves which may be possible for knight (works as attack as well)
knightPossibleMoves = [(1, 2), (1, -2), (-1, 2), (-1, -2), (2, 1), (2, -1), (-2, 1), (-2, -1)]
for move in knightPossibleMoves:
if piece.checkMove(self.boardArray, coordHorizontal=(x + move[0]) % len(self.boardArray[0]),
coordVert=(y + move[1]) % len(self.boardArray[0])):
legalMove = ((x, y), ((x + move[0]) % 8, (y + move[1]) % 8))
legalMoves.append(legalMove)
continue
# Bishop
if type(piece) == pieceBishop and piece.faction == faction:
# Tuples of moves which may be possible for bishop (works as attack as well)
bishopPossibleMoves = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8),
(1, -1), (2, -2), (3, -3), (4, -4), (5, -5), (6, -6), (7, -7),
(8, -8)]
for move in bishopPossibleMoves:
if piece.checkMove(self.boardArray, coordHorizontal=(x + move[0]) % 8, coordVert=(y + move[1]) % 8):
legalMove = ((x, y), ((x + move[0]) % 8, (y + move[1]) % 8))
legalMoves.append(legalMove)
continue
# Queen
if type(piece) == pieceQueen and piece.faction == faction:
# Tuples of moves which may be possible for queen (works as attack as well)
queenPossibleMoves = [(1, 1), (2, 2), (3, 3), (4, 4), (5, 5), (6, 6), (7, 7), (8, 8),
(1, -1), (2, -2), (3, -3), (4, -4), (5, -5), (6, -6), (7, -7), (8, -8),
(1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (6, 0), (7, 0), (8, 0),
(0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8)]
for move in queenPossibleMoves:
if piece.checkMove(self.boardArray, coordHorizontal=(x + move[0]) % len(self.boardArray[0]),
coordVert=(y + move[1]) % len(self.boardArray[0])):
legalMove = ((x, y), ((x + move[0]) % 8, (y + move[1]) % 8))
legalMoves.append(legalMove)
continue
# King
if type(piece) == pieceKing and piece.faction == faction:
#Tuples of moves which may be possible for king
kingPossibleMoves = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
#Tuples of attacks which may be possible for king
kingPossibleAttacks = [(1, 1), (1, 0), (1, -1), (0, 1), (0, -1), (-1, 1), (-1, 0), (-1, -1)]
for move in kingPossibleMoves:
if piece.checkMove(self.boardArray, coordHorizontal=(x + move[0]) % len(self.boardArray[0]),
coordVert=(y + move[1]) % len(self.boardArray[0])):
if not self.tileIsChecked((x + move[0]) % 8, (y + move[1]) % 8, self.getOppositeFaction(piece.faction)):
legalMove = ((x, y), ((x + move[0]) % 8, (y + move[1]) % 8))
legalMoves.append(legalMove)
for attack in kingPossibleAttacks:
if piece.checkAttack(self.boardArray, coordHorizontal=(x + attack[0]) % len(self.boardArray[0]),
coordVert=(y + attack[1]) % len(self.boardArray[0])):
if not self.tileIsChecked((x + attack[0]) % 8, (y + attack[1]) % 8, self.getOppositeFaction(piece.faction)):
legalMove = ((x, y), ((x + attack[0]) % 8, (y + attack[1]) % 8))
legalMoves.append(legalMove)
continue
return legalMoves
def movePiece(self, pieceToMove, xNew, yNew):
"""this method performs both attack and move"""
# check if it can move
isAttack = pieceToMove.checkAttack(self.boardArray, coordHorizontal=xNew, coordVert=yNew)
if isAttack == True and pieceToMove.faction != self.whoMoved:
self.__move(pieceToMove, xNew, yNew)
return True
# check if it can attack
isLegal = pieceToMove.checkMove(self.boardArray, coordHorizontal=xNew, coordVert=yNew)
if isLegal == True and pieceToMove.faction != self.whoMoved:
self.__move(pieceToMove, xNew, yNew)
return True
# cant do neither
return False
def __move(self, pieceToMove, xNew, yNew):
xCurrent = pieceToMove.x
yCurrent = pieceToMove.y
lastMove = (pieceToMove, xCurrent, yCurrent, self.boardArray[xNew][yNew], xNew, yNew)
self.lastMoves.append(lastMove)
#change coords inside piece object
pieceToMove.x=xNew
pieceToMove.y=yNew
self.boardArray[xNew][yNew] = pieceToMove
self.whoMoved = pieceToMove.faction
self.boardArray[xCurrent][yCurrent] = None
if type(self.boardArray[xNew][yNew]) == piecePawn:
self.pawnPromotion(xNew, yNew)
def pawnPromotion(self, xNew, yNew):
if yNew == 7:
self.boardArray[xNew][yNew] = pieceQueen(xNew, yNew, factionColor.FACTION_WHITE)
elif yNew == 0:
self.boardArray[xNew][yNew] = pieceQueen(xNew, yNew, factionColor.FACTION_BLACK)
def getPiece(self,coordHorizontal, coordVert):
return self.boardArray[coordHorizontal][coordVert]
def PRINTBOARD(self):
for i in range(8):
print("")
for j in range(8):
piece = self.getPiece(j, 7 - i)
if type(piece) is piecePawn:
if piece.faction is factionColor.FACTION_WHITE:
print("\033[0;37;49mP", end="")
else:
print("\033[0;30;49mP", end="")
elif type(piece) is pieceKing:
if piece.faction is factionColor.FACTION_WHITE:
print("\033[0;37;49mK", end="")
else:
print("\033[0;30;49mK", end="")
elif type(piece) is pieceBishop:
if piece.faction is factionColor.FACTION_WHITE:
print("\033[0;37;49mB", end="")
else:
print("\033[0;30;49mB", end="")
elif type(piece) is pieceKnight:
if piece.faction is factionColor.FACTION_WHITE:
print("\033[0;37;49mN", end="")
else:
print("\033[0;30;49mN", end="")
elif type(piece) is pieceRook:
if piece.faction is factionColor.FACTION_WHITE:
print("\033[0;37;49mR", end="")
else:
print("\033[0;30;49mR", end="")
elif type(piece) is pieceQueen:
if piece.faction is factionColor.FACTION_WHITE:
print("\033[0;37;49mQ",end="")
else:
print("\033[0;30;49mQ", end="")
else:
print("\033[0;33;49m-", end="")
def isVictory(self):
whiteExist = False
blackExist = False
for i in range(8):
for j in range(8):
piece = self.getPiece(i, j)
if piece is None:
continue
if type(piece) == pieceKing and piece.faction == factionColor.FACTION_WHITE:
whiteExist = True
if type(piece) == pieceKing and piece.faction == factionColor.FACTION_BLACK:
blackExist = True
if not whiteExist:
print("Black victory!")
return True
if not blackExist:
print("White victory!")
return True
return False
#function checks if square (x,y) is attacked by faction.
def tileIsAttacked(self, x, y):
square=self.boardArray[x][y]
for i in range(8):
for j in range(8):
if self.boardArray[i][j]==None:
continue
if self.boardArray[i][j].faction == square.faction:
continue
if self.boardArray[i][j].faction is not square.faction:
if self.getPiece(i, j).checkAttack(self.boardArray, x, y):
return True
else:
continue
return False
#function denermines the position of the king belonging to a faction
def getKing(self, faction):
for i in range(8):
for j in range(8):
king = self.getPiece(i, j)
if (type(king) == pieceKing) and (king.faction == faction):
return i, j
#function checks whether a king from a faction is under attack
def isChecked(self, faction):
king=self.getKing(faction)
if self.tileIsAttacked(king[0], king[1]):
return True
else:
return False
#function checks if tile is attacked by given faction
def tileIsChecked(self, x, y, faction):
for i in range(8):
for j in range(8):
if self.boardArray[i][j] is None:
continue
if self.boardArray[i][j].faction == faction:
if self.getPiece(i, j).checkAttack(self.boardArray, x, y):
return True
else:
continue
return False
#function which gets opposite faction to the piece's faction
def getOppositeFaction(self, faction):
if faction == factionColor.FACTION_BLACK:
return factionColor.FACTION_WHITE
else:
return factionColor.FACTION_BLACK
# function which undoes move
def undoMove(self):
if self.lastMoves:
lastMove = self.lastMoves[-1]
movingPiece = lastMove[0]
startingX = lastMove[1]
startingY = lastMove[2]
targetPiece = lastMove[3]
targetX = lastMove[4]
targetY = lastMove[5]
self.lastMoves.pop()
movingPiece.x = startingX
movingPiece.y = startingY
self.boardArray[startingX][startingY] = movingPiece
self.whoMoved = self.getOppositeFaction(self.whoMoved)
if targetPiece is not None:
targetPiece.x = targetX
targetPiece.y = targetY
self.boardArray[targetX][targetY] = targetPiece
else:
self.boardArray[targetX][targetY] = None
def factionLost(self, faction):
factionExist = False
for i in range(8):
for j in range(8):
piece = self.getPiece(i, j)
if piece is None:
continue
if type(piece) == pieceKing and piece.faction == faction:
factionExist = True
if not factionExist:
print("Victory!")
return True
return False