4 unsigned int ttbllimit;
7 * ptype is used to separate black and white pawns, like this; ptyp =
8 * ptype[side][piece] piece can be used directly in nextpos/nextdir when
9 * generating moves for pieces that are not white pawns.
12 const small_short ptype[2][NO_PIECES] =
15 ptype_no_piece, ptype_pawn,
17 ptype_lance, ptype_knight,
19 ptype_silver, ptype_gold, ptype_bishop, ptype_rook,
22 ptype_gold, ptype_gold,
25 ptype_pbishop, ptype_prook, ptype_king
28 ptype_no_piece, ptype_wpawn,
30 ptype_wlance, ptype_wknight,
32 ptype_wsilver, ptype_wgold, ptype_bishop, ptype_rook,
35 ptype_wgold, ptype_wgold,
38 ptype_pbishop, ptype_prook, ptype_king
42 const small_short promoted[NO_PIECES] =
48 psilver, gold, pbishop, prook,
53 psilver, pbishop, prook, king
56 const small_short unpromoted[NO_PIECES] =
62 silver, gold, bishop, rook,
67 silver, bishop, rook, king
71 /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
74 #define max(a, b) (((a) < (b))?(b):(a))
76 #define odd(a) ((a) & 1)
78 const small_short piece_of_ptype[NO_PTYPE_PIECES] =
84 silver, gold, bishop, rook, pbishop, prook, king,
93 /* FIXME: all bishops and rooks are black ? */
94 const small_short side_of_ptype[NO_PTYPE_PIECES] =
100 black, black, black, black, black, black, black,
110 Initialize_data(void)
122 ShowMessage("datatype 'small_short' is unsigned; "
123 "check gnushogi.h\n");
128 n = sizeof(struct leaf) * (size_t)TREE;
133 sprintf(buffer, "Cannot allocate %ld bytes for search tree",
139 n = sizeof(hashcode_array);
140 hashcode = malloc(n);
144 sprintf(buffer, "Cannot allocate %ld bytes for hashcode", (long)n);
149 n = sizeof(drop_hashcode_array);
150 drop_hashcode = malloc(n);
155 "Cannot allocate %ld bytes for drop_hashcode",
161 n = sizeof(struct GameRec) * (size_t)(MAXMOVES + MAXDEPTH);
162 GameList = malloc(n);
167 "Cannot allocate %ld bytes for game record",
173 #if !defined SAVE_NEXTPOS
174 n = sizeof(next_array);
176 for (i = 0; i < NO_PTYPE_PIECES; i++)
178 nextdir[i] = use_nextpos ? malloc(n) : NULL;
184 sprintf(buffer, "cannot allocate %ld space for nextdir %d",
193 nextpos[i] = use_nextpos ? malloc(n) : NULL;
199 sprintf(buffer, "cannot allocate %ld space for nextpos %d",
214 n = sizeof(value_array);
219 ShowMessage("cannot allocate value space");
223 n = sizeof(fscore_array);
228 ShowMessage("cannot allocate fscore space");
238 sprintf(buffer, "Cannot allocate %ld bytes for history table",
239 (long)sizeof_history);
246 n = sizeof(struct etable) * (size_t)ETABLE;
248 for (i = 0; i < 2; i++)
250 etab[i] = use_etable ? malloc(n) : 0;
254 sprintf(buffer, "Cannot allocate %ld bytes for cache table %ld",
267 n = sizeof(struct hashentry)*(ttblsize + rehash);
269 while (doit && ttblsize > MINTTABLE)
271 ttable[0] = malloc(n); /* FIXME: cast to the correct type. */
272 ttable[1] = ttable[0] ? malloc(n) : NULL;
274 if (!ttable[0] || !ttable[1])
282 ttblsize = ttblsize >> 1;
283 n = sizeof(struct hashentry) * (ttblsize + rehash);
291 if (ttblsize <= MINTTABLE)
298 /* CHECKME: is the precedence here correct? */
299 /* ttbllimit = ttblsize << 1 - ttblsize >> 2; */
300 ttbllimit = (ttblsize << 1) - (ttblsize >> 2);
304 sprintf(buffer, "Cannot allocate %ld bytes for transposition table",
307 ttable[0] = ttable[1] = NULL;
311 #if !defined SAVE_DISTDATA
312 n = sizeof(distdata_array);
313 distdata = malloc(n);
317 ShowMessage("cannot allocate distdata space...");
318 use_distdata = false;
322 #if !defined SAVE_PTYPE_DISTDATA
323 n = sizeof(distdata_array);
325 for (i = 0; i < NO_PTYPE_PIECES; i++)
327 ptype_distdata[i] = use_ptype_distdata ? malloc(n) : 0;
329 if (!ptype_distdata[i])
332 "cannot allocate %ld bytes for ptype_distdata %d...",
334 use_ptype_distdata = false;
343 #ifdef SAVE_PTYPE_DISTDATA
345 piece_distance(short side, short piece, short f, short t)
347 return ((f > NO_SQUARES)
349 : (short)ptype_distance(ptype[side][piece], f, t));
353 piece_distance(short side, short piece, short f, short t)
355 return ((f > NO_SQUARES)
357 : (use_ptype_distdata
358 ? (short)(*ptype_distdata[ptype[side][piece]])[f][t]
359 : (short)ptype_distance(ptype[side][piece], f, t)));
365 * Determine the minimum number of moves for a piece from
366 * square "f" to square "t". If the piece cannot reach "t",
367 * the count is set to CANNOT_REACH.
370 #define csquare(sq) ((side == black) ? sq : (NO_SQUARES - 1 - sq))
371 #define crow(sq) row(csquare(sq))
372 #define ccol(sq) column(csquare(sq))
375 ptype_distance(short ptyp, short f, short t)
378 short colf, colt, rowf, rowt, dcol, drow;
383 piece = piece_of_ptype[ptyp];
384 side = side_of_ptype[ptyp];
386 dcol = (colt = ccol(t)) - (colf = ccol(f));
387 drow = (rowt = crow(t)) - (rowf = crow(f));
392 if ((dcol != 0) || (drow < 1))
399 if ((dcol != 0) || (drow < 1))
405 if (odd(drow) || (odd(drow / 2) != odd(dcol)))
407 else if ((drow == 0) || ((drow / 2) < abs(dcol)))
416 if (odd(drow) == odd(dcol))
418 return max(abs(drow), abs(dcol));
422 if (abs(dcol) <= drow)
425 return (max(abs(drow), abs(dcol)) + 1);
430 if (odd(drow) == odd(dcol))
431 return (max(abs(drow), abs(dcol)));
433 return (max(abs(drow) + 1, abs(dcol)) + 1);
446 return max(drow, abs(dcol));
448 return (abs(dcol) - drow);
451 if (odd(dcol) != odd(drow))
454 return ((abs(dcol) == abs(drow)) ? 1 : 2);
457 if (odd(dcol) != odd(drow))
459 if ((abs(dcol) <= 1) && (abs(drow) <= 1))
461 else if (abs(abs(dcol) - abs(drow)) == 1)
468 return ((abs(dcol) == abs(drow)) ? 1 : 2);
472 if ((dcol == 0) || (drow == 0))
478 if ((dcol == 0) || (drow == 0))
480 else if ((abs(dcol) == 1) && (abs(drow) == 1))
486 return max(abs(drow), abs(dcol));
489 /* should never occur */
490 return (CANNOT_REACH);