Subversion Repositories Games.Chess Giants

Rev

Rev 33 | Rev 154 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33 Rev 108
Line 2... Line 2...
2
#include "data.h"
2
#include "data.h"
3
#include "epdglue.h"
3
#include "epdglue.h"
4
/* last modified 01/17/09 */
4
/* last modified 04/21/15 */
5
/*
5
/*
6
 *******************************************************************************
6
 *******************************************************************************
7
 *                                                                             *
7
 *                                                                             *
8
 *   RootMoveList() is used to set up the ply one move list.  It is a  more    *
8
 *   RootMoveList() is used to set up the ply one move list.  It is a  more    *
9
 *   accurate ordering of the move list than that done for plies deeper than   *
9
 *   accurate ordering of the move list than that done for plies deeper than   *
Line 11... Line 11...
11
 *   expected gain/loss for pieces that can be captured.                       *
11
 *   expected gain/loss for pieces that can be captured.                       *
12
 *                                                                             *
12
 *                                                                             *
13
 *******************************************************************************
13
 *******************************************************************************
14
 */
14
 */
15
void RootMoveList(int wtm) {
15
void RootMoveList(int wtm) {
16
  int *mvp, *lastm, rmoves[256], sort_value[256];
-
 
17
  int i, done, temp, value;
-
 
18
  TREE *const tree = block[0];
16
  TREE *const tree = block[0];
-
 
17
  unsigned mvp, *lastm, rmoves[256];
-
 
18
  int temp, value;
19
#if !defined(NOEGTB)
19
#if !defined(NOEGTB)
20
  int mating_via_tb = 0, tb_value;
20
  int tb_value;
21
#endif
21
#endif
22
 
22
 
23
/*
23
/*
24
 ************************************************************
24
 ************************************************************
25
 *                                                          *
25
 *                                                          *
Line 42... Line 42...
42
      Castle(1, white) + Castle(1, black) == 0 &&
42
      Castle(1, white) + Castle(1, black) == 0 &&
43
      EGTBProbe(tree, 1, wtm, &tb_value)) {
43
      EGTBProbe(tree, 1, wtm, &tb_value)) {
44
    if (swindle_mode && (tb_value == DrawScore(wtm)))
44
    if (swindle_mode && (tb_value == DrawScore(wtm)))
45
      if ((wtm && Material > 0) || (!wtm && Material < 0))
45
      if ((wtm && Material > 0) || (!wtm && Material < 0))
46
        EGTB_draw = 1;
46
        EGTB_draw = 1;
47
    if (tb_value > 32000)
-
 
48
      mating_via_tb = -tb_value - 1;
-
 
49
  }
47
  }
50
#endif
48
#endif
51
/*
49
/*
52
 ************************************************************
50
 ************************************************************
53
 *                                                          *
51
 *                                                          *
Line 57... Line 55...
57
 ************************************************************
55
 ************************************************************
58
 */
56
 */
59
  lastm = GenerateCaptures(tree, 1, wtm, rmoves);
57
  lastm = GenerateCaptures(tree, 1, wtm, rmoves);
60
  lastm = GenerateNoncaptures(tree, 1, wtm, lastm);
58
  lastm = GenerateNoncaptures(tree, 1, wtm, lastm);
61
  n_root_moves = lastm - rmoves;
59
  n_root_moves = lastm - rmoves;
-
 
60
  for (mvp = 0; mvp < (unsigned int) n_root_moves; mvp++) // Pierre-Marie Baty -- added type cast
-
 
61
    root_moves[mvp].move = rmoves[mvp];
62
/*
62
/*
63
 ************************************************************
63
 ************************************************************
64
 *                                                          *
64
 *                                                          *
65
 *  Now make each move and use Quiesce() to analyze the     *
65
 *  Now make each move and use Quiesce() to analyze the     *
66
 *  potential captures and return a minimax score.          *
66
 *  potential captures and return a minimax score.          *
Line 71... Line 71...
71
 *  are left with a very bad sorting score to move them to  *
71
 *  are left with a very bad sorting score to move them to  *
72
 *  the end of the root move list.                          *
72
 *  the end of the root move list.                          *
73
 *                                                          *
73
 *                                                          *
74
 ************************************************************
74
 ************************************************************
75
 */
75
 */
-
 
76
  abort_search = 0;
76
  for (mvp = rmoves; mvp < lastm; mvp++) {
77
  for (mvp = 0; mvp < (unsigned int) n_root_moves; mvp++) { // Pierre-Marie Baty -- added type cast
77
    value = -4000000;
78
    value = -4000000;
78
#if defined(TRACE)
79
#if defined(TRACE)
79
    if (trace_level >= 1) {
80
    if (trace_level >= 1) {
80
      tree->curmv[1] = *mvp;
81
      tree->curmv[1] = root_moves[mvp].move;
81
      tree->phase[1] = HASH_MOVE;
-
 
82
      Trace(tree, 1, 0, wtm, -MATE, MATE, "RootMoves()", tree->phase[1]);
82
      Trace(tree, 1, 0, wtm, -MATE, MATE, "RootMoves()", serial, HASH,
-
 
83
          mvp + 1);
83
    }
84
    }
84
#endif
85
#endif
85
    MakeMove(tree, 1, *mvp, wtm);
86
    MakeMove(tree, 1, wtm, root_moves[mvp].move);
86
    tree->nodes_searched++;
87
    tree->nodes_searched++;
87
    if (!Check(wtm))
88
    if (!Check(wtm))
88
      do {
89
      do {
89
        tree->curmv[1] = *mvp;
90
        tree->curmv[1] = root_moves[mvp].move;
90
#if !defined(NOEGTB)
91
#if !defined(NOEGTB)
91
        if (TotalAllPieces <= EGTBlimit && EGTB_draw &&
92
        if (TotalAllPieces <= EGTBlimit && EGTB_draw &&
92
            Castle(1, white) + Castle(1, black) == 0) {
93
            Castle(1, white) + Castle(1, black) == 0) {
93
          i = EGTBProbe(tree, 2, Flip(wtm), &tb_value);
94
          temp = EGTBProbe(tree, 2, Flip(wtm), &tb_value);
94
          if (i && tb_value != DrawScore(Flip(wtm)))
95
          if (temp && tb_value != DrawScore(Flip(wtm)))
95
            break;
-
 
96
        }
-
 
97
        if (mating_via_tb && TotalAllPieces <= EGTBlimit &&
-
 
98
            Castle(1, white) + Castle(1, black) == 0) {
-
 
99
          i = EGTBProbe(tree, 2, Flip(wtm), &tb_value);
-
 
100
          if (i && ((mating_via_tb > DrawScore(Flip(wtm))
-
 
101
                      && tb_value < mating_via_tb)
-
 
102
                  || (mating_via_tb < DrawScore(Flip(wtm))
-
 
103
                      && tb_value > mating_via_tb)))
-
 
104
            break;
96
            break;
105
        }
97
        }
106
#endif
98
#endif
107
        value = -Quiesce(tree, -MATE, MATE, Flip(wtm), 2, 0);
99
        value = -Quiesce(tree, 2, Flip(wtm), -MATE, MATE, 0);
108
/*
100
/*
109
 ************************************************************
101
 ************************************************************
110
 *                                                          *
102
 *                                                          *
111
 *  Add in a bonus if this move is part of the previous     *
103
 *  Add in a bonus if this move is part of the previous     *
112
 *  principal variation.  It was good in the search, we     *
104
 *  principal variation.  It was good in the search, we     *
113
 *  should try it first now.                                *
105
 *  should try it first now.                                *
114
 *                                                          *
106
 *                                                          *
115
 ************************************************************
107
 ************************************************************
116
 */
108
 */
117
        if ((Piece(*mvp) == Piece(last_pv.path[1]))
109
        if ((Piece(root_moves[mvp].move) == Piece(last_pv.path[1]))
118
            && (From(*mvp) == From(last_pv.path[1]))
110
            && (From(root_moves[mvp].move) == From(last_pv.path[1]))
119
            && (To(*mvp) == To(last_pv.path[1]))
111
            && (To(root_moves[mvp].move) == To(last_pv.path[1]))
120
            && (Captured(*mvp) == Captured(last_pv.path[1]))
112
            && (Captured(root_moves[mvp].move) == Captured(last_pv.path[1]))
121
            && (Promote(*mvp) == Promote(last_pv.path[1])))
113
            && (Promote(root_moves[mvp].move) == Promote(last_pv.path[1])))
122
          value += 2000000;
114
          value += 2000000;
123
/*
115
/*
124
 ************************************************************
116
 ************************************************************
125
 *                                                          *
117
 *                                                          *
126
 *  Fudge the score for promotions so that promotion to a   *
118
 *  Fudge the score for promotions so that promotion to a   *
127
 *  queen is tried first.                                   *
119
 *  queen is tried first.                                   *
128
 *                                                          *
120
 *                                                          *
129
 ************************************************************
121
 ************************************************************
130
 */
122
 */
-
 
123
        if (Promote(root_moves[mvp].move) &&
131
        if (Promote(*mvp) && (Promote(*mvp) != queen))
124
            (Promote(root_moves[mvp].move) != queen))
132
          value -= 50;
125
          value -= 50;
133
      } while (0);
126
      } while (0);
-
 
127
    root_moves[mvp].path = tree->pv[1];
134
    sort_value[mvp - rmoves] = value;
128
    root_moves[mvp].path.pathv = value;
-
 
129
    root_moves[mvp].status = 0;
-
 
130
    root_moves[mvp].bm_age = 0;
135
    UnmakeMove(tree, 1, *mvp, wtm);
131
    UnmakeMove(tree, 1, wtm, root_moves[mvp].move);
136
  }
132
  }
137
/*
133
/*
138
 ************************************************************
134
 ************************************************************
139
 *                                                          *
135
 *                                                          *
140
 *  Sort the moves into order based on the scores returned  *
136
 *  Sort the moves into order based on the scores returned  *
141
 *  by Quiesce() which includes evaluation + captures.      *
137
 *  by Quiesce() which includes evaluation + captures.      *
142
 *                                                          *
138
 *                                                          *
143
 ************************************************************
139
 ************************************************************
144
 */
140
 */
145
  do {
-
 
146
    done = 1;
-
 
147
    for (i = 0; i < lastm - rmoves - 1; i++) {
-
 
148
      if (sort_value[i] < sort_value[i + 1]) {
-
 
149
        temp = sort_value[i];
-
 
150
        sort_value[i] = sort_value[i + 1];
-
 
151
        sort_value[i + 1] = temp;
-
 
152
        temp = rmoves[i];
-
 
153
        rmoves[i] = rmoves[i + 1];
-
 
154
        rmoves[i + 1] = temp;
-
 
155
        done = 0;
-
 
156
      }
-
 
157
    }
-
 
158
  } while (!done);
141
  SortRootMoves();
159
/*
142
/*
160
 ************************************************************
143
 ************************************************************
161
 *                                                          *
144
 *                                                          *
162
 *  Trim the move list to eliminate those moves that hang   *
145
 *  Trim the move list to eliminate those moves that hang   *
163
 *  the king and are illegal.  This also culls any non-     *
146
 *  the king and are illegal.  This also culls any non-     *
Line 166... Line 149...
166
 *  preserve the draw.                                      *
149
 *  preserve the draw.                                      *
167
 *                                                          *
150
 *                                                          *
168
 ************************************************************
151
 ************************************************************
169
 */
152
 */
170
  for (; n_root_moves; n_root_moves--)
153
  for (; n_root_moves; n_root_moves--)
171
    if (sort_value[n_root_moves - 1] > -3000000)
154
    if (root_moves[n_root_moves - 1].path.pathv > -3000000)
172
      break;
155
      break;
173
  if (sort_value[0] > 1000000)
156
  if (root_moves[0].path.pathv > 1000000)
174
    sort_value[0] -= 2000000;
157
    root_moves[0].path.pathv -= 2000000;
175
/*
158
/*
176
 ************************************************************
159
 ************************************************************
177
 *                                                          *
160
 *                                                          *
178
 *  Debugging output to dump root move list and the stuff   *
161
 *  Debugging output to dump root move list and the stuff   *
179
 *  used to sort them, for testing and debugging.           *
162
 *  used to sort them, for testing and debugging.           *
180
 *                                                          *
163
 *                                                          *
181
 ************************************************************
164
 ************************************************************
182
 */
165
 */
183
  if (display_options & 512) {
166
  if (display_options & 128) {
184
    Print(512, "%d moves at root\n", n_root_moves);
167
    Print(128, "%d moves at root\n", n_root_moves);
185
    Print(512, "        move   score\n");
168
    Print(128, "     score    move/pv\n");
186
    for (i = 0; i < n_root_moves; i++) {
169
    for (mvp = 0; mvp < (unsigned int) n_root_moves; mvp++) // Pierre-Marie Baty -- added type cast
187
      tree->curmv[1] = rmoves[i];
-
 
188
      Print(512, "%12s", OutputMove(tree, rmoves[i], 1, wtm));
170
      Print(128, "%10s    %s\n", DisplayEvaluation(root_moves[mvp].path.pathv,
189
      Print(512, "%8d\n", sort_value[i]);
171
              wtm), DisplayPath(tree, wtm, &root_moves[mvp].path));
190
    }
-
 
191
  }
172
  }
192
/*
173
/*
193
 ************************************************************
174
 ************************************************************
194
 *                                                          *
175
 *                                                          *
195
 *  Check to see if we are in the special mode where moves  *
176
 *  Finally, before we return the list of moves, we need to *
196
 *  need to be searched because of missing EGTBs.  This is  *
-
 
197
 *  sorto fo a hack that handles the case where we have an  *
-
 
198
 *  EGTB file like KRPKR, but we don't have the files for   *
-
 
199
 *  promotions for the pawn.  The program would avoid any   *
-
 
200
 *  pawn promotion since it likely could not see the mate,  *
-
 
201
 *  because the EGTB position does have a mate score.  In   *
177
 *  set the values to an impossible negative value so that  *
202
 *  this case, we turn EGTBs off for this search so that we *
178
 *  as we sort the root move list after fail highs and lows *
203
 *  can see a reason to promote the pawn and make progress  *
179
 *  the un-searched moves won't pop to the top of the list. *
204
 *  rather than just sitting on our pawn advantage.         *
-
 
205
 *                                                          *
180
 *                                                          *
206
 ************************************************************
181
 ************************************************************
207
 */
182
 */
208
#if !defined(NOEGTB)
-
 
209
  if (mating_via_tb) {
-
 
210
    for (i = 0; i < n_root_moves; i++) {
-
 
211
      tree->curmv[1] = rmoves[i];
-
 
212
      MakeMove(tree, 1, rmoves[i], wtm);
-
 
213
      if (mating_via_tb && TotalAllPieces <= EGTBlimit &&
-
 
214
          Castle(1, white) + Castle(1, black) == 0)
-
 
215
        temp =
-
 
216
            (EGTBProbe(tree, 2, Flip(wtm),
-
 
217
                &tb_value) != DrawScore(Flip(wtm)));
-
 
218
      else
-
 
219
        temp = 0;
-
 
220
      UnmakeMove(tree, 1, rmoves[i], wtm);
-
 
221
      if (temp)
-
 
222
        break;
-
 
223
    }
-
 
224
    EGTB_search = (i == n_root_moves);
-
 
225
  } else
-
 
226
    EGTB_search = 0;
-
 
227
#endif
-
 
228
/*
-
 
229
 ************************************************************
-
 
230
 *                                                          *
-
 
231
 *  Copy the root moves into the root_move structure array  *
183
  for (mvp = 1; mvp < (unsigned int) n_root_moves; mvp++) // Pierre-Marie Baty -- added type cast
232
 *  for use by NextRootMove().                              *
-
 
233
 *                                                          *
-
 
234
 ************************************************************
-
 
235
 */
-
 
236
  for (i = 0; i < n_root_moves; i++) {
-
 
237
    root_moves[i].move = rmoves[i];
-
 
238
    root_moves[i].status = 4;
184
    root_moves[mvp].path.pathv = -99999999;
239
    root_moves[i].bm_age = 0;
-
 
240
  }
-
 
241
  root_moves[0].status = 0;
-
 
242
  return;
185
  return;
243
}
186
}