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