-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.py
More file actions
283 lines (265 loc) · 14.9 KB
/
Copy pathBoard.py
File metadata and controls
283 lines (265 loc) · 14.9 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
import Enums, Piece, Cell
from move_logic import *
from game_events import *
import pygame
import os
class board:
selectedPiece = None
pawnInPromotion = None
possibleMoves = []
blackDeadPieces = []
whiteDeadPieces = []
whiteTurn = True
whitePlayerMoved = False
gameOver = None
pawnToBePromoted = None
def __init__(self, tilesNumber,color1, color2, tileSize, xOffset, yOffset, window, width, height):
self.tilesNumber = tilesNumber
self.window = window
self.color1 = color1
self.color2 = color2
self.xOffset = xOffset
self.yOffset = yOffset
self.tileSize = tileSize
self.width = width
self.height = height
self.pawnPromotion = False
self.cells = [[Cell.cell(xOffset + x*tileSize,yOffset + y*tileSize) for x in range(8)] for y in range(8)]
def DrawBoard(self, window):
for i in range(self.tilesNumber):
for g in range(self.tilesNumber):
if((i + g) % 2 == 0):
pygame.draw.rect(window, self.color1, (g*self.tileSize +self.xOffset, i*self.tileSize+self.yOffset,self.tileSize, self.tileSize))
else:
pygame.draw.rect(window, self.color2, (g*self.tileSize +self.xOffset, i*self.tileSize+self.yOffset,self.tileSize, self.tileSize))
for deadPiece in self.blackDeadPieces:
deadPiece.image = pygame.transform.scale(deadPiece.image, (40, 40))
index = self.blackDeadPieces.index(deadPiece)
if(index > 12):
window.blit(deadPiece.image, (self.xOffset - 120, self.yOffset + (index - 13) * 50))
else:
window.blit(deadPiece.image, (self.xOffset - 60, self.yOffset + index * 50 ))
for deadPiece in self.whiteDeadPieces:
deadPiece.image = pygame.transform.scale(deadPiece.image, (40, 40))
index = self.whiteDeadPieces.index(deadPiece)
if(index > 12):
window.blit(deadPiece.image, (self.xOffset + self.tilesNumber*self.tileSize + 120,self.yOffset + (index - 13) * 50 ))
else:
window.blit(deadPiece.image, (self.xOffset + self.tilesNumber*self.tileSize + 20,self.yOffset + index * 50 ))
def InitializePieces(self):
BLACK, WHITE = Enums.Color.BLACK, Enums.Color.WHITE
PAWN = Enums.PieceType.PAWN
BISHOP = Enums.PieceType.BISHOP
KING = Enums.PieceType.KING
QUEEN = Enums.PieceType.QUEEN
ROOK = Enums.PieceType.ROOK
KNIGHT = Enums.PieceType.KNIGHT
black_bishp_img = pygame.image.load(os.path.join('assets','black-bishop.png'))
black_king_img = pygame.image.load(os.path.join('assets','black-king.png'))
black_knight_img = pygame.image.load(os.path.join('assets','black-knight.png'))
black_pawn_img = pygame.image.load(os.path.join('assets','black-pawn.png'))
black_queen_img = pygame.image.load(os.path.join('assets','black-queen.png'))
black_rook_img = pygame.image.load(os.path.join('assets','black-rook.png'))
white_pawn_img = pygame.image.load(os.path.join('assets','white-pawn.png'))
white_bishp_img = pygame.image.load(os.path.join('assets','white-bishop.png'))
white_king_img = pygame.image.load(os.path.join('assets','white-king.png'))
white_knight_img = pygame.image.load(os.path.join('assets','white-knight.png'))
white_queen_img = pygame.image.load(os.path.join('assets','white-queen.png'))
white_rook_img = pygame.image.load(os.path.join('assets','white-rook.png'))
black_rook_left = Piece.piece(BLACK, ROOK, True, False, black_rook_img)
self.cells[0][0].SetPiece(black_rook_left)
black_rook_right = Piece.piece(BLACK, ROOK, True, False, black_rook_img)
self.cells[0][7].SetPiece(black_rook_right)
black_knight_left = Piece.piece(BLACK, KNIGHT, True, False, black_knight_img)
self.cells[0][1].SetPiece(black_knight_left)
black_rook_left.SetInitialCor(0, 0)
black_knight_right = Piece.piece(BLACK, KNIGHT, True, False, black_knight_img)
self.cells[0][6].SetPiece(black_knight_right)
black_rook_right.SetInitialCor(0, 7)
black_bishop_left = Piece.piece(BLACK, BISHOP, True, False, black_bishp_img)
self.cells[0][2].SetPiece(black_bishop_left)
black_bishop_right = Piece.piece(BLACK, BISHOP, True, False, black_bishp_img)
self.cells[0][5].SetPiece(black_bishop_right)
black_queen = Piece.piece(BLACK, QUEEN, True, False, black_queen_img)
self.cells[0][3].SetPiece(black_queen)
black_king = Piece.piece(BLACK, KING, True, False, black_king_img)
self.cells[0][4].SetPiece(black_king)
black_king.SetInitialCor(0, 4)
black_pawn1 =Piece.piece(BLACK, PAWN, True, False, black_pawn_img)
self.cells[1][0].SetPiece(black_pawn1)
black_pawn2 =Piece.piece(BLACK, PAWN, True, False, black_pawn_img)
self.cells[1][1].SetPiece(black_pawn2)
black_pawn3 =Piece.piece(BLACK, PAWN, True, False, black_pawn_img)
self.cells[1][2].SetPiece(black_pawn3)
black_pawn4 =Piece.piece(BLACK, PAWN, True, False, black_pawn_img)
self.cells[1][3].SetPiece(black_pawn4)
black_pawn5 =Piece.piece(BLACK, PAWN, True, False, black_pawn_img)
self.cells[1][4].SetPiece(black_pawn5)
black_pawn6 =Piece.piece(BLACK, PAWN, True, False, black_pawn_img)
self.cells[1][5].SetPiece(black_pawn6)
black_pawn7 =Piece.piece(BLACK, PAWN, True, False, black_pawn_img)
self.cells[1][6].SetPiece(black_pawn7)
black_pawn8 =Piece.piece(BLACK, PAWN, True, False, black_pawn_img)
self.cells[1][7].SetPiece(black_pawn8)
white_rook_left = Piece.piece(WHITE, ROOK, True, False, white_rook_img)
self.cells[7][0].SetPiece(white_rook_left)
white_rook_left.SetInitialCor(7, 0)
white_rook_right = Piece.piece(WHITE, ROOK, True, False, white_rook_img)
self.cells[7][7].SetPiece(white_rook_right)
white_rook_right.SetInitialCor(7, 7)
white_knight_left = Piece.piece(WHITE, KNIGHT, True, False, white_knight_img)
self.cells[7][1].SetPiece(white_knight_left)
white_knight_right = Piece.piece(WHITE, KNIGHT, True, False, white_knight_img)
self.cells[7][6].SetPiece(white_knight_right)
white_bishop_left = Piece.piece(WHITE, BISHOP, True, False, white_bishp_img)
self.cells[7][2].SetPiece(white_bishop_left)
white_bishop_right = Piece.piece(WHITE, BISHOP, True, False, white_bishp_img)
self.cells[7][5].SetPiece(white_bishop_right)
white_queen = Piece.piece(WHITE, QUEEN, True, False, white_queen_img)
self.cells[7][3].SetPiece(white_queen)
white_king = Piece.piece(WHITE, KING, True, False, white_king_img)
self.cells[7][4].SetPiece(white_king)
white_king.SetInitialCor(7, 4)
white_pawn1 =Piece.piece(WHITE, PAWN, True, False, white_pawn_img)
self.cells[6][0].SetPiece(white_pawn1)
white_pawn2 =Piece.piece(WHITE, PAWN, True, False, white_pawn_img)
self.cells[6][1].SetPiece(white_pawn2)
white_pawn3 =Piece.piece(WHITE, PAWN, True, False, white_pawn_img)
self.cells[6][2].SetPiece(white_pawn3)
white_pawn4 =Piece.piece(WHITE, PAWN, True, False, white_pawn_img)
self.cells[6][3].SetPiece(white_pawn4)
white_pawn5 =Piece.piece(WHITE, PAWN, True, False, white_pawn_img)
self.cells[6][4].SetPiece(white_pawn5)
white_pawn6 =Piece.piece(WHITE, PAWN, True, False, white_pawn_img)
self.cells[6][5].SetPiece(white_pawn6)
white_pawn7 =Piece.piece(WHITE, PAWN, True, False, white_pawn_img)
self.cells[6][6].SetPiece(white_pawn7)
white_pawn8 =Piece.piece(WHITE, PAWN, True, False, white_pawn_img)
self.cells[6][7].SetPiece(white_pawn8)
def setSelectedPiece(self, x, y, piece):
self.cells[y][x].pieceInCell.previousX = y
self.cells[y][x].pieceInCell.previousY = x
self.selectedPiece = piece
self.selectedPiece.possibleMoves.clear()
moves[piece.type](x, y, self.selectedPiece, self)
CheckForCastleMoves(self.selectedPiece, x, y, self.cells)
if(piece.type == Enums.PieceType.KING):
GetCheckMates(piece, x, y, self)
def DrawPieces(self,window, pngOffsets):
for cell in self.cells:
for eachCell in cell:
if(eachCell.pieceInCell != None):
eachCell.DrawPiece(window, pngOffsets)
if(self.selectedPiece != None):
for cell in self.selectedPiece.possibleMoves:
found = False
if len(self.selectedPiece.invalidMoves) == 0:
self.DrawMove(window, cell.x, cell.y)
else:
for move in self.selectedPiece.invalidMoves:
if ((move[0] == ((cell.y - self.yOffset) // self.tileSize) and
move[1] == (cell.x - self.xOffset) // self.tileSize)):
found = True
if not found:
self.DrawMove(window, cell.x, cell.y)
def DrawMove(self, window, x, y):
pygame.draw.rect(window, (200, 0, 0), (x + 2, y + 2, 76, 76), 4)
def HandlMovement(self, position):
# x -> col, y-> row
x = (position[0] - self.xOffset) // self.tileSize
y = (position[1] - self.yOffset) // self.tileSize
if(y in range(8) and x in range(8)):
inCell = self.cells[y][x].pieceInCell
# if chosen cell is not empty
if(inCell != None):
if(self.selectedPiece != None):
# changing the selected piece
if(self.cells[y][x].pieceInCell.color == self.selectedPiece.color and
not self.selectedPiece.canCastle):
self.setSelectedPiece(x, y, self.cells[y][x].pieceInCell)
# moving the selected piece to a valid position
print(self.selectedPiece.possibleMoves)
for cell in self.selectedPiece.possibleMoves:
if (y == ((cell.y - self.yOffset)//self.tileSize)) and (x == ((cell.x - self.xOffset)//self.tileSize)):
# kill enemy
if(self.cells[y][x].pieceInCell.color != self.selectedPiece.color):
if(self.selectedPiece.color == Enums.Color.WHITE):
self.blackDeadPieces.append(self.cells[y][x].pieceInCell)
else:
self.whiteDeadPieces.append(self.cells[y][x].pieceInCell)
self.gameOver = CheckForGameOver(self.cells[y][x].pieceInCell)
self.MovePiece(y, x)
# casteling
else:
if(self.selectedPiece.canCastle):
self.CastleMove(y, x)
# selecting a piece
else:
if((self.whiteTurn and inCell.color == Enums.Color.WHITE) or (not self.whiteTurn and inCell.color == Enums.Color.BLACK)):
self.setSelectedPiece(x, y, inCell)
else:
# a piece is selected and its time to move it
if(self.selectedPiece != None):
for cell in self.selectedPiece.possibleMoves:
if (y == ((cell.y - self.yOffset)//self.tileSize)) and (x == ((cell.x - self.xOffset)//self.tileSize)):
# move
canMove = True
if(len(self.selectedPiece.invalidMoves) == 0):
self.MovePiece(y, x)
else:
for move in self.selectedPiece.invalidMoves:
if (move[0] == y and move[1] == x):
canMove = False
if canMove:
self.MovePiece(y, x)
def MovePiece(self, y, x):
self.cells[y][x].pieceInCell = self.selectedPiece
self.cells[self.selectedPiece.previousX][self.selectedPiece.previousY].pieceInCell = None
if(self.selectedPiece.type == Enums.PieceType.PAWN):
CheckForPawnPromoption(self.selectedPiece, y, self)
if self.pawnPromotion:
self.pawnToBePromoted = self.selectedPiece
CheckForPawnPromoption(self.selectedPiece, y, self)
if(self.pawnPromotion):
self.pawnInPromotion = self.selectedPiece
self.pawnColor = self.selectedPiece.color
self.Move()
def Move(self):
if(self.whiteTurn):
self.whiteTurn = False
else:
self.whiteTurn = True
self.selectedPiece.Moved()
self.selectedPiece = None
def CastleMove(self, y, x):
if self.selectedPiece.type == Enums.PieceType.KING:
# king casteling to the right
if x > self.selectedPiece.previousY:
self.cells[y][7].pieceInCell.Moved()
self.cells[y][7].pieceInCell.canCastle = False
self.cells[y][self.selectedPiece.kingCastleRightX].pieceInCell = self.selectedPiece
self.cells[y][self.selectedPiece.rightRookCastleX].pieceInCell = self.cells[y][7].pieceInCell
self.cells[y][7].pieceInCell = None
# king casteling to the left
else:
self.cells[y][0].pieceInCell.Moved()
self.cells[y][0].pieceInCell.canCastle = False
self.cells[y][self.selectedPiece.kingCastleLeftX].pieceInCell = self.selectedPiece
self.cells[y][self.selectedPiece.leftRookCastleX].pieceInCell = self.cells[y][0].pieceInCell
self.cells[y][0].pieceInCell = None
elif self.selectedPiece.type == Enums.PieceType.ROOK:
self.cells[y][4].pieceInCell.Moved()
self.cells[y][4].pieceInCell.canCastle = False
# left rook casteling
if x > self.selectedPiece.previousY:
self.cells[y][self.selectedPiece.leftRookCastleX].pieceInCell = self.selectedPiece
self.cells[y][self.selectedPiece.kingCastleLeftX].pieceInCell = self.cells[y][4].pieceInCell
self.cells[y][4].pieceInCell = None
# right rook casteling
else:
self.cells[y][self.selectedPiece.rightRookCastleX].pieceInCell = self.selectedPiece
self.cells[y][self.selectedPiece.kingCastleRightX].pieceInCell = self.cells[y][4].pieceInCell
self.cells[y][4].pieceInCell = None
self.cells[self.selectedPiece.previousX][self.selectedPiece.previousY].pieceInCell = None
self.selectedPiece.canCastle = False
self.Move()