Force iteration to start at 1 in analyze mode
[bonanza.git] / problem.c
1 #include <math.h>
2 #include <limits.h>
3 #include "shogi.h"
4
5 int CONV
6 solve_problems( tree_t * restrict ptree, unsigned int nposition )
7 {
8   const char *str_move;
9   uint64_t total_node;
10   unsigned int game_status_save, move, uposition, te1, te0;
11   int iret, success, failure, ianswer, istatus, iresult;
12
13   success = failure = 0;
14   total_node = 0;
15   if ( get_elapsed( &te0 ) < 0 ) { return -1; };
16
17   for ( uposition = 0; uposition < nposition; uposition++ )
18     {
19       istatus = in_CSA( ptree, &record_problems, NULL,
20                         ( flag_nomake_move | flag_detect_hang ) );
21       if ( istatus < 0 ) { return istatus; }
22
23       if ( istatus > record_next )
24         {
25           snprintf( str_message, SIZE_MESSAGE, str_fmt_line,
26                     record_problems.lines, str_bad_record );
27           str_error = str_message;
28           return -2;
29         }
30
31       /* examine all of answers */
32       Out( "Answers:" );
33       for ( ianswer = 0; ianswer < MAX_ANSWER; ianswer++ )
34         {
35           str_move = &( record_problems.info.str_move[ianswer][0] );
36           if ( str_move[0] == '\0' ) { break; }
37           if ( ( root_turn && str_move[0] != '-' )
38                || ( ! root_turn && str_move[0] != '+' ) )
39             {
40               snprintf( str_message, SIZE_MESSAGE, str_fmt_line,
41                         record_problems.lines,
42                         "Answers has invalid sign of turn." );
43               str_error = str_message;
44               return -2;
45             }
46
47           iret = interpret_CSA_move( ptree, &move, str_move+1 );
48           if ( iret < 0 ) { return iret; }
49
50           iret = make_move_root( ptree, move, ( flag_detect_hang | flag_rep
51                                                 | flag_nomake_move ) );
52           if ( iret < 0 ) { return iret; }
53
54           Out( "%s ", str_move );
55         }
56       Out( "\n" );
57       if ( ! ianswer )
58         {
59           snprintf( str_message, SIZE_MESSAGE, str_fmt_line,
60                     record_problems.lines,
61                     "No answers in the record" );
62           str_error = str_message;
63           return -2;
64         }
65
66       iret = out_board( ptree, stdout, 0, 0 );
67       if ( iret < 0 ) { return iret; }
68
69       if ( get_elapsed( &time_start ) < 0 ) { return -1; };
70       time_turn_start = time_start;
71
72       game_status_save  = game_status;
73       game_status      |= flag_problem | flag_nopeek | flag_thinking;
74       iresult           = iterate( ptree );
75       game_status       = game_status_save;
76       if ( iresult < 0 ) { return iresult; }
77
78       if ( iresult ) { success++; }
79       else           { failure++; }
80
81       total_node += ptree->node_searched;
82
83       str_move = str_CSA_move( last_pv.a[1] );
84       Out( "problem #%d answer=%s -- %s (correct=%d, incorrect=%d)\n\n",
85            success+failure, str_move, iresult ? "correct" : "incorrect",
86            success, failure );
87
88       if ( istatus == record_eof ) { break; }
89       if ( istatus == record_misc )
90         {
91           iret = record_wind( &record_problems );
92           if ( iret < 0 )           { return iret; }
93           if ( iret == record_eof ) { break; }
94         }
95     }
96
97   if ( get_elapsed( &te1 ) < 0 ) { return -1; }
98
99   Out( "Total Nodes:   %" PRIu64 "\n", total_node );
100   Out( "Total Elapsed: %.2f\n", (double)( te1 - te0 ) / 1000.0 );
101
102   return 1;
103 }
104
105
106 #if defined(DFPN)
107 int CONV
108 solve_mate_problems( tree_t * restrict ptree, unsigned int nposition )
109 {
110   unsigned int uposition;
111   int iret;
112
113   for ( uposition = 0; uposition < nposition; uposition++ )
114     {
115       int imove, istatus;
116       unsigned int record_move;
117
118       for ( imove = 0; ; imove++ )
119         {
120           istatus = in_CSA( ptree, &record_problems, &record_move,
121                             ( flag_nomake_move | flag_detect_hang
122                               | flag_nofmargin ) );
123           if ( istatus < 0 )
124             {
125               str_error = str_illegal_move;
126               return -1;
127             }
128
129           if ( istatus >= record_eof ) { break; }
130
131           iret = make_move_root( ptree, record_move, 0 );
132           if ( iret < 0 ) { return iret; }
133         }
134
135       Out( "Problem #%d %s\n",
136            uposition, ptree->nsuc_check[1] ? "(in check)" : "" );
137         
138       iret = dfpn( ptree, root_turn, 1 );
139       if ( iret < 0 ) { return iret; }
140
141       if ( istatus == record_eof ) { break; }
142       if ( istatus == record_misc )
143         {
144           iret = record_wind( &record_problems );
145           if ( iret < 0 )           { return iret; }
146           if ( iret == record_eof ) { break; }
147         }
148     }
149
150   return 1;
151 }
152 #endif