Add XBoard protocol drivers
[bonanza.git] / main.c
1 /*
2   BUG LIST                                                       
3
4   - detection of repetitions can be wrong due to collision of hash keys and
5     limitation of history table size.
6
7   - detection of mates fails if all of pseudo-legal evasions are perpetual
8     checks.  Father more, inferior evasions, such as unpromotion of
9     bishop, rook, and lance at 8th rank, are not counted for the mate
10     detection. 
11
12   - detection of perpetual checks fails if one of those inferior
13     evasions makes a position that occurred four times.
14 */
15 /*
16   TODO:
17   - idirec && is_pinned_on_black_king();
18   - aifile and airank
19   - incheck at quies
20   - max legal moves
21   - tactical macro
22   - out_warning( "A node returns a value lower than mate." ); is obsolate.
23   - do_mate in hash
24   - pv store to hash
25   - no threat
26   - use IsDiscover macro
27   - change hash_store_pv()
28   - dek.c is obsolate.
29   - limit time ? ? num
30   - hash.bin
31   - SHARE to all transition table
32  */
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #if defined(_WIN32)
38 #  include <fcntl.h>
39 #endif
40 #include "shogi.h"
41
42 static int main_child( tree_t * restrict ptree );
43
44 int CONV_CDECL
45 #if defined(CSASHOGI)
46 main( int argc, char *argv[] )
47 #else
48 main()
49 #endif
50 {
51   int iret;
52   tree_t * restrict ptree;
53
54 #ifdef XBOARD
55   Out("feature done=0\n");
56 #endif
57 #if defined(TLP)
58   ptree = tlp_atree_work;
59 #else
60   ptree = &tree;
61 #endif
62
63 #if defined(CSASHOGI) && defined(_WIN32)
64   FreeConsole();
65   if ( argc != 2 || strcmp( argv[1], "csa_shogi" ) )
66     {
67       MessageBox( NULL,
68                   "The executable image is not intended\x0d"
69                   "as an independent program file.\x0d"
70                   "Execute CSA.EXE instead.",
71                   str_myname, MB_OK | MB_ICONINFORMATION );
72       return EXIT_FAILURE;
73     }
74 #endif
75
76   if ( ini( ptree ) < 0 )
77     {
78       out_error( "%s", str_error );
79       return EXIT_SUCCESS;
80     }
81
82   for ( ;; )
83     {
84       iret = main_child( ptree );
85       if ( iret == -1 )
86         {
87           out_error( "%s", str_error );
88           ShutdownClient;
89           break;
90         }
91       else if ( iret == -2 )
92         {
93           out_warning( "%s", str_error );
94           ShutdownClient;
95           continue;
96         }
97       else if ( iret == -3 ) { break; }
98     }
99
100   if ( fin() < 0 ) { out_error( "%s", str_error ); }
101
102   return EXIT_SUCCESS;
103 }
104
105
106 static int
107 main_child( tree_t * restrict ptree )
108 {
109   int iret;
110
111 #if defined(DEKUNOBOU)
112   if ( dek_ngame && ( game_status & mask_game_end ) )
113     {
114       TlpEnd();
115       if ( dek_next_game( ptree ) < 0 )
116         {
117           out_error( "%s", str_error );
118           return -3;
119         }
120     }
121 #endif
122
123   /* ponder a move */
124   ponder_move = 0;
125   iret = ponder( ptree );
126   if ( iret < 0 ) { return iret; }
127   else if ( game_status & flag_quit ) { return -3; }
128
129   /* move prediction succeeded, pondering finished,
130      and computer made a move. */
131   else if ( iret == 2 ) { return 1; }
132
133   /* move prediction failed, pondering aborted,
134      and we have opponent's move in input buffer. */
135   else if ( ponder_move == MOVE_PONDER_FAILED )
136     {
137     }
138
139   /* pondering is interrupted or ended.
140      do nothing until we get next input line. */
141   else {
142     TlpEnd();
143     show_prompt();
144   }
145
146   
147   iret = next_cmdline( 1 );
148   if ( iret < 0 ) { return iret; }
149   else if ( game_status & flag_quit ) { return -3; }
150
151
152   iret = procedure( ptree );
153   if ( iret < 0 ) { return iret; }
154   else if ( game_status & flag_quit ) { return -3; }
155
156   return 1;
157 }