This repository has been archived by the owner on Dec 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
376 lines (353 loc) · 11.6 KB
/
logic.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
372
373
374
375
376
def calcuatePos(grid, x, y, xp=None, yp=None, xn=None, yn=None, amt=8):
posLis = []
try:
for ind in range(amt + 1):
# bishop movement conditions
if xp and yp:
posLis.append(grid[y + ind][x + ind])
if xp and yn:
# check if the index is negative cause it will go to the opposite side of the board
if y - ind < 0:
raise IndexError
posLis.append(grid[y - ind][x + ind])
if xn and yp:
if x - ind < 0:
raise IndexError
posLis.append(grid[y + ind][x - ind])
if xn and yn:
if y - ind < 0 or x - ind < 0:
raise IndexError
posLis.append(grid[y - ind][x - ind])
# rook movement conditions
if xp and xn is None and yn is None and yp is None:
posLis.append(grid[y][x + ind])
if yp and yn is None and xp is None and xn is None:
posLis.append(grid[y + ind][x])
if xn and yn is None and yp is None and xp is None:
posLis.append(grid[y][abs(x - ind)])
if yn and xn is None and xp is None and yp is None:
posLis.append(grid[abs(y - ind)][x])
except IndexError:
return posLis
return posLis
# check knight moves its annoying so i made a diffrent function
def calcuatePosSpical(grid, x, y, upr=False, rup=False, rdown=False, downr=False, upl=False, lup=False, ldown=False, downl=False):
posLis = []
try:
if upr:
if y - 2 < 0:
raise IndexError
return grid[y - 2][x + 1]
if rup:
if y - 1 < 0:
raise IndexError
return grid[y - 1][x + 2]
if rdown:
return grid[y + 1][x + 2]
if downr:
return grid[y + 2][x + 1]
if upl:
if y - 2 < 0 or x - 1 < 0:
raise IndexError
return grid[y - 2][x - 1]
if lup:
if y - 1 < 0 or x - 2 < 0:
raise IndexError
return grid[y - 1][x - 2]
if ldown:
if x - 2 < 0:
raise IndexError
return grid[y + 1][x - 2]
if downl:
if x - 1 < 0:
raise IndexError
return grid[y + 2][x - 1]
except IndexError:
return posLis
return posLis
def bishopMove(move, poslist, grid, rank, sq, amtx=8):
picePos = grid[sq][rank]
legalMove = []
nn = []
np = []
pn = []
pp = []
# checking what move it is set the peice list and opposite peice list
if move == "w":
pl = poslist[0]
opl = poslist[1]
elif move == "b":
pl = poslist[1]
opl = poslist[0]
# enumrating all the positions possible the bishop can go
nn = calcuatePos(grid, rank, sq, xn=True, yn=True, amt=amtx)
np = calcuatePos(grid, rank, sq, xn=True, yp=True, amt=amtx)
pn = calcuatePos(grid, rank, sq, xp=True, yn=True, amt=amtx)
pp = calcuatePos(grid, rank, sq, xp=True, yp=True, amt=amtx)
# conditions to limit teh movement depending on peices in the way etc
for i in nn:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
legalMove.append(i)
break
else:
legalMove.append(i)
for i in np:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
legalMove.append(i)
break
else:
legalMove.append(i)
for i in pn:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
legalMove.append(i)
break
else:
legalMove.append(i)
for i in pp:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
legalMove.append(i)
break
else:
legalMove.append(i)
dumthing = []
# removing dupilicates
[dumthing.append(x) for x in legalMove if x not in dumthing]
return dumthing
def rookMove(move, poslist, grid, rank, sq, amtx=8):
picePos = grid[sq][rank]
legalMove = []
nn = []
np = []
pn = []
pp = []
# checking what move it is set the peice list and opposite peice list
if move == "w":
pl = poslist[0]
opl = poslist[1]
elif move == "b":
pl = poslist[1]
opl = poslist[0]
# enumrating all the positions possible the rook can go
nn = calcuatePos(grid, rank, sq, yn=True, amt=amtx)
np = calcuatePos(grid, rank, sq, xn=True, amt=amtx)
pn = calcuatePos(grid, rank, sq, xp=True, amt=amtx)
pp = calcuatePos(grid, rank, sq, yp=True, amt=amtx)
# conditions to limit teh movement depending on peices in the way etc
for i in nn:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
legalMove.append(i)
break
else:
legalMove.append(i)
for i in np:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
legalMove.append(i)
break
else:
legalMove.append(i)
for i in pn:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
legalMove.append(i)
break
else:
legalMove.append(i)
for i in pp:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
legalMove.append(i)
break
else:
legalMove.append(i)
dumthing = []
# removing duplicates
[dumthing.append(x) for x in legalMove if x not in dumthing]
return dumthing
def queenMove(move, piclist, grid, rank, sq):
# using the rook and bishop movement to get all the possible squares
rookm = rookMove(move, piclist, grid, rank, sq)
bishm = bishopMove(move, piclist, grid, rank, sq)
legalMove = []
# making one long list not a list in a list
for i in rookm:
legalMove.append(i)
for i in bishm:
legalMove.append(i)
return legalMove
def kingMove(move, poslist, grid, rank, sq, castel={}):
# same think as queen but limited to 1 so it can only go 1 square away
rookm = rookMove(move, poslist, grid, rank, sq, 1)
bishm = bishopMove(move, poslist, grid, rank, sq, 1)
picePos = grid[sq][rank]
if move == "w":
pl = poslist[0]
opl = poslist[1]
elif move == "b":
pl = poslist[1]
opl = poslist[0]
try:
for caslSid in castel:
if move == "w" and caslSid.isupper():
if caslSid == "K":
kingside = calcuatePos(grid, rank, sq, xp=True, amt=2)
if caslSid == "Q":
queenside = calcuatePos(grid, rank, sq, xn=True, amt=2)
elif move == "b" and caslSid.islower():
if caslSid == "k":
kingside = calcuatePos(grid, rank, sq, xp=True, amt=2)
if caslSid == "q":
queenside = calcuatePos(grid, rank, sq, xn=True, amt=2)
except TypeError:
kingside = []
queenside = []
legalMove = []
for i in rookm:
legalMove.append(i)
for i in bishm:
legalMove.append(i)
try:
for i in kingside:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
break
else:
legalMove.append(i)
except UnboundLocalError:
pass
try:
for i in queenside:
if i in pl and i != picePos:
break
elif i in opl and i != picePos:
break
else:
legalMove.append(i)
except UnboundLocalError:
pass
dumthing = []
# removing empyt lists and dupilicates
[dumthing.append(x) for x in legalMove if x not in dumthing and x != []]
return dumthing
def knightMove(move, poslist, grid, rank, sq):
picepos = grid[sq][rank]
li = [calcuatePosSpical(grid, rank, sq, upr=True),
calcuatePosSpical(grid, rank, sq, rup=True),
calcuatePosSpical(grid, rank, sq, rdown=True),
calcuatePosSpical(grid, rank, sq, downr=True),
calcuatePosSpical(grid, rank, sq, upl=True),
calcuatePosSpical(grid, rank, sq, lup=True),
calcuatePosSpical(grid, rank, sq, ldown=True),
calcuatePosSpical(grid, rank, sq, downl=True)]
if move == "w":
pl = poslist[0]
elif move == "b":
pl = poslist[1]
dumthing = []
thing = []
# conditional to prevent friendly fire
for i in li:
if i in pl and i != picepos:
pass
else:
dumthing.append(i)
for i in dumthing:
if i != []:
thing.append(i)
thing.append(picepos)
return thing
def pawnMove(move, poslist, grid, rank, sq, encrassont):
picepos = grid[sq][rank]
up = []
down = []
leftup = []
rightup = []
leftdown = []
rightdown = []
legalmoves = []
discard = []
dubol = False
if move == "w":
pl = poslist[0]
opl = poslist[1]
elif move == "b":
pl = poslist[1]
opl = poslist[0]
# i know i could do this with nested ifs but that makes it more complicated and i don't want that
if picepos[1] == 750 and move == "w":
dubol = True
elif picepos[1] == 125 and move == "b":
dubol = True
else:
dubol = False
# calcuating the positions the pawn can go, up is for the whight pawns and down is for black pawns
if dubol:
up = calcuatePos(grid, rank, sq, yn=True, amt=2)
down = calcuatePos(grid, rank, sq, yp=True, amt=2)
else:
up = calcuatePos(grid, rank, sq, yn=True, amt=1)
down = calcuatePos(grid, rank, sq, yp=True, amt=1)
# all the taking squares for black and white
leftup = calcuatePos(grid, rank, sq, yn=True, xn=True, amt=1)
rightup = calcuatePos(grid, rank, sq, yn=True, xp=True, amt=1)
leftdown = calcuatePos(grid, rank, sq, yp=True, xn=True, amt=1)
rightdown = calcuatePos(grid, rank, sq, yp=True, xp=True, amt=1)
# who move
if move == "w":
# check for peice in the way
for whyyyy, i in enumerate(up):
if whyyyy == 0:
legalmoves.append(i)
elif i in pl or i in opl:
break
else:
legalmoves.append(i)
# checks if peice is able to be taken
for i in leftup:
if i in opl or i == encrassont:
legalmoves.append(i)
else:
discard.append(i)
for i in rightup:
if i in opl or i == encrassont:
legalmoves.append(i)
else:
discard.append(i)
elif move == "b":
# check for peice in the way
for whyyyy, i in enumerate(down):
if whyyyy == 0:
legalmoves.append(i)
elif i in pl or i in opl:
break
else:
legalmoves.append(i)
# checks if peice is able to be taken
for i in leftdown:
if i in opl or i == encrassont:
legalmoves.append(i)
else:
discard.append(i)
for i in rightdown:
if i in opl or i == encrassont:
legalmoves.append(i)
else:
discard.append(i)
dumthing = []
# removing empyt lists and dupilicates
[dumthing.append(x) for x in legalmoves if x not in dumthing and x != []]
return dumthing, discard