-
Notifications
You must be signed in to change notification settings - Fork 0
/
HUOC.bas
2471 lines (1939 loc) · 103 KB
/
HUOC.bas
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
'HUOC: Huo Chess by Spiros I. Kakos (huo) - DRAFT VERSION (NOT FULLY TESTED, STILL IN PROGRESS)
'Version 0.2: Thinking in 3 half-moves depth
'Version 0.3: Thinking in 3 half-moves depth with MinMax algorithm
'Version 0.4: Thinking up to 5 half-moves depth
'Version 0.5: Improved score function
'Version 0.6: Added opening functionality
'Based on the Huo Chess implementation in C++ and C#
'Check "Huo Chess" in C# at Harmonia Philosophica for code in C# and relative tutorials
'Check https://harmoniaphilosophica.com/2019/02/13/huo-chess-c-micro-chess-updated/
'Other QBasic Internet resources
'https://www.qb64.org/wiki/Arrays#Working_with_the_Array_Elements
'https://www.qb64.org/wiki/CALL
'https://www.qb64.org/wiki/SUB
'https://www.qb64.org/wiki/IF...THEN
'What is not supported...
'1. En passant [FIXED: 2020-08-06 - Not included in computer thought!]
'2. Castling [FIXED only for human player: 2020-08-04 - Not included in computer thought!]
'3. Pawn promotion [FIXED only for queen promotion: 2020-08-04 - Not included in computer thought!]
'4. Check [FIXED: 2020-08-05 - Not included in computer thought!]
'5. Mate [FIXED: 2020-08-05 - Not included in computer thought!]
'6. Inform user he has entered an invalid move [FIXED: 2020-08-06]
DECLARE SUB movValidation (aline AS INTEGER, fline AS INTEGER, arcol AS INTEGER, telcol AS INTEGER)
DECLARE SUB drawBoard ()
DECLARE SUB ElegxosNomimotitas(ENSkakiera() AS STRING, startColumn AS INTEGER, startRank AS INTEGER, finishColumn AS INTEGER, finishRank AS INTEGER, MovingPieceEN AS STRING)
DEFINT A-Z
OPTION BASE 1 'Make tables dimensions start from 1
DIM SHARED chessboard$(8, 8)
COMMON SHARED playerColor$
COMMON SHARED startingRank, startingColumn
COMMON SHARED finishingRank, finishingColumn
COMMON SHARED startingRankText$, startingColumnText$
COMMON SHARED finishingRankText$, finishingColumnText$
COMMON SHARED Move$
COMMON SHARED MovingPiece$
COMMON SHARED ProsorinoKommati$
COMMON SHARED Nomimotita
COMMON SHARED debugMode
COMMON SHARED positionScore
COMMON SHARED bestStartingRank, bestStartingColumn
COMMON SHARED bestFinishingRank, bestFinishingColumn
COMMON SHARED bestStartingRankMate, bestStartingColumnMate '14/3/2021
COMMON SHARED bestFinishingRankMate, bestFinishingColumnMate '14/3/2021
COMMON SHARED bestPositionScore
COMMON SHARED whoPlays$
COMMON SHARED whichColorPlays$ '14/3/2021
COMMON SHARED computerLogs$, humanLogs$
COMMON SHARED MoveNumber
'Castling
COMMON SHARED whiteCastling
COMMON SHARED whiteBigCastling
COMMON SHARED whiteSmallCastling
COMMON SHARED blackCastling
COMMON SHARED blackBigCastling
COMMON SHARED blackSmallCastling
'Check variables
COMMON SHARED whiteCheck
COMMON SHARED blackCheck
'En passant
COMMON SHARED enPassantColumn
COMMON SHARED enPassantRank
COMMON SHARED enPassantHappened
'COMMON SHARED enPassantTest
COMMON SHARED MateFound '14/3/2021
COMMON SHARED PatFound '14/3/2021
COMMON SHARED HumanMoveFound '14/3/2021
'MinMax
'Important note: All dimensions in the tables are +1 compared to the Huo Chess in C#! (tables start from 1 here)
COMMON SHARED foundMove
DIM SHARED NodesAnalysis0(1000000, 6)
DIM SHARED NodesAnalysis1(1000000, 2)
DIM SHARED NodesAnalysis2(1000000, 2)
DIM SHARED NodesAnalysis3(10000000, 2) AS LONG
DIM SHARED NodesAnalysis4(10000000, 2) AS LONG
COMMON SHARED NodeLevel_0_count AS LONG
COMMON SHARED NodeLevel_1_count AS LONG
COMMON SHARED NodeLevel_2_count AS LONG
COMMON SHARED NodeLevel_3_count AS LONG
COMMON SHARED NodeLevel_4_count AS LONG
COMMON SHARED parentNodeAnalyzed AS LONG
DIM SHARED bestNodes0(10000000)
DIM SHARED bestNodes1(10000000)
DIM SHARED bestNodes2(10000000)
DIM SHARED bestNodes3(10000000)
DIM SHARED bestNodes4(10000000)
COMMON SHARED bestNode2, bestNode1, bestNode0
COMMON SHARED counter0 AS LONG
COMMON SHARED counter1 AS LONG
COMMON SHARED counter2 AS LONG
COMMON SHARED counter3 AS LONG
COMMON SHARED counter4 AS LONG
COMMON SHARED huologs$, logLine$
COMMON SHARED thinkingDepth
COMMON SHARED Move
COMMON SHARED CalledFromCheckCheck '14/3/2021
'Set debugMode = 1 if you want debugging messages to appear, else set to 0
debugMode = 0
PRINT "Huo Chess - HUOC v0.9 by Spiros Kakos (2020, 2021, 2022)"
PRINT ""
PRINT "The best open source chess in BASIC for educational purposes"
PRINT "Check HUO CHESS in C# at Harmonia Philosophica as well!"
PRINT ""
PRINT "How to play: Enter move coordinates without spaces (e.g. c2c4) and press Enter"
PRINT ""
SetPlayerColor:
'Set the colour of the player
INPUT "Set your color. Please select 'w' or 'b': ", playerColor$
IF playerColor$ <> "w" AND playerColor$ <> "b" THEN GOTO SetPlayerColor
SetThinkingDepth:
'Set the thinking depth
PRINT "": INPUT "Set thinking depth. Please select 1 or 3 or 5: ", thinkingDepth
IF thinkingDepth <> 1 AND thinkingDepth <> 3 AND thinkingDepth <> 5 THEN GOTO SetThinkingDepth
PRINT ""
computerLogs:
'INPUT "Activate debug messages for computer? (y/n) ", computerLogs$: IF computerLogs$ <> "y" AND computerLogs$ <> "n" THEN GOTO computerLogs
humanLogs:
'INPUT "Activate debug messages for the human? (y/n) ", humanLogs$: IF humanLogs$ <> "y" AND humanLogs$ <> "n" THEN GOTO humanLogs
'Turn off debug messages (set to "y" to turn on)
computerLogs$ = "n"
humanLogs$ = "n"
CLS
CALL initialPosition
CALL drawBoard
'Initialize variables
MoveNumber = 0
'Check variables
whiteCheck = 0
blackCheck = 0
'Castling
whiteCastling = 0
blackCastling = 0
whiteBigCastling = 0
whiteSmallCastling = 0
blackBigCastling = 0
blackSmallCastling = 0
'En Passant
'enPassantTest = 0
enPassantHappened = 0
CHDIR "Huo Chess Opening Book"
'Set thinking depth
'thinkingDepth = 3
LOCATE 12, 1: PRINT "Huo Chess - HUOC"
IF playerColor$ = "w" THEN
whoPlays$ = "Human"
CALL PlayerMove
END IF
IF playerColor$ = "b" THEN
whoPlays$ = "HY"
LOCATE 13, 1: PRINT "Thinking..."
CALL computerMove
END IF
SUB PlayerMove
'Initialize variables
'Check
whiteCheck = 0
blackCheck = 0
'En Passant
enPassantHappened = 0
IF playerColor$ = "b" THEN whichColorPlays$ = "Black" '14/3/2021
IF playerColor$ = "w" THEN whichColorPlays$ = "White" '14/3/2021
'Check the Check
IF playerColor$ = "b" THEN CALL checkBlackCheck(chessboard$())
IF playerColor$ = "w" THEN CALL checkWhiteCheck(chessboard$())
'PRINT "PlayerMove: blackCheck = " + STR$(blackCheck): INPUT aa$
IF whiteCheck = 1 OR blackCheck = 1 THEN LOCATE 12, 1: PRINT "Check! ": LOCATE 13, 1: PRINT "Press Enter": DO: LOOP UNTIL INKEY$ <> "": LOCATE 12, 1: PRINT " ": LOCATE 13, 1: PRINT " "
LOCATE 13, 1: INPUT "Enter your move: ", Move$
startingColumnText$ = MID$(Move$, 1, 1)
startingRankText$ = MID$(Move$, 2, 1)
finishingColumnText$ = MID$(Move$, 3, 1)
finishingRankText$ = MID$(Move$, 4, 1)
IF humanLogs$ = "y" THEN debugMode = 1 ELSE debugMode = 0
SELECT CASE startingRankText$
CASE "1"
startingRank = 1
CASE "2"
startingRank = 2
CASE "3"
startingRank = 3
CASE "4"
startingRank = 4
CASE "5"
startingRank = 5
CASE "6"
startingRank = 6
CASE "7"
startingRank = 7
CASE "8"
startingRank = 8
END SELECT
SELECT CASE finishingRankText$
CASE "1"
finishingRank = 1
CASE "2"
finishingRank = 2
CASE "3"
finishingRank = 3
CASE "4"
finishingRank = 4
CASE "5"
finishingRank = 5
CASE "6"
finishingRank = 6
CASE "7"
finishingRank = 7
CASE "8"
finishingRank = 8
END SELECT
SELECT CASE startingColumnText$
CASE "A", "a"
startingColumn = 1
CASE "B", "b"
startingColumn = 2
CASE "C", "c"
startingColumn = 3
CASE "D", "d"
startingColumn = 4
CASE "E", "e"
startingColumn = 5
CASE "F", "f"
startingColumn = 6
CASE "G", "g"
startingColumn = 7
CASE "H", "h"
startingColumn = 8
END SELECT
SELECT CASE finishingColumnText$
CASE "A", "a"
finishingColumn = 1
CASE "B", "b"
finishingColumn = 2
CASE "C", "c"
finishingColumn = 3
CASE "D", "d"
finishingColumn = 4
CASE "E", "e"
finishingColumn = 5
CASE "F", "f"
finishingColumn = 6
CASE "G", "g"
finishingColumn = 7
CASE "H", "h"
finishingColumn = 8
END SELECT
MovingPiece$ = chessboard$(startingColumn, startingRank)
ProsorinoKommati$ = chessboard$(finishingColumn, finishingRank)
IF (debugMode = 1) THEN
PRINT "[Move: " + STR$(startingColumn) + STR$(startingRank) + " -> " + STR$(finishingColumn) + STR$(finishingRank) + " ]"
END IF
'Check legality of the move entered
CalledFromCheckCheck = 0 '14/3/2021
CALL ElegxosNomimotitas(chessboard$(), 0, startingColumn, startingRank, finishingColumn, finishingRank, MovingPiece$)
' ------------------ Check the Check! START ------------------
'Temporarily do the move
chessboard$(finishingColumn, finishingRank) = chessboard$(startingColumn, startingRank)
chessboard$(startingColumn, startingRank) = ""
'Check the check
IF playerColor$ = "b" AND Nomimotita = 1 THEN
'whoPlays$ = "HY"
CALL checkBlackCheck(chessboard$())
'Restore Nomimotita! (because it is 'broken' in the checkCheck function
Nomimotita = 1
IF blackCheck = 1 THEN Nomimotita = 0
'whoPlays$ = "Human"
END IF
IF playerColor$ = "w" AND Nomimotita = 1 THEN
'whoPlays$ = "HY"
CALL checkWhiteCheck(chessboard$())
'Restore Nomimotita! (because it is 'broken' in the checkCheck function
Nomimotita = 1
IF whiteCheck = 1 THEN Nomimotita = 0
'whoPlays$ = "Human"
END IF
'Undo the move
chessboard$(startingColumn, startingRank) = MovingPiece$
chessboard$(finishingColumn, finishingRank) = ProsorinoKommati$
' ------------------ Check the Check! END ------------------
'Castling
IF (playerColor$ = "w" AND whiteCastling = 0) OR (playerColor$ = "b" AND blackCastling = 0) THEN CALL Castling
IF playerColor$ = "w" AND whiteCastling = 1 THEN Nomimotita = 1
IF playerColor$ = "b" AND blackCastling = 1 THEN Nomimotita = 1
'If move is legal, then do the move and present it in the chessbooard
IF Nomimotita = 1 THEN
IF debugMode = 1 THEN
PRINT "Now we will redraw the chessboard"
END IF
'Do the move
chessboard$(finishingColumn, finishingRank) = chessboard$(startingColumn, startingRank)
chessboard$(startingColumn, startingRank) = ""
'Castling
'After castling set variable value to 2 so that it does not place the rook on f8/f1 - c8/c1 again...
IF playerColor$ = "w" AND whiteBigCastling = 1 THEN chessboard$(4, 1) = "wrook": chessboard$(1, 1) = "": whiteBigCastling = 2
IF playerColor$ = "w" AND whiteSmallCastling = 1 THEN chessboard$(6, 1) = "wrook": chessboard$(8, 1) = "": whiteSmallCastling = 2
IF playerColor$ = "b" AND blackBigCastling = 1 THEN chessboard$(4, 8) = "brook": chessboard$(1, 8) = "": blackBigCastling = 2
IF playerColor$ = "b" AND blackSmallCastling = 1 THEN chessboard$(6, 8) = "brook": chessboard$(8, 8) = "": blackSmallCastling = 2
'Check for pawn promotion
CALL PawnPromotion
'En passant happened?
IF enPassantHappened = 1 THEN
IF enPassantRank = 3 THEN chessboard$(finishingColumn, 4) = ""
IF enPassantRank = 6 THEN chessboard$(finishingColumn, 5) = ""
END IF
'En passant possible?
enPassantColumn = 0
enPassantRank = 0
IF startingColumn = finishingColumn AND startingRank = 2 AND finishingRank = 4 AND MovingPiece$ = "wpawn" THEN
enPassantColumn = finishingColumn
enPassantRank = 3
ELSEIF startingColumn = finishingColumn AND startingRank = 7 AND finishingRank = 5 AND MovingPiece$ = "bpawn" THEN
enPassantColumn = finishingColumn
enPassantRank = 6
END IF
CLS
CALL drawBoard
'Increase move number of the game
IF playerColor$ = "w" THEN MoveNumber = MoveNumber + 1
'Time for the computer to think now...
IF playerColor$ = "b" THEN whichColorPlays$ = "White" '14/3/2021
IF playerColor$ = "w" THEN whichColorPlays$ = "Black" '14/3/2021
LOCATE 12, 1: PRINT "Huo Chess - HUOC"
LOCATE 13, 1: PRINT "Thinking..."
whoPlays$ = "HY"
CALL computerMove
ELSE
LOCATE 12, 1: PRINT "Invalid move - Please try again"
LOCATE 13, 1: PRINT "Press any key to continue"
DO: LOOP UNTIL INKEY$ <> ""
LOCATE 12, 1: PRINT " "
LOCATE 13, 1: PRINT " "
LOCATE 12, 1: PRINT "Huo Chess - HUOC"
CALL PlayerMove
END IF
END SUB
SUB computerMove
'Initialize variables
Move = 1
bestPositionScore = 0
bestStartingRank = 0
bestStartingColumn = 0
bestFinishingRank = 0
bestFinishingColumn = 0
IF playerColor$ = "w" THEN bestPositionScore = 999
IF playerColor$ = "b" THEN bestPositionScore = -999
'MinMax
foundMove = 0
NodeLevel_0_count = 1
NodeLevel_1_count = 1
NodeLevel_2_count = 1
NodeLevel_3_count = 1
NodeLevel_4_count = 1
'Check
whiteCheck = 0
blackCheck = 0
'En Passant
enPassantHappened = 0
MateFound = 0 '14/3/2021
PatFound = 0 '14/3/2021
bestStartingRankMate = 0 '14/3/2021
bestStartingColumnMate = 0 '14/3/2021
bestFinishingRankMate = 0 '14/3/2021
bestFinishingColumnMate = 0 '14/3/2021
'Check the Check
IF playerColor$ = "b" THEN CALL checkWhiteCheck(chessboard$())
IF playerColor$ = "w" THEN CALL checkBlackCheck(chessboard$())
'PRINT "PlayerMove: blackCheck = " + STR$(blackCheck): INPUT aa$
IF whiteCheck = 1 OR blackCheck = 1 THEN LOCATE 12, 1: PRINT "Check! ": LOCATE 13, 1: PRINT "Press Enter": DO: LOOP UNTIL INKEY$ <> "": LOCATE 12, 1: PRINT " ": LOCATE 13, 1: PRINT " "
IF computerLogs$ = "y" THEN debugMode = 1 ELSE debugMode = 0
IF debugMode = 1 THEN PRINT "chessboard$(1,1) = " + chessboard$(1, 1)
IF debugMode = 1 THEN PRINT "MID$(chessboard$(1, 1), 1, 1) = " + MID$(chessboard$(1, 1), 1, 1)
' ------------------- OPENING (start) -------------------
'Search for the position
FileNum = 0
positionFile$ = ""
foundOpening = 0
newLine$ = ""
DO
FileNum = FileNum + 1
openingFile$ = "Opening" + LTRIM$(STR$(FileNum)) + ".txt"
IF _FILEEXISTS(openingFile$) THEN
verifyPosition = 1
CLOSE #4
OPEN openingFile$ FOR INPUT AS #4
FOR i = 1 TO 8
FOR j = 1 TO 8
LINE INPUT #4, newLine$
IF CHR$(34) + chessboard$(i, j) + CHR$(34) <> newLine$ THEN verifyPosition = 0
NEXT j
NEXT i
'If position is found, store the file and read the suggested move!
IF verifyPosition = 1 THEN
positionFile$ = openingFile$
foundOpening = 1
'Read Suggested Move line
LINE INPUT #4, newLine$
'Read the coordinates of the suggested move
LINE INPUT #4, newLine$
bestStartingColumn = VAL(newLine$)
LINE INPUT #4, newLine$
bestStartingRank = VAL(newLine$)
LINE INPUT #4, newLine$
bestFinishingColumn = VAL(newLine$)
LINE INPUT #4, newLine$
bestFinishingRank = VAL(newLine$)
END IF
ELSE
positionFile$ = "Not found"
END IF
LOOP WHILE positionFile$ = ""
IF foundOpening = 1 THEN
'PRINT "Found position in file: " + positionFile$
'PRINT "Suggested move: " + STR$(openingStartColumn) + STR$(openingStartRank) + " -> " + STR$(openingFinishColumn) + STR$(openingFinishRank)
'INPUT H$
GOTO bestMoveSection
END IF
' ------------------- OPENING (end) -------------------
'Scan the chessboard...
FOR i = 1 TO 8
FOR j = 1 TO 8
'If you find a piece of the computer...
IF ((MID$(chessboard$(i, j), 1, 1) = "w" AND playerColor$ = "b") OR (MID$(chessboard$(i, j), 1, 1) = "b" AND playerColor$ = "w")) THEN
IF debugMode = 1 THEN PRINT "Inside Computer Mode - Checkpoint A"
'Scan all possible destination squares...
FOR ii = 1 TO 8
FOR jj = 1 TO 8
startingColumn = i
startingRank = j
finishingColumn = ii
finishingRank = jj
MovingPiece$ = chessboard$(i, j)
ProsorinoKommati$ = chessboard$(ii, jj)
'Check legality of the move entered
CalledFromCheckCheck = 0 '14/3/2021
CALL ElegxosNomimotitas(chessboard$(), 0, startingColumn, startingRank, finishingColumn, finishingRank, MovingPiece$)
' ------------------ Check the Check! START ------------------
'Temporarily do the move
chessboard$(finishingColumn, finishingRank) = chessboard$(startingColumn, startingRank)
chessboard$(startingColumn, startingRank) = ""
'Trela = 0
'IF Nomimotita = 1 THEN PRINT "Check A: whiteCheck = " + STR$(whiteCheck) + " , Nomimotita = " + STR$(Nomimotita): Trela = 1: INPUT A$
'Check the check
IF playerColor$ = "b" AND Nomimotita = 1 THEN
'whoPlays$ = "Human"
CALL checkWhiteCheck(chessboard$())
'Restore Nomimotita! (because it is 'broken' in the checkCheck function
Nomimotita = 1
IF whiteCheck = 1 THEN Nomimotita = 0
'IF Trela = 1 THEN PRINT "Check B: whiteCheck = " + STR$(whiteCheck) + " , Nomimotita = " + STR$(Nomimotita): INPUT A$
'whoPlays$ = "HY"
END IF
IF playerColor$ = "w" AND Nomimotita = 1 THEN
'whoPlays$ = "Human"
CALL checkBlackCheck(chessboard$())
'Restore Nomimotita! (because it is 'broken' in the checkCheck function
Nomimotita = 1
IF blackCheck = 1 THEN Nomimotita = 0
'whoPlays$ = "HY"
END IF
'Undo the move
chessboard$(startingColumn, startingRank) = MovingPiece$
chessboard$(finishingColumn, finishingRank) = ProsorinoKommati$
' ------------------ Check the Check! END ------------------
IF debugMode = 1 THEN
PRINT ""
PRINT "NEW MOVE ANALYZED: " + STR$(startingColumn) + STR$(startingRank) + " -> " + STR$(finishingColumn) + STR$(finishingRank)
PRINT "Legality of the move analyzed: " + STR$(Nomimotita)
END IF
'If move is legal, then do the move and present it in the chessbooard
IF Nomimotita = 1 THEN
'-------------- MinMax --------------
'Important note: All dimensions in the tables are +1 compared to the Huo Chess in C#! (tables start from 1 here)
NodesAnalysis0(NodeLevel_0_count, 3) = startingColumn
NodesAnalysis0(NodeLevel_0_count, 4) = finishingColumn
NodesAnalysis0(NodeLevel_0_count, 5) = startingRank
NodesAnalysis0(NodeLevel_0_count, 6) = finishingRank
'If first move found, then store it (This is useless)
IF foundMove = 0 THEN
'NodesAnalysis0(NodeLevel_0_count, 3) = startingColumn
'NodesAnalysis0(NodeLevel_0_count, 4) = finishingColumn
'NodesAnalysis0(NodeLevel_0_count, 5) = startingRank
'NodesAnalysis0(NodeLevel_0_count, 6) = finishingRank
'PRINT "startingColumn = " + STR$(startingColumn)
'PRINT "finishingColumn = " + STR$(finishingColumn)
'PRINT "startingRank = " + STR$(startingRank)
'PRINT "finishingRank = " + STR$(finishingRank)
'INPUT A$
foundMove = 1
END IF
'Do the move
chessboard$(finishingColumn, finishingRank) = chessboard$(startingColumn, startingRank)
chessboard$(startingColumn, startingRank) = ""
'Count the score of the move
CALL countScore(chessboard$())
'-------------- MinMax --------------
'Store scores
NodesAnalysis0(NodeLevel_0_count, 1) = positionScore
' Store parents
NodesAnalysis0(NodeLevel_0_count, 2) = 0
IF Move = thinkingDepth THEN
'If the score is better than the existing best score, then this is the best move now (and the best score)
IF ((playerColor$ = "b" AND positionScore >= bestPositionScore) OR (playerColor$ = "w" AND positionScore <= bestPositionScore)) THEN
bestStartingRank = startingRank
bestStartingColumn = startingColumn
bestFinishingRank = finishingRank
bestFinishingColumn = finishingColumn
bestPositionScore = positionScore
END IF
END IF
IF Move < thinkingDepth THEN
Move = Move + 1
IF playerColor$ = "b" THEN whichColorPlays$ = "Black" '14/3/2021
IF playerColor$ = "w" THEN whichColorPlays$ = "White" '14/3/2021
CALL HumanMove1(chessboard$())
END IF
'Restore the color playing (14/3/2021)
IF playerColor$ = "b" THEN whichColorPlays$ = "White" '14/3/2021
IF playerColor$ = "w" THEN whichColorPlays$ = "Black" '14/3/2021
'Undo the move
chessboard$(startingColumn, startingRank) = MovingPiece$
chessboard$(finishingColumn, finishingRank) = ProsorinoKommati$
'If pat (= stalemate in Greek) found, then nullify the score of the move (14/3/2021)
IF PatFound = 1 THEN
NodesAnalysis0(NodeLevel_0_count, 1) = 0
END IF
'-------------- MinMax --------------
'Increase node count
NodeLevel_0_count = NodeLevel_0_count + 1
END IF
NEXT jj
NEXT ii
END IF
NEXT j
NEXT i
IF debugMode = 1 THEN
PRINT "bestStartingRank = " + STR$(bestStartingRank)
PRINT "bestStartingColumn = " + STR$(bestStartingColumn)
PRINT "bestFinishingRank = " + STR$(bestFinishingRank)
PRINT "bestFinishingColumn = " + STR$(bestFinishingColumn)
END IF
'-------------- MinMax --------------
CALL MinMax
'If mate found, then go for it! (14/3/2021)
IF (MateFound = 1) THEN
bestStartingRank = bestStartingRankMate
bestStartingColumn = bestStartingColumnMate
bestFinishingRank = bestFinishingRankMate
bestFinishingColumn = bestFinishingColumnMate
END IF
'No move found? Then resign! Else, make the move!
IF bestFinishingColumn = 0 THEN
LOCATE 12, 1: PRINT "I resign! "
ELSE
'En passant test
'IF enPassantTest = 0 THEN
' bestStartingColumn = 5: bestStartingRank = 2
' bestFinishingColumn = 5: bestFinishingRank = 4
' enPassantTest = 1
'END IF
bestMoveSection:
'Do the best move found
chessboard$(bestFinishingColumn, bestFinishingRank) = chessboard$(bestStartingColumn, bestStartingRank)
chessboard$(bestStartingColumn, bestStartingRank) = ""
'Check for pawn promotion
CALL PawnPromotion
'En passant happened?
IF (finishColumn = enPassantColumn) AND (finishRank = enPassantRank) THEN enPassantHappened = 1 ELSE enPassantHappened = 0
'En passant happened?
IF enPassantHappened = 1 THEN
IF enPassantRank = 3 THEN chessboard$(finishingColumn, 4) = ""
IF enPassantRank = 6 THEN chessboard$(finishingColumn, 5) = ""
END IF
'En passant possible?
enPassantColumn = 0
enPassantRank = 0
IF bestStartingColumn = bestFinishingColumn AND bestStartingRank = 2 AND bestFinishingRank = 4 AND chessboard$(bestFinishingColumn, bestFinishingRank) = "wpawn" THEN
enPassantColumn = bestFinishingColumn
enPassantRank = 3
ELSEIF bestStartingColumn = bestFinishingColumn AND bestStartingRank = 7 AND bestFinishingRank = 5 AND chessboard$(bestFinishingColumn, bestFinishingRank) = "bpawn" THEN
enPassantColumn = bestFinishingColumn
enPassantRank = 6
END IF
CLS
CALL drawBoard
'Increase move number of the game
IF playerColor$ = "b" THEN MoveNumber = MoveNumber + 1
SELECT CASE bestStartingColumn
CASE 1
startingColumnText$ = "a"
CASE 2
startingColumnText$ = "b"
CASE 3
startingColumnText$ = "c"
CASE 4
startingColumnText$ = "d"
CASE 5
startingColumnText$ = "e"
CASE 6
startingColumnText$ = "f"
CASE 7
startingColumnText$ = "g"
CASE 8
startingColumnText$ = "h"
END SELECT
SELECT CASE bestFinishingColumn
CASE 1
finishingColumnText$ = "a"
CASE 2
finishingColumnText$ = "b"
CASE 3
finishingColumnText$ = "c"
CASE 4
finishingColumnText$ = "d"
CASE 5
finishingColumnText$ = "e"
CASE 6
finishingColumnText$ = "f"
CASE 7
finishingColumnText$ = "g"
CASE 8
finishingColumnText$ = "h"
END SELECT
LOCATE 12, 1: PRINT "My move: " + startingColumnText$ + STR$(bestStartingRank) + " -> " + finishingColumnText$ + STR$(bestFinishingRank)
'Time for the human to play now...
whoPlays$ = "Human"
IF playerColor$ = "b" THEN whichColorPlays$ = "Black" '14/3/2021
IF playerColor$ = "w" THEN whichColorPlays$ = "White" '14/3/2021
CALL PlayerMove
END IF
END SUB
'-----------------------------------------------------------------------------------------------------
SUB initialPosition
chessboard$(1, 1) = "wrook": chessboard$(1, 2) = "wpawn"
chessboard$(2, 1) = "wknight": chessboard$(2, 2) = "wpawn"
chessboard$(3, 1) = "wbishop": chessboard$(3, 2) = "wpawn"
chessboard$(4, 1) = "wqueen": chessboard$(4, 2) = "wpawn"
chessboard$(5, 1) = "wking": chessboard$(5, 2) = "wpawn"
chessboard$(6, 1) = "wbishop": chessboard$(6, 2) = "wpawn"
chessboard$(7, 1) = "wknight": chessboard$(7, 2) = "wpawn"
chessboard$(8, 1) = "wrook": chessboard$(8, 2) = "wpawn"
chessboard$(1, 7) = "bpawn": chessboard$(1, 8) = "brook"
chessboard$(2, 7) = "bpawn": chessboard$(2, 8) = "bknight"
chessboard$(3, 7) = "bpawn": chessboard$(3, 8) = "bbishop"
chessboard$(4, 7) = "bpawn": chessboard$(4, 8) = "bqueen"
chessboard$(5, 7) = "bpawn": chessboard$(5, 8) = "bking"
chessboard$(6, 7) = "bpawn": chessboard$(6, 8) = "bbishop"
chessboard$(7, 7) = "bpawn": chessboard$(7, 8) = "bknight"
chessboard$(8, 7) = "bpawn": chessboard$(8, 8) = "brook"
'Test
'FOR q = 1 TO 8
' FOR w = 1 TO 8
' chessboard$(q, w) = ""
' NEXT w
'NEXT q
'chessboard$(4, 4) = "wbishop"
'chessboard$(5, 5) = "bbishop"
'chessboard$(1, 1) = "wrook"
'chessboard$(1, 8) = "brook"
'chessboard$(2, 2) = "wpawn"
'---------- TEST ----------
'chessboard$(1, 1) = "wrook": chessboard$(1, 2) = "wpawn"
'chessboard$(2, 1) = "wknight": chessboard$(2, 2) = "bpawn"
'chessboard$(3, 1) = "": chessboard$(3, 2) = "wpawn"
'chessboard$(4, 1) = "": chessboard$(4, 2) = "wpawn"
'chessboard$(5, 1) = "wking": chessboard$(5, 2) = ""
'chessboard$(6, 1) = "": chessboard$(6, 2) = "wpawn"
'chessboard$(7, 1) = "wknight": chessboard$(7, 2) = "wpawn"
'chessboard$(8, 1) = "wrook": chessboard$(8, 2) = ""
'chessboard$(1, 7) = "": chessboard$(1, 8) = "brook"
'chessboard$(2, 7) = "wbishop": chessboard$(2, 8) = "bknight"
'chessboard$(3, 7) = "bpawn": chessboard$(3, 8) = ""
'chessboard$(4, 7) = "bpawn": chessboard$(4, 8) = "bqueen"
'chessboard$(5, 7) = "bpawn": chessboard$(5, 8) = "bking"
'chessboard$(6, 7) = "bpawn": chessboard$(6, 8) = "bbishop"
'chessboard$(7, 7) = "bpawn": chessboard$(7, 8) = "bknight"
'chessboard$(8, 7) = "bpawn": chessboard$(8, 8) = "brook"
'chessboard$(4, 4) = "wpawn"
'chessboard$(2, 4) = "wbishop"
'chessboard$(6, 4) = "wqueen"
'chessboard$(7, 6) = "bbishop"
'---------- CASTLING TEST ----------
'chessboard$(1, 1) = "wrook": chessboard$(1, 2) = "wpawn"
'chessboard$(3, 3) = "wknight": chessboard$(2, 2) = "wpawn"
'chessboard$(6, 4) = "wbishop": chessboard$(3, 2) = "wpawn"
'chessboard$(4, 2) = "wqueen": chessboard$(4, 3) = "wpawn"
'chessboard$(5, 1) = "wking": chessboard$(5, 5) = "wpawn"
'chessboard$(3, 4) = "wbishop": chessboard$(6, 2) = "wpawn"
'chessboard$(6, 3) = "wknight": chessboard$(7, 2) = "wpawn"
'chessboard$(8, 1) = "wrook": chessboard$(8, 2) = "wpawn"
'chessboard$(1, 7) = "bpawn": chessboard$(1, 8) = "brook"
'chessboard$(2, 6) = "bpawn": chessboard$(3, 6) = "bknight"
'chessboard$(3, 7) = "bpawn": chessboard$(1, 4) = "bbishop"
'chessboard$(4, 5) = "bpawn": chessboard$(4, 7) = "bqueen"
'chessboard$(5, 6) = "bpawn": chessboard$(5, 8) = "bking"
'chessboard$(6, 7) = "bpawn": chessboard$(5, 7) = "bbishop"
'chessboard$(7, 7) = "bpawn": chessboard$(8, 6) = "bknight"
'chessboard$(8, 7) = "bpawn": chessboard$(8, 8) = "brook"
'---------- CHECK TEST ----------
'chessboard$(1, 1) = "wrook": chessboard$(1, 2) = "wpawn"
'chessboard$(2, 5) = "wknight": chessboard$(2, 2) = "wpawn"
'chessboard$(6, 4) = "wbishop": chessboard$(3, 2) = "wpawn"
'chessboard$(4, 2) = "wqueen": chessboard$(4, 3) = "wpawn"
'chessboard$(5, 1) = "wking": chessboard$(5, 5) = "wpawn"
'chessboard$(3, 4) = "wbishop": chessboard$(6, 2) = "wpawn"
'chessboard$(6, 3) = "wknight": chessboard$(7, 2) = "wpawn"
'chessboard$(8, 1) = "wrook": chessboard$(8, 2) = "wpawn"
'chessboard$(1, 7) = "bpawn": chessboard$(1, 8) = "brook"
'chessboard$(2, 6) = "bpawn": chessboard$(3, 6) = "bknight"
'chessboard$(3, 7) = "bpawn": chessboard$(1, 4) = "bbishop"
'chessboard$(4, 5) = "bpawn": chessboard$(4, 7) = "bqueen"
'chessboard$(5, 6) = "bpawn": chessboard$(5, 8) = "bking"
'chessboard$(6, 7) = "bpawn": chessboard$(5, 7) = "bbishop"
'chessboard$(7, 7) = "bpawn": chessboard$(8, 4) = "bknight"
'chessboard$(8, 7) = "bpawn": chessboard$(8, 8) = "brook"
'---------- EN PASSANT TEST ----------
'chessboard$(1, 1) = "wrook": chessboard$(1, 2) = "wpawn"
'chessboard$(2, 1) = "wknight": chessboard$(2, 2) = "wpawn"
'chessboard$(3, 1) = "wbishop": chessboard$(3, 4) = "wpawn"
'chessboard$(4, 1) = "wqueen": chessboard$(4, 2) = "wpawn"
'chessboard$(5, 1) = "wking": chessboard$(5, 2) = "wpawn"
'chessboard$(6, 1) = "wbishop": chessboard$(6, 2) = "wpawn"
'chessboard$(7, 1) = "wknight": chessboard$(7, 5) = "wpawn"
'chessboard$(8, 1) = "wrook": chessboard$(8, 2) = "wpawn"
'chessboard$(1, 7) = "bpawn": chessboard$(1, 8) = "brook"
'chessboard$(2, 7) = "bpawn": chessboard$(2, 8) = "bknight"
'chessboard$(3, 7) = "bpawn": chessboard$(3, 8) = "bbishop"
'chessboard$(4, 4) = "bpawn": chessboard$(4, 8) = "bqueen"
'chessboard$(5, 7) = "bpawn": chessboard$(5, 8) = "bking"
'chessboard$(6, 7) = "bpawn": chessboard$(6, 8) = "bbishop"
'chessboard$(7, 7) = "bpawn": chessboard$(7, 8) = "bknight"
'chessboard$(8, 5) = "bpawn": chessboard$(8, 8) = "brook"
END SUB
' -------------------------------------------------------------------------------------------------------
SUB drawBoard
sqcolor$ = "" 'Square color
piece$ = "" 'Piece to be printed
pfcolor = 0 'Piece front color
pbcolor = 1 'Piece back color
MT$ = CHR$(219)
SQ$ = MT$ + MT$ + MT$
'CLS 0
SCREEN 0
'COLOR 6
'LINE (0, 0)-(30, 30), 0, BF
LOCATE 1, 1
PRINT "HUO Chess (HUOC) by Spiros (h uo) Kakos - Alpha version"
FOR i = 1 TO 8
FOR j = 1 TO 8
IF (i + j) MOD 2 = 0 THEN
sqcolor$ = "b"
ELSEIF (i + j) MOD 2 <> 0 THEN
sqcolor$ = "w"
END IF
'Columns are the first number inside the parenthesis and
'because we start drawing the board from upside-up, we
'must apply this (9 - i) to draw the board correctly
'Print the square
LOCATE 2 + (9 - i), 1 + (j - 1) * 3
IF sqcolor$ = "w" THEN
COLOR 7, 0
ELSEIF sqcolor$ = "b" THEN
COLOR 0, 7
END IF
PRINT SQ$
'Determine the color of the piece to print
IF LEFT$(chessboard$(j, i), 1) = "w" AND sqcolor$ = "w" THEN
pfcolor = 15
pbcolor = 7
ELSEIF LEFT$(chessboard$(j, i), 1) = "w" AND sqcolor$ = "b" THEN
pfcolor = 15
pbcolor = 0
ELSEIF LEFT$(chessboard$(j, i), 1) = "b" AND sqcolor$ = "w" THEN
pfcolor = 5
pbcolor = 7
ELSEIF LEFT$(chessboard$(j, i), 1) = "b" AND sqcolor$ = "b" THEN
pfcolor = 5
pbcolor = 0
END IF
SELECT CASE chessboard$(j, i)
CASE "wking"
piece$ = "K"
CASE "wqueen"
piece$ = "Q"
CASE "wrook"
piece$ = "R"
CASE "wbishop"
piece$ = "B"
CASE "wknight"
piece$ = "N"
CASE "wpawn"
piece$ = "o"
CASE "bking"
piece$ = "K"
CASE "bqueen"
piece$ = "Q"
CASE "brook"
piece$ = "R"
CASE "bbishop"
piece$ = "B"
CASE "bknight"
piece$ = "N"
CASE "bpawn"
piece$ = "o"
CASE ""
piece$ = ""
END SELECT
'Print the piece
LOCATE 2 + (9 - i), 2 + (j - 1) * 3
COLOR pfcolor, pbcolor
PRINT piece$
NEXT j
NEXT i
'Restore color of screen and text
COLOR 7, 0
'Restore cursor
LOCATE 13, 1
END SUB
' -------------------------------------------------------------------------------------------------------
SUB ElegxosNomimotitas (ENSkakiera() AS STRING, checkForDanger AS INTEGER, startColumn AS INTEGER, startRank AS INTEGER, finishColumn AS INTEGER, finishRank AS INTEGER, MovingPieceEN AS STRING)
'Some GREEK: Skakiera in Greek means "chessboard". Nomimotita in Greek means "legality".
'This SUB checks the legality of the moves enetered by the player or thought by the computer
DIM ProsorinoKommatiEN AS STRING
DIM NomimotitaEN
NomimotitaEN = 1 'Set legality (= Nomimotita in Greek) to TRUE. If a problem is found then it will be set to FALSE.
IF (debugMode = 1) THEN
PRINT ""
PRINT "--------------- DEBUG MESSAGE ---------------"
PRINT "ElegxosNomimotitas CALLED"
PRINT ""
PRINT "Start column: " + STR$(startColumn)
PRINT "Start rank : " + STR$(startRank)
PRINT "End column : " + STR$(finishColumn)
PRINT "End rank : " + STR$(finishRank)
PRINT ""
PRINT "Moving piece: " + MovingPieceEN$
PRINT "ENSkakiera(1,1) = " + ENSkakiera(1, 1)
PRINT "ENSkakiera(1,2) = " + ENSkakiera(1, 2)
PRINT ""