-
Notifications
You must be signed in to change notification settings - Fork 0
/
grid.py
288 lines (274 loc) · 11.6 KB
/
grid.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
import pygame
from pygame.locals import *
from pieces import Pieces
class Map():
def __init__(self, dimm):
self.ended = False
self.turn = False #false = white turn true black turn
self.moving = False
self.pieceSurfaces = [
pygame.Surface((1,1)),
pygame.transform.scale(pygame.image.load("img/whiteKing.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/whiteQueen.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/whiteRook.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/whiteBishop.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/whiteKnight.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/whitePawn.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/blackKing.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/blackQueen.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/blackRook.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/blackBishop.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/blackKnight.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8))),
pygame.transform.scale(pygame.image.load("img/blackPawn.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8)))
]
self.movingI = -1
self.movingJ = -1
self.frameSurface = pygame.transform.scale(pygame.image.load("img/frame.png").convert_alpha(), (int(dimm[0]/8), int(dimm[1]/8)))
self.circleSurface = pygame.transform.scale(pygame.image.load("img/point.png").convert_alpha(), (int(dimm[0]/32), int(dimm[1]/32)))
self.greenSquare = pygame.Surface((int(dimm[0]/8), int(dimm[1]/8)))
self.greenSquare.fill((119, 149, 86))
self.yellowSquare = pygame.Surface((int(dimm[0]/8), int(dimm[1]/8)))
self.yellowSquare.fill((235, 236, 208))
self.pieceSurfaces[0].set_alpha(0)
self.circleSurface.set_alpha(70)
self.grid = [[[0, Pieces.NONE, 0] for x in range(8)] for y in range(8)]
for i in range(0,8):
for j in range(0,8):
if (i+j)%2 == 0:
self.grid[i][j][0] = 1
self.grid[0][6][1] = Pieces.W_PAWN
self.grid[1][6][1] = Pieces.W_PAWN
self.grid[2][6][1] = Pieces.W_PAWN
self.grid[3][6][1] = Pieces.W_PAWN
self.grid[4][6][1] = Pieces.W_PAWN
self.grid[5][6][1] = Pieces.W_PAWN
self.grid[6][6][1] = Pieces.W_PAWN
self.grid[7][6][1] = Pieces.W_PAWN
self.grid[0][1][1] = Pieces.B_PAWN
self.grid[1][1][1] = Pieces.B_PAWN
self.grid[2][1][1] = Pieces.B_PAWN
self.grid[3][1][1] = Pieces.B_PAWN
self.grid[4][1][1] = Pieces.B_PAWN
self.grid[5][1][1] = Pieces.B_PAWN
self.grid[6][1][1] = Pieces.B_PAWN
self.grid[7][1][1] = Pieces.B_PAWN
self.grid[0][7][1] = Pieces.W_ROOK
self.grid[1][7][1] = Pieces.W_KNIGHT
self.grid[2][7][1] = Pieces.W_BISHOP
self.grid[3][7][1] = Pieces.W_QUEEN
self.grid[4][7][1] = Pieces.W_KING
self.grid[5][7][1] = Pieces.W_BISHOP
self.grid[6][7][1] = Pieces.W_KNIGHT
self.grid[7][7][1] = Pieces.W_ROOK
self.grid[0][0][1] = Pieces.B_ROOK
self.grid[1][0][1] = Pieces.B_KNIGHT
self.grid[2][0][1] = Pieces.B_BISHOP
self.grid[3][0][1] = Pieces.B_QUEEN
self.grid[4][0][1] = Pieces.B_KING
self.grid[5][0][1] = Pieces.B_BISHOP
self.grid[6][0][1] = Pieces.B_KNIGHT
self.grid[7][0][1] = Pieces.B_ROOK
def possibleMove(self, i, j, team):
if (i >= 8 or i < 0 or j >= 8 or j < 0):
return False
if (self.grid[i][j][1].value >= 1 and self.grid[i][j][1].value <= 6 and not team) or (self.grid[i][j][1].value > 6 and team):
return False
return True
def calculatePossibleMoves(self, i, j, team):
if self.grid[i][j][1].value == 1 or self.grid[i][j][1].value == 7:
if self.possibleMove(i+1, j, team ):
self.mark(i+1, j)
if self.possibleMove(i+1, j+1, team ):
self.mark(i+1, j+1)
if self.possibleMove(i, j+1, team ):
self.mark(i,j+1)
if self.possibleMove(i-1, j+1, team ):
self.mark(i-1,j+1)
if self.possibleMove(i+1, j-1, team ):
self.mark(i+1,j-1)
if self.possibleMove(i, j-1, team ):
self.mark(i, j-1)
if self.possibleMove(i-1, j, team ):
self.mark(i-1,j)
if self.possibleMove(i-1, j-1, team ):
self.mark(i-1,j-1)
elif self.grid[i][j][1].value == 2 or self.grid[i][j][1].value == 8:
self.evaluateIrect(i, j, team)
self.evaluateJrect(i, j, team)
self.evaluateInegrect(i, j, team)
self.evaluateJnegrect(i, j, team)
self.evaluateIJdiag(i , j, team)
self.evaluateInegJdiag(i, j, team)
self.evaluateIJnegdiag(i, j, team)
self.evaluateInegJnegdiag(i, j, team)
elif self.grid[i][j][1].value == 3 or self.grid[i][j][1].value == 9:
self.evaluateIrect(i, j, team)
self.evaluateInegrect(i, j, team)
self.evaluateJrect(i, j, team)
self.evaluateJnegrect(i, j, team)
elif self.grid[i][j][1].value == 4 or self.grid[i][j][1].value == 10:
self.evaluateIJdiag(i, j, team)
self.evaluateInegJdiag(i, j, team)
self.evaluateInegJnegdiag(i, j, team)
self.evaluateIJnegdiag(i, j, team)
elif self.grid[i][j][1].value == 5 or self.grid[i][j][1].value == 11:
if self.possibleMove(i+2, j+1, team):
self.mark(i+2, j+1)
if self.possibleMove(i+2, j-1, team):
self.mark(i+2, j-1)
if self.possibleMove(i-2, j+1, team):
self.mark(i-2, j+1)
if self.possibleMove(i-2, j-1, team):
self.mark(i-2, j-1)
if self.possibleMove(i+1, j+2, team):
self.mark(i+1, j+2)
if self.possibleMove(i-1, j+2, team):
self.mark(i-1, j+2)
if self.possibleMove(i+1, j-2, team):
self.mark(i+1, j-2)
if self.possibleMove(i-1, j-2, team):
self.mark(i-1, j-2)
elif self.grid[i][j][1].value == 6:
can_move_second = False
if self.empty(i, j-1):
can_move_second = True
self.mark(i, j-1)
if self.grid[i-1][j-1][1].value > 6:
self.mark(i-1, j-1)
if self.grid[i+1][j-1][1].value > 6:
self.mark(i+1, j-1)
if j == 6 and can_move_second:
if self.empty(i, j-2):
self.mark(i, j-2)
elif self.grid[i][j][1].value == 12:
can_move_second = False
if self.empty(i, j+1):
can_move_second = True
self.mark(i, j+1)
if self.grid[i-1][j+1][1].value <= 6 and self.grid[i-1][j+1][1].value > 0:
self.mark(i-1, j+1)
if self.grid[i+1][j+1][1].value <= 6 and self.grid[i+1][j+1][1].value > 0:
self.mark(i+1, j+1)
if j == 1 and can_move_second:
if self.empty(i, j+2):
self.mark(i, j+2)
def evaluateIrect(self, i, j, team):
b = True
while b:
i+=1
if self.possibleMove(i, j, team):
self.mark(i, j)
if self.grid[i][j][1].value > 0:
b = False
else:
b = False
def evaluateInegrect(self,i, j, team):
b = True
while b:
i-=1
if self.possibleMove(i, j, team):
self.mark(i, j)
if self.grid[i][j][1].value > 0:
b = False
else:
b = False
def evaluateJrect(self,i, j, team):
b = True
while b:
j+=1
if self.possibleMove(i, j, team):
self.mark(i, j)
if self.grid[i][j][1].value > 0:
b = False
else:
b = False
def evaluateJnegrect(self,i, j, team):
b = True
while b:
j-=1
if self.possibleMove(i, j, team):
self.mark(i, j)
if self.grid[i][j][1].value > 0:
b = False
else:
b = False
def evaluateIJdiag(self,i, j, team):
b = True
while b:
j+=1
i+=1
if self.possibleMove(i, j, team):
self.mark(i, j)
if self.grid[i][j][1].value > 0:
b = False
else:
b = False
def evaluateIJnegdiag(self,i, j, team):
b = True
while b:
j-=1
i+=1
if self.possibleMove(i, j, team):
self.mark(i, j)
if self.grid[i][j][1].value > 0:
b = False
else:
b = False
def evaluateInegJdiag(self,i,j, team):
b = True
while b:
j+=1
i-=1
if self.possibleMove(i, j, team):
self.mark(i, j)
if self.grid[i][j][1].value > 0:
b = False
else:
b = False
def evaluateInegJnegdiag(self,i,j, team):
b = True
while b:
j-=1
i-=1
if self.possibleMove(i, j, team):
self.mark(i, j)
if self.grid[i][j][1].value > 0:
b = False
else:
b = False
def click(self, i, j):
if ((not self.turn and self.grid[i][j][1].value >= 1 and self.grid[i][j][1].value <= 6) or (self.turn and self.grid[i][j][1].value > 6)) and not self.moving:
self.grid[i][j][2] = 1
self.calculatePossibleMoves(i, j, self.turn)
self.moving = True
self.movingI = i
self.movingJ = j
elif self.grid[i][j][2] > 0 and self.moving and (self.movingI, self.movingJ) != (i, j):
if self.grid[i][j][1] == Pieces.W_KING or self.grid[i][j][1] == Pieces.B_KING:
self.ended = True
self.grid[i][j][1] = self.grid[self.movingI][self.movingJ][1]
self.grid[self.movingI][self.movingJ][1] = Pieces.NONE
self.movingI = -1
self.movingJ = -1
self.moving = False
self.turn = not self.turn
self.clear_board_markers()
else:
self.movingI = -1
self.movingJ = -1
self.moving = False
self.clear_board_markers()
def mark(self, i, j):
if self.grid[i][j][1].value == 0:
self.grid[i][j][2] = 2
else:
self.grid[i][j][2] = 1
def empty(self, i, j):
if i > 7 or i < 0 or j < 0 or j > 7: return False
if self.grid[i][j][1].value == 0:
return True
return False
def clear_board_markers(self):
for i in self.grid:
for elem in i:
elem[2] = 0