4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
9 * GNU SHOGI is based on GNU CHESS
11 * Copyright (c) 1988, 1989, 1990 John Stanback
12 * Copyright (c) 1992 Free Software Foundation
14 * This file is part of GNU SHOGI.
16 * GNU Shogi is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 3 of the License,
19 * or (at your option) any later version.
21 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with GNU Shogi; see the file COPYING. If not, see
28 * <http://www.gnu.org/licenses/>.
29 * ----------------------------------------------------------------------
35 unsigned int ttbllimit;
38 * ptype is used to separate black and white pawns, like this; ptyp =
39 * ptype[side][piece] piece can be used directly in nextpos/nextdir when
40 * generating moves for pieces that are not white pawns.
43 const small_short ptype[2][NO_PIECES] =
46 ptype_no_piece, ptype_pawn,
48 ptype_lance, ptype_knight,
50 ptype_silver, ptype_gold, ptype_bishop, ptype_rook,
53 ptype_gold, ptype_gold,
56 ptype_pbishop, ptype_prook, ptype_king
59 ptype_no_piece, ptype_wpawn,
61 ptype_wlance, ptype_wknight,
63 ptype_wsilver, ptype_wgold, ptype_bishop, ptype_rook,
66 ptype_wgold, ptype_wgold,
69 ptype_pbishop, ptype_prook, ptype_king
73 const small_short promoted[NO_PIECES] =
79 psilver, gold, pbishop, prook,
84 psilver, pbishop, prook, king
87 const small_short unpromoted[NO_PIECES] =
93 silver, gold, bishop, rook,
98 silver, bishop, rook, king
102 /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
105 #define max(a, b) (((a) < (b))?(b):(a))
107 #define odd(a) ((a) & 1)
109 const small_short piece_of_ptype[NO_PTYPE_PIECES] =
115 silver, gold, bishop, rook, pbishop, prook, king,
124 /* FIXME: all bishops and rooks are black ? */
125 const small_short side_of_ptype[NO_PTYPE_PIECES] =
131 black, black, black, black, black, black, black,
142 int n = sizeof(struct hashentry)*(size + rehash);
146 if(oldSize == size) return use_ttable;
147 oldSize = ttblsize = size;
149 if(ttable[0]) free(ttable[0]);
150 if(ttable[1]) free(ttable[1]);
152 while (doit && ttblsize > MINTTABLE)
154 ttable[0] = malloc(n); /* FIXME: cast to the correct type. */
155 ttable[1] = ttable[0] ? malloc(n) : NULL;
157 if (!ttable[0] || !ttable[1])
165 ttblsize = ttblsize >> 1;
166 n = sizeof(struct hashentry) * (ttblsize + rehash);
174 if (ttblsize <= MINTTABLE)
181 /* CHECKME: is the precedence here correct? */
182 /* ttbllimit = ttblsize << 1 - ttblsize >> 2; */
183 ttbllimit = (ttblsize << 1) - (ttblsize >> 2);
187 ttable[0] = ttable[1] = NULL;
194 Initialize_data(void)
205 dsp->ShowMessage("datatype 'small_short' is unsigned; "
206 "check gnushogi.h\n");
211 n = sizeof(struct leaf) * (size_t)TREE;
216 sprintf(buffer, "Cannot allocate %ld bytes for search tree",
218 dsp->ShowMessage(buffer);
222 n = sizeof(hashcode_array);
223 hashcode = malloc(n);
227 sprintf(buffer, "Cannot allocate %ld bytes for hashcode", (long)n);
228 dsp->ShowMessage(buffer);
232 n = sizeof(drop_hashcode_array);
233 drop_hashcode = malloc(n);
238 "Cannot allocate %ld bytes for drop_hashcode",
240 dsp->ShowMessage(buffer);
244 n = sizeof(struct GameRec) * (size_t)(MAXMOVES + MAXDEPTH);
245 GameList = malloc(n);
250 "Cannot allocate %ld bytes for game record",
252 dsp->ShowMessage(buffer);
256 #if !defined SAVE_NEXTPOS
257 n = sizeof(next_array);
259 for (i = 0; i < NO_PTYPE_PIECES; i++)
261 nextdir[i] = use_nextpos ? malloc(n) : NULL;
267 sprintf(buffer, "cannot allocate %ld space for nextdir %d",
269 dsp->ShowMessage(buffer);
276 nextpos[i] = use_nextpos ? malloc(n) : NULL;
282 sprintf(buffer, "cannot allocate %ld space for nextpos %d",
284 dsp->ShowMessage(buffer);
297 n = sizeof(value_array);
302 dsp->ShowMessage("cannot allocate value space");
306 n = sizeof(fscore_array);
311 dsp->ShowMessage("cannot allocate fscore space");
321 sprintf(buffer, "Cannot allocate %ld bytes for history table",
322 (long)sizeof_history);
323 dsp->ShowMessage(buffer);
329 n = sizeof(struct etable) * (size_t)ETABLE;
331 for (i = 0; i < 2; i++)
333 etab[i] = use_etable ? malloc(n) : 0;
337 sprintf(buffer, "Cannot allocate %ld bytes for cache table %ld",
339 dsp->ShowMessage(buffer);
350 if (!AllocateTT(ttblsize))
352 sprintf(buffer, "Cannot allocate %ld bytes for transposition table",
354 dsp->ShowMessage(buffer);
358 #if !defined SAVE_DISTDATA
359 n = sizeof(distdata_array);
360 distdata = malloc(n);
364 dsp->ShowMessage("cannot allocate distdata space...");
365 use_distdata = false;
369 #if !defined SAVE_PTYPE_DISTDATA
370 n = sizeof(distdata_array);
372 for (i = 0; i < NO_PTYPE_PIECES; i++)
374 ptype_distdata[i] = use_ptype_distdata ? malloc(n) : 0;
376 if (!ptype_distdata[i])
379 "cannot allocate %ld bytes for ptype_distdata %d...",
381 use_ptype_distdata = false;
390 #ifdef SAVE_PTYPE_DISTDATA
392 piece_distance(short side, short piece, short f, short t)
394 return ((f > NO_SQUARES)
396 : (short)ptype_distance(ptype[side][piece], f, t));
400 piece_distance(short side, short piece, short f, short t)
402 return ((f > NO_SQUARES)
404 : (use_ptype_distdata
405 ? (short)(*ptype_distdata[ptype[side][piece]])[f][t]
406 : (short)ptype_distance(ptype[side][piece], f, t)));
412 * Determine the minimum number of moves for a piece from
413 * square "f" to square "t". If the piece cannot reach "t",
414 * the count is set to CANNOT_REACH.
417 #define csquare(sq) ((side == black) ? sq : (NO_SQUARES - 1 - sq))
418 #define crow(sq) row(csquare(sq))
419 #define ccol(sq) column(csquare(sq))
422 ptype_distance(short ptyp, short f, short t)
425 short colf, colt, rowf, rowt, dcol, drow;
430 piece = piece_of_ptype[ptyp];
431 side = side_of_ptype[ptyp];
433 dcol = (colt = ccol(t)) - (colf = ccol(f));
434 drow = (rowt = crow(t)) - (rowf = crow(f));
439 if ((dcol != 0) || (drow < 1))
446 if ((dcol != 0) || (drow < 1))
452 if (odd(drow) || (odd(drow / 2) != odd(dcol)))
454 else if ((drow == 0) || ((drow / 2) < abs(dcol)))
463 if (odd(drow) == odd(dcol))
465 return max(abs(drow), abs(dcol));
469 if (abs(dcol) <= drow)
472 return (max(abs(drow), abs(dcol)) + 1);
477 if (odd(drow) == odd(dcol))
478 return (max(abs(drow), abs(dcol)));
480 return (max(abs(drow) + 1, abs(dcol)) + 1);
493 return max(drow, abs(dcol));
495 return (abs(dcol) - drow);
498 if (odd(dcol) != odd(drow))
501 return ((abs(dcol) == abs(drow)) ? 1 : 2);
504 if (odd(dcol) != odd(drow))
506 if ((abs(dcol) <= 1) && (abs(drow) <= 1))
508 else if (abs(abs(dcol) - abs(drow)) == 1)
515 return ((abs(dcol) == abs(drow)) ? 1 : 2);
519 if ((dcol == 0) || (drow == 0))
525 if ((dcol == 0) || (drow == 0))
527 else if ((abs(dcol) == 1) && (abs(drow) == 1))
533 return max(abs(drow), abs(dcol));
536 /* should never occur */
537 return (CANNOT_REACH);