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
,
141 Initialize_data(void)
153 dsp
->ShowMessage("datatype 'small_short' is unsigned; "
154 "check gnushogi.h\n");
159 n
= sizeof(struct leaf
) * (size_t)TREE
;
164 sprintf(buffer
, "Cannot allocate %ld bytes for search tree",
166 dsp
->ShowMessage(buffer
);
170 n
= sizeof(hashcode_array
);
171 hashcode
= malloc(n
);
175 sprintf(buffer
, "Cannot allocate %ld bytes for hashcode", (long)n
);
176 dsp
->ShowMessage(buffer
);
180 n
= sizeof(drop_hashcode_array
);
181 drop_hashcode
= malloc(n
);
186 "Cannot allocate %ld bytes for drop_hashcode",
188 dsp
->ShowMessage(buffer
);
192 n
= sizeof(struct GameRec
) * (size_t)(MAXMOVES
+ MAXDEPTH
);
193 GameList
= malloc(n
);
198 "Cannot allocate %ld bytes for game record",
200 dsp
->ShowMessage(buffer
);
204 #if !defined SAVE_NEXTPOS
205 n
= sizeof(next_array
);
207 for (i
= 0; i
< NO_PTYPE_PIECES
; i
++)
209 nextdir
[i
] = use_nextpos
? malloc(n
) : NULL
;
215 sprintf(buffer
, "cannot allocate %ld space for nextdir %d",
217 dsp
->ShowMessage(buffer
);
224 nextpos
[i
] = use_nextpos
? malloc(n
) : NULL
;
230 sprintf(buffer
, "cannot allocate %ld space for nextpos %d",
232 dsp
->ShowMessage(buffer
);
245 n
= sizeof(value_array
);
250 dsp
->ShowMessage("cannot allocate value space");
254 n
= sizeof(fscore_array
);
259 dsp
->ShowMessage("cannot allocate fscore space");
269 sprintf(buffer
, "Cannot allocate %ld bytes for history table",
270 (long)sizeof_history
);
271 dsp
->ShowMessage(buffer
);
277 n
= sizeof(struct etable
) * (size_t)ETABLE
;
279 for (i
= 0; i
< 2; i
++)
281 etab
[i
] = use_etable
? malloc(n
) : 0;
285 sprintf(buffer
, "Cannot allocate %ld bytes for cache table %ld",
287 dsp
->ShowMessage(buffer
);
298 n
= sizeof(struct hashentry
)*(ttblsize
+ rehash
);
300 while (doit
&& ttblsize
> MINTTABLE
)
302 ttable
[0] = malloc(n
); /* FIXME: cast to the correct type. */
303 ttable
[1] = ttable
[0] ? malloc(n
) : NULL
;
305 if (!ttable
[0] || !ttable
[1])
313 ttblsize
= ttblsize
>> 1;
314 n
= sizeof(struct hashentry
) * (ttblsize
+ rehash
);
322 if (ttblsize
<= MINTTABLE
)
329 /* CHECKME: is the precedence here correct? */
330 /* ttbllimit = ttblsize << 1 - ttblsize >> 2; */
331 ttbllimit
= (ttblsize
<< 1) - (ttblsize
>> 2);
335 sprintf(buffer
, "Cannot allocate %ld bytes for transposition table",
337 dsp
->ShowMessage(buffer
);
338 ttable
[0] = ttable
[1] = NULL
;
342 #if !defined SAVE_DISTDATA
343 n
= sizeof(distdata_array
);
344 distdata
= malloc(n
);
348 dsp
->ShowMessage("cannot allocate distdata space...");
349 use_distdata
= false;
353 #if !defined SAVE_PTYPE_DISTDATA
354 n
= sizeof(distdata_array
);
356 for (i
= 0; i
< NO_PTYPE_PIECES
; i
++)
358 ptype_distdata
[i
] = use_ptype_distdata
? malloc(n
) : 0;
360 if (!ptype_distdata
[i
])
363 "cannot allocate %ld bytes for ptype_distdata %d...",
365 use_ptype_distdata
= false;
374 #ifdef SAVE_PTYPE_DISTDATA
376 piece_distance(short side
, short piece
, short f
, short t
)
378 return ((f
> NO_SQUARES
)
380 : (short)ptype_distance(ptype
[side
][piece
], f
, t
));
384 piece_distance(short side
, short piece
, short f
, short t
)
386 return ((f
> NO_SQUARES
)
388 : (use_ptype_distdata
389 ? (short)(*ptype_distdata
[ptype
[side
][piece
]])[f
][t
]
390 : (short)ptype_distance(ptype
[side
][piece
], f
, t
)));
396 * Determine the minimum number of moves for a piece from
397 * square "f" to square "t". If the piece cannot reach "t",
398 * the count is set to CANNOT_REACH.
401 #define csquare(sq) ((side == black) ? sq : (NO_SQUARES - 1 - sq))
402 #define crow(sq) row(csquare(sq))
403 #define ccol(sq) column(csquare(sq))
406 ptype_distance(short ptyp
, short f
, short t
)
409 short colf
, colt
, rowf
, rowt
, dcol
, drow
;
414 piece
= piece_of_ptype
[ptyp
];
415 side
= side_of_ptype
[ptyp
];
417 dcol
= (colt
= ccol(t
)) - (colf
= ccol(f
));
418 drow
= (rowt
= crow(t
)) - (rowf
= crow(f
));
423 if ((dcol
!= 0) || (drow
< 1))
430 if ((dcol
!= 0) || (drow
< 1))
436 if (odd(drow
) || (odd(drow
/ 2) != odd(dcol
)))
438 else if ((drow
== 0) || ((drow
/ 2) < abs(dcol
)))
447 if (odd(drow
) == odd(dcol
))
449 return max(abs(drow
), abs(dcol
));
453 if (abs(dcol
) <= drow
)
456 return (max(abs(drow
), abs(dcol
)) + 1);
461 if (odd(drow
) == odd(dcol
))
462 return (max(abs(drow
), abs(dcol
)));
464 return (max(abs(drow
) + 1, abs(dcol
)) + 1);
477 return max(drow
, abs(dcol
));
479 return (abs(dcol
) - drow
);
482 if (odd(dcol
) != odd(drow
))
485 return ((abs(dcol
) == abs(drow
)) ? 1 : 2);
488 if (odd(dcol
) != odd(drow
))
490 if ((abs(dcol
) <= 1) && (abs(drow
) <= 1))
492 else if (abs(abs(dcol
) - abs(drow
)) == 1)
499 return ((abs(dcol
) == abs(drow
)) ? 1 : 2);
503 if ((dcol
== 0) || (drow
== 0))
509 if ((dcol
== 0) || (drow
== 0))
511 else if ((abs(dcol
) == 1) && (abs(drow
) == 1))
517 return max(abs(drow
), abs(dcol
));
520 /* should never occur */
521 return (CANNOT_REACH
);