-
Notifications
You must be signed in to change notification settings - Fork 0
/
HUOC_GRAPHICS_DEV.bas
2819 lines (2237 loc) · 116 KB
/
HUOC_GRAPHICS_DEV.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/
'Graphics taken from Deep Basic by Thomas McBurney
'Check https://web.archive.org/web/20081230182814/http://home.pacific.net.au/~tommyinoz/db.html
'Check also https://archive.org/details/DeepBasic
'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 NodesMoves0$(1000000)
DIM SHARED NodesMoves1$(1000000)
DIM SHARED NodesMoves2$(1000000)
DIM SHARED NodesMoves3$(1000000)
DIM SHARED NodesMoves4$(1000000)
'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 CalledFromCheckCheck '14/3/2021
COMMON SHARED huologs$, logLine$
OPEN "Huo Chess Logs - Nodes before.txt" FOR OUTPUT AS #1
OPEN "Huo Chess Logs - Nodes after.txt" FOR OUTPUT AS #2
WRITE #1, "Huo Chess Logs - Nodes before"
WRITE #1, ""
WRITE #2, "Huo Chess Logs - Nodes after"
WRITE #2, ""
COMMON SHARED thinkingDepth
COMMON SHARED Move
'Set debugMode = 1 if you want debugging messages to appear, else set to 0
debugMode = 0
PRINT "Huo Chess - HUOC GRAPHICS v0.9 by Spiros Kakos (2020, 2021, 2022)"
PRINT "DEVELOPERS EDITION"
PRINT ""
PRINT "IMPORTANT NOTE: Remember to delete text log files in the application folder!"
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 "Graphics taken from Deep Basic by Thomas McBurney"
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
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"
CALL initialPosition
DIM SHARED P$(6), PIECE%(7, 16, 8, 2), CMOVES%(6, 100), HMOVES%(6, 100)
DIM SHARED BO%(8, 9), CB%(9, 9)
DIM SHARED ENGINE%(10, 100, 5), SCORE%(12, 2)
DIM SHARED PAWN%(8, 2), BESTL%(10, 12, 4), HISTORY%(4, 10)
DIM SHARED QENGINE%(15, 30, 5), QSCORE%(15, 2), QB%(8, 9)
DIM SHARED FIRSTPLY%(218)
DIM SHARED FIND$(10), QUERY$(9), dsc$(10), RECORD$(10)
DIM SHARED TAKEBACK%(5, 200), v%(6)
DIM SHARED TESTMODE%, NODES, EVAL%, MATEH%, MATEC%, SIDE%, PHASE%, HKY%, HKX%
DIM SHARED CKY%, CKX%, SCORE%, TEMP1%, TEMP2%, TEMP3%, A%, TX%, TY%
DIM C$(8), CY$(8)
' GRAPHICS
DIM SHARED Gpiece(0 TO 400) AS INTEGER
DIM SHARED blot(0 TO 400) AS INTEGER
DIM SHARED GraphicImage(0 TO 4800) AS INTEGER
'SIDE 0 = COMPUTER PLAYS BLACK
'SIDE 1 = COMPUTER PLAYS WHITE
'SIDE% = 0
SCREEN 9 '12
'**** LOAD IMAGES OF PIECES
LoadPieces:
'**** LOAD GRAPHICS OF PIECES OFF DISK
DEF SEG = VARSEG(GraphicImage(0))
BLOAD "PIECES.EGA", VARPTR(GraphicImage(0))
P$(1) = "P": P$(2) = "N": P$(3) = "B": P$(4) = "R": P$(5) = "Q": P$(6) = "K"
C$(1) = "A": C$(2) = "B": C$(3) = "C": C$(4) = "D": C$(5) = "E": C$(6) = "F": C$(7) = "G": C$(8) = "H"
CY$(1) = "8": CY$(2) = "7": CY$(3) = "6": CY$(4) = "5": CY$(5) = "4": CY$(6) = "3": CY$(7) = "2": CY$(8) = "1"
' PIECE VALUE
v%(1) = 100: v%(2) = 300: v%(3) = 305
v%(4) = 500: v%(5) = 900: v%(6) = 19000
'MAXQDEPTH% = 4
'LEVEL% = 10
'MOVESPERHOUR% = 360
'BEEPMODE% = 0
' *** KNIGHT
PIECE%(2, 1, 1, 1) = -1: PIECE%(2, 1, 1, 2) = -2
PIECE%(2, 2, 1, 1) = 1: PIECE%(2, 2, 1, 2) = -2
PIECE%(2, 3, 1, 1) = 2: PIECE%(2, 3, 1, 2) = -1
PIECE%(2, 4, 1, 1) = 2: PIECE%(2, 4, 1, 2) = 1
PIECE%(2, 5, 1, 1) = 1: PIECE%(2, 5, 1, 2) = 2
PIECE%(2, 6, 1, 1) = -1: PIECE%(2, 6, 1, 2) = 2
PIECE%(2, 7, 1, 1) = -2: PIECE%(2, 7, 1, 2) = 1
PIECE%(2, 8, 1, 1) = -2: PIECE%(2, 8, 1, 2) = -1
'*** BISHOP
FOR I = 1 TO 8
PIECE%(3, 1, I, 1) = I * -1: PIECE%(3, 1, I, 2) = I * -1
PIECE%(3, 2, I, 1) = I: PIECE%(3, 2, I, 2) = I * -1
PIECE%(3, 3, I, 1) = I: PIECE%(3, 3, I, 2) = I
PIECE%(3, 4, I, 1) = I * -1: PIECE%(3, 4, I, 2) = I
NEXT
' **** ROOK
FOR I = 1 TO 8
PIECE%(4, 1, I, 1) = 0: PIECE%(4, 1, I, 2) = I * -1
PIECE%(4, 2, I, 1) = I: PIECE%(4, 2, I, 2) = 0
PIECE%(4, 3, I, 1) = 0: PIECE%(4, 3, I, 2) = I
PIECE%(4, 4, I, 1) = I * -1: PIECE%(4, 4, I, 2) = 0
NEXT
' **** QUEEN
FOR D = 1 TO 4
FOR I = 1 TO 8
PIECE%(5, D, I, 1) = PIECE%(3, D, I, 1): PIECE%(5, D, I, 2) = PIECE%(3, D, I, 2)
NEXT
NEXT
FOR D = 1 TO 4
FOR I = 1 TO 8
PIECE%(5, D + 4, I, 1) = PIECE%(4, D, I, 1): PIECE%(5, D + 4, I, 2) = PIECE%(4, D, I, 2)
NEXT
NEXT
' **** KING
FOR I = 1 TO 8
PIECE%(6, I, 1, 1) = PIECE%(5, I, 1, 1): PIECE%(6, I, 1, 2) = PIECE%(5, I, 1, 2)
NEXT
' ***** SUPER PIECE
FOR D = 1 TO 8
FOR S = 1 TO 8
PIECE%(7, D, S, 1) = PIECE%(5, D, S, 1): PIECE%(7, D, S, 2) = PIECE%(5, D, S, 2)
NEXT
NEXT
FOR D = 1 TO 8
PIECE%(7, D + 8, 1, 1) = PIECE%(2, D, 1, 1): PIECE%(7, D + 8, 1, 2) = PIECE%(2, D, 1, 2)
NEXT
START:
'************************** START **************************************
COLOR 7
MAIN:
INITBOARD:
'******************************************************************
' Initialise the board
'******************************************************************
FOR Y = 1 TO 9
FOR X = 1 TO 8
BO%(X, Y) = 0
NEXT
NEXT
FOR X = 1 TO 8: BO%(X, 2) = -1: BO%(X, 7) = 1: NEXT
BO%(1, 1) = -4: BO%(8, 1) = -4: BO%(2, 1) = -2: BO%(7, 1) = -2
BO%(3, 1) = -3: BO%(6, 1) = -3: BO%(4, 1) = -5: BO%(5, 1) = -6
FOR X = 1 TO 8: BO%(X, 8) = BO%(X, 1) * -1: NEXT
FOR I = 1 TO 4: BO%(I, 9) = 1: BO%(I + 4, 9) = 0: NEXT
CALL DRAWBOARD
COLOR 7
'LOCATE 2, 58: PRINT "MOVES LIST"
LOCATE 2, 58: PRINT "H UO CHESS"
'LOCATE 5, 55: INPUT "Enter move:", Move$
'LOCATE 7, 55: PRINT "Thinking..."
'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"
IF playerColor$ = "w" THEN
whoPlays$ = "Human"
CALL PlayerMove
END IF
IF playerColor$ = "b" THEN
whoPlays$ = "HY"
LOCATE 7, 55: 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 9, 55: PRINT "Check!": LOCATE 10, 55: PRINT "Press Enter": DO: LOOP UNTIL INKEY$ <> "": LOCATE 9, 55: PRINT " ": LOCATE 10, 55: PRINT " "
LOCATE 5, 67: PRINT " "
LOCATE 5, 55: INPUT "Enter 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
'LOCATE 8, 55: PRINT "Move OK"
'LOCATE 9, 55: INPUT "Checkpoint", A$
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
whoPlays$ = "HY"
LOCATE 5, 55: PRINT "Your move: "
LOCATE 7, 55: PRINT "Thinking... "
CALL computerMove
ELSE
LOCATE 5, 55: PRINT "Invalid move "
LOCATE 7, 55: PRINT "Press a key "
DO: LOOP UNTIL INKEY$ <> ""
LOCATE 7, 55: PRINT " "
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 9, 55: PRINT "Check!": LOCATE 10, 55: PRINT "Press Enter": DO: LOOP UNTIL INKEY$ <> "": LOCATE 9, 55: PRINT " ": LOCATE 10, 55: PRINT " "
IF computerLogs$ = "y" THEN debugMode = 1 ELSE debugMode = 0
' ------------------- 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
'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) = ""
'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
'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)
IF startingColumn = 2 AND startingRank = 4 AND finishingColumn = 5 AND finishingRank = 7 THEN
PRINT ""
PRINT "BISHOP MOVE ANALYZED: " + STR$(startingColumn) + STR$(startingRank) + " -> " + STR$(finishingColumn) + STR$(finishingRank)
PRINT "What is in e7: " + chessboard$(5, 7)
PRINT "Moving piece : " + MovingPiece$
PRINT "Prosorino kommati : " + ProsorinoKommati$
PRINT "Legality of the move analyzed: " + STR$(Nomimotita)
INPUT A$
END IF
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$())
IF debugMode = 1 THEN
PRINT ""
PRINT "NodeLevel_0_count = " + STR$(NodeLevel_0_count)
PRINT "NodeLevel_1_count = " + STR$(NodeLevel_1_count)
END IF
'-------------- MinMax --------------
'Store scores
NodesAnalysis0(NodeLevel_0_count, 1) = positionScore
' Store parents
NodesAnalysis0(NodeLevel_0_count, 2) = 0
'Store the move
NodesMoves0$(NodeLevel_0_count) = STR$(startingColumn) + STR$(startingRank) + " -> " + STR$(finishingColumn) + STR$(finishingRank)
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 7, 55: 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
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 7, 55: PRINT "My move: " + startingColumnText$ + STR$(bestStartingRank) + " -> " + finishingColumnText$ + STR$(bestFinishingRank)
CALL DRAWBOARD
'Increase move number of the game
IF playerColor$ = "b" THEN MoveNumber = MoveNumber + 1
'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 ----------
'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"