Check in Bonanza Feliz 0.0
[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 #if defined(TLP)
55   ptree = tlp_atree_work;
56 #else
57   ptree = &tree;
58 #endif
59
60 #if defined(CSASHOGI) && defined(_WIN32)
61   FreeConsole();
62   if ( argc != 2 || strcmp( argv[1], "csa_shogi" ) )
63     {
64       MessageBox( NULL,
65                   "The executable image is not intended\x0d"
66                   "as an independent program file.\x0d"
67                   "Execute CSA.EXE instead.",
68                   str_myname, MB_OK | MB_ICONINFORMATION );
69       return EXIT_FAILURE;
70     }
71 #endif
72
73   if ( ini( ptree ) < 0 )
74     {
75       out_error( "%s", str_error );
76       return EXIT_SUCCESS;
77     }
78
79   for ( ;; )
80     {
81       iret = main_child( ptree );
82       if ( iret == -1 )
83         {
84           out_error( "%s", str_error );
85           ShutdownClient;
86           break;
87         }
88       else if ( iret == -2 )
89         {
90           out_warning( "%s", str_error );
91           ShutdownClient;
92           continue;
93         }
94       else if ( iret == -3 ) { break; }
95     }
96
97   if ( fin() < 0 ) { out_error( "%s", str_error ); }
98
99   return EXIT_SUCCESS;
100 }
101
102
103 static int
104 main_child( tree_t * restrict ptree )
105 {
106   int iret;
107
108 #if defined(DEKUNOBOU)
109   if ( dek_ngame && ( game_status & mask_game_end ) )
110     {
111       TlpEnd();
112       if ( dek_next_game( ptree ) < 0 )
113         {
114           out_error( "%s", str_error );
115           return -3;
116         }
117     }
118 #endif
119
120   /* ponder a move */
121   ponder_move = 0;
122   iret = ponder( ptree );
123   if ( iret < 0 ) { return iret; }
124   else if ( game_status & flag_quit ) { return -3; }
125
126   /* move prediction succeeded, pondering finished,
127      and computer made a move. */
128   else if ( iret == 2 ) { return 1; }
129
130   /* move prediction failed, pondering aborted,
131      and we have opponent's move in input buffer. */
132   else if ( ponder_move == MOVE_PONDER_FAILED )
133     {
134     }
135
136   /* pondering is interrupted or ended.
137      do nothing until we get next input line. */
138   else {
139     TlpEnd();
140     show_prompt();
141   }
142
143   
144   iret = next_cmdline( 1 );
145   if ( iret < 0 ) { return iret; }
146   else if ( game_status & flag_quit ) { return -3; }
147
148
149   iret = procedure( ptree );
150   if ( iret < 0 ) { return iret; }
151   else if ( game_status & flag_quit ) { return -3; }
152
153   return 1;
154 }