-
Notifications
You must be signed in to change notification settings - Fork 0
/
approach9.py
1531 lines (1438 loc) · 72.6 KB
/
approach9.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
### Variation of 8
from direct.showbase.ShowBase import ShowBase
from panda3d.core import CollisionTraverser, CollisionNode
from panda3d.core import CollisionHandlerQueue, CollisionRay
from panda3d.core import AmbientLight, DirectionalLight, LightAttrib
from panda3d.core import LPoint3, LVector3, BitMask32, LPoint3f
from direct.gui.OnscreenText import OnscreenText
from direct.showbase.DirectObject import DirectObject
from direct.task.Task import Task
def PointAtZ(z, point, vec):
# Not mine
return point + vec * ((z - point.getZ()) / vec.getZ())
# Takes any tuple of 3 numbers and returns a tuple of the round of each number
def roundTuple(t):
t = tuple(t)
a = round(t[0])
b = round(t[1])
c = round(t[2])
return (a, b, c)
# Mapping the key Presed
keyMap = {
"up" : False,
"down" : False,
"left": False,
"right": False,
"u": False,
"d": False,
"enter":False,
"space":False,
"m": False} # m is for mouse false for not using mouse and true for using mouse
# Changes the above mapping of the keys
def updateKeyMap(key):
if not keyMap["m"]:
keyMap[key] = not keyMap[key]
elif key == "m":
keyMap[key] = not keyMap[key]
# Checks whether to tupples are equal
def checkEqTuples(p1,p2):
return p1 == p2
# Change the color format
def color(r,g,b):
return((r/255, g/255, b/255))
class MilleniumChess(ShowBase):
# Forms Board place the pieces
# And draws the selected (floating) square
def formBoardAndPieces(self):
for z in range (1,-2,-1):
count= 1
for x in range(-3,5):
# going to next row
for y in range(13,5,-1):
cord = (x, y+(8 * z), z)
# going throug a constant row
if count % 2 == 0:
square = loader.loadModel("models/square.egg")
self.squares[(cord)] = square
self.squares[(cord[0], cord[1], -1 * cord[2])] = square
self.squares[(cord)].reparentTo(render)
self.squares[(cord)].setPos(cord)
self.squares[(cord)].setColor(0,0,0)
self.recordColor[(cord)] = (0,0,0)
self.recordColor[(cord[0], cord[1], -1 * cord[2])] = (0,0,0)
self.pieces[(cord)] = None
self.pieces[(cord[0], cord[1], -1 * cord[2])] = None
self.sCord[square] = cord
else:
square = loader.loadModel("models/square.egg")
self.squares[(cord)] = square
self.squares[(cord[0], cord[1], -1 * cord[2])] = square
self.squares[(cord)].reparentTo(render)
self.squares[(cord)].setPos(cord)
self.squares[(cord)].setColor(1,1,1)
self.recordColor[(cord)] = (1,1,1)
self.recordColor[(cord[0], cord[1], -1 * cord[2])] = (1,1,1)
self.pieces[(cord)] = None
self.pieces[(cord[0], cord[1], -1 * cord[2])] = None
self.sCord[square] = cord
if y == 12 and z != 0 and z != -1:
piece = loader.loadModel("models/pawn.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(x ,y + (8 * z), z)
self.pieces[(cord)].setColor(1,1,1)
elif y == 7 and z != 0 and z != 1:
piece = loader.loadModel("models/pawn.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(color(150, 75, 0)[0],color(150, 75, 0)[1],color(150, 75, 0)[2])
elif (x, y) in [(-3, 13), (4,13), (-3,6), (4,6)] and z != 0:
if y == 13 and z != -1:
piece = loader.loadModel("models/rook.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(1,1,1)
elif y == 6 and z != 1:
piece = loader.loadModel("models/rook.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(color(150, 75, 0)[0],color(150, 75, 0)[1],color(150, 75, 0)[2])
#self.records["darkRook"] = self.pieces[(cord)]
elif (x, y) in [(-2, 13), (3,13), (-2,6), (3,6)]and z != 0:
if y == 13 and z != -1:
piece = loader.loadModel("models/knight.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(1,1,1)
#self.records["lightKnight"] = self.pieces[(cord)]
elif y == 6 and z != 1:
piece = loader.loadModel("models/knight.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(color(150, 75, 0)[0],
color(150, 75, 0)[1],color(150, 75, 0)[2])
elif (x, y) in [(-1, 13), (2,13), (-1,6), (2,6)]and z != 0:
if y == 13 and z != -1:
piece = loader.loadModel("models/bishop.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(1,1,1)
elif y == 6 and z != 1:
piece = loader.loadModel("models/bishop.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(color(150, 75, 0)[0],
color(150, 75, 0)[1],color(150, 75, 0)[2])
elif (x, y) in [(0, 13), (0,6)]and z != 0:
if y == 13 and z != -1:
piece = loader.loadModel("models/king.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(1,1,1)
self.records["lightKing"] = piece
elif y == 6 and z != 1:
piece = loader.loadModel("models/king.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(color(150, 75, 0)[0],color(150, 75, 0)[1],color(150, 75, 0)[2])
self.records["darkKing"] = piece
elif (x, y) in [(1, 13), (1,6)]and z != 0:
if y == 13 and z != -1:
piece = loader.loadModel("models/queen.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(1,1,1)
elif y == 6 and z != 1:
piece = loader.loadModel("models/queen.egg")
self.pCord[piece] = cord
self.pieces[(cord)] = piece
self.pieces[(cord)].reparentTo(render)
self.pieces[(cord)].setPos(cord)
self.pieces[(cord)].setColor(color(150, 75, 0)[0],
color(150, 75, 0)[1],color(150, 75, 0)[2])
count+= 1
count+= 1
#Takes a piece in path form and returns cords in tuple
def findCords(self, piece):
for key in self.pieces:
if self.pieces[key] == piece:
return key
else:
return False
# REFERENCE: https://www.panda3d.org/
def setupLights(self): # This function sets up some default lighting
ambientLight = AmbientLight("ambientLight")
ambientLight.setColor((.8, .8, .8, 1))
directionalLight = DirectionalLight("directionalLight")
directionalLight.setDirection(LVector3(0, 45, -45))
directionalLight.setColor((0.2, 0.2, 0.2, 1))
render.setLight(render.attachNewNode(directionalLight))
render.setLight(render.attachNewNode(ambientLight))
# Checks whether a square is free or not
# If True then Free else not free
def checkFreeSq(self, square):
if square not in self.pieces:
return True
elif square in self.pieces:
return self.pieces[square] == None
# CALLED every frame
# Changes the position based on blue sq
def update(self, task):
# Changing the camera position
if self.turn == 0:
self.camera.setPos(0.5,-20,25)
self.camera.setHpr(0,-42.5,0)
self.dx = 1
else:
self.camera.setPos(0.5,39,25)
self.camera.setHpr(180,-42.5 ,0)
self.dx = -1
if keyMap["m"]:
self.keyboard = 0
elif keyMap["m"] == False:
self.keyboard = 1
if self.keyboard:
self.updateKeyBoard(task)
else:
self.updateMouse(task)
return task.cont
def updateMouse(self, task):
if self.mouseWatcherNode.hasMouse():
# Get the mouse position
mpos = self.mouseWatcherNode.getMouse()
# Set the position of the ray based on the mouse position
self.pickerRay.setFromLens(self.camNode, mpos.getX(), mpos.getY())
pointOfRay = render.getRelativePoint(camera, self.pickerRay.getOrigin())
# Same thing with the direction of the ray
vectorOfRay = render.getRelativeVector(
camera, self.pickerRay.getDirection())
self.changeCurrent_Z()
point = PointAtZ(self.current_z, pointOfRay, vectorOfRay)
point = roundTuple(point)
if point in self.squares:
self.square.setPos(point[0], point[1], point[2] + 0.01)
if self.piece != None:
self.piece.setPos(point)
return task.cont
def changeCurrent_Z(self):
if self.mouseWatcherNode.hasMouse():
# get the mouse position
mpos = self.mouseWatcherNode.getMouse()
# Set the position of the ray based on the mouse position
self.pickerRay.setFromLens(self.camNode, mpos.getX(), mpos.getY())
pointOfRay = render.getRelativePoint(camera, self.pickerRay.getOrigin())
# Same thing with the direction of the ray
vectorOfRay = render.getRelativeVector(
camera, self.pickerRay.getDirection())
point = PointAtZ(0, pointOfRay, vectorOfRay)
pointY = roundTuple(point)[1]
if self.turn == 0:
if -3 <= pointY <= 5.25:
self.current_z = - 0.99
elif 5.25 < pointY <= 14.25:
self.current_z = 0.01
elif 15 <= pointY <= 23.25:
self.current_z = 1.01
elif self.turn == 1:
if -3 <= pointY <= 5.25:
self.current_z = 1.01
elif 5.25 < pointY <= 14.25:
self.current_z = 0.01
elif 15 <= pointY <= 23.25:
self.current_z = -0.99
pass
def mousePressedLeft(self):
if keyMap["m"]:
if self.select == False:
if self.mouseWatcherNode.hasMouse():
# get the mouse position
mpos = self.mouseWatcherNode.getMouse()
# Set the position of the ray based on the mouse position
self.pickerRay.setFromLens(self.camNode, mpos.getX(), mpos.getY())
pointOfRay = render.getRelativePoint(camera, self.pickerRay.getOrigin())
# Same thing with the direction of the ray
vectorOfRay = render.getRelativeVector(
camera, self.pickerRay.getDirection())
point = PointAtZ(self.current_z, pointOfRay, vectorOfRay)
point = roundTuple(point)
# Check whther the non blue sq is empty or not
if self.checkFreeSq(point):
return
elif self.checkFreeSq(point) == False: # If the square contains something
self.piece = self.pieces[point] # It would same point as the posofsq
self.colorOfPiece = roundTuple(self.piece.getColor())
if self.turn == 1 and self.colorOfPiece == (1, 1, 1):
self.select = True # Seleect thaat piece
self.pieceKey = point # The key needs to be stored for future ref
self.showMoves() # potential check point for pinned
elif self.turn == 0 and self.colorOfPiece != (1, 1, 1):
self.select = True # Seleect thaat piece
self.pieceKey = point # The key needs to be stored for future ref
self.showMoves() # potential check point for pinned
else:
self.piece = None
self.colorOfPiece = None
elif self.select :
if self.mouseWatcherNode.hasMouse():
# get the mouse position
mpos = self.mouseWatcherNode.getMouse()
# Set the position of the ray based on the mouse position
self.pickerRay.setFromLens(self.camNode, mpos.getX(), mpos.getY())
pointOfRay = render.getRelativePoint(camera, self.pickerRay.getOrigin())
# Same thing with the direction of the ray
vectorOfRay = render.getRelativeVector(
camera, self.pickerRay.getDirection())
point = roundTuple(PointAtZ(self.current_z, pointOfRay, vectorOfRay))
if point in self.squares and self.squares[point] != None and roundTuple(self.squares[point].getColor()) == (0, 1, 0):
if point == self.pieceKey:
self.piece.setPos(self.pieceKey)
self.select = False # Now we donot have the piece
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
elif not self.moveCondition(point):
if self.pieces[point] != None:
self.pCord[self.pieces[point]] = (100, 100, 100)
self.pieces[point].setPos(100, 100, 100)
self.pieces[point] = self.piece # Replacing/ placing the piece
self.pCord[self.piece] = point
if point != self.pieceKey:
# If pointnot then the piece will be lost from the dictionary
self.pieces[self.pieceKey] = None
# Change the previous place to None
self.select = False # Now we donot have the piece
print(self.pieceKey)
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
self.checkCheckMate()
self.square.setPos(1, 8, 0.01)
self.switch()
elif "king" in str(self.piece):
if self.pieces[point] != None:
self.pCord[self.pieces[point]] = (100, 100, 100)
self.pieces[point].setPos(100, 100, 100)
self.pieces[point] = self.piece # Replacing/ placing the piece
self.pCord[self.piece] = point
if point != self.pieceKey:
# If not then the piece will be lost from the dictionary
self.pieces[self.pieceKey] = None
# Change the previous place to None
self.select = False # Now we donot have the piece
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
self.switch()
else:
self.piece.setPos(self.pieceKey)
self.select = False # Now we donot have the piece
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
return
else:
self.piece.setPos(self.pieceKey)
self.select = False # Now we donot have the piece
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
def updateKeyBoard(self, task):
if self.turn == 0:
self.camera.setPos(0.5,-20,12)
self.camera.setHpr(0,-22.5,0)
self.dx = 1
else:
self.camera.setPos(0.5,39,25)
self.camera.setHpr(180,-42.5 ,0)
self.dx = -1
posOfSq = self.square.getPos() # The blue square position
# Now start checking which button is pressed and change position accordingly
if keyMap["left"]:
posOfSq.x -= self.dx
if posOfSq.x < -3: # restart blue sq from right
posOfSq.setX(4)
elif posOfSq.x > 4: # restart blue sq from left
posOfSq.setX(-3)
keyMap["left"] = False
elif keyMap["right"]:
posOfSq.x += self.dx
if posOfSq.x > 4: # restart blue sq from left
posOfSq.setX(-3)
elif posOfSq.x < -3: # restart blue sq from right
posOfSq.setX(4)
keyMap["right"] = False
elif keyMap["up"]:
posOfSq.y += self.dx
if self.turn == 0:
if 13 >= posOfSq.y > 5 : # Going to 2nd level in from 1st level
posOfSq.setZ(0.01)
elif 21 >= posOfSq.y > 13 : # Going to 3 level in from 2nd level
posOfSq.setZ(1.01)
elif 5 >= posOfSq.y > -2: # Staying in the first level
posOfSq.setZ(-0.99)
elif posOfSq.y > 21: # Going back to 1st chess board from 3rd board
posOfSq.setY(-2)
posOfSq.setZ(-0.99)
else: # If flipped
if 13 >= posOfSq.y > 5 :
posOfSq.setZ(0.01)
elif 21 >= posOfSq.y > 13 :
posOfSq.setZ(-0.99)
elif 5 >= posOfSq.y > -2:
posOfSq.setZ(1.01)
elif posOfSq.y < -2:
posOfSq.setY(21)
posOfSq.setZ(-0.99)
keyMap["up"] = False
elif keyMap["down"]:
posOfSq.y -= self.dx
if self.turn == 1:
if 13 >= posOfSq.y > 5 : # Going to 2nd level in from 1st level
posOfSq.setZ(0.01)
elif 21 >= posOfSq.y > 13 : # Going to 3 level in from 2nd level
posOfSq.setZ(-0.99)
elif 5 >= posOfSq.y > -2: # Staying in the first level
posOfSq.setZ(1.01)
elif posOfSq.y > 21: # Going back to 1st chess board from 3rd board
posOfSq.setY(-2)
posOfSq.setZ(-0.99)
else: # if flipped
if 13 >= posOfSq.y > 5 :
posOfSq.setZ(0.01)
elif 21 >= posOfSq.y > 13 :
posOfSq.setZ(1.01)
elif 5 >= posOfSq.y > -2:
posOfSq.setZ(-0.99)
elif posOfSq.y < -2:
posOfSq.setY(21)
posOfSq.setZ(1.01)
keyMap["down"] = False
elif keyMap["u"]: # Going through +z cordinate
posOfSq.z += abs(self.dx)
if self.turn == 0:
if 0 < posOfSq.z < 1 and -2 <= posOfSq.y <= 5:
posOfSq.y += 8
elif 1 < posOfSq.z < 2 and 6 <= posOfSq.y <= 13:
posOfSq.y += 8
elif posOfSq.z > 2 and 14 <= posOfSq.y <=21:
posOfSq.y -= 16
posOfSq.z = -0.99
elif self.turn == 1:
if 0 < posOfSq.z < 1 and 14 <= posOfSq.y <= 21:
posOfSq.y -= 8
elif 1 < posOfSq.z < 2 and 6 <= posOfSq.y <= 13:
posOfSq.y -= 8
elif posOfSq.z > 2 and -2 <= posOfSq.y <= 5:
posOfSq.y += 16
posOfSq.z = -0.99
keyMap["u"] = False
elif keyMap["d"]:# Going through -z cordinate
posOfSq.z -= abs(self.dx)
if self.turn == 0:
if 0 < posOfSq.z < 1 and 14 <= posOfSq.y <= 21:
posOfSq.y -= 8
elif -1 < posOfSq.z < 0 and 6 <= posOfSq.y <= 13:
posOfSq.y -= 8
elif posOfSq.z < -1 and -2 <= posOfSq.y <= 5:
posOfSq.y += 16
posOfSq.z = 1.01
elif self.turn == 1:
if 0 < posOfSq.z < 1 and -2 <= posOfSq.y <= 5:
posOfSq.y += 8
elif -1 < posOfSq.z < 0 and 6 <= posOfSq.y <= 13:
posOfSq.y += 8
elif posOfSq.z < -1 and 14 <= posOfSq.y <= 21:
posOfSq.y -= 16
posOfSq.z = 1.01
keyMap["d"] = False
if keyMap["enter"]:
self.enterPressed(posOfSq)
posOfSq = self.square.getPos()
elif keyMap["enter"] == False and self.select:
self.pieces[self.pieceKey].setPos(roundTuple(posOfSq))
self.square.setPos(posOfSq)
if self.state:
return task.cont
else:
return task.done
def enterPressed(self, posOfSq):
cord = roundTuple(posOfSq)
if self.select == False:
# Check whther the non blue sq is empty or not
if self.checkFreeSq(cord):
keyMap["enter"] = False # Does not do any thing if an empty sq is selected
return
elif self.checkFreeSq(cord) == False: # If the square contains something
self.piece = self.pieces[cord] # It would same cord as the posofsq
self.colorOfPiece = roundTuple(self.piece.getColor())
if self.turn == 1 and self.colorOfPiece == (1, 1, 1):
self.select = True # Seleect thaat piece
self.pieceKey = cord # The key needs to be stored for future ref
keyMap["enter"] = False # Reset the keymap
self.showMoves() # potential check point for pinned
elif self.turn == 0 and self.colorOfPiece != (1, 1, 1):
self.select = True # Seleect thaat piece
self.pieceKey = cord # The key needs to be stored for future ref
keyMap["enter"] = False # Reset the keymap
self.showMoves() # potential check point for pinned
else:
keyMap["enter"] = False # Does not do any thing if an empty sq is selected
self.piece = None
self.colorOfPiece = None
return
elif self.select: # When we have the piece in our hand and pressed enter
if cord in self.squares and self.squares[cord] != None and roundTuple(self.squares[cord].getColor()) == (0, 1, 0):
if cord == self.pieceKey:
self.piece.setPos(self.pieceKey)
self.select = False # Now we donot have the piece
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
keyMap["enter"] = False # Reset the keymap
elif not self.moveCondition(cord):
if self.pieces[cord] != None:
self.pCord[self.pieces[cord]] = (100, 100, 100)
self.pieces[cord].setPos(100, 100, 100)
self.pieces[cord] = self.piece # Replacing/ placing the piece
self.pCord[self.piece] = cord
if cord != self.pieceKey:
# If not then the piece will be lost from the dictionary
self.pieces[self.pieceKey] = None
# Change the previous place to None
self.select = False # Now we donot have the piece
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
keyMap["enter"] = False # Reset the keymap
self.checkCheckMate()
self.square.setPos(1, 8, 0.01)
self.switch()
elif "king" in str(self.piece):
if self.pieces[cord] != None:
self.pCord[self.pieces[cord]] = (100, 100, 100)
self.pieces[cord].setPos(100, 100, 100)
self.pieces[cord] = self.piece # Replacing/ placing the piece
self.pCord[self.piece] = cord
if cord != self.pieceKey:
# If not then the piece will be lost from the dictionary
self.pieces[self.pieceKey] = None
# Change the previous place to None
self.select = False # Now we donot have the piece
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
keyMap["enter"] = False # Reset the keymap
self.switch()
else:
self.piece.setPos(self.pieceKey)
self.select = False # Now we donot have the piece
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
keyMap["enter"] = False # Reset the keymap
return
else:
self.piece.setPos(self.pieceKey)
self.select = False # Now we donot have the piece
self.piece = None # No piece is selected
self.pieceKey = None # Then there is no piece key
self.reColorSq()
keyMap["enter"] = False # Reset the keymap
return
# Changes the top most board to lowest and vice versa
def switch(self):
self.turn = int(not self.turn)
for square in self.sCord:
self.squares[self.sCord[square]] = None
square.setZ(square.getZ() * -1)
self.sCord[square] = roundTuple(square.getPos())
self.squares[self.sCord[square]] = square
for piece in self.pCord:
self.pieces[self.pCord[piece]] = None
piece.setZ(piece.getZ() * -1)
self.pCord[piece] = roundTuple(piece.getPos())
self.pieces[self.pCord[piece]] = piece
# Conditions to move a piece
def moveCondition(self, cord):
initialPiece = None
potentialEmpSq = self.pCord[self.piece]
self.pieces[potentialEmpSq] = None
if cord in self.pieces:
initialPiece = self.pieces[cord]
self.pieces[cord] = self.piece
if self.colorOfPiece == (1, 1, 1):
tempPiece = self.records["lightKing"]
threatenedCord = self.pCord[tempPiece]
check = self.checkThreat(threatenedCord)
elif self.colorOfPiece != (1, 1, 1):
tempPiece = self.records["darkKing"]
threatenedCord = self.pCord[tempPiece]
check = self.checkThreat(threatenedCord)
self.pieces[potentialEmpSq] = self.piece
self.pieces[cord] = initialPiece
return check
# Removing the green color
def reColorSq(self):
for square in self.squares:
if self.squares[square] != None:
x = self.recordColor[square][0]
y = self.recordColor[square][1]
z = self.recordColor[square][2]
self.squares[square].setColor(x, y, z)
# Showing the valid moves by green square
def showMoves(self):
validMoves = None
nameOfPiece = str(self.piece).split("/")[1][0:-4]
if nameOfPiece == "knight":
validMoves = self.findKnightMoves()
self.showValidKnightSq(validMoves)
elif nameOfPiece == "rook":
validMoves = self.findValidRookSq()
self.showValidSq(validMoves)
elif nameOfPiece == "bishop":
validMoves = self.findValidBishopSq()
self.showValidSq(validMoves)
elif nameOfPiece == "queen":
self.showValidQueenSq()
elif nameOfPiece == "king":
validMoves = self.findKingMoves()
self.showValidSq(validMoves)
elif nameOfPiece == "pawn":
validMoves = self.findPawnMoves()
self.showValidSq(validMoves)
def findPawnMoves(self, currentSq = None):
if currentSq == None:
currentSq= self.pieceKey
posOfPawn = currentSq
x = posOfPawn[0]
y = posOfPawn[1]
z = posOfPawn[2]
attackingMoves = []
moves = [(x, y, z)]
if self.turn == 0 : # Brown pawn
moves += [(x, y + 1, z)]
moves += [(x, y + 8, z + 1)] + [(x, y + 9, z + 1)]
moves += [(x, y - 8, z - 1)] + [(x, y - 7, z - 1)]
attackingMoves += [(x + 1, y + 1, z)] + [(x - 1, y + 1, z)]
attackingMoves += [(x + 1, y + 9, z + 1)] + [(x - 1, y + 9, z + 1)]
attackingMoves += [(x + 1, y - 9, z - 1)] + [(x - 1, y + 9, z - 1)]
if y == -1:
moves += [(x, y + 2, z)]
moves += [(x, y + 18, z+2)]
moves += [(x, y + 16, z + 2 )]
elif self.turn == 1 :
moves += [(x, y - 1, z)]
moves += [(x, y - 8, z + 1)] + [(x, y - 9, z + 1)]
moves += [(x, y + 8, z - 1)] + [(x, y + 7, z - 1)]
moves += [(x, y + 8, z + 1)] + [(x, y + 7, z + 1)]
attackingMoves += [(x + 1, y - 1, z)] + [(x - 1, y - 1, z)]
attackingMoves += [(x + 1, y + 9, z + 1)] + [(x - 1, y + 9, z + 1)]
attackingMoves += [(x + 1, y - 9, z - 1)] + [(x - 1, y + 9, z - 1)]
if y == 20:
moves += [(x, y - 2, z)]
moves += [(x, y - 18, z + 2)]
moves += [(x, y - 16, z + 2 )]
attackingMoves = list(set(self.removeExtra(attackingMoves)))
if "king" in str(self.piece):
return attackingMoves
for move in attackingMoves:
if move in self.pieces and self.pieces[move] != None :
moves += [move]
moves = list(set(self.removeExtra(moves)))
return moves
def findKingMoves(self, square = None):
if square == None:
square = self.piece.getPos()
posOfKing = roundTuple(square)
x = posOfKing[0]
y = posOfKing[1]
z = posOfKing[2]
moves = []
moves += [(x + 1, y, z)] + [(x - 1, y, z)]
moves += [(x , y+ 1, z)] + [(x , y - 1, z)]
moves += [(x + 1, y + 1, z)] + [(x - 1, y - 1, z)]
moves += [(x - 1, y+ 1, z)] + [(x + 1, y - 1, z)]
if self.turn == 0:
moves += [(x , y - 8, z - 1)] + [(x - 1, y + 8, z + 1)]
moves += [(x + 1, y + 8, z + 1)] + [(x - 1, y + 8, z + 1)]
moves += [(x + 1, y - 8, z - 1)] + [(x - 1, y - 8, z - 1)]
moves += [(x , y+ 9, z+1)] + [(x , y - 9, z-1)]
moves += [(x, y + 8, z + 1)] + [(x, y - 8, z - 1)] + [(x, y, z)]
moves += [(x, y + 7, z + 1)] + [(x, y - 7, z - 1)]
moves += [(x + 1, y + 7, z + 1)] + [(x + 1, y - 7, z - 1)]
moves += [(x - 1, y + 7, z + 1)] + [(x - 1, y - 7, z - 1)]
moves += [(x + 1 , y+ 9, z + 1)] + [(x + 1 , y - 9, z-1)]
moves += [(x - 1 , y+ 9, z + 1)] + [(x - 1 , y - 9, z-1)]
else:
moves += [(x , y + 8, z - 1)] + [(x - 1, y - 8, z + 1)]
moves += [(x + 1, y - 8, z + 1)] + [(x - 1, y - 8, z + 1)]
moves += [(x + 1, y + 8, z - 1)] + [(x - 1, y + 8, z - 1)]
moves += [(x , y - 9, z+1)] + [(x , y + 9, z - 1)]
moves += [(x, y - 8, z + 1)] + [(x, y + 8, z - 1)] + [(x, y, z)]
moves += [(x, y - 7, z + 1)] + [(x, y + 7, z - 1)]
moves += [(x + 1, y - 7, z + 1)] + [(x + 1, y + 7, z - 1)]
moves += [(x - 1, y - 7, z + 1)] + [(x - 1, y + 7, z - 1)]
moves += [(x + 1 , y - 9, z + 1)] + [(x + 1 , y + 9, z-1)]
moves += [(x - 1 , y - 9, z + 1)] + [(x - 1 , y + 9, z-1)]
moves = list(set(self.removeExtra(moves))) # removal of extras squares
for cord in moves[:]:
if self.checkThreat(cord):
moves.remove(cord)
return moves
def checkThreat(self, square):
x = square[0]
y = square[1]
z = square[2]
findPotentialRookPos = self.findValidRookSq(square)
for sq in findPotentialRookPos:
pos = self.sCord[sq]
if pos in self.pieces and self.pieces[pos] != None:
if "rook" in str(self.pieces[pos]) or "queen" in str(self.pieces[pos]):
if roundTuple(self.pieces[pos].getColor()) != self.colorOfPiece and pos != square:
return True
findPotentialBishopPos = self.findValidBishopSq(square)
for sq in findPotentialBishopPos:
pos = self.sCord[sq]
if pos in self.pieces and self.pieces[pos] != None:
if "bishop" in str(self.pieces[pos]) or "queen" in str(self.pieces[pos]):
if roundTuple(self.pieces[pos].getColor()) != self.colorOfPiece and pos != square:
return True
findPotentialKnightPos = self.findKnightMoves(square)
for pos in findPotentialKnightPos:
if pos in self.pieces and self.pieces[pos] != None:
if "knight" in str(self.pieces[pos]) and pos != square:
if roundTuple(self.pieces[pos].getColor()) != self.colorOfPiece:
return True
findPotentialPawnPos = self.findPawnMoves(square)
for pos in findPotentialPawnPos:
if pos in self.pieces and self.pieces[pos] != None:
if "pawn" in str(self.pieces[pos]) :
if roundTuple(self.pieces[pos].getColor()) != self.colorOfPiece:
return True
return False
def checkPiece(self, potentialThreats, piece):
for square in potentialThreats:
if square in self.pieces and piece in str(self.pieces[square]):
if self.colorOfPiece != self.pieces[square].getColor():
return True
else:
return False
def removeExtra(self, moves):
for move in moves[:]:
if move not in self.squares:
moves.remove(move)
elif move in self.pieces and self.pieces[move] != None:
if roundTuple(self.pieces[move].getColor()) == self.colorOfPiece:
if self.piece != self.pieces[move]:
moves.remove(move)
return moves
def showValidSq(self, validMoves):
# Valid moves are in a list of tuples
for move in validMoves:
if isinstance(move, type((1,2))) == False:
move.setColor(0, 1, 0)
elif isinstance(move, type((1,2))):
if self.squares[move] != None:
self.squares[move].setColor(0, 1, 0)
def showValidQueenSq(self):
validMoves = []
validMoves += self.findValidRookSq()
validMoves += self.findValidBishopSq()
self.showValidSq(validMoves)
def findValidBishopSq(self, currentSq = None):
# Changes the color of valid squares of bishop into green
if currentSq == None:
currentSq = self.pieceKey
posOfBishop = currentSq
x = posOfBishop[0]
y = posOfBishop[1]
z = posOfBishop[2]
validSq = []
for i in range(9): # Top right corners
a = x + i
b = y + i
potentialSq = (a, b, z)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
elif self.pieces[potentialSq] == self.piece:
validSq += [self.squares[potentialSq]]
else:
break
for i in range(1, 9): # Top left corners
a = x - i
b = y + i
potentialSq = (a, b, z)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
for i in range(1, 9): # Bottom left corners
a = x - i
b = y - i
potentialSq = (a, b, z)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
for i in range(1, 9): # Bottom right corners
a = x + i
b = y - i
potentialSq = (a, b, z)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
if self.turn == 0:
for i in range(1, 3): # upper top right
a = x + i
b = y + (8 * i) + i
c = z + i
potentialSq = (a, b, c)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
for i in range(1, 3): # upper bottom right
a = x + i
b = y + (8 * i) - i
c = z + i
potentialSq = (a, b, c)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
for i in range(1, 3): # upper top left
a = x - i
b = y + (8 * i) + i
c = z + i
potentialSq = (a, b, c)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
for i in range(1, 3): # upper bottom left
a = x - i
b = y + (8 * i) - i
c = z + i
potentialSq = (a, b, c)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
for i in range(1, 3): # lower top right
a = x + i
b = y - (8 * i) + i
c = z - i
potentialSq = (a, b, c)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
for i in range(1, 3): # lower bottom right
a = x + i
b = y - (8 * i) - i
c = z - i
potentialSq = (a, b, c)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
for i in range(1, 3): # lower top left
a = x - i
b = y - (8 * i) + i
c = z - i
potentialSq = (a, b, c)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
for i in range(1, 3): # lower bottom left
a = x - i
b = y - (8 * i) - i
c = z - i
potentialSq = (a, b, c)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break
elif self.turn == 1:
for i in range(1, 3): # upper top right
a = x + i
b = y - (8 * i) + i
c = z + i
potentialSq = (a, b, c)
if self.checkFreeSq(potentialSq) and potentialSq in self.squares:
validSq += [self.squares[potentialSq]]
elif not self.checkFreeSq(potentialSq) and potentialSq in self.squares:
if roundTuple(self.pieces[potentialSq].getColor()) != self.colorOfPiece:
validSq += [self.squares[potentialSq]]
break
else:
break