Subversion Repositories Games.Chess Giants

Rev

Rev 108 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
33 pmbaty 1
#include <signal.h>
2
#include "chess.h"
3
#include "data.h"
4
/* last modified 01/17/09 */
5
/*
6
 *******************************************************************************
7
 *                                                                             *
8
 *   Interrupt() is used to read in a move when the operator types something   *
9
 *   while a search is in progress (during pondering as one example.)  This    *
10
 *   routine reads in a command (move) and then makes two attempts to use this *
11
 *   input:  (1) call Option() to see if the command can be executed;  (2) try *
12
 *   InputMove() to see if this input is a legal move;  If so, and we are      *
13
 *   pondering see if it matches the move we are pondering.                    *
14
 *                                                                             *
15
 *******************************************************************************
16
 */
17
void Interrupt(int ply) {
18
  int temp, i, left = 0, readstat, result, time_used;
19
  int save_move_number;
20
  TREE *const tree = block[0];
21
 
22
/*
23
 ************************************************************
24
 *                                                          *
25
 *  If trying to find a move to ponder, and the operator    *
26
 *  types a command, exit a.s.a.p.                          *
27
 *                                                          *
28
 ************************************************************
29
 */
30
  if (puzzling)
31
    abort_search = 2;
32
/*
33
 ************************************************************
34
 *                                                          *
35
 *  First check to see if this is a command by calling      *
36
 *  Option().  Option() will return a 0 if it didn't        *
37
 *  recognize the command; otherwise it returns a 1 if the  *
38
 *  command was executed, or a 2 if we need to abort the    *
39
 *  search to execute the command.                          *
40
 *                                                          *
41
 ************************************************************
42
 */
43
  else
44
    do {
45
      readstat = Read(0, buffer, sizeof (buffer)); // Pierre-Marie Baty -- use safe version
46
      if (readstat <= 0)
47
        break;
48
      nargs = ReadParse(buffer, args, "         ;");
49
      if (nargs == 0) {
50
        Print(128, "ok.\n");
51
        break;
52
      }
53
      if (strcmp(args[0], ".")) {
54
        save_move_number = move_number;
55
        if (!game_wtm)
56
          move_number--;
57
        if (root_wtm)
58
          Print(128, "Black(%d): %s\n", move_number, buffer);
59
        else
60
          Print(128, "White(%d): %s\n", move_number, buffer);
61
        move_number = save_move_number;
62
      }
63
/*
64
 ************************************************************
65
 *                                                          *
66
 *  "." command displays status of current search (this is  *
67
 *  part of winboard protocol.)                             *
68
 *                                                          *
69
 ************************************************************
70
 */
71
      if (!strcmp(args[0], ".")) {
72
        if (xboard) {
73
          end_time = ReadClock();
74
          time_used = (end_time - start_time);
75
          printf("stat01: %d ", time_used);
76
          printf("%" PRIu64 " ", tree->nodes_searched);
77
          printf("%d ", iteration_depth);
78
          for (i = 0; i < n_root_moves; i++)
79
            if (!(root_moves[i].status & 8))
80
              left++;
81
          printf("%d %d\n", left, n_root_moves);
82
          fflush(stdout);
83
          break;
84
        } else {
85
          end_time = ReadClock();
86
          time_used = (end_time - start_time);
87
          printf("time:%s ", DisplayTime(time_used));
88
          printf("nodes:%" PRIu64 "\n", tree->nodes_searched);
89
          DisplayTreeState(block[0], 1, 0, ply);
90
        }
91
      }
92
/*
93
 ************************************************************
94
 *                                                          *
95
 *  "?" command says "move now!"                            *
96
 *                                                          *
97
 ************************************************************
98
 */
99
      else if (!strcmp(args[0], "?")) {
100
        if (thinking) {
101
          abort_search = 1;
102
        }
103
      }
104
/*
105
 ************************************************************
106
 *                                                          *
107
 *  Next see if Option() recognizes this as a command. Note *
108
 *  some commands can't be executed in the middle of a      *
109
 *  search.  Option() returns a value >= 2 in such cases.   *
110
 *  If we are pondering, we can back out of the search and  *
111
 *  execute the command, otherwise we produce an error and  *
112
 *  continue searching for our move.                        *
113
 *                                                          *
114
 ************************************************************
115
 */
116
      else {
117
        save_move_number = move_number;
118
        if (!analyze_mode && !game_wtm)
119
          move_number--;
120
        result = Option(tree);
121
        move_number = save_move_number;
122
        if (result >= 2) {
123
          if (thinking && result != 3)
124
            Print(128, "command not legal now.\n");
125
          else {
126
            abort_search = 2;
127
            input_status = 2;
128
            break;
129
          }
130
        } else if ((result != 1) && analyze_mode) {
131
          abort_search = 1;
132
          input_status = 2;
133
          break;
134
        }
135
/*
136
 ************************************************************
137
 *                                                          *
138
 *  Option() didn't recognize the input as a command so now *
139
 *  we check to see if the operator typed a move.  If so,   *
140
 *  and it matched the predicted move, switch from          *
141
 *  pondering to thinking to start the timer.  If this is a *
142
 *  move, but not the predicted move, abort the search and  *
143
 *  start over with the right move.                         *
144
 *                                                          *
145
 ************************************************************
146
 */
147
        else if (!result) {
148
          if (pondering) {
149
            nargs = ReadParse(buffer, args, "   ;");
150
            temp = InputMove(tree, args[0], 0, Flip(root_wtm), 1, 1);
151
            if (temp) {
152
              if ((From(temp) == From(ponder_move))
153
                  && (To(temp) == To(ponder_move))
154
                  && (Piece(temp) == Piece(ponder_move))
155
                  && (Captured(temp) == Captured(ponder_move))
156
                  && (Promote(temp) == Promote(ponder_move))) {
157
                predicted++;
158
                input_status = 1;
159
                pondering = 0;
160
                thinking = 1;
161
                opponent_end_time = ReadClock();
162
                program_start_time = ReadClock();
163
                Print(128, "predicted move made.\n");
164
              } else {
165
                input_status = 2;
166
                abort_search = 2;
167
                break;
168
              }
169
            } else if (!strcmp(args[0], "go") || !strcmp(args[0], "move")
170
                || !strcmp(args[0], "SP")) {
171
              abort_search = 2;
172
              break;
173
            } else
174
              Print(4095, "Illegal move: %s\n", args[0]);
175
          } else
176
            Print(4095, "unrecognized/illegal command: %s\n", args[0]);
177
        }
178
      }
179
    } while (1);
180
  if (log_file)
181
    fflush(log_file);
182
}