Fix force mode after setboard
[bonanza.git] / main.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #if defined(_WIN32)
5 #  include <fcntl.h>
6 #endif
7 #include "shogi.h"
8
9 static int main_child( tree_t * restrict ptree );
10
11 int
12 #if defined(CSASHOGI) || defined(USI)
13 main( int argc, char *argv[] )
14 #else
15 main()
16 #endif
17 {
18   int iret;
19   tree_t * restrict ptree;
20
21 #ifdef XBOARD
22   Out("feature done=0\n");
23 #endif
24 #if defined(TLP)
25   ptree = tlp_atree_work;
26 #else
27   ptree = &tree;
28 #endif
29
30 #if defined(CSASHOGI) && defined(_WIN32)
31   if ( argc != 2 || strcmp( argv[1], "csa_shogi" ) )
32     {
33       MessageBox( NULL,
34                   "The executable image is not intended\x0d"
35                   "as an independent program file.\x0d"
36                   "Execute CSA.EXE instead.",
37                   str_myname, MB_OK | MB_ICONINFORMATION );
38       return EXIT_FAILURE;
39     }
40 #endif
41
42 #if defined(USI)
43   if ( argc == 2 && ! strcmp( argv[1], "usi" ) ) { usi_mode = usi_on; }
44   else                                           { usi_mode = usi_off; }
45 #endif
46
47   if ( ini( ptree ) < 0 )
48     {
49       out_error( "%s", str_error );
50       return EXIT_SUCCESS;
51     }
52
53   for ( ;; )
54     {
55       iret = main_child( ptree );
56       if ( iret == -1 )
57         {
58           out_error( "%s", str_error );
59           ShutdownAll();
60           break;
61         }
62       else if ( iret == -2 )
63         {
64           out_warning( "%s", str_error );
65           ShutdownAll();
66           continue;
67         }
68       else if ( iret == -3 ) { break; }
69     }
70
71   if ( fin() < 0 ) { out_error( "%s", str_error ); }
72
73   return EXIT_SUCCESS;
74 }
75
76
77 static int
78 main_child( tree_t * restrict ptree )
79 {
80   int iret;
81
82   /* ponder a move */
83   ponder_move = 0;
84   iret = ponder( ptree );
85   if ( iret < 0 ) { return iret; }
86   else if ( game_status & flag_quit ) { return -3; }
87
88   /* move prediction succeeded, pondering finished,
89      and computer made a move. */
90   else if ( iret == 2 ) { return 1; }
91
92   /* move prediction failed, pondering aborted,
93      and we have opponent's move in input buffer. */
94   else if ( ponder_move == MOVE_PONDER_FAILED )
95     {
96     }
97
98   /* pondering is interrupted or ended.
99      do nothing until we get next input line. */
100   else {
101     TlpEnd();
102     show_prompt();
103   }
104
105   
106   iret = next_cmdline( 1 );
107   if ( iret < 0 ) { return iret; }
108   else if ( game_status & flag_quit ) { return -3; }
109
110
111   iret = procedure( ptree );
112   if ( iret < 0 ) { return iret; }
113   else if ( game_status & flag_quit ) { return -3; }
114
115   return 1;
116 }