-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathvalidate.jl
1357 lines (1164 loc) · 40.1 KB
/
validate.jl
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
#=validate.jl - replay the game from start to the current move, at every step making
sure that the rules were kept. Print 0 is the game was played cleanly so far and the move ID
of the violating move if it wasn’t. Accepts 1 command line argument,<filename> => database
=#
#=
Only captured targets can be dropped
Dropped cannot capture a piece
It is not promoted instantly
Pawn, knight, lance cannot be dropped on the furthest rank
Pawn cannot be dropped on the same column (y) as another unpromoted pawn
Pawn cannot checkmate only checks
=#
module validateMod
include("board.jl")
include("dParse.jl")
gameType = "standard" #TO DO: don't hard code this
using BM
using SQLite
board = (BM.startGame(gameType)).state
database = ARGS[1] #/path/to/database/file {string}
db = SQLite.DB(database) #Opens the database gamefile
#returns True if move is Valid, False otherwise
function moveValidate(unit, moveType, gameType, team, sourcex, sourcey, targetx, targety, targetx2, targety2)
#edge 0, sourcex or source y are null but it is not drop
if ( (sourcex == -1 || sourcey == -1) && moveType != "drop")
return false
end
#edge 1, source and target are the same
if (sourcex == targetx) && (sourcey == targety)
return false
end
#edge 2, source or target are out of bounds
if (gameType == "standard") #normalShogi
if (~(sourcex >= 1 && sourcex <= 9) || ~(sourcey >= 1 && sourcey <= 9) || ~(targetx >= 1 && targetx <= 9) || ~(targety >= 1 && targety <= 9))
return false
end
elseif (gameType == "minishogi") #miniShogi
if (~(sourcex >= 1 && sourcex <= 5) || ~(sourcey >= 1 && sourcey <= 5) || ~(targetx >= 1 && targetx <= 5) || ~(targety >= 1 && targety <= 5))
return false
end
elseif (gameType == "chu") #chuShogi
if (~(sourcex >= 1 && sourcex <= 12) || ~(sourcey >= 1 && sourcey <= 12) || ~(targetx >= 1 && targetx <= 12) || ~(targety >= 1 && targety <= 12))
return false
end
end
#drop case
if (moveType == "drop")
#Chu Shogi has no drops
if (moveType == "chu")
return false
end
#drop case 1: Dropped cannot capture
if (targetx,targety) in keys(board)
return false
end
#drop case 2: Pawn, Knight, Lance cannot be dropped on the furthest rank
if (unit == "pawn" || unit == "knight" || unit == "lance")
if (team == 'b') #team black
if (targetx == 1)
return false
end
elseif (team =='w') #team white
if (targetx == 9)
return false
end
end
end
#drop case3: It is not promoted
if (unit == "promotedbishop" || unit == "promotedlance" || unit == "promotedknight" || unit == "promotedpawn" || unit == "promotedrook" || unit == "promotedsilver general")
return false
end
#drop case4: Pawns cannot be dropped on the same column as another unpromoted pawn
if (unit == "pawn")
for x in 1:9
if (board[(x,targety)][1] == "pawn") #there is a pawn
return false
end
end
end
#drop case5: Pawns cannot checkmate #missing
#drop case6: Capture targets can be dropped #missing
end
#case 1 bishop
if unit == "bishop"
return bishopValidate(team,sourcex,sourcey,targetx,targety)
#case 1.2 promoted bishop or dragon Horse
elseif unit == "promotedbishop"
return pBishopValidate(team,sourcex,sourcey,targetx,targety)
end
#case 2 gold general
elseif unit == "goldgeneral"
return goldGeneralValidate(team,sourcex,sourcey,targetx,targety)
#case 3 king
elseif unit == "king"
return kingValidate(team,sourcex,sourcey,targetx,targety)
#case 4 lance
elseif unit == "lance"
return lanceValidate(team,sourcex,sourcey,targetx,targety)
#case 4.2 promoted lance
elseif unit == "promotedlance"
#promotedLance moves same as gold general
return goldGeneralValidate(team,sourcex,sourcey,targetx,targety)
#case 5 knight or Kirin
elseif unit == "knight"
return knightValidate(team,sourcex,sourcey,targetx,targety)
elseif unit == "kirin" && gameType == "chu"
return kirinValidate(team,sourcex,sourcey,targetx,targety)
#case 5.2 promoted knight
elseif unit == "promotedknight"
#promotedKnight moves same as gold general
return goldGeneralValidate(team,sourcex,sourcey,targetx,targety)
#case 6 pawn
elseif unit == "pawn"
return pawnValidate(team,sourcex,sourcey,targetx,targety)
#case 6.2 promoted pawn
elseif unit == "promotedpawn"
#promotedPawn moves same as gold general
return goldGeneralValidate(team,sourcex,sourcey,targetx,targety)
#case 7 rook
elseif unit == "rook"
return rookValidate(team,sourcex,sourcey,targetx,targety)
#case 7.2 promoted rook or dragon king
elseif unit == "promotedrook"
return pRookValidate(team,sourcex,sourcey,targetx,targety)
#case 8 silver general
elseif unit == "silvergeneral"
return silverGeneralValidate(team,sourcex,sourcey,targetx,targety)
#case 8.2 promoted silver general or veritcal mover
elseif unit == "promotedsilver general"
#promoted silver general moves same as gold general
return goldGeneralValidate(team,sourcex,sourcey,targetx,targety)
#CHU SHOGI
#case 9 Reverse Chariot promotes to whale
elseif unit == "reversechariot" && gameType == "chu"
return reverseChariotValidate(team,sourcex,sourcey,targetx,targety)
#case 9.2 whale
elseif unit == "whale" && gameType == "chu"
return whaleValidate(team,sourcex,sourcey,targetx,targety)
#case 10 Copper general promotes to side mover
elseif unit == "coppergeneral" && gameType == "chu"
return copperGeneralValidate(team,sourcex,sourcey,targetx,targety)
#case 11 Dragon King promotes to soaring eagle
elseif unit == "dragonking" && gameType == "chu"
return dragonKingValidate(team,sourcex,sourcey,targetx,targety)
#case 11.2 Soaring eagle
elseif unit == "soaringeagle" && gameType == "chu"
return soaringEagleValidate(team,sourcex,sourcey,targetx,targety,targetx2,targety2)
#case 12 Drunk Elephant promotes to prince
elseif unit == "drunkelephant" && gameType == "chu"
return drunkElephantValidate(team,sourcex,sourcey,targetx,targety)
#case 12.2 prince
elseif unit == "prince" && gameType == "chu"
#move like king
return kingValidate(team,sourcex,sourcey,targetx,targety)
#case 13 ferocious leopard promotes bishop
elseif unit == "ferociousleopard" && gameType == "chu"
return ferociousLeopardValidate(team,sourcex,sourcey,targetx,targety)
#case 14 Dragon Horse promotes to horned falcon
elseif unit == "dragonhorse" && gameType == "chu"
return dragonHorseValidate(team,sourcex,sourcey,targetx,targety)
#case 14.2 horned falcon
elseif unit == "hornedfalcon" && gameType == "chu"
return hornedFalconValidate(team,sourcex,sourcey,targetx,targety,targetx2,targety2)
#case 15 Lion
elseif unit == "lion" && gameType == "chu"
return lionValidate(team,sourcex,sourcey,targetx,targety,targetx2,targety2)
#case 16 Side Mover promotes to free boar
elseif unit == "sidemover" && gameType == "chu"
return sideMoverValidate(team,sourcex,sourcey,targetx,targety)
#case 16.2 free boar
elseif unit == "freeboar" && gameType == "chu"
return freeBoarValidate(team,sourcex,sourcey,targetx,targety)
#case 17 Go-between promotes to drunk Elephant
elseif unit == "gobetween" && gameType == "chu"
return goBetweenValidate(team,sourcex,sourcey,targetx,targety)
#case 18 Blind tiger promotes to flying stag
elseif unit == "blindtiger" && gameType == "chu"
return blindTigerValidate(team,sourcex,sourcey,targetx,targety)
#case 18.2 flying stag
elseif unit == "flyingstag" && gameType == "chu"
return flyingStagValidate(team,sourcex,sourcey,targetx,targety)
#case 19 Queen
elseif unit == "queen" && gameType == "chu"
return queenValidate(team,sourcex,sourcey,targetx,targety)
#case 20 Vertical Mover promotes to flying ox
elseif unit == "verticalmover" && gameType == "chu"
return verticalMoverValidate(team,sourcex,sourcey,targetx,targety)
#case 20.2
elseif unit == "flyingox" && gameType == "chu"
return flyingOxValidate(team,sourcex,sourcey,targetx,targety)
#case 21 Phoenix promotes to queen
elseif unit == "phoenix" && gameType == "chu"
return phoenixValidate(team,sourcex,sourcey,targetx,targety)
else
return false #no valid unit
end
end
#moveUpValidate <=== Return true if unit can move veritcally upwards to target ===>
function moveUpValidate(sourcex, sourcey, targetx, targety)
if (sourcey != targety)
return false
end
unitCheck = abs(sourcex - targetx) - 1
x = sourcex - 1
for unit in 1:unitCheck
if !((x,targety) in keys(board)) #Empty
x = x - 1
else
return false
end
end
return true
end #moveUpValidate
#moveDownValidate <=== Return true if unit can move veritcally downwards to target ===>
function moveDownValidate(sourcex, sourcey, targetx, targety)
if (sourcey != targety)
return false
end
unitCheck = abs(sourcex - targetx) - 1
x = sourcex + 1
for unit in 1:unitCheck
if !((x,targety) in keys(board)) #isEmpty
x = x + 1
else
return false
end
end
return true
end #moveDownValidate
#moveLeftValidate <=== Return true if unit can move horizontally left to target ===>
function moveLeftValidate(sourcex, sourcey, targetx, targety)
if (sourcex != targetx)
return false
end
unitCheck = abs(sourcey - targety) - 1
y = sourcey - 1
for unit in 1:unitCheck
if !((targetx,y) in keys(board)) #isEmpty
y = y - 1
else
return false
end
end
return true
end #moveLeftValidate
#moveRightValidate <=== Return true if unit can move horizontally Right to target ===>
function moveRightValidate(sourcex, sourcey, targetx, targety)
if (sourcex != targetx)
return false
end
unitCheck = abs(sourcey - targety) - 1
y = sourcey + 1
for unit in 1:unitCheck
if !((targetx,y) in keys(board))
y = y + 1
else
return false
end
end
return true
end #moveLeftValidate
#moveOrthogonalValidate <=== Return true if unit can NESW directions ===>
function moveOrthogonalValidate(sourcex,sourcey,targetx,targety)
#checks for moves like rook
if (sourcex == targetx) #horizontal
if (sourcey < targety) #move right
if (moveRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #move left
if (moveLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
elseif (sourcey == targety) #vertical
if (sourcex < targetx) #move down
if (moveDownValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #move up
if (moveUpValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
end
return false
end
#moveAdjacentValidate <=== Return true if unit can move 1 UNIT adjacent ===>
function moveAdjacentValidate(sourcex,sourcey,targetx,targety)
if ((abs(sourcex - targetx) == 1) || (abs(sourcex - targetx) == 0))
if ((abs(sourcey - targety) == 1) || (abs(sourcey - targety) == 0))
return true
end
end
return false
end
#diagonalUpLeft <=== Return true if unit can move diagonally upleft to target ===>
function diagonalUpLeftValidate(sourcex, sourcey, targetx, targety)
if (sourcex - targetx) == (sourcey - targety)
if (sourcex > targetx) #moving diagonal up left
unitCheck == abs(sourcex - targetx) - 1
x = sourcex - 1
y = sourcey - 1
for unit in 1:unitCheck
if !((x,y) in keys(board)) #isEmpty
x = x - 1
y = y - 1
else
return false
end
end
return true
end
end
return false
end #diagonalUpLeftValidate
#digonalUpRight <=== Return true if unit can move diagonally upleft to target ===>
function diagonalUpRightValidate(sourcex, sourcey, targetx, targety)
if (sourcex - targetx) == (targety - sourcey)
if (sourcex > targetx) #moving diagonal up right
unitCheck == abs(sourcex - targetx) - 1
x = sourcex - 1
y = sourcey + 1
for unit in 1:unitCheck
if !((x,y) in keys(board)) #isEmpty
x = x - 1
y = y + 1
else
return false
end
end
return true
end
end
return false
end #diagonalUpRightValidate
#diagonalDownLeft <=== Return true if unit can move diagonally downleft to target ===>
function diagonalDownLeftValidate(sourcex, sourcey, targetx, targety)
if (sourcex - targetx) == (targety - sourcey)
if (sourcex < targetx) #moving diagonal down left
unitCheck == abs(sourcex - targetx) - 1
x = sourcex + 1
y = sourcey - 1
for unit in 1:unitCheck
if !((x,y) in keys(board)) #isEmpty
x = x + 1
y = y - 1
else
return false
end
end
return true
end
end
return false
end #diagonalDownLeftValidate
#diagonalDownRight <=== Return true if unit can move diagonally downright to target ===>
function diagonalDownRightValidate(sourcex, sourcey, targetx, targety)
if (sourcex - targetx) == (sourcey - targety)
if (sourcex < targetx) #moving diagonal down left
unitCheck == abs(sourcex - targetx) - 1
x = sourcex + 1
y = sourcey + 1
for unit in 1:unitCheck
if !((x, y) in keys(board)) #isEmpty
x = x + 1
y = y + 1
else
return false
end
end
return true
end
end
return false
end #diagonalDownRightValidate
#moveDiagonalValidate <=== Return true if unit can move diagonally===>
function moveDiagonalValidate(sourcex, sourcey, targetx, targety)
#moves like bishop
if (abs(sourcex - targetx) == abs(sourcey - targety)) #moves diagonally
if (sourcex < targetx) #downwards
if (sourcey < targety) #diagonalDownRight
if (diagonalDownRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #diagonalDownLeft
if (diagonalDownLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
else #upwards
if (sourcey < targety) #diagonalUpRightValidate
if (diagonalUpRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #diagonalUpLeft
if (diagonalUpLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
end
end
return false
end
#case 1 #moves diagonally by any
function bishopValidate(team, sourcex, sourcey, targetx, targety)
if (moveDiagonalValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
#Not moving diagonally
return false
end #function bishopValidate end
#case 1.2 moves like king and bishop
function pBishopValidate(team,sourcex,sourcey,targetx,targety)
#checks if it moves like king
if ((abs(sourcex - targetx) == 1) || (abs(sourcex - targetx) == 0))
if ((abs(sourcey - targety) == 1) || (abs(sourcey - targety) == 0))
return true
end
end
#checks moves like normal bishop
if (moveDiagonalValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
return false #does not move like king or bishop
end #function pBishopValidate end
# case 2 gold general #moves like king but no diagonal backwards
function goldGeneralValidate(team,sourcex,sourcey,targetx,targety)
#check if it moves like king
if ((abs(sourcex - targetx) == 1) || (abs(sourcex - targetx) == 0))
if (abs(sourcey - targety) == 1) || (abs(sourcey - targety) == 0)
if (team == 'b') #team black
if (sourcex - targetx == -1) && (sourcey - targety == 1) #bottom left
return false
elseif (sourcex - targetx == -1) && (sourcey - targety == -1) #bottom right
return false
end
elseif (team == 'w') #team white
if (sourcex - targetx == 1) && (sourcey - targety == 1)#top left
return false
elseif (sourcex - targetx == 1) && (sourcey - targety == -1) #top right
return false
end
else #invalid team
return false
end
#when it is in either team and not in one of the two units
return true
end
end
return false #does not move like goldGeneral
end #goldGeneralValidate
#case 3 king #moves any by 1
function kingValidate(team,sourcex,sourcey,targetx,targety)
if (moveAdjacentValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
return false
end #kingValidate end
#case 4 lance #moves forward by any
#hopping check
function lanceValidate(team,sourcex,sourcey,targetx,targety)
if (team == 'b') #team black
if (targetx < sourcex) && (targety == sourcey)
if (moveUpValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
elseif (team == 'w') #team white
if (targetx > sourcex) && (targety == sourcey)
if (moveDownValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
else
return false #invalid team
end
return false
end #lanceValidate end
#case 4.2 promoted lance #moves same as gold general
#case 5 knight #moves like L
function knightValidate(team,sourcex,sourcey,targetx,targety)
if (team == 'b') #team black
if (sourcex - targetx == 2)
if (abs(targety - sourcey) == 1)
return true
end
end
elseif (team == 'w') #team white
if (sourcex - targetx == -2)
if (abs(targety - sourcey) == 1)
return trues
end
end
else #invalid team
return false
end
return false
end #knigthValidate
#case 5.2 promoted knight #moves same as gold general
#case 6 pawn
function pawnValidate(team,sourcex,sourcey,targetx,targety)
if (team == 'b') #team black
if (targetx == sourcex - 1) && (targety == sourcey)
return true
end
elseif (team == 'w') #team white
if (targetx == sourcex + 1) && (targety == sourcey)
return true
end
else
return false #invalid team
end
return false
end #pawnValidate end
#case 6.2 promotedPawn #moves same as gold general
#case 7 rook
function rookValidate(team,sourcex,sourcey,targetx,targety)
#moving horizontally
if (moveOrthogonalValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
return false
end #rookValidate end
#case 7.2 promotedRook #moves like king or normal rook
function pRookValidate(team,sourcex,sourcey,targetx,targety)
#checks if it moves like king
if ((abs(sourcex - targetx) == 1) || (abs(sourcex - targetx) == 0))
if ((abs(sourcey - targety) == 1) || (abs(sourcey - targety) == 0))
return true
end
end
#checks if it moves like normal rook
#moving horizontally
if (sourcex == targetx) && (sourcey != targety)
#horizontal left
if (sourcey > targety)
if (moveLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
#horizontal right
else #(sourcey < targety)
if (moveRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
#moving vertically
elseif (sourcey == targety) && (sourcex != targetx)
#vertical up
if (sourcex > targetx)
if (moveUpValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
#vertical down
else #(sourcex < targetx)
if (moveDownValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
end
return false
end #pRookValidate end
#case 8 silverGeneral
function silverGeneralValidate(team,sourcex,sourcey,targetx,targety)
res = false
if (team == 'b') #team black
if (abs(sourcex - targetx) == 1)
if (abs(sourcey - targety) == 1 ) #every diagonal corner
res = true
elseif (sourcey == targety) && (sourcex == targetx + 1) #up 1
res = true
end
end
elseif (team == 'w') #team white
#println("white")
if (abs(sourcex - targetx) == 1)
if (abs(sourcey - targety) == 1) #every diagonal corner
res = true
elseif (sourcey == targety) && (sourcex == targetx - 1) #down 1
res = true
end
end
end
return res
end #silverGeneralValidate end
#case 8.2 pSilverGeneral moves same as gold general
#case 9 reverseChariotValidate
function reverseChariotValidate(team,sourcex,sourcey,targetx,targety)
if (sourcey == targety)
if (sourcex > targetx)
if (moveUpValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #sourcex < targety
if (moveDownValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
end
return false
end #reverseChariotValidate end
#case 9.2 whale
function whaleValidate(team,sourcex,sourcey,targetx,targety)
if (sourcey == targety)
#moving up
if (sourcex > targetx)
if (moveUpValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
#moving down
else
if (moveDownValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
elseif (abs(sourcex - targetx) == abs(sourcey - targety)) #diagonal
if (team == 'b') && (sourcex < targetx)
if (sourcey < targety) #diagonalDownRight
if (diagonalDownRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #diagonalDownLeft
if (diagonalDownLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
elseif (team == 'w') && (sourcex > targetx)
if (sourcey < targety) #diagonalUpRight
if (diagonalUpRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #diagonalUpLeft
if (diagonalUpLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
end
end
return false
end #whaleValidate end
#case 10 copperGeneralValidate
function copperGeneralValidate(team,sourcex,sourcey,targetx,targety)
if (team == 'b')
if (targetx == sourcex - 1)
if ((sourcey == targety) || (abs(sourcey - targety) == 1)) #forward any direction by 1
return true
end
elseif (targetx == sourcex + 1)
if (sourcey == targety) #back 1
return true
end
end
elseif (team == 'w')
if (targetx == sourcex + 1)
if ((sourcey == targety) || (abs(sourcey - targety) == 1)) #forward any direction by 1
return true
end
elseif (targetx == sourcex - 1)
if (sourcey == targety)
return true
end
end
end
return false
end #copperGeneralValidate end
#case 10.2 sideMoverValidate
function sideMoverValidate(team,sourcex,sourcey,targetx,targety)
if (sourcex == targetx)
if (sourcey > targety) #moveLeftValidate
if (moveLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #moveRightValidate
if (moveRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
elseif (sourcey == targety)
if (abs(sourcex - targetx) == 1)
return true
end
end
return false
end #sideMoverValidate end
#case 11 dragonKingValidate #moves like king and rook
function dragonKingValidate(team,sourcex,sourcey,targetx,targety)
#checks for moves like king
if (moveAdjacentValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
#checks for moves like rook
if (moveOrthogonalValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
return false
end #dragonKingValidate end
#case 11.2 Soaring eagle
function soaringEagleValidate(team,sourcex,sourcey,targetx,targety,targetx2,targety2)
#range move
if (isnull(targetx2) && isnull(targety2))
#moves like rook
if (sourcex == targetx) && (sourcey != targety) #moving horizontally
if (sourcey > targety) #horizontal left
if (moveLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #horizontal right
if (moveRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
elseif (sourcey == targety) && (sourcex != targetx) #moving vertically
if (sourcex > targetx) #vertical up
if (moveUpValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #vertical down
if (moveDownValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
end
#diagonal down
if (team == 'b')
if (sourcex < targetx) #move down
if (sourcey < targety) #move right
if (diagonalDownRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #move left
if (diagonalDownLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
else #move up diagonal 1 or 2
if (sourcex - targetx == 1) && (abs(sourcey - targety) == 1)
return true
elseif (sourcex - targetx == 2) && (abs(sourcey - targety) == 2)
return true
end
end
elseif (team == 'w')
if (sourcex > targetx) #move up
if (sourcey < targety) #move right
if (diagonalUpRightValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #move left
if (diagonalUpLeftValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
end
else #move up diagonal 1 or 2
if (sourcex - targetx == -1) && (abs(sourcey - targety) == 1)
return true
elseif (sourcex - targetx == -2) && (abs(sourcey - targety) == 2)
return true
end
end
end
else #lion move
#case 1 capture without movin
if (sourcex == targetx2 && sourcey == targety2)
if (team == 'b')
if ( (sourcex - targetx == 1) && (abs(sourcey - targety) == 1) )
return true
end
elseif (team == 'w')
if ( (targetx - sourcex == 1) && (abs(sourcey - targety) == 1) )
return true
end
end
end
#case 2 eat 1 hop another
if (team =='b')
if (sourcex - targetx2 == 2) && (sourcex - targetx == 1)
if (abs(sourcex - targetx) == 1) && (abs(sourcex - targetx2) == 2)
return true
end
end
elseif (team == 'w')
if (targetx2 - sourcex == 2) && (targetx - sourcex == 1)
if (abs(sourcex - targetx) == 1) && (abs(sourcex - targetx2) == 2)
return true
end
end
end
end
return false
end #soaringEagleValidate end
#case 12 drunkElephantValidate
function drunkElephantValidate(team,sourcex,sourcey,targetx,targety)
if ((abs(sourcex - targetx) == 1) || (abs(sourcex - targetx) == 0))
if ((abs(sourcey - targety) == 1) || (abs(sourcey - targety) == 0))
if (team == 'b')
if (sourcey == targety) && (targetx == sourcex + 1) #moveback
return false
else
return true
end
elseif (team == 'w')
if (sourcey == targety) && (targetx == sourcex - 1) #moveUp
return false
else
return true
end
end
end
end
return false
end #drunkElephantValidate
#case 12.2 princeValidate moves like king
#case 13 ferociousLeopardValidate
function ferociousLeopardValidate(team,sourcex,sourcey,targetx,targety)
if ((abs(sourcex - targetx) == 1) || (abs(sourcex - targetx) == 0))
if ((abs(sourcey - targety) == 1) || (abs(sourcey - targety) == 0))
if (sourcex != targetx)
return true
end
end
end
return false
end #ferociousLeopardValidate end
#case 13.2 bishop
#case 14 dragonHorseValidate
function dragonHorseValidate(team,sourcex,sourcey,targetx,targety)
#check for moves like king
if (moveAdjacentValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
#check for moves like bishop
if (moveDiagonalValidate(sourcex,sourcey, targetx, targety) == true)
return true
end
return false
end #dragonHorseValidate end
#case 14.2 hornedFalconValidate
function hornedFalconValidate(team,sourcex,sourcey,targetx,targety,targetx2,targety2)
#range moves
if isnull(targetx2) && isnull(targety2)
#check if moves like bishop
if (moveDiagonalValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
#Check for vertical down moves and jump 2 forward
if (sourcey == targety)
if (team == 'b')
if (sourcex < targetx)
if (moveDownValidate(sourcex,sourcey,targetx,targety) == true)
return true
end
else #jump 2 up
if (sourcex - targetx == 2)
return true
end
end
elseif (team == 'w')
if (sourcex > targetx)
if (moveUpValidate(sourcex,sourcey,targetx,targety) == true)
return true