forked from Chessforeva/Csharp4chess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
c0_chess.cs
2673 lines (2272 loc) · 98.3 KB
/
c0_chess.cs
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
//=============================================================================
// C# chess logic library as a class file
// No chess engine here. Samples below.
//
// Just define variable in upper class
// c0_chess C0 = new c0_chess();
//=============================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
public class c0_chess
{
public string c0_position;
public int c0_side;
public int c0_sidemoves;
public bool c0_wKingmoved, c0_bKingmoved, c0_wLRockmoved, c0_wRRockmoved;
public bool c0_bLRockmoved, c0_bRRockmoved, c0_w00, c0_b00;
public int c0_lastmovepawn;
public string c0_become;
public string c0_become_from_engine; // just promos from engine
public string c0_moveslist;
public string c0_moves2do;
public string c0_foundmove;
public string c0_start_FEN;
public bool c0_fischer;
public string c0_fischer_cst;
public bool c0_PG_viewer; // Set true for on-error-prints or false for silent processing...
public string c0_PG_1;
public string[] c0_PGN_header = new String[20];
public bool c0_errflag;
public string PGN_text; // PGN support, the game data will be here....
public string c0_NAGs;
public string[] c0_opn = new String[12];
public c0_chess()
{
c0_Initvariables();
// Show samples of chess logic in Log window...
//c0_SAMPLES();
}
public void c0_Initvariables()
{
c0_position="";
c0_side=1;
c0_sidemoves=1;
c0_wKingmoved = false;
c0_bKingmoved = false;
c0_wLRockmoved = false;
c0_wRRockmoved = false;
c0_bLRockmoved = false;
c0_bRRockmoved = false;
c0_w00 = false;
c0_b00 = false;
c0_lastmovepawn = 0;
c0_become="";
c0_become_from_engine="";
c0_moveslist = "";
c0_moves2do="";
c0_foundmove="";
c0_start_FEN="";
c0_fischer=false;
c0_fischer_cst="";
c0_PG_viewer=true;
c0_PG_1="";
c0_errflag=false;
PGN_text="";
c0_NAGs="";
c0_Openings_define();
}
public string Mid(string s, int f, int l) { return (f<0 || l<=0 || f>=s.Length ? "" : s.Substring(f,l)); }
public string Mid2(string s, int f) { return (f<0 || f>=s.Length ? "" : s.Substring(f)); }
public int Asc(string s) { return (int)(s[0]); }
public string Chr(int i) { return (""+(char)i); }
public int charCodeAt(string str, int at) { return Asc(Mid(str,at,1)); }
public int Int(string s)
{ int c=charCodeAt(s,0)-48; return ((c>=0 && c<=9) ? Convert.ToInt32(s) : 0); }
public bool window_confirm(string messtxt) { return true; } // No dialogs at the moment...
public string c0_ReplaceAll(string Source, string stringToFind, string stringToReplace)
{ return Source.Replace(stringToFind,stringToReplace); }
public string LowCase(string Str) { return Str.ToLower(); } // Lowercase
public string Caps(string Str) { return Str.ToUpper(); } // Uppercase
public int Len(string s) { return s.Length; }
public int InStr(string s, string s2) { return s.IndexOf(s2); }
public string Str(int i) { return i.ToString(); }
public void Log(string s) { System.Diagnostics.Debug.WriteLine(s); }
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Call this public to get samples working...
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void c0_SAMPLES()
{
string PGN0, mlist0, PGN3, mlist3;
Log("============================================");
Log("===== Routine of SAMPLES for chess logic ===");
Log("============================================");
c0_side=1; // This side is white. For black set -1
c0_set_start_position(""); // Set the initial position...
// 1. Test for basic chess publics (ok)...
Log( "Setting up the starting position" );
c0_set_start_position("");
Log( c0_position );
Log( "FEN public : " + c0_get_FEN() );
// Make a first move e4...
c0_move_to("e2","e4");
Log( "Position after e4:");
Log( c0_position );
// Show the last move made...
Log( "Last move:" + c0_D_last_move_was() );
// switch sides...
c0_sidemoves = -c0_sidemoves;
Log( "All movements till now:" + c0_moveslist );
// To see possible movements...
Log( "Now possible moves:");
Log( c0_get_next_moves() );
// And take it back...
c0_take_back();
Log( "Position after takeback" );
Log( c0_position );
c0_sidemoves = -c0_sidemoves;
// Other publics:
// Is e2-e4 a legal move in current position...
Log( "Can move a2-a4? ");
Log( (c0_D_can_be_moved("a2","a4") ? "can" : "can't") );
// Is there stalemate to the white king right now? ("b"/"w"- the parameter)
Log( "White king in stalemate? ");
Log( (c0_D_is_pate_to_king("w") ? "is" : "isn't" ) );
// Is there check to the white king right now?
Log( "Check to white king?");
Log( (c0_D_is_check_to_king("w") ? "is" : "isn't") );
// Is there checkmate to the white king right now?
Log( "Is checkmate to white king?");
Log( (c0_D_is_mate_to_king("w") ? "is" : "isn't") );
// What a piece on the square g7?
Log( "Piece on g7:" + c0_D_what_at("g7") );
// Is the square g6 empty? (no piece on it)
Log( "Is empty square g6? ");
Log( (c0_D_is_empty("g6") ? "is" : "isn't") );
// FEN position setup test
c0_start_FEN="7k/Q7/2P2K2/8/8/8/8/8 w - - 0 70";
c0_set_start_position("");
// other way
//c0_set_FEN ("7k/Q7/2P2K2/8/8/8/8/8 w - - 0 70");
Log( "Position after setup FEN=7k/Q7/2P2K2/8/8/8/8/8 w - - 0 70");
Log( c0_position );
c0_start_FEN="";
c0_set_start_position("");
// 2.PGN publics test (ok):
Log("PGN -> moves");
PGN0="1.d4 d5 2.c4 e6 {comment goes here} 3.Nf3 Nf6 4.g3 Be7 (4.h4 or variant) 5.Bg2 0-0 6.0-0 dxc4 7.Qc2 a6 8.Qxc4 b5 9.Qc2 Bb7 10.Bd2 Be4 11.Qc1 Bb7 12.Qc2 Ra7 13.Rc1 Be4 14.Qb3 Bd5 15.Qe3 Nbd7 16.Ba5 Bd6 17.Nc3 Bb7 18.Ng5 Bxg2 19.Kxg2 Qa8+ 20.Qf3 Qxf3+ 21.Kxf3 e5 22.e3 Be7 23.Ne2 Re8 24.Kg2 Nd5 25.Nf3 Bd6 26.dxe5 Nxe5 27.Nxe5 Rxe5 28.Nd4 Ra8 29.Nc6 Re6 30.Rc2 Nb6 31.b3 Kf8 32.Rd1 Ke8 33.Nd4 Rf6 34.e4 Rg6 35.e5 Be7 36.Rxc7 Nd5 37.Rb7 Bd8 38.Nf5 Nf4+ 39.Kf3 Bxa5 40.gxf4 Bb4 41.Rdd7 Rc8 42.Rxf7 Rc3+ 43.Ke4 1-0";
mlist0=c0_get_moves_from_PGN(PGN0);
Log(mlist0);
Log("moves -> PGN (reverse action)");
Log( c0_put_to_PGN(mlist0) );
c0_start_FEN="";
c0_set_start_position("");
// 3.Fischerrandom support test (ok):
Log("Fischer-random PGN -> moves");
PGN3="[White Aronian, Levon][Black Rosa, Mike][Result 0:1][SetUp 1][FEN bbrkqnrn/pppppppp/8/8/8/8/PPPPPPPP/BBRKQNRN w GCgc - 0 0]";
PGN3 = PGN3 + "1. c4 e5 2. Nhg3 Nhg6 3. b3 f6 4. e3 b6 5. Qe2 Ne6 6. Qh5 Rh8 7. Nf5 Ne7 8. Qxe8+ Kxe8 9. N1g3 h5 10. Nxe7 Kxe7 11. d4 d6 12. h4 Kf7 13. d5 Nf8 14. f4 c6 15. fxe5 dxe5 16. e4 Bd6 17. Bd3 Ng6 18. O-O Nxh4 19. Be2 Ng6 20. Nf5 Bc5+ 21. Kh2 Nf4 22. Rc2 cxd5 23. exd5 h4 24. Bg4 Rce8 25. Bb2 g6 26. Nd4 exd4 27. Rxf4 Bd6 0-1";
mlist3= c0_get_moves_from_PGN(PGN3);
Log(mlist3);
Log("moves -> PGN (reverse action)");
Log( c0_put_to_PGN(mlist3) );
// clear it all
c0_start_FEN="";
c0_set_start_position("");
Log("Starting position...");
// There is a short data openings database public
Log("Openings, variants on request: e2e4...");
Log( c0_Opening("e2e4") );
Log("On request: 1.d4 d5 2.c4...");
Log( c0_Opening("d2d4d7d5c2c4" ));
Log("Chess for C# code :)");
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// set up starting position...
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void c0_set_start_position( string c0_mlist )
{
int c0_z, c0_q;
string c0_from_at, c0_to_at;
string c0_pos2;
if( Len(c0_start_FEN)>0 )
{
c0_set_FEN( c0_start_FEN );
if(c0_fischer) c0_fischer_adjustmoved();
}
else
{
c0_position="";
if(Len(c0_mlist)==0)
{
c0_add_piece("wpa2"); c0_add_piece("wpb2"); c0_add_piece("wpc2"); c0_add_piece("wpd2");
c0_add_piece("wpe2"); c0_add_piece("wpf2"); c0_add_piece("wpg2"); c0_add_piece("wph2");
c0_add_piece("wRa1"); c0_add_piece("wNb1"); c0_add_piece("wBc1"); c0_add_piece("wQd1");
c0_add_piece("wKe1"); c0_add_piece("wBf1"); c0_add_piece("wNg1"); c0_add_piece("wRh1");
c0_add_piece("bpa7"); c0_add_piece("bpb7"); c0_add_piece("bpc7"); c0_add_piece("bpd7");
c0_add_piece("bpe7"); c0_add_piece("bpf7"); c0_add_piece("bpg7"); c0_add_piece("bph7");
c0_add_piece("bRa8"); c0_add_piece("bNb8"); c0_add_piece("bBc8"); c0_add_piece("bQd8");
c0_add_piece("bKe8"); c0_add_piece("bBf8"); c0_add_piece("bNg8"); c0_add_piece("bRh8");
}
else
{
c0_position = "wpa2;wpb2;wpc2;wpd2;wpe2;wpf2;wpg2;wph2;" +
"wRa1;wNb1;wBc1;wQd1;wKe1;wBf1;wNg1;wRh1;" +
"bpa7;bpb7;bpc7;bpd7;bpe7;bpf7;bpg7;bph7;" +
"bRa8;bNb8;bBc8;bQd8;bKe8;bBf8;bNg8;bRh8;";
}
c0_wKingmoved = false;
c0_bKingmoved = false;
c0_wLRockmoved = false;
c0_wRRockmoved = false;
c0_bLRockmoved = false;
c0_bRRockmoved = false;
c0_w00 = false;
c0_b00 = false;
c0_lastmovepawn = 0;
c0_sidemoves=1;
}
c0_become="";
c0_become_from_engine=""; // just engine
c0_moveslist = "";
if(Len(c0_mlist)>0)
{
for(c0_z=0;c0_z<Len(c0_mlist);c0_z+=4)
{
c0_from_at=Mid(c0_mlist,c0_z,2);
c0_to_at=Mid(c0_mlist,c0_z+2,2);
if(c0_z+4<Len(c0_mlist) && Mid(c0_mlist,c0_z+4,1)=="[")
{
c0_become_from_engine=Mid(c0_mlist,c0_z+5,1);
c0_z+=3;
}
else c0_become_from_engine="";
if(c0_fischer) c0_fischer_cstl_move(c0_from_at + c0_to_at,false);
else
c0_moveto(c0_convH888(c0_from_at), c0_convH888(c0_to_at), false);
c0_sidemoves=-c0_sidemoves;
}
if( Len(c0_start_FEN)>0 )
{
c0_set_board_situation( c0_position, c0_wKingmoved, c0_wLRockmoved, c0_wRRockmoved, c0_w00,
c0_bKingmoved, c0_bLRockmoved, c0_bRRockmoved, c0_b00, c0_lastmovepawn, c0_moveslist, c0_sidemoves );
}
else
{
c0_pos2=c0_position;
c0_position="";
for(c0_q=0;c0_q<Len(c0_pos2);c0_q+=5) c0_add_piece(Mid(c0_pos2,c0_q,4));
}
}
c0_moveslist = c0_mlist;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// Set board situation...
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void c0_set_board_situation( string c0_figlist, bool c0_wK, bool c0_wLR, bool c0_wRR,
bool c0_w_00, bool c0_bK, bool c0_bLR, bool c0_bRR, bool c0_b_00, int c0_elpas, string c0_ml, int c0_s )
{
int i;
c0_position="";
i=0;
while(i<Len(c0_figlist))
{
c0_add_piece( Mid(c0_figlist,i,4) );
i+=4; if( i<Len(c0_figlist) && Mid(c0_figlist,i,1)==";" ) i++;
}
c0_wKingmoved = c0_wK;
c0_bKingmoved = c0_bK;
c0_wLRockmoved = c0_wLR;
c0_wRRockmoved = c0_wRR;
c0_bLRockmoved = c0_bLR;
c0_bRRockmoved = c0_bRR;
c0_w00 = c0_w_00;
c0_b00 = c0_b_00;
c0_lastmovepawn = c0_elpas;
c0_become="";
c0_become_from_engine=""; // just engine
c0_moveslist = c0_ml;
c0_sidemoves=c0_s;
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// add a piece at position...
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void c0_add_piece( string c0_pstring )
{
string c0_1_at;
c0_1_at=Mid(c0_pstring,2,2);
// There were other visual activities before...
if(InStr(c0_position,c0_1_at)<0) c0_position = c0_position + c0_pstring + ";";
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// remove a piece from position...
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void c0_clear_at( string c0_1_at )
{
int c0_a;
c0_a=InStr(c0_position,c0_1_at);
if(c0_a>=0) c0_position=Mid(c0_position,0,c0_a-2) + Mid2(c0_position,c0_a+3);
}
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
// move visualy a piece...
//----------------------------------------------------------------------------------------------------------------------------------------------------------------------
public void c0_just_move_piece( string c0_2_from, string c0_2_to )
{
int c0_a;
c0_clear_at( c0_2_to );
c0_a=InStr(c0_position,c0_2_from);
if(c0_a>=0)
{
c0_position=c0_ReplaceAll( c0_position, c0_2_from, c0_2_to );
c0_moves2do = c0_moves2do + c0_2_from + c0_2_to;
}
}
// and add a promotion (or castling) indicator...
public void c0_and_promote_or_castle()
{
if(Len(c0_become)>0) c0_moves2do = c0_moves2do + "[" + c0_become + "]";
}
//############################################################
// CHESS related part for chess play
//############################################################
//-------------------------------------------------
public string c0_convE2(int c0_vertikali, int c0_horizontali)
{
return Chr(96+c0_horizontali) + Str(c0_vertikali);
}
//-------------------------------------------------
public string c0_convE777(string c0_verthoriz)
{
return Chr(96+Int(Mid(c0_verthoriz,1,1))) + Mid(c0_verthoriz,0,1);
}
//-------------------------------------------------
public string c0_conv52(int c0_vertikali, int c0_horizontali)
{
return Str(c0_vertikali) + Str(c0_horizontali);
}
//-------------------------------------------------
public string c0_convH888(string c0_at8)
{
int c0_8horiz, c0_8vert;
c0_8horiz=Asc( Mid(c0_at8,0,1) ) - 96;
c0_8vert=Int( Mid(c0_at8,1,1));
return Str(c0_8vert) + Str(c0_8horiz);
}
//-------------------------------------------------
public void c0_move_to(string c0_Zstr1, string c0_Zstr2)
{
c0_moveto( c0_convH888(c0_Zstr1), c0_convH888(c0_Zstr2), true );
}
//-------------------------------------------------
public void c0_moveto(string c0_from_at, string c0_to_at, bool c0_draw)
{
int c0_vert, c0_horiz, c0_vert2, c0_horiz2, c0_p, c0_p2, c0_p3;
string c0_color, c0_figure, save_c0_position;
c0_vert = Int(Mid(c0_from_at,0,1));
c0_horiz= Int(Mid(c0_from_at,1,1));
c0_vert2 = Int(Mid(c0_to_at,0,1));
c0_horiz2= Int(Mid(c0_to_at,1,1));
c0_p=InStr(c0_position, c0_convE2(c0_vert,c0_horiz) );
c0_color=Mid(c0_position,c0_p-2,1);
c0_figure=Mid(c0_position,c0_p-1,1);
save_c0_position=c0_position;
c0_lastmovepawn = 0;
if(c0_draw) c0_become="";
if(c0_draw)
{
save_c0_position=c0_position;
c0_just_move_piece( c0_convE2(c0_vert, c0_horiz), c0_convE2(c0_vert2, c0_horiz2) );
c0_position=save_c0_position;
}
c0_p2=InStr(c0_position, c0_convE2(c0_vert2,c0_horiz2) );
if(c0_p2>=0)
{
c0_position = Mid(c0_position,0,c0_p2-2) + Mid2(c0_position,c0_p2+3);
if(!c0_wLRockmoved && c0_convE2(c0_vert2,c0_horiz2)=="a1") c0_wLRockmoved=true;
if(!c0_wRRockmoved && c0_convE2(c0_vert2,c0_horiz2)=="h1") c0_wRRockmoved=true;
if(!c0_bLRockmoved && c0_convE2(c0_vert2,c0_horiz2)=="a8") c0_bLRockmoved=true;
if(!c0_bRRockmoved && c0_convE2(c0_vert2,c0_horiz2)=="h8") c0_bRRockmoved=true;
}
else
{
if(c0_figure=="R")
{
if(c0_color=="w")
{
if(c0_convE2(c0_vert,c0_horiz)=="a1") c0_wLRockmoved=true;
if(c0_convE2(c0_vert,c0_horiz)=="h1") c0_wRRockmoved=true;
}
else
{
if(c0_convE2(c0_vert,c0_horiz)=="a8") c0_bLRockmoved=true;
if(c0_convE2(c0_vert,c0_horiz)=="h8") c0_bRRockmoved=true;
}
}
if(c0_figure=="K")
{
if(!c0_wKingmoved && c0_color=="w")
{
if(c0_convE2(c0_vert,c0_horiz)=="e1" && c0_convE2(c0_vert2,c0_horiz2)=="g1") // 0-0
{
if(c0_draw)
{
save_c0_position=c0_position;
c0_just_move_piece("h1","f1");
c0_position=save_c0_position;
}
c0_position = c0_ReplaceAll(c0_position, "h1", "f1" ); // Rf1
c0_w00 = true;
c0_become="0";
}
if(c0_convE2(c0_vert,c0_horiz)=="e1" && c0_convE2(c0_vert2,c0_horiz2)=="c1") // 0-0-0
{
if(c0_draw)
{
save_c0_position=c0_position;
c0_just_move_piece("a1","d1");
c0_position=save_c0_position;
}
c0_position = c0_ReplaceAll(c0_position, "a1", "d1" ); // Rd1
c0_w00 = true;
c0_become="0";
}
c0_wKingmoved=true;
}
if(!c0_bKingmoved && c0_color=="b")
{
if(c0_convE2(c0_vert,c0_horiz)=="e8" && c0_convE2(c0_vert2,c0_horiz2)=="g8") // 0-0
{
if(c0_draw)
{
save_c0_position=c0_position;
c0_just_move_piece("h8","f8");
c0_position=save_c0_position;
}
c0_position = c0_ReplaceAll(c0_position, "h8", "f8" ); // Rf8
c0_b00 = true;
c0_become="0";
}
if(c0_convE2(c0_vert,c0_horiz)=="e8" && c0_convE2(c0_vert2,c0_horiz2)=="c8") // 0-0-0
{
if(c0_draw)
{
save_c0_position=c0_position;
c0_just_move_piece("a8","d8");
c0_position=save_c0_position;
}
c0_position = c0_ReplaceAll(c0_position, "a8", "d8" ); // Rd8
c0_b00 = true;
c0_become="0";
}
c0_bKingmoved=true;
}
}
}
if(c0_figure=="p") // pawn
{
if(c0_vert2==8 || c0_vert2==1)
{
if(Len(c0_become_from_engine)>0)
{
c0_figure= c0_become_from_engine;
}
else
{
if(c0_draw)
{
if(window_confirm("Promote a QUEEN?"))
{
c0_figure = "Q";
}
else if(window_confirm("Then a ROOK?"))
{
c0_figure = "R";
}
else if(window_confirm("Maybe a BISHOP?"))
{
c0_figure = "B";
}
else if(window_confirm("Really a KNIGHT????"))
{
c0_figure = "N";
}
else
{
//Log("I know, You need a new QUEEN.");
c0_figure = "Q";
}
}
else c0_figure="Q";
}
if(c0_draw)
{
c0_become=c0_figure;
// just put in queue... (no,will be detected above in 3D)...
//save_c0_position=c0_position;
//c0_moves2do = c0_moves2do + c0_convE2(c0_vert2,c0_horiz2) + "=" + c0_become;
//c0_position=save_c0_position;
}
c0_position = c0_ReplaceAll(c0_position, "p" + c0_convE2(c0_vert,c0_horiz),
c0_figure + c0_convE2(c0_vert,c0_horiz) );
}
if(c0_p2<0 && c0_horiz!=c0_horiz2)
{
if(c0_draw)
{
save_c0_position=c0_position;
c0_clear_at( c0_convE2(c0_vert,c0_horiz2) );
c0_position=save_c0_position;
}
c0_p3=InStr(c0_position, c0_convE2(c0_vert,c0_horiz2) );
c0_position = Mid(c0_position,0,c0_p3-2) + Mid2(c0_position,c0_p3+3);
}
if((c0_vert==2 && c0_vert2==4) || (c0_vert==7 && c0_vert2==5)) c0_lastmovepawn = c0_horiz;
}
c0_position = c0_ReplaceAll(c0_position, c0_convE2(c0_vert,c0_horiz), c0_convE2(c0_vert2,c0_horiz2) );
if(c0_draw)
{
c0_moveslist = c0_moveslist + c0_convE2(c0_vert,c0_horiz) + c0_convE2(c0_vert2,c0_horiz2);
if(Len(c0_become)>0) c0_moveslist = c0_moveslist + "[" + c0_become + "]";
c0_and_promote_or_castle();
}
}
//-------------------------------------------------
public string c0_D_last_move_was()
{
string c0_ret;
c0_ret="";
if( Len(c0_moveslist)>0 )
{
if (Mid(c0_moveslist, Len(c0_moveslist)-1, 1 )=="]" ) c0_ret= Mid(c0_moveslist, Len(c0_moveslist)-7, 7 );
else c0_ret= Mid(c0_moveslist, Len(c0_moveslist)-4, 4 );
}
return c0_ret;
}
//-------------------------------------------------
public void c0_take_back()
{
string c0_movespre;
c0_movespre="";
if( Len(c0_moveslist)>0 )
{
if( Mid(c0_moveslist, Len(c0_moveslist)-1, 1 )=="]" ) c0_movespre= Mid(c0_moveslist, 0, Len(c0_moveslist)-7 );
else c0_movespre= Mid(c0_moveslist, 0, Len(c0_moveslist)-4 );
}
c0_set_start_position( c0_movespre );
}
//-------------------------------------------------
public bool c0_D_is_empty(string c0_Zstr)
{
string c0_Zs2;
c0_Zs2=c0_convH888(c0_Zstr);
return c0_is_empty( Int(Mid(c0_Zs2,0,1)), Int(Mid(c0_Zs2,1,1)));
}
//-------------------------------------------------
public bool c0_is_empty(int c0_Zvert, int c0_Zhoriz)
{
bool c0_good;
int c0_pz2;
c0_good = true;
if(c0_Zvert<1 || c0_Zvert>8 || c0_Zhoriz<1 || c0_Zhoriz>8) c0_good=false;
else
{
c0_pz2=InStr( c0_position, c0_convE2(c0_Zvert,c0_Zhoriz) );
if(c0_pz2>=0) c0_good=false;
}
return c0_good;
}
//-------------------------------------------------
public string c0_D_what_at(string c0_Zstr1)
{
string c0_ret;
int c0_pz2;
c0_ret="";
c0_pz2=InStr(c0_position, c0_Zstr1 );
if(c0_pz2>=0) c0_ret=Mid(c0_position,c0_pz2-2,2);
return c0_ret;
}
//-------------------------------------------------
public bool c0_D_is_enemy(string c0_Zstr,string c0_mycolor)
{
string c0_Zs2;
c0_Zs2=c0_convH888(c0_Zstr);
return c0_is_enemy( Int(Mid(c0_Zs2,0,1)),Int(Mid(c0_Zs2,1,1)), c0_mycolor);
}
//-------------------------------------------------
public bool c0_is_enemy(int c0_Zvert, int c0_Zhoriz, string c0_mycolor)
{
bool c0_is_there;
int c0_pz2;
c0_is_there =false;
if(c0_Zvert>=1 && c0_Zvert<=8 && c0_Zhoriz>=1 && c0_Zhoriz<=8)
{
c0_pz2=InStr(c0_position, c0_convE2(c0_Zvert,c0_Zhoriz) );
if(c0_pz2>=0 && Mid(c0_position,c0_pz2-2,1)!=c0_mycolor) c0_is_there=true;
}
return c0_is_there;
}
//-------------------------------------------------
public bool c0_D_is_emptyline(string c0_Zstr1, string c0_Zstr2 )
{
string c0_Zs1, c0_Zs2;
c0_Zs1=c0_convH888(c0_Zstr1);
c0_Zs2=c0_convH888(c0_Zstr2);
return c0_is_emptyline( Int(Mid(c0_Zs1,0,1)), Int(Mid(c0_Zs1,1,1)) , Int(Mid(c0_Zs2,0,1)), Int(Mid(c0_Zs2,1,1)));
}
//-------------------------------------------------
public bool c0_is_emptyline(int c0_Zvert, int c0_Zhoriz, int c0_Zvert2, int c0_Zhoriz2)
{
bool c0_good;
int c0_DZvert, c0_DZhoriz, c0_PZvert, c0_PZhoriz;
c0_good = true;
c0_DZvert=c0_Zvert2-c0_Zvert; if(c0_DZvert<0) c0_DZvert=-1; else if(c0_DZvert>0) c0_DZvert=1;
c0_DZhoriz=c0_Zhoriz2-c0_Zhoriz; if(c0_DZhoriz<0) c0_DZhoriz=-1; else if(c0_DZhoriz>0) c0_DZhoriz=1;
c0_PZvert=c0_Zvert+c0_DZvert;
c0_PZhoriz=c0_Zhoriz+c0_DZhoriz;
while( c0_PZvert!=c0_Zvert2 || c0_PZhoriz!=c0_Zhoriz2 )
{
if( !c0_is_empty( c0_PZvert, c0_PZhoriz ) )
{
c0_good=false;
break;
}
c0_PZvert+=c0_DZvert;
c0_PZhoriz+=c0_DZhoriz;
}
return c0_good;
}
//-------------------------------------------------
public bool c0_D_is_check_to_king(string c0_ZKcolor)
{
return c0_is_check_to_king(c0_ZKcolor);
}
//-------------------------------------------------
public bool c0_is_check_to_king(string c0_ZKcolor)
{
bool c0_is_check;
int c0_Zp, c0_ZKhoriz, c0_ZKvert, c0_i, c0_Zhoriz, c0_Zvert;
string c0_Zcolor, c0_ZK_at, c0_Z_at;
c0_is_check=false;
c0_Zp=InStr(c0_position,c0_ZKcolor + "K");
c0_ZKhoriz=Asc(Mid(c0_position,c0_Zp+2,1)) - 96;
c0_ZKvert=Int(Mid(c0_position,c0_Zp+3,1));
c0_ZK_at = Str(c0_ZKvert) + Str(c0_ZKhoriz);
for(c0_i=0;Len(c0_position)>c0_i; c0_i+=5)
{
c0_Zcolor=Mid(c0_position,c0_i,1);
if(c0_Zcolor!=c0_ZKcolor)
{
c0_Zhoriz=Asc(Mid(c0_position,c0_i+2,1)) - 96;
c0_Zvert=Int(Mid(c0_position,c0_i+3,1));
c0_Z_at = Str(c0_Zvert) + Str(c0_Zhoriz);
if(c0_can_be_moved( c0_Z_at, c0_ZK_at, true))
{
c0_is_check=true;
break;
}
}
}
return c0_is_check;
}
//-------------------------------------------------
public bool c0_is_attacked_king_before_move(string c0_Qfrom_at, string c0_Qto_at, string c0_Qcolor)
{
bool c0_is_attack;
string c0_save_position, c0_save_become;
bool c0_save_wKingmoved, c0_save_bKingmoved, c0_save_wLRockmoved, c0_save_wRRockmoved;
bool c0_save_bLRockmoved, c0_save_bRRockmoved, c0_save_w00, c0_save_b00;
int c0_save_sidemoves, c0_save_lastmovepawn;
c0_is_attack=false;
c0_save_position=c0_position;
c0_save_sidemoves=c0_sidemoves;
c0_save_wKingmoved=c0_wKingmoved;
c0_save_bKingmoved=c0_bKingmoved;
c0_save_wLRockmoved=c0_wLRockmoved;
c0_save_wRRockmoved=c0_wRRockmoved;
c0_save_bLRockmoved=c0_bLRockmoved;
c0_save_bRRockmoved=c0_bRRockmoved;
c0_save_w00=c0_w00;
c0_save_b00=c0_b00;
c0_save_become=c0_become;
c0_save_lastmovepawn=c0_lastmovepawn;
c0_moveto(c0_Qfrom_at, c0_Qto_at, false);
c0_sidemoves=-c0_sidemoves;
if( c0_is_check_to_king(c0_Qcolor) )
{
c0_is_attack=true;
}
c0_position=c0_save_position;
c0_sidemoves=c0_save_sidemoves;
c0_wKingmoved=c0_save_wKingmoved;
c0_bKingmoved=c0_save_bKingmoved;
c0_wLRockmoved=c0_save_wLRockmoved;
c0_wRRockmoved=c0_save_wRRockmoved;
c0_bLRockmoved=c0_save_bLRockmoved;
c0_bRRockmoved=c0_save_bRRockmoved;
c0_lastmovepawn=c0_save_lastmovepawn;
c0_w00=c0_save_w00;
c0_b00=c0_save_b00;
c0_become=c0_save_become;
return c0_is_attack;
}
//-------------------------------------------------
public bool c0_D_is_mate_to_king(string c0_ZKcolor)
{
return c0_is_mate_to_king(c0_ZKcolor, false);
}
//-------------------------------------------------
public bool c0_is_mate_to_king(string c0_ZKcolor, bool c0_just_mate)
{
bool c0_is_mate;
int c0_i, c0_Zhoriz, c0_Zvert, c0_vi, c0_vj;
string c0_Z_at, c0_Z_to_at, c0_Zcolor;
c0_is_mate=false;
if( c0_just_mate || c0_is_check_to_king(c0_ZKcolor) )
{
c0_i=0;
for(c0_is_mate=true;c0_is_mate && (Len(c0_position)>c0_i); c0_i+=5)
{
c0_Zcolor=Mid(c0_position,c0_i,1);
if(c0_Zcolor==c0_ZKcolor)
{
c0_Zhoriz=Asc(Mid(c0_position,c0_i+2,1)) - 96;
c0_Zvert=Int(Mid(c0_position,c0_i+3,1));
c0_Z_at = Str(c0_Zvert) + Str(c0_Zhoriz);
for(c0_vi=1;c0_is_mate && c0_vi<=8;c0_vi++)
for(c0_vj=1;c0_is_mate && c0_vj<=8;c0_vj++)
{
c0_Z_to_at=Str(c0_vi) + Str(c0_vj);
if(c0_can_be_moved( c0_Z_at, c0_Z_to_at, false))
{
c0_is_mate=false;
break;
}
}
}
}
}
return c0_is_mate;
}
//-------------------------------------------------
public bool c0_D_is_pate_to_king(string c0_ZWcolor)
{
return c0_is_pate_to_king(c0_ZWcolor) && !c0_is_mate_to_king(c0_ZWcolor, false);
}
//-------------------------------------------------
public bool c0_is_pate_to_king(string c0_ZWcolor)
{
bool c0_is_pate;
int c0_j, c0_Whoriz, c0_Wvert, c0_wi, c0_wj;
string c0_Wcolor, c0_W_at, c0_W_to_at;
c0_is_pate=true;
for(c0_j=0;c0_is_pate && Len(c0_position)>c0_j; c0_j+=5)
{
c0_Wcolor=Mid(c0_position,c0_j,1);
if(c0_Wcolor==c0_ZWcolor)
{
c0_Whoriz=Asc(Mid(c0_position,c0_j+2,1)) - 96;
c0_Wvert=Int(Mid(c0_position,c0_j+3,1));
c0_W_at = Str(c0_Wvert) + Str(c0_Whoriz);
for(c0_wi=1;c0_is_pate && c0_wi<=8;c0_wi++)
for(c0_wj=1;c0_is_pate && c0_wj<=8;c0_wj++)
{
c0_W_to_at=Str(c0_wi) + Str(c0_wj);
if(c0_can_be_moved( c0_W_at, c0_W_to_at, false))
{
c0_is_pate=false;
break;
}
}
}
}
return c0_is_pate;
}
//-------------------------------------------------
public bool c0_D_can_be_moved(string c0_Zstr1, string c0_Zstr2)
{
return c0_can_be_moved( c0_convH888(c0_Zstr1), c0_convH888(c0_Zstr2), false);
}
//-------------------------------------------------
public bool c0_can_be_moved(string c0_from_at, string c0_to_at, bool c0_just_move_or_eat)
{
bool c0_can;
int c0_vert, c0_horiz, c0_vert2, c0_horiz2, c0_p, c0_Dvert, c0_Dhoriz, c0_virziens;
string c0_color, c0_figure;
c0_can = false;
c0_vert = Int(Mid(c0_from_at,0,1));
c0_horiz= Int(Mid(c0_from_at,1,1));
c0_vert2 = Int(Mid(c0_to_at,0,1));
c0_horiz2= Int(Mid(c0_to_at,1,1));
c0_p=InStr(c0_position, c0_convE2(c0_vert,c0_horiz) );
if(c0_p>=0)
{
c0_color=Mid(c0_position,c0_p-2,1);
c0_figure=Mid(c0_position,c0_p-1,1);
if(c0_is_empty(c0_vert2,c0_horiz2) || c0_is_enemy(c0_vert2,c0_horiz2,c0_color))
{
c0_Dvert=c0_vert2-c0_vert; if(c0_Dvert<0) c0_Dvert=-c0_Dvert;
c0_Dhoriz=c0_horiz2-c0_horiz; if(c0_Dhoriz<0) c0_Dhoriz=-c0_Dhoriz;
if(c0_figure=="p")
{
if( c0_color=="w" ) c0_virziens=1; else c0_virziens=-1;
if(c0_horiz2==c0_horiz)
{
if( (c0_vert2==c0_vert+c0_virziens && c0_is_empty(c0_vert2,c0_horiz2)) ||
(c0_color=="w" && c0_vert2==4 && c0_vert==2 && c0_is_empty(3,c0_horiz2) && c0_is_empty(4,c0_horiz2)) ||
(c0_color=="b" && c0_vert2==5 && c0_vert==7 && c0_is_empty(5,c0_horiz2) && c0_is_empty(6,c0_horiz2)) )
c0_can = true;
}
else
{
if( (c0_horiz2==c0_horiz+1 || c0_horiz2==c0_horiz-1) && c0_vert2==c0_vert+c0_virziens)
if(c0_is_enemy(c0_vert2,c0_horiz2,c0_color) ||
(c0_lastmovepawn==c0_horiz2 &&
((c0_color=="w" && c0_vert2==6) || (c0_color=="b" && c0_vert2==3)) ) ) c0_can=true;
}
}
if(c0_figure=="N")
{
if( c0_Dvert+c0_Dhoriz==3 && c0_Dvert!=0 && c0_Dhoriz!=0 ) c0_can=true;
}
if(c0_figure=="B")
{
if( (c0_Dvert>0 && c0_Dvert==c0_Dhoriz) && c0_is_emptyline(c0_vert,c0_horiz,c0_vert2,c0_horiz2)) c0_can=true;
}
if(c0_figure=="R")
{
if( ((c0_Dvert==0||c0_Dhoriz==0) && c0_Dvert!=c0_Dhoriz) && c0_is_emptyline(c0_vert,c0_horiz,c0_vert2,c0_horiz2)) c0_can=true;
}
if(c0_figure=="Q")
{
if( (c0_Dvert==0||c0_Dhoriz==0||c0_Dvert==c0_Dhoriz) && c0_is_emptyline(c0_vert,c0_horiz,c0_vert2,c0_horiz2)) c0_can=true;
}
if(c0_figure=="K")
{
if((c0_Dvert==0 && c0_Dhoriz==1)||(c0_Dhoriz==0 && c0_Dvert==1)||(c0_Dhoriz==1 && c0_Dvert==1)) c0_can=true;
else
if (!c0_just_move_or_eat && !c0_is_check_to_king(c0_color) && (!c0_fischer))
{
if(c0_color=="w")
{
if(!c0_wKingmoved && c0_vert==1 && c0_horiz==5 && c0_vert2==1)
{