-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproce.c
More file actions
1919 lines (1621 loc) · 46.6 KB
/
proce.c
File metadata and controls
1919 lines (1621 loc) · 46.6 KB
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
#include <ctype.h>
#include <limits.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shogi.h"
/* unacceptable when the program is thinking, or quit pondering */
#define AbortDifficultCommand \
if ( game_status & flag_thinking ) \
{ \
str_error = str_busy_think; \
return -2; \
} \
else if ( game_status & ( flag_pondering | flag_puzzling ) ) \
{ \
game_status |= flag_quit_ponder; \
return 2; \
}
#if defined(MINIMUM)
# define CmdBook(x,y) cmd_book(y);
static int cmd_book( char **lasts );
#else
# define CmdBook(x,y) cmd_book(x,y);
static int cmd_learn( tree_t * restrict ptree, char **lasts );
static int cmd_book( tree_t * restrict ptree, char **lasts );
#endif
#if ! defined(NO_STDOUT)
static int cmd_stress( char **lasts );
#endif
#if defined(DEKUNOBOU)
static int cmd_dek( char **lasts );
#endif
#if defined(CSA_LAN)
static int proce_csalan( tree_t * restrict ptree );
static int cmd_connect( tree_t * restrict ptree, char **lasts );
#endif
#if defined(MNJ_LAN)
static int proce_mnj( tree_t * restrict ptree );
static int cmd_mnj( tree_t * restrict ptree, char **lasts );
static int cmd_mnjmove( tree_t * restrict ptree, char **lasts, int is_alter );
#endif
#if defined(TLP)
static int cmd_thread( char **lasts );
#endif
#if defined(MPV)
static int cmd_mpv( tree_t * restrict ptree, char **lasts );
#endif
static int proce_cui( tree_t * restrict ptree );
static int cmd_usrmove( tree_t * restrict ptree, const char *str_move,
char **last );
static int cmd_move_now( void );
static int cmd_ponder( char **lasts );
static int cmd_limit( char **lasts );
static int cmd_quit( void );
static int cmd_beep( char **lasts );
static int cmd_peek( char **lasts );
static int cmd_hash( char **lasts );
static int cmd_ping( void );
static int cmd_suspend( void );
static int cmd_problem( tree_t * restrict ptree, char **lasts );
static int cmd_display( tree_t * restrict ptree, char **lasts );
static int cmd_move( tree_t * restrict ptree, char **lasts );
static int cmd_new( tree_t * restrict ptree, char **lasts );
static int cmd_read( tree_t * restrict ptree, char **lasts );
static int cmd_resign( tree_t * restrict ptree, char **lasts );
static int cmd_time( char **lasts );
static int cmd_undo( tree_t * restrict ptree ); // [HGM] undo
static int cmd_analyze( tree_t * restrict ptree ); // [HGM] analyze
static int cmd_exit( void );
static int is_move( const char *str );
int
procedure( tree_t * restrict ptree )
{
#if defined(CSA_LAN)
if ( sckt_csa != SCKT_NULL ) { return proce_csalan( ptree ); }
#endif
#if defined(MNJ_LAN)
if ( sckt_mnj != SCKT_NULL ) { return proce_mnj( ptree ); }
#endif
return proce_cui( ptree );
}
char *start_pos, start_data[512]; // [HGM] undo: for remembering start position
int move_list[1024], move_ptr;
char analyze_mode;
int exclude_list[MAX_LEGAL_MOVES], all_moves[MAX_LEGAL_MOVES];
#ifdef XBOARD
#define IF(X) else if(!strcmp(command, X))
int myTime, hisTime, movesPerSession, inc, plyNr;
char xboard_mode;
int
xboard_to_CSA( tree_t * restrict ptree, char *in, char *out, int status )
{
char fromX=in[0], fromY=in[1], toX=in[2], toY=in[3], promo=in[4];
int piece=0, bonanza_move=0;
if(fromY == '@') { // drop (contains all info needed to convert it)
if(fromX >= 'a') fromX += 'A' - 'a';
switch(fromX) { // encode piece
case 'P': piece = pawn; break;
case 'L': piece = lance; break;
case 'N': piece = knight; break;
case 'S': piece = silver; break;
case 'G': piece = gold; break;
case 'B': piece = bishop; break;
case 'R': piece = rook; break;
}
bonanza_move = Drop2Move(piece) | To2Move( ('9' - toY)*9 + (toX - 'a') );
sprintf(out, "00%c%c%s", 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
} else { // board move (need to figure out moved piece)
int from = ('9' - fromY)*9 + (fromX - 'a');
int flag = (promo == '+' ? FLAG_PROMO : 0);
piece = abs( BOARD[from] ); // this only works when not searching!
printf("# piece from board: %d\n", piece);fflush(stdout);
if( status & ( flag_pondering | flag_puzzling | flag_thinking ) ) {
int i, to = ('9' - toY)*9 + (toX - 'a'), l = (status == (flag_pondering | flag_thinking));
piece = 0; // kludge to force illegal CSA move
for( i = 0; l ? all_moves[i] : i < root_nmove; i++ ) { // determine the piece from the move list
int move = (l ? all_moves[i] : root_move_list[i].move);
if( I2To(move) != to ) continue;
if( I2From(move) != from ) continue;
if( (move & FLAG_PROMO) != flag ) continue;
piece = I2PieceMove( move ); // we found the move; take the piece from it
bonanza_move = move;
break;
}
printf("# piece corrected to %d\n", piece);fflush(stdout);
}
if( promo ) piece += promote;
sprintf(out, "%c%c%c%c%s", 'a'+'9'-fromX, '1'+'9'-fromY, 'a'+'9'-toX, '1'+'9'-toY, astr_table_piece[piece]);
}
return bonanza_move;
}
static void
update_exclude_list( tree_t * restrict ptree, int add, char *move_str )
{ // [HGM] exclude: manage list of excluded moves
char dummy[20];
int i;
if( ! exclude_list[0] ) { // nothing is excluded yet; make a copy of root move list;
for( i = 0; i < root_nmove; i++) all_moves[i] = root_move_list[i].move;
all_moves[i] = 0;
}
if ( ! strcmp(move_str, "all") ) { // all moves
if( add ) { // copy entire list of legal moves
for( i = 0; i < root_nmove; i++ ) exclude_list[i] = all_moves[i];
} else exclude_list[0] = 0; // clear list
} else { // single move
int move = xboard_to_CSA( ptree, move_str, dummy, flag_pondering | flag_thinking); // kludge with impossible flag
for( i = 0; exclude_list[i]; i++ ) if( move == exclude_list[i] ) break;
if( add ) { // we must add the move
if( exclude_list[i] ) return; // but it was already in list
if( i >= MAX_LEGAL_MOVES - 2 ) return; // overflow
exclude_list[i] = move; exclude_list[i+1] = 0; // append move
} else { // we must delete the move
if( exclude_list[i] == 0 ) return; // but it was not there
while( (exclude_list[i] = exclude_list[i+1]) ) i++; // squeeze it out
}
}
}
static void
SetTimes(void)
{ // set white and black times from own and opponent time.
int moves;
if(movesPerSession <= 0) moves = 35; else {
moves = - plyNr/2;
while(moves <= 0) moves += movesPerSession;
}
time_limit = (myTime-inc-30)/(moves+2) + inc;
time_max_limit = 3*time_limit;
if(time_max_limit > myTime - 30)time_max_limit = myTime - 30; // keep 0.3 sec margin, as Bonanza reads the clock infrequently
time_limit *= 10; // msec
time_max_limit *= 10;
Out("# moves=%d, time=%d inc=%d t=%d, max=%d\n", moves, myTime, inc, time_limit, time_max_limit);
}
static int
proce_xboard(char *line, const char *command, tree_t * restrict ptree)
{ // [HGM] added to make Bonanza.exe a native WinBoard engine
int value = -100000;
static char forceMode = 0;
sscanf(line + strlen(command) + 1, "%d", &value);
Out("# command = '%s'\n", line);
if(0) ;
IF("protover") {
#if defined(MPV)
Out("feature option=\"MultiPV -spin 1 1 100\"\n");
Out("feature option=\"centi-Pawn margin -spin 200 0 25000\"\n");
#endif
Out("feature variants=\"shogi\" usermove=1 myname=\"Bonanza " BNZ_VER
"\" memory=1 smp=1 debug=1 colors=0 setboard=1 ping=1 sigint=0 exclude=1 done=1\n");
}
IF("new") { forceMode = plyNr = 0; SetTimes(); return 0; }
IF("easy") { strcpy(line, "ponder off"); return 0; }
IF("hard") { strcpy(line, "ponder on"); return 0; }
IF("post") { ; }
IF("nopost") { ; }
IF("time") { sscanf(line+5, "%d", &myTime); }
IF("otim") { sscanf(line+5, "%d", &hisTime); }
IF("force") { forceMode = 1; }
IF("go") { forceMode = 0; SetTimes(); plyNr++; strcpy(line, "move"); return 0; }
IF("memory") { ; }
IF("cores") { sprintf(line, "tlp num %d", value); return 0; }
IF("sd") { sprintf(line, "limit depth %d", value); return 0; }
IF("st") { ; }
IF("quit") { return 0; }
IF("analyze") { return 0; }
IF("exit") { return 0; }
IF("variant") { /* ignore, since it must be Shogi */; }
IF("setboard") { ; }
IF("option") {
if(sscanf(line+7, "MultiPV=%d", &value) == 1) { sprintf(line, "mpv num %d", value); return 0; }
if(sscanf(line+7, "centi-Pawn margin=%d", &value) == 1) { sprintf(line, "mpv width %d", value); return 0; }
}
IF("level") { int min, sec; float fsec=0.;
if(sscanf(line+6, "%d %d:%d %f", &movesPerSession, &min, &sec, &fsec) != 4)
sscanf(line+6, "%d %d %f", &movesPerSession, &min, &fsec);
min = 60*min + sec; myTime = hisTime = 100*min; inc = 100 * fsec;
}
IF("usermove") { char buf[20];
xboard_to_CSA( ptree, line+9, buf, game_status );
if(forceMode || analyze_mode) strcpy(line, "move "), line += 5; else plyNr++, SetTimes();
strcpy( line, buf );
plyNr++;
return 0;
}
IF("undo") { return 0; }
IF("remove") { ; }
IF("ping") { Out("pong %d\n", value); }
IF("exclude") { update_exclude_list( ptree, 1, line+8 ); strcpy(line, "analyze"); return 0; }
IF("include") { update_exclude_list( ptree, 0, line+8 ); strcpy(line, "analyze"); return 0; }
return 1;
}
#endif
static int
proce_cui( tree_t * restrict ptree )
{
const char *token;
char *last;
token = strtok_r( str_cmdline, str_delimiters, &last );
if ( token == NULL || *token == '#' ) { return 1; }
#ifdef XBOARD
{
if(xboard_mode) {
if( proce_xboard(str_cmdline, token, ptree) ) return 1; // command already processed
Out("# translated command '%s'\n", str_cmdline); // command translated for processing by Bonanza
token = strtok_r( str_cmdline, str_delimiters, &last ); // redo parsing
} else
if ( ! strcmp( token, "xboard" ) ) { xboard_mode = 1; game_status |= flag_noprompt; return 1; }
}
#endif
if ( ! strcmp( token, "analyze" ) ) { return cmd_analyze( ptree ); } // [HGM] analyze
if ( ! strcmp( token, "exit" ) ) { return cmd_exit(); } // [HGM] analyze
if ( ! strcmp( token, "move" ) ) { return cmd_move( ptree, &last ); }
if ( ! strcmp( token, "undo" ) ) { return cmd_undo( ptree ); } // [HGM] undo
#if defined(MPV)
if ( ! strcmp( token, "mpv" ) ) { return cmd_mpv( ptree, &last ); }
#endif
analyze_mode = 0; // [HGM] analyze: all other commands terminate analysis
if ( is_move( token ) ) { return cmd_usrmove( ptree, token, &last ); }
if ( ! strcmp( token, "s" ) ) { return cmd_move_now(); }
if ( ! strcmp( token, "beep" ) ) { return cmd_beep( &last); }
if ( ! strcmp( token, "book" ) ) { return CmdBook( ptree, &last ); }
if ( ! strcmp( token, "display" ) ) { return cmd_display( ptree, &last ); }
if ( ! strcmp( token, "hash" ) ) { return cmd_hash( &last ); }
if ( ! strcmp( token, "limit" ) ) { return cmd_limit( &last ); }
if ( ! strcmp( token, "new" ) ) { return cmd_new( ptree, &last ); }
if ( ! strcmp( token, "peek" ) ) { return cmd_peek( &last ); }
if ( ! strcmp( token, "ping" ) ) { return cmd_ping(); }
if ( ! strcmp( token, "ponder" ) ) { return cmd_ponder( &last ); }
if ( ! strcmp( token, "problem" ) ) { return cmd_problem( ptree, &last ); }
if ( ! strcmp( token, "quit" ) ) { return cmd_quit(); }
if ( ! strcmp( token, "read" ) ) { return cmd_read( ptree, &last ); }
if ( ! strcmp( token, "resign" ) ) { return cmd_resign( ptree, &last ); }
if ( ! strcmp( token, "suspend" ) ) { return cmd_suspend(); }
if ( ! strcmp( token, "time" ) ) { return cmd_time( &last ); }
#if defined(CSA_LAN)
if ( ! strcmp( token, "connect" ) ) { return cmd_connect( ptree, &last ); }
#endif
#if defined(MNJ_LAN)
if ( ! strcmp( token, "mnj" ) ) { return cmd_mnj( ptree, &last ); }
#endif
#if defined(DEKUNOBOU)
if ( ! strcmp( token, "dekunobou" ) ) { return cmd_dek( &last ); }
#endif
#if defined(TLP)
if ( ! strcmp( token, "tlp" ) ) { return cmd_thread( &last ); }
#endif
#if ! defined(NO_STDOUT)
if ( ! strcmp( token, "stress" ) ) { return cmd_stress( &last ); }
#endif
#if ! defined(MINIMUM)
if ( ! strcmp( token, "learn" ) ) { return cmd_learn( ptree, &last ); }
#endif
str_error = str_bad_cmdline;
return -2;
}
#if defined(CSA_LAN)
static int
proce_csalan( tree_t * restrict ptree )
{
const char *token;
char *last;
token = strtok_r( str_cmdline, str_delimiters, &last );
if ( token == NULL ) { return 1; }
if ( *token == ach_turn[client_turn] && is_move( token+1 ) )
{
char *ptr;
long l;
token = strtok_r( NULL, str_delimiters, &last );
if ( token == NULL || *token != 'T' )
{
str_error = str_bad_cmdline;
return -1;
}
l = strtol( token+1, &ptr, 0 );
if ( token+1 == ptr || l == LONG_MAX || l < 1 )
{
str_error = str_bad_cmdline;
return -1;
}
adjust_time( (unsigned int)l, client_turn );
Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
return 1;
}
if ( *token == ach_turn[Flip(client_turn)] && is_move( token+1 ) )
{
return cmd_usrmove( ptree, token+1, &last );
}
if ( ! strcmp( token, str_resign ) ) { return cmd_resign( ptree, &last ); }
if ( ! strcmp( token, "#WIN" )
|| ! strcmp( token, "#LOSE" )
|| ! strcmp( token, "#DRAW" )
|| ! strcmp( token, "#CHUDAN" ) )
{
if ( game_status & ( flag_thinking | flag_pondering | flag_puzzling ) )
{
game_status |= flag_suspend;
return 2;
}
ShutdownClient;
if ( client_ngame == client_max_game ) { return cmd_quit(); }
return client_next_game( ptree, client_str_addr, (int)client_port );
}
return 1;
}
#endif
#if defined(MNJ_LAN)
static int
proce_mnj( tree_t * restrict ptree )
{
const char *token;
char *last;
int iret;
token = strtok_r( str_cmdline, str_delimiters, &last );
if ( token == NULL ) { return 1; }
if ( ! strcmp( token, "new" ) )
{
iret = cmd_suspend();
if ( iret != 1 ) { return iret; }
mnj_posi_id = 0;
iret = cmd_new( ptree, &last );
if ( iret < 0 ) { return iret; }
return analyze( ptree );
}
if ( ! strcmp( token, "idle" ) ) { return cmd_suspend(); }
if ( ! strcmp( token, "alter" ) ) { return cmd_mnjmove( ptree, &last, 1 ); }
if ( ! strcmp( token, "move" ) ) { return cmd_mnjmove( ptree, &last, 0 ); }
str_error = str_bad_cmdline;
return -2;
}
static int
cmd_mnjmove( tree_t * restrict ptree, char **lasts, int is_alter )
{
const char *str1 = strtok_r( NULL, str_delimiters, lasts );
const char *str2 = strtok_r( NULL, str_delimiters, lasts );
char *ptr;
long lid;
unsigned int move;
int iret;
if ( sckt_mnj == SCKT_NULL || str1 == NULL || str2 == NULL )
{
str_error = str_bad_cmdline;
return -1;
}
lid = strtol( str2, &ptr, 0 );
if ( ptr == str2 || lid == LONG_MAX || lid < 1 )
{
str_error = str_bad_cmdline;
return -1;
}
AbortDifficultCommand;
if ( is_alter ) { unmake_move_root( ptree, mnj_move_last ); };
iret = interpret_CSA_move( ptree, &move, str1 );
if ( iret < 0 ) { return iret; }
iret = get_elapsed( &time_turn_start );
if ( iret < 0 ) { return iret; }
mnj_posi_id = (int)lid;
mnj_move_last = move;
iret = make_move_root( ptree, move, ( flag_history | flag_time | flag_rep
| flag_detect_hang
| flag_rejections ) );
if ( iret < 0 ) { return iret; }
# if ! defined(NO_STDOUT)
iret = out_board( ptree, stdout, 0, 0 );
if ( iret < 0 ) { return iret; }
# endif
return analyze( ptree );
}
#endif
static int
do_analyze( tree_t * restrict ptree )
{ // [HGM] analyze: do a ponder search on the current position
int iret;
if ( get_elapsed( &time_start ) < 0 ) { return -1; }
time_limit = time_max_limit = 1e9; // kludge: use huge time to mimic infinity
#ifdef XBOARD
Out("1 0 0 0 New Search\n"); // make sure lower depth is emitted, so XBoard undestand new search started
#endif
game_status |= flag_pondering;
iret = iterate( ptree, flag_refer_rest );
game_status &= ~flag_pondering;
return iret;
}
static int
cmd_undo( tree_t * restrict ptree )
{ // [HGM] undo: restart the game, and feed all moves except the last
int i, last = move_ptr;
char *p = start_data;
if( move_ptr <= 0 ) {
str_error = "undo past start of game ignored";
return -2;
}
AbortDifficultCommand;
last--;
cmd_new( ptree, &p );
for(i=0; i<last; i++) {
make_move_root( ptree, move_list[i], 0);
}
exclude_list[0] = 0; // [HGM] exclude: exclude list cleared for new position
if ( analyze_mode ) return do_analyze ( ptree ); // [HGM] analyze: analysis should continue after undo
return 1;
}
static int
cmd_analyze( tree_t * restrict ptree )
{ // [HGM] analyze: switch on analyze mode, and start analyzing (also used to force a restart)
AbortDifficultCommand;
analyze_mode = 1;
return do_analyze( ptree );
}
static int
cmd_exit( void )
{ // [HGM] analyze: switch off analysis mode
if ( !analyze_mode ) {
str_error = "was not analyzing";
return -2;
}
if ( game_status & flag_pondering ) { game_status |= flag_quit_ponder; return 2; }
analyze_mode = 0;
exclude_list[0] = 0; // [HGM] exclude: exclude list cleared after analysis
return 1;
}
static int
is_move( const char *str )
{
if ( isdigit( (int)str[0] ) && isdigit( (int)str[1] )
&& isdigit( (int)str[2] ) && isdigit( (int)str[3] )
&& isupper( (int)str[4] ) && isupper( (int)str[5] )
&& str[6] == '\0' ) { return 1; }
return 0;
}
static int
cmd_move_now( void )
{
if ( game_status & flag_thinking ) { game_status |= flag_move_now; }
return 1;
}
static int
cmd_usrmove( tree_t * restrict ptree, const char *str_move, char **lasts )
{
const char *str;
char *ptr;
long lelapsed;
unsigned int move;
int iret;
if ( game_status & mask_game_end )
{
str_error = str_game_ended;
return -2;
}
if ( game_status & flag_thinking )
{
str_error = str_busy_think;
return -2;
}
str = strtok_r( NULL, str_delimiters, lasts );
if ( str == NULL ) { lelapsed = 0; }
else {
if ( *str != 'T' )
{
str_error = str_bad_cmdline;
return -2;
}
str += 1;
lelapsed = strtol( str, &ptr, 0 );
if ( ptr == str || lelapsed == LONG_MAX || lelapsed < 1 )
{
str_error = str_bad_cmdline;
return -2;
}
}
if ( game_status & ( flag_pondering | flag_puzzling ) )
{
int i;
for ( i = 0; i < ponder_nmove; i++ )
{
if ( ! strcmp( str_move, str_CSA_move(ponder_move_list[i]) ) )
{
break;
}
}
if ( i == ponder_nmove )
{
#if defined(CSA_LAN)
if ( sckt_csa != SCKT_NULL ) { AbortDifficultCommand; }
#endif
#if defined(DEKUNOBOU)
if ( dek_ngame ) { AbortDifficultCommand; }
#endif
#if defined(CSASHOGI)
AbortDifficultCommand;
#else
str_error = str_illegal_move;
return -2;
#endif
}
if ( ( game_status & flag_puzzling )
|| strcmp( str_move, str_CSA_move(ponder_move) ) )
{
ponder_move = MOVE_PONDER_FAILED;
game_status |= flag_quit_ponder;
return 2;
}
else {
iret = renovate_time( Flip(root_turn) );
if ( iret < 0 ) { return iret; }
if ( lelapsed )
{
adjust_time( (unsigned int)lelapsed, Flip(root_turn) );
}
history_book_learn[ record_game.moves ].move_played = ponder_move;
history_book_learn[ record_game.moves ].hand_played
= ptree->rep_hand_list[ root_nrep-1 ];
history_book_learn[ record_game.moves ].key_played
= (unsigned int)ptree->rep_board_list[ root_nrep-1 ];
out_CSA( ptree, &record_game, ponder_move );
game_status &= ~flag_pondering;
game_status |= flag_thinking;
n_nobook_move += 1;
#ifdef XBOARD
if(!xboard_mode)
#endif
set_search_limit_time( root_turn );
OutCsaShogi( "info ponder end\n" );
str = str_time_symple( time_turn_start - time_start );
Out( " %6s MOVE PREDICTION HIT\n"
" elapsed: b%u, w%u\n", str, sec_b_total, sec_w_total );
return 1;
}
}
iret = interpret_CSA_move( ptree, &move, str_move );
if ( iret < 0 ) { return iret; }
move_evasion_pchk = 0;
iret = make_move_root( ptree, move, ( flag_rep | flag_history | flag_time
| flag_rejections
| flag_detect_hang ) );
if ( iret < 0 )
{
#if defined(CSA_LAN)
if ( sckt_csa != SCKT_NULL )
{
if ( move_evasion_pchk )
{
str = str_CSA_move( move_evasion_pchk );
iret = sckt_out( sckt_csa, "%c%s\n",
ach_turn[Flip(root_turn)], str );
if ( iret < 0 ) { return iret; }
}
return cmd_suspend();
}
#endif
#if defined(DEKUNOBOU)
if ( dek_ngame )
{
if ( move_evasion_pchk )
{
dek_win += 1;
OutDek( "%%TORYO\n" );
}
return cmd_suspend();
}
#endif
if ( move_evasion_pchk )
{
str = str_CSA_move( move_evasion_pchk );
#if defined(CSASHOGI)
OutCsaShogi( "move%s\n", str );
return cmd_suspend();
#else
snprintf( str_message, SIZE_MESSAGE, "perpetual check (%c%s)",
ach_turn[Flip(root_turn)], str );
str_error = str_message;
return -2;
#endif
}
return iret;
}
if ( lelapsed ) { adjust_time( (unsigned int)lelapsed, Flip(root_turn) ); }
Out( " elapsed: b%u, w%u\n", sec_b_total, sec_w_total );
#if defined(CSA_LAN)
if ( sckt_csa != SCKT_NULL && ( game_status & flag_mated ) )
{
iret = sckt_out( sckt_csa, "%%TORYO\n" );
if ( iret < 0 ) { return iret; }
}
#endif
#if defined(DEKUNOBOU)
if ( dek_ngame && ( game_status & flag_drawn ) ) { OutDek( "%%TORYO\n" ); }
#endif
if ( ! ( game_status & mask_game_end ) )
{
iret = com_turn_start( ptree, 0 );
if ( iret < 0 ) { return iret; }
}
return 1;
}
static int
cmd_beep( char **lasts )
{
const char *str = strtok_r( NULL, str_delimiters, lasts );
if ( str == NULL )
{
str_error = str_bad_cmdline;
return -2;
}
if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nobeep; }
else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nobeep; }
else {
str_error = str_bad_cmdline;
return -2;
}
return 1;
}
static int
cmd_peek( char **lasts )
{
const char *str = strtok_r( NULL, str_delimiters, lasts );
if ( str == NULL )
{
str_error = str_bad_cmdline;
return -2;
}
if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nopeek; }
else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nopeek; }
else {
str_error = str_bad_cmdline;
return -2;
}
return 1;
}
static int
cmd_ponder( char **lasts )
{
const char *str = strtok_r( NULL, str_delimiters, lasts );
if ( str == NULL )
{
str_error = str_bad_cmdline;
return -2;
}
if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_noponder; }
else if ( ! strcmp( str, str_off ) )
{
if ( game_status & ( flag_pondering | flag_puzzling ) )
{
game_status |= flag_quit_ponder;
}
game_status |= flag_noponder;
}
else {
str_error = str_bad_cmdline;
return -2;
}
return 1;
}
#if ! defined(NO_STDOUT)
static int
cmd_stress( char **lasts )
{
const char *str = strtok_r( NULL, str_delimiters, lasts );
if ( str == NULL )
{
str_error = str_bad_cmdline;
return -2;
}
if ( ! strcmp( str, str_on ) ) { game_status &= ~flag_nostress; }
else if ( ! strcmp( str, str_off ) ) { game_status |= flag_nostress; }
else {
str_error = str_bad_cmdline;
return -2;
}
return 1;
}
#endif
static int
#if defined(MINIMUM)
cmd_book( char **lasts )
#else
cmd_book( tree_t * restrict ptree, char **lasts )
#endif
{
const char *str = strtok_r( NULL, str_delimiters, lasts );
int iret = 1;
if ( str == NULL )
{
str_error = str_bad_cmdline;
return -2;
}
if ( ! strcmp( str, str_on ) ) { iret = book_on(); }
else if ( ! strcmp( str, str_off ) ) { iret = book_off(); }
else if ( ! strcmp( str, "narrow" ) ) { game_status |= flag_narrow_book; }
else if ( ! strcmp( str, "wide" ) ) { game_status &= ~flag_narrow_book; }
#if ! defined(MINIMUM)
else if ( ! strcmp( str, "create" ) )
{
AbortDifficultCommand;
iret = book_create( ptree );
if ( iret < 0 ) { return iret; }
iret = ini_game( ptree, &min_posi_no_handicap, flag_history,
NULL, NULL );
if ( iret < 0 ) { return iret; }
iret = get_elapsed( &time_turn_start );
}
#endif
else {
str_error = str_bad_cmdline;
iret = -2;
}
return iret;
}
static int
cmd_display( tree_t * restrict ptree, char **lasts )
{
const char *str = strtok_r( NULL, str_delimiters, lasts );
char *ptr;
long l;
int iret;
if ( str != NULL )
{
l = strtol( str, &ptr, 0 );
if ( ptr == str )
{
str_error = str_bad_cmdline;
return -2;
}
if ( l == 1 ) { game_status &= ~flag_reverse; }
else if ( l == 2 ) { game_status |= flag_reverse; }
else {
str_error = str_bad_cmdline;
return -2;
}
}
Out( "\n" );
iret = out_board( ptree, stdout, 0, 0 );
if ( iret < 0 ) { return iret; }
#if ! defined(NO_LOGGING)
iret = out_board( ptree, pf_log, 0, 0 );
if ( iret < 0 ) { return iret; }
#endif
Out( "\n" );
return 1;
}
static int
cmd_ping( void )
{
OutCsaShogi( "pong\n" );
Out( "pong\n" );
return 1;
}
static int
cmd_hash( char **lasts )
{
const char *str = strtok_r( NULL, str_delimiters, lasts );
char *ptr;
long l;
if ( str == NULL )
{
str_error = str_bad_cmdline;
return -2;
}
if ( ! strcmp( str, "learn" ) )
{
str = strtok_r( NULL, str_delimiters, lasts );
if ( str != NULL && ! strcmp( str, str_on ) )
{
return hash_learn_on();
}
else if ( str != NULL && ! strcmp( str, str_off ) )
{
return hash_learn_off();
}
#if ! defined(MINIMUM)
else if ( str != NULL && ! strcmp( str, "create" ) )
{
return hash_learn_create();
}
#endif
else {
str_error = str_bad_cmdline;
return -2;
}
}
l = strtol( str, &ptr, 0 );
if ( ptr == str || l == LONG_MAX || l < 1 || l > 31 )
{
str_error = str_bad_cmdline;
return -2;
}
AbortDifficultCommand;
log2_ntrans_table = (int)l;
memory_free( (void *)ptrans_table_orig );
return ini_trans_table();
}
static int
cmd_limit( char **lasts )
{
const char *str = strtok_r( NULL, str_delimiters, lasts );
char *ptr;
long l1, l2, l3;
if ( str == NULL )
{
str_error = str_bad_cmdline;
return -2;
}
AbortDifficultCommand;
if ( ! strcmp( str, "depth" ) )
{
str = strtok_r( NULL, str_delimiters, lasts );
if ( str == NULL )
{
str_error = str_bad_cmdline;
return -2;
}
l1 = strtol( str, &ptr, 0 );
if ( ptr == str || l1 == LONG_MAX || l1 < 1 )
{
str_error = str_bad_cmdline;
return -2;
}
sec_limit_up = UINT_MAX;