4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
8 * GNU SHOGI is based on GNU CHESS
10 * Copyright (c) 1988, 1989, 1990 John Stanback
11 * Copyright (c) 1992 Free Software Foundation
13 * This file is part of GNU SHOGI.
15 * GNU Shogi is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 3 of the License,
18 * or (at your option) any later version.
20 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
21 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
22 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 * You should have received a copy of the GNU General Public License along
26 * with GNU Shogi; see the file COPYING. If not, see
27 * <http://www.gnu.org/licenses/>.
28 * ----------------------------------------------------------------------
38 /* Hash table for preventing multiple scoring of the same position */
40 int EADD = 0; /* number of writes to the cache table */
41 int EGET = 0; /* number of hits to the cache table */
42 int PUTVAR = false; /* shall the current scoring be cached? */
45 /* Pieces and colors of initial board setup */
48 const small_short Stboard[NO_SQUARES] =
50 lance, knight, silver, gold, king, gold, silver, knight, lance,
51 0, bishop, 0, 0, 0, 0, 0, rook, 0,
52 pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
53 0, 0, 0, 0, 0, 0, 0, 0, 0,
54 0, 0, 0, 0, 0, 0, 0, 0, 0,
55 0, 0, 0, 0, 0, 0, 0, 0, 0,
56 pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn, pawn,
57 0, rook, 0, 0, 0, 0, 0, bishop, 0,
58 lance, knight, silver, gold, king, gold, silver, knight, lance
62 const small_short Stcolor[NO_SQUARES] =
64 black, black, black, black,
65 black, black, black, black, black,
66 neutral, black, neutral, neutral,
67 neutral, neutral, neutral, black, neutral,
68 black, black, black, black,
69 black, black, black, black, black,
70 neutral, neutral, neutral, neutral,
71 neutral, neutral, neutral, neutral, neutral,
72 neutral, neutral, neutral, neutral,
73 neutral, neutral, neutral, neutral, neutral,
74 neutral, neutral, neutral, neutral,
75 neutral, neutral, neutral, neutral, neutral,
76 white, white, white, white,
77 white, white, white, white, white,
78 neutral, white, neutral, neutral,
79 neutral, neutral, neutral, white, neutral,
80 white, white, white, white,
81 white, white, white, white, white
84 const small_short Stboard[NO_SQUARES] =
86 king, gold, silver, bishop, rook,
90 rook, bishop, silver, gold, king,
94 const small_short Stcolor[NO_SQUARES] =
96 black, black, black, black, black,
97 black, neutral, neutral, neutral, neutral,
98 neutral, neutral, neutral, neutral, neutral,
99 neutral, neutral, neutral, neutral, white,
100 white, white, white, white, white
104 /* Actual pieces and colors */
106 small_short board[NO_SQUARES], color[NO_SQUARES];
109 /* relative piece values at the beginning of main stages */
111 #define MAIN_STAGES 4
113 static small_short ispvalue[NO_PIECES][MAIN_STAGES] =
115 { 0, 35, 70, 99 }, /* main stage borders */
116 /* ------------------------------------------ */
117 { 7, 7, 8, 10 }, /* Pawn */
119 { 20, 35, 45, 60 }, /* Lance */
120 { 20, 35, 45, 60 }, /* Knight */
122 { 35, 40, 60, 80 }, /* Silver */
123 { 35, 50, 65, 80 }, /* Gold */
124 { 90, 90, 90, 90 }, /* Bishop */
125 { 95, 95, 95, 95 }, /* Rook */
126 { 15, 25, 40, 65 }, /* promoted Pawn */
128 { 25, 45, 55, 65 }, /* promoted Lance */
129 { 25, 45, 55, 65 }, /* promoted Knight */
131 { 35, 55, 75, 75 }, /* promoted Silver */
132 { 99, 99, 99, 99 }, /* promoted Bishop */
133 { 97, 97, 99, 99 }, /* promoted Rook */
134 { 100, 100, 100, 100 }, /* King */
137 /* Features and Weights */
164 #define KNGPROTECT 25
172 static small_short weight[NO_FEATURES + 1][MAIN_STAGES + 2] =
174 { 80, 100, 100, 40, 10, 15 }, /* ATTACKED */
175 { 80, 100, 100, 50, 14, 10 }, /* HUNGP */
176 { 80, 100, 100, 50, 18, 12 }, /* HUNGX */
177 { 100, 50, 0, 0, 2, 1 }, /* CNTRL5TH */
178 { 100, 100, 60, 10, 4, 2 }, /* HOLES */
179 { 100, 50, 0, 0, 14, 7 }, /* PCASTLE */
180 { 100, 50, 0, 0, 6, 12 }, /* PATTACK */
181 { 10, 40, 70, 100, 10, 15 }, /* CTRLK */
182 { 100, 80, 50, 40, 2, 1 }, /* PROTECT */
183 { 40, 100, 40, 5, 4, 4 }, /* HCLSD */
184 { 80, 100, 80, 30, 10, 15 }, /* PINVAL */
185 { 80, 100, 60, 15, 6, 10 }, /* XRAY */
186 { 100, 50, 0, 0, 15, 15 }, /* OPENWRONG */
187 { 0, 40, 70, 100, 8, 12 }, /* SEED */
188 { 50, 100, 80, 20, 5, 3 }, /* LOOSE */
189 { 50, 100, 80, 50, 100, 100 }, /* MOBILITY (%) */
190 { 50, 100, 80, 50, 4, 8 }, /* TARGET */
191 { 50, 40, 100, 80, 8, 4 }, /* KSFTY */
192 { 80, 100, 60, 20, 5, 5 }, /* HOPN */
193 { 20, 40, 80, 100, 3, 6 }, /* PROMD */
194 { 20, 40, 80, 100, 4, 1 }, /* KINGOD */
195 { 5, 40, 100, 50, 0, 4 }, /* PWNDROP */
196 { 0, 20, 80, 100, 0, 4 }, /* DFFDROP */
197 { 20, 50, 100, 80, 0, 4 }, /* FCLATTACK */
198 { 0, 20, 80, 100, 0, 8 }, /* KNGATTACK */
199 { 40, 80, 100, 80, 6, 0 }, /* KNGPROTECT */
200 { 50, 100, 60, 10, 0, 8 }, /* DNGPC */
201 { 30, 100, 60, 5, 0, 6 }, /* LSATTACK */
202 { 0, 50, 80, 100, 0, 8 }, /* NIHATTACK */
203 { 50, 100, 80, 60, 8, 0 }, /* COHESION */
204 { 100, 100, 80, 60, 4, 4 }, /* OPPDROP */
208 short ADVNCM[NO_PIECES];
210 /* distance to enemy king */
211 static const short EnemyKingDistanceBonus[10] =
212 { 0, 6, 4, -1, -3, -4, -6, -8, -10, -12 };
214 /* distance to own king */
215 static const short OwnKingDistanceBonus[10] =
216 { 0, 5, 2, 1, 0, -1, -2, -3, -4, -5 };
218 /* distance to promotion zone */
220 static const int PromotionZoneDistanceBonus[NO_ROWS] =
221 { 0, 0, 0, 0, 2, 6, 6, 8, 8 };
223 static const int PromotionZoneDistanceBonus[NO_ROWS] =
224 { 0, 0, 2, 6, 8 }; /* FIXME ? */
227 #define MAX_BMBLTY 20
228 #define MAX_RMBLTY 20
231 /* Bishop mobility bonus indexed by # reachable squares */
232 static const short BMBLTY[MAX_BMBLTY] =
233 { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 16, 16, 16, 16 };
235 /* Rook mobility bonus indexed by # reachable squares */
236 static const short RMBLTY[MAX_RMBLTY] =
237 { 0, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 14, 14, 16, 16, 16, 16 };
240 /* Lance mobility bonus indexed by # reachable squares */
241 static const short LMBLTY[MAX_LMBLTY] =
242 { 0, 0, 0, 0, 4, 6, 8, 10 };
245 static const short MBLTY[NO_PIECES] =
256 static const short KTHRT[36] =
257 { 0, -8, -20, -36, -52, -68, -80, -80, -80, -80, -80, -80,
258 -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80,
259 -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80, -80 };
261 static small_short fvalue[2][NO_FEATURES];
263 long attack[2][NO_SQUARES]; /* threats to squares */
264 small_short sseed[NO_SQUARES]; /* square occupied by a seed piece? */
266 struct signature threats_signature[2] = /* statistics valid for position.. */
267 { { -1, -1 }, { -1, -1 } }; /* attack and sseed available */
269 small_short starget[2][NO_SQUARES]; /* significance as a target for a
270 * side of a square */
271 small_short sloose[NO_SQUARES]; /* square occupied by a loose piece? */
272 small_short shole[NO_SQUARES]; /* empty square a hole? */
273 small_short shung[NO_SQUARES]; /* hung piece? */
275 struct signature squares_signature = /* statistics valid for position ... */
276 { 0, 0 }; /* starget, sloose, shole, shung available */
278 short target[2], seed[2], loose[2], hole[2];
280 short captured[2]; /* number of captured pieces */
281 short dcaptured[2]; /* different types of captured pieces */
283 small_short Kdist[2][NO_SQUARES]; /* distance to king */
285 short MAXADIST, MAXCDIST; /* maximum half move distance to pattern */
287 char GameType[2] = { UNKNOWN, UNKNOWN }; /* chosen game type of each side */
290 static short attack_opening_sequence[2]; /* current castle patterns */
291 static short castle_opening_sequence[2]; /* current attack formations */
293 static small_short Mpawn [2][NO_SQUARES];
294 static small_short Msilver[2][NO_SQUARES];
295 static small_short Mgold [2][NO_SQUARES];
296 static small_short Mking [2][NO_SQUARES];
298 static small_short Mlance [2][NO_SQUARES];
299 static small_short Mknight[2][NO_SQUARES];
301 static small_short Mbishop[2][NO_SQUARES];
302 static small_short Mrook [2][NO_SQUARES];
304 static Mpiece_array Mpawn,
308 Msilver, Mgold, Mbishop, Mrook, Mking;
310 Mpiece_array *Mpiece[NO_PIECES] =
315 &Msilver, &Mgold, &Mbishop, &Mrook,
320 &Mgold, &Mbishop, &Mrook, &Mking };
324 static small_short *PC1, *PC2;
325 static small_short *fv1;
326 static long *atk1, *atk2;
329 #define csquare(side, sq) ((side == black) ? sq : (NO_SQUARES - 1 - sq))
330 #define crow(side, sq) row(csquare(side, sq))
331 #define ccolumn(side, sq) column(csquare(side, sq))
335 on_csquare(short side, short piece, short square)
338 /* FIXME: un-obfuscate this! */
339 return ((board[sq = csquare(side, square)] == piece)
340 && (color[sq] == side));
345 on_column(short side, short piece, short c)
349 for (sq = c; sq < NO_SQUARES; sq += NO_COLS)
351 if (on_csquare(side, piece, sq))
359 #define empty_csquare(side, square) \
360 (board[csquare(side, square)] == no_piece)
363 on_left_side(short side, short piece)
367 for (c = 0; c < 4; c++)
369 if (on_column(side, piece, c))
378 on_right_side(short side, short piece)
382 for (c = 5; c < NO_COLS; c++)
384 if (on_column(side, piece, c))
392 short pscore[2]; /* piece score for each side */
398 * Fill array attack[side][] with info about attacks to a square. Bits
399 * 16-31 are set if the piece (king .. pawn) attacks the square. Bits 0-15
400 * contain a count of total attacks to the square. Fill array sseed[] with
401 * info about occupation by a seed piece.
413 unsigned char *ppos, *pdir;
415 short i, kd, piece, xside;
418 if (MatchSignature(threats_signature[side]))
420 /* data valid for current positional signature */
427 array_zero(a, NO_SQUARES * sizeof(a[0]));
429 PL = PieceList[side];
431 for (i = PieceCnt[side]; i >= 0; i--)
437 ptyp = ptype[side][piece];
440 u = first_direction(ptyp, &d, sq);
442 ppos = (*nextpos[ptyp])[sq];
443 pdir = (*nextdir[ptyp])[sq];
449 a[u] = ((a[u] + 1) | c);
451 if ((kd = Kdist[xside][u]) < 2)
458 u = ((color[u] == neutral)
459 ? next_position(ptyp, &d, sq, u)
460 : next_direction(ptyp, &d, sq));
462 u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
468 /* data valid for current positional signature */
469 CopySignature(threats_signature[side]);
475 * Compute the board square with nunmap offset "id". If side == white,
476 * offset is negated. inunmap[sq] is the corresponding nunmap index isq.
477 * nunmap[isq + id] computes the board square. If negative, it is outside
482 add_target(short sq, short side, short id)
484 short isq, tsq, xside;
486 tsq = (side == black) ? nunmap[isq + id] : nunmap[isq - id];
490 target[xside = side^1]++;
492 if (attack[side][tsq])
493 starget[xside][tsq]++; /* protected target square */
495 starget[xside][tsq] += 2; /* unprotected target square */
502 * Target squares can be vertically ahead, diagonally ahead
503 * or diagonally behind.
507 CheckTargetPiece(short sq, short side)
511 case pawn: /* vertically ahead if unprotected */
512 if (!attack[side][sq])
513 add_target(sq, side, 11);
516 case king: /* diagonally and vertically ahead */
517 add_target(sq, side, 10);
518 add_target(sq, side, 11);
519 add_target(sq, side, 12);
522 case rook: /* diagonally ahead and behind */
523 add_target(sq, side, 10);
524 add_target(sq, side, 12);
525 add_target(sq, side, -10);
526 add_target(sq, side, -12);
529 case bishop: /* vertically ahead */
530 add_target(sq, side, 11);
534 case knight: /* vertically ahead if advanced */
535 /* FIXME: gotta love them magic numbers... */
536 if ((sq != 1) && (sq != 7) && (sq != 73) && (sq != 79))
537 add_target(sq, side, 11);
546 ScoreKingOpeningFeatures(void)
548 short s = 0, sq = OwnKing, ds;
550 if (GameType[c1] == STATIC_ROOK)
552 /* Penalty for king on right side or fifth file */
554 c = 4 - ccolumn(c1, sq);
556 if ((c < 0) || ((c == 0) && (sq != kingP[c1])))
557 s += (ds = -c - c - fv1[OPENWRONG]);
559 else if (GameType[c1] == RANGING_ROOK)
561 /* Penalty for king on left side or fifth file */
563 c = 4 - ccolumn(c1, sq);
565 if ((c > 0) || ((c == 0) && (sq != kingP[c1])))
567 s += (ds = -c - c - fv1[OPENWRONG]);
570 /* Penalty for king moved before rook switch */
573 if (on_csquare(c1, rook, 16))
575 s += (ds = -4 * fv1[OPENWRONG]);
580 /* Penalty for sitting king after rook switch */
581 if (!on_csquare(c1, rook, 16))
583 s += (ds = -2 * fv1[OPENWRONG]);
587 /* Penalty for defending general moved
588 * before king switch to right side */
589 if (ccolumn(c1, sq) < 6)
591 if (Mvboard[csquare(c1, 5)] || Mvboard[csquare(c1, 6)])
593 s += (ds = -2 * fv1[OPENWRONG]);
607 short sq, side, piece, n;
609 if (MatchSignature(squares_signature))
611 /* data valid for current positional signature */
615 array_zero(shole, sizeof(shole));
616 array_zero(sloose, sizeof(sloose));
617 array_zero(starget, sizeof(starget));
619 hole[0] = hole[1] = loose[0] = loose[1]
620 = target[0] = target[1] = 0;
622 for (sq = 0; sq < NO_SQUARES; sq++)
624 if ((side = color[sq]) == neutral)
628 if (!attack[white][sq])
634 else if (InBlackCamp(sq))
636 if (!attack[black][sq])
645 /* occupied by "side" piece */
646 if (!attack[side][sq])
652 CheckTargetPiece(sq, side);
656 for (side = black; side <= white; side++)
658 captured[side] = dcaptured[side] = 0;
660 for (piece = pawn; piece <= rook; piece++)
662 if ((n = Captured[side][piece]) != 0)
672 /* Data valid for current positional signature */
673 CopySignature(squares_signature);
678 /* ............ POSITIONAL EVALUATION ROUTINES ............ */
682 * mtl[side] - value of all material
683 * hung[side] - count of hung pieces
684 * Tscore[ply] - search tree score for ply ply
685 * Pscore[ply] - positional score for ply ply
686 * INCscore - bonus score or penalty for certain moves
687 * Sdepth - search goal depth
688 * xwndw - evaluation window about alpha/beta
689 * EWNDW - second evaluation window about alpha/beta
690 * ChkFlag[ply] - checking piece at level ply or 0 if no check
691 * TesujiFlag[ply] - 1 if tesuji move at level ply or 0 if no tesuji
692 * PC1[column] - # of my pawns in this column
693 * PC2[column] - # of opponents pawns in column
694 * PieceCnt[side] - just what it says
701 * Compute an estimate of the score by adding the positional score from the
702 * previous ply to the material difference. If this score falls inside a
703 * window which is 180 points wider than the alpha-beta window (or within a
704 * 50 point window during quiescence search) call ScorePosition() to
705 * determine a score, otherwise return the estimated score. "side" is the
706 * side which has to move.
715 short *InChk, /* output Check flag */
716 short *blockable) /* king threat blockable */
722 s = -Pscore[ply - 1] + mtl[side] - mtl[xside] /* - INCscore */;
723 hung[black] = hung[white] = 0;
725 /* should we use the estimete or score the position */
728 || (ply > Sdepth && s >= (alpha - 30) && s <= (beta + 30))
730 || (use_etable && CheckEETable(side))
736 /* score the position */
737 array_zero(sseed, sizeof(sseed));
739 seed[0] = seed[1] = 0;
742 if (Anyattack(side, sq = PieceList[xside][0]) && (board[sq] == king))
744 *InChk = (board[sq = PieceList[side][0]] == king)
745 ? SqAttacked(sq, xside, blockable)
748 return ((SCORE_LIMIT + 1001) - ply);
752 *InChk = (board[sq = PieceList[side][0]] == king)
753 ? Anyattack(xside, sq)
762 s = ScorePosition(side);
767 /* use the estimate but look at check */
770 *InChk = (board[sq = PieceList[side][0]] == king)
771 ? SqAttacked(sq, xside, blockable)
774 if ((board[sq = PieceList[xside][0]] == king)
775 && SqAttacked(sq, side, blockable))
777 return ((SCORE_LIMIT + 1001) - ply);
781 Pscore[ply] = s - mtl[side] + mtl[xside];
782 ChkFlag[ply - 1] = ((*InChk) ? Pindex[TOsquare] : 0);
790 value_of_weakest_attacker(long a2)
794 min_value = SCORE_LIMIT;
796 for (piece = pawn; piece <= king; piece++)
798 if (control[piece] & a2)
800 if (min_value > (v = (*value)[stage][piece]))
812 * Find (promoted) Bishop, (promoted) Rook, and Lance mobility, x-ray
813 * attacks, and pins. Let BRL be the bishop, rook, or lance. Let P be the
814 * first piece (not a king or a pawn) in a direction and let Q be the
815 * second piece in the same direction. If Q is an unprotected opponent's
816 * piece with bigger relative value than BRL, there is a pin if P is an
817 * opponent's piece and there is an x-ray attack if P belongs to this side.
818 * Increment the hung[] array if a pin is found.
822 BRLscan(short sq, short *mob)
827 unsigned char *ppos, *pdir;
831 short u, xu, pin, ptyp, csq = column(sq);
832 short piece, upiece, xupiece, rvalue, ds;
833 small_short *Kd = Kdist[c2];
838 rvalue = (*value)[stage][piece];
839 ptyp = ptype[c1][upiece = unpromoted[piece]];
840 rvalue = (*value)[stage][upiece];
843 u = first_direction(ptyp, &d, sq);
845 ppos = (*nextpos[ptyp])[sq];
846 pdir = (*nextdir[ptyp])[sq];
850 pin = -1; /* start new direction */
856 s += (ds = fv1[CTRLK] * (2 - Kd[u]));
859 if ((ds = starget[c1][u]) != 0)
861 /* threatening a target square */
862 if ((pin < 0) /* direct threat */
863 || (color[pin] == c2)) /* pin threat */
865 s += (ds *= fv1[TARGET]);
869 if ((ds = shole[u]) != 0)
871 /* attacking or protecting a hole */
872 s += (ds = fv1[HOLES]);
874 else if (InPromotionZone(c1, u))
876 /* attacking a square in promotion zone */
877 s += (ds = fv1[HOLES] / 2);
880 if (color[u] == neutral)
884 xu = next_position(ptyp, &d, sq, u);
886 if (xu == next_direction(ptyp, &dd, sq))
887 pin = -1; /* oops new direction */
890 if ((xu = ppos[u]) == pdir[u])
891 pin = -1; /* oops new direction */
898 /* there is a piece in current direction */
901 /* it's the first piece in the current direction */
905 /* own intercepting piece in x-ray attack */
909 if (board[u] == pawn)
911 s += (ds = 2*fv1[PROTECT]);
913 else if (in_opening_stage)
915 s += (ds = -2*fv1[PROTECT]);
921 /* bishop or rook x-ray */
922 if ((upiece == bishop) && (board[u] == pawn)
923 && (GameType[c1] == STATIC_ROOK))
925 s += (ds = -2*fv1[HCLSD]);
928 else if ((upiece == rook) && (board[u] == lance)
929 && (GameType[c1] == STATIC_ROOK)
930 && (column(u) == csq))
932 s += (ds = fv1[XRAY]);
939 /* enemy's intercepting piece in pin attack */
943 /* lance pin attack */
944 if (board[u] == pawn)
946 s += (ds = -2*fv1[PROTECT]);
948 else if (in_opening_stage)
950 s += (ds = 2 * fv1[PROTECT]);
956 /* bishop or rook pin attack */
957 if (board[u] == pawn)
959 s += (ds = -fv1[HCLSD]);
966 xu = next_position(ptyp, &d, sq, u);
968 if (xu != next_direction(ptyp, &dd, sq))
969 pin = u; /* not on the edge and on to find a pin */
971 if ((xu = ppos[u]) != pdir[u])
972 pin = u; /* not on the edge and on to find a pin */
978 /* it's the second piece in the current direction */
982 /* second piece is an own piece */
983 if ((upiece == bishop) && (board[u] == pawn)
984 && (GameType[c1] == STATIC_ROOK))
986 s += (ds = -fv1[HCLSD]);
991 /* second piece is an enemy piece */
992 if (upiece == bishop && board[u] == pawn)
994 s += (ds = -fv1[HCLSD]/2);
997 if (((*value)[stage][xupiece = unpromoted[board[u]]]
1001 if (color[pin] == c2)
1003 if ((xupiece == king) && (in_endgame_stage))
1005 s += (ds = 2 * fv1[PINVAL]);
1009 s += (ds = fv1[PINVAL]);
1012 if ((atk2[pin] == 0)
1013 || (atk1[pin] > control[board[pin]] + 1))
1022 if (upiece == lance)
1024 s += (ds = fv1[XRAY] / 2);
1029 s += (ds = fv1[XRAY]);
1035 pin = -1; /* new direction */
1038 u = next_direction(ptyp, &d, sq);
1053 #define ctlSG (ctlS | ctlG | ctlPp | ctlLp | ctlNp | ctlSp)
1058 * Assign penalties if king can be threatened by checks, if squares near
1059 * the king are controlled by the enemy (especially by promoted pieces), or
1060 * if there are no own generals near the king.
1062 * The following must be true:
1063 * board[sq] == king, c1 == color[sq], c2 == otherside[c1]
1066 #define SCORETHREAT \
1068 if (color[u] != c2) \
1070 if ((atk1[u] == 0) || ((atk2[u] & CNT_MASK) != 0)) \
1076 s += (ds = -fv1[CTRLK]); \
1092 unsigned char *ppos, *pdir;
1099 /* Penalties, if a king can be threatened by checks. */
1107 for (p = pawn; p < king; p++)
1109 if (HasPiece[c2][p] || Captured[c2][p])
1113 /* if a c1 piece can reach u from sq,
1114 * then a c2 piece can reach sq from u.
1115 * That means, each u is a square, from which a
1116 * piece of type p and color c2 threats square sq.
1118 ptyp = ptype[c1][p];
1121 u = first_direction(ptyp, &d, sq);
1123 ppos = (*nextpos[ptyp])[sq];
1124 pdir = (*nextdir[ptyp])[sq];
1130 /* If a p piece can reach (controls or can drop to)
1131 * square u, then score threat.
1134 if (atk2[u] & control[p])
1136 else if ((Captured[c2][p] && color[u]) == neutral)
1140 u = ((color[u] == neutral)
1141 ? next_position(ptyp, &d, sq, u)
1142 : next_direction(ptyp, &d, sq));
1144 u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
1152 s += (ds = fv1[KSFTY] * KTHRT[cnt] / 16);
1154 /* Penalties, if squares near king are controlled by enemy. */
1158 ptyp = ptype[c1][king];
1161 u = first_direction(ptyp, &d, sq);
1163 pdir = (*nextpos[ptyp])[sq];
1169 if (!ok && color[u] == c1)
1171 short ptype_piece = ptype[black][board[u]];
1173 if ((ptype_piece == ptype_silver) || (ptype_piece == ptype_gold))
1177 if (atk2[u] > atk1[u])
1181 if (atk2[u] & ctlSG)
1183 s += (ds = -fv1[KSFTY] / 2);
1188 u = next_direction(ptyp, &d, sq);
1195 if (!ok || (cnt > 1))
1198 s += (ds = -fv1[KSFTY]/2);
1200 s += (ds = -fv1[KSFTY]);
1207 static short checked_trapped;
1211 * See if the attacked piece has unattacked squares to move to. The following
1212 * must be true: c1 == color[sq] c2 == otherside[c1]
1222 unsigned char *ppos, *pdir;
1228 rvalue = (*value)[stage][piece];
1229 ptyp = ptype[c1][piece];
1232 u = first_direction(ptyp, &d, sq);
1234 ppos = (*nextpos[ptyp])[sq];
1235 pdir = (*nextdir[ptyp])[sq];
1243 if ((atk2[u] == 0) || ((*value)[stage][board[u]] >= rvalue))
1248 u = ((color[u] == neutral)
1249 ? next_position(ptyp, &d, sq, u)
1250 : next_direction(ptyp, &d, sq));
1252 u = ((color[u] == neutral) ? ppos[u] : pdir[u]);
1257 checked_trapped = true;
1265 AttackedPieceValue(short sq, short side)
1271 ds = -fv1[HUNGP] * 2;
1287 OpenFileValue(short sq, short hopn, short hopnx)
1291 if (PC1[fyle = column(sq)] == 0)
1307 /* Distance bonus */
1309 #define PromotionZoneDistanceValue(sq, dd) \
1310 if ((ds = fv1[PROMD])) \
1312 s += (ds = ds * PromotionZoneDistanceBonus[crow(c1, sq)] * dd); \
1315 #define OwnKingDistanceValue(sq, dd, maxd) \
1316 if ((ds = fv1[KINGOD]) && ((ad = Kdist[c1][sq]) <= maxd)) \
1318 s += (ds = ds * OwnKingDistanceBonus[ad] * dd); \
1321 #define EnemyKingDistanceValue(sq, dd, maxd) \
1322 if ((ds = fv1[KINGOD]) && ((ad = Kdist[c2][sq]) <= maxd)) \
1324 s += (ds = ds * EnemyKingDistanceBonus[ad] * dd); \
1332 * Calculate the positional value for a pawn on 'sq'.
1336 PawnValue(short sq, short side)
1340 short ccol = ccolumn(c1, sq);
1342 PromotionZoneDistanceValue(sq, 3);
1345 if (color[(c1 == black) ? (sq + NO_COLS) : (sq - NO_COLS)] == neutral)
1347 s += (ds = MBLTY[pawn]);
1350 if ((a1 & ((ctlR | ctlRp) | ctlL)))
1352 s += (ds = fv1[ATTACKED]);
1355 if (in_opening_stage)
1358 /* FIXME: [HGM] The 3rd-rank Pawn section is meaningless in mini-Shogi,
1359 * (which does not have opposing Pawns), and can do out-of-bound access,
1360 * as the promotion zone is only 1 rank, so Pawns can be closer than 3 ranks
1361 * to the board edge.
1363 if (crow(c1, sq) == 2) /* pawn on 3d rank */
1365 if (board[(c1 == black) ?
1366 (sq + 3*NO_COLS) : (sq - 3*NO_COLS)] == pawn)
1368 /* opposing pawn has been moved (even column == (sq & 1)) */
1376 m = (side == c1) ? 3 : 5;
1380 m = (side == c1) ? 2 : 3;
1384 m = (side == c1) ? 1 : 2;
1388 s += (ds = -m * MBLTY[pawn]);
1392 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1393 * board - and anyway we don't know the stage really :)
1395 if ((GameType[c1] == STATIC_ROOK) && (sq == csquare(c1, 43)))
1397 if ((atk2[csquare(c1, 52)] & CNT_MASK) < 2)
1399 s += (ds = fv1[ATTACKED]);
1403 if ((GameType[c2] == STATIC_ROOK) && (ccol == 1))
1405 if (sq == csquare(c1, 28))
1407 s += (ds = -fv1[ATTACKED]);
1410 if (((atk1[csquare(c1, 19)] & CNT_MASK) < 2)
1411 && ((atk1[csquare(c1, 28)] & CNT_MASK) < 2))
1413 s += (ds = -2 * fv1[ATTACKED]);
1425 * Calculate the positional value for a lance on 'sq'.
1429 LanceValue(short sq, short side)
1431 short s = 0, ds, ad;
1433 OwnKingDistanceValue(sq, 1, 2);
1435 OpenFileValue(sq, -fv1[HOPN], fv1[HOPN]);
1437 if (!checked_trapped && (crow(c1, sq) > 2))
1439 if (in_opening_stage || trapped(sq))
1441 s += (ds = -3 * fv1[ATTACKED]);
1445 s += (ds = -2 * fv1[ATTACKED]);
1455 * Calculate the positional value for a knight on 'sq'.
1459 KnightValue(short sq, short side)
1462 short ds, checked_trapped = false;
1463 short c = column(sq);
1465 PromotionZoneDistanceValue(sq, 1);
1466 OwnKingDistanceValue(sq, 1, 2);
1468 if (!checked_trapped && (crow(c1, sq) > 2))
1472 s += (ds = -4 * fv1[ATTACKED]);
1476 s += (ds = -3 * fv1[ATTACKED]);
1480 if ((c == 0) || (c == 8))
1482 s += (ds = -fv1[ATTACKED]);
1492 * Calculate the positional value for a silver on 'sq'.
1496 SilverValue(short sq, short side)
1500 OwnKingDistanceValue(sq, 2, 3);
1502 if ((Kdist[c1][sq] < 3)
1503 && (atk1[sq] & (control[gold] | control[silver])))
1505 s += (ds = fv1[COHESION]);
1508 if (in_opening_stage)
1511 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1512 * board - and anyway we don't know the stage really :)
1514 if (GameType[c1] == STATIC_ROOK)
1516 if (csquare(c1, sq) == 12)
1520 if ((board[csq = csquare(c1, 20)] == bishop)
1521 && (color[csq] == c1))
1523 s += (ds = -2 * fv1[OPENWRONG]);
1531 EnemyKingDistanceValue(sq, 2, 3);
1540 * Calculate the positional value for a gold on 'sq'.
1544 GoldValue(short sq, short side)
1546 short s = 0, ds, ad;
1548 OwnKingDistanceValue(sq, 2, 3);
1550 if ((Kdist[c1][sq] < 3)
1551 && (atk1[sq] & (control[gold] | control[silver])))
1553 s += (ds = fv1[COHESION]);
1556 if (in_opening_stage)
1559 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1560 * board - and anyway we don't know the stage really :)
1562 if ((GameType[c1] == STATIC_ROOK) && (GameType[c2] != STATIC_ROOK))
1564 if (Mvboard[csquare(c1, 3)])
1566 s += (ds = -2 * fv1[OPENWRONG]);
1573 EnemyKingDistanceValue(sq, 2, 3);
1582 * Calculate the positional value for a bishop on 'sq'.
1586 BishopValue(short sq, short side)
1588 short s = 0, ds, ad;
1590 if (in_opening_stage)
1593 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1594 * board - and anyway we don't know the stage really :)
1596 if (GameType[c1] == RANGING_ROOK)
1598 /* Bishops diagonal should not be open */
1599 if (!on_csquare(c1, pawn, 30))
1601 s += (ds = -fv1[OPENWRONG]);
1604 else if (GameType[c2] == RANGING_ROOK)
1606 /* Bishops diagonal should be open */
1607 if ((csquare(c1, sq) == 10)
1608 && (!empty_csquare(c1, 20) || !empty_csquare(c1, 30)))
1610 s += (ds = -fv1[OPENWRONG]);
1612 else if ((csquare(c1, sq) == 20) && !empty_csquare(c1, 30))
1614 s += (ds = -fv1[OPENWRONG]);
1621 EnemyKingDistanceValue(sq, 1, 3);
1630 * Calculate the positional value for a rook on 'sq'.
1634 RookValue(short sq, short side)
1636 short s = 0, ds, ad;
1638 OpenFileValue(sq, 2 * fv1[HOPN], 4*fv1[HOPN]);
1640 if (in_opening_stage)
1643 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1644 * board - and anyway we don't know the stage really :)
1646 short WRONG = fv1[OPENWRONG], OPOK = WRONG / 3;
1648 if (GameType[c1] == STATIC_ROOK)
1650 short c = ccolumn(c1, sq);
1652 /* Bonus for rook on 8th file */
1659 * Bonus for rook on right side,
1660 * penalty for rook on left side
1668 s += (ds = c + c + OPOK);
1672 s += (ds = -c - c - WRONG);
1675 else if (GameType[c1] == RANGING_ROOK)
1677 /* Bonus for rook on left side and
1678 * bishops diagonal closed, penalty otherwise. */
1681 c = 4 - ccolumn(c1, sq);
1686 /* Bishops diagonal should not be open. */
1687 if (on_csquare(c1, pawn, 30))
1690 s += (ds = -c - c - WRONG);
1694 s += (ds = -c - c - WRONG);
1696 /* Penalty for king not on initial square. */
1697 if (!on_csquare(side, king, 4))
1708 EnemyKingDistanceValue(sq, 1, 3);
1717 * Calculate the positional value for a promoted pawn on 'sq'.
1721 PPawnValue(short sq, short side)
1723 short s = 0, ds, ad;
1725 EnemyKingDistanceValue(sq, 3, 10);
1734 * Calculate the positional value for a promoted lance on 'sq'.
1738 PLanceValue(short sq, short side)
1740 short s = 0, ds, ad;
1742 EnemyKingDistanceValue(sq, 3, 10);
1750 * Calculate the positional value for a promoted knight on 'sq'.
1754 PKnightValue(short sq, short side)
1756 short s = 0, ds, ad;
1758 EnemyKingDistanceValue(sq, 3, 10);
1767 * Calculate the positional value for a promoted silver on 'sq'.
1771 PSilverValue(short sq, short side)
1773 short s = 0, ds, ad;
1775 EnemyKingDistanceValue(sq, 3, 10);
1783 * Calculate the positional value for a promoted bishop on 'sq'.
1787 PBishopValue(short sq, short side)
1789 short s = 0, ds, ad;
1791 EnemyKingDistanceValue(sq, 3, 4);
1799 * Calculate the positional value for a promoted rook on 'sq'.
1803 PRookValue(short sq, short side)
1805 short s = 0, ds, ad;
1807 EnemyKingDistanceValue(sq, 3, 4);
1809 OpenFileValue(sq, 3 * fv1[HOPN], 2 * fv1[HOPN]);
1817 * Calculate the positional value for a king on 'sq'.
1821 KingValue(short sq, short side)
1825 if (fv1[KSFTY] != 0)
1829 /* FIXME: calculations below are wrong for minishogi, all done for 9x9
1830 * board - and anyway we don't know the stage really :)
1832 if (in_opening_stage)
1834 if ((GameType[c1] != UNKNOWN) && (ccolumn(c1, sq) == 4))
1836 s += (ds = -fv1[OPENWRONG] / 3);
1838 else if ((GameType[c1] == STATIC_ROOK) && on_right_side(c1, sq))
1840 s += (ds = -fv1[OPENWRONG] / 2);
1842 else if ((GameType[c1] == RANGING_ROOK) && on_left_side(c1, sq))
1844 s += (ds = -fv1[OPENWRONG] / 2);
1849 /* CHECKME: is this correct? */
1850 if ((ds = fv1[HOPN]))
1852 s += OpenFileValue(sq, -2 * ds, -4 * ds);
1861 * Calculate the positional value for a piece on 'sq'.
1865 PieceValue(short sq, short side)
1872 if (piece == no_piece)
1875 s = (*Mpiece[piece])[c1][sq];
1877 checked_trapped = false;
1881 /* pin/x-ray attack and mobility for sweeping pieces */
1882 s += (ds = BRLscan(sq, &mob));
1884 if ((piece == bishop) || (piece == pbishop))
1885 s += (ds = BMBLTY[mob] * fv1[MOBILITY] / 100);
1886 else if ((piece == rook) || (piece == prook))
1887 s += (ds = RMBLTY[mob] * fv1[MOBILITY] / 100);
1890 s += (ds = LMBLTY[mob] * fv1[MOBILITY] / 100);
1895 /* mobility for non-sweeping pieces */
1903 /* opponent attacks piece */
1906 /* undefended piece */
1907 s += AttackedPieceValue(sq, side);
1911 /* defended piece */
1912 short attack_value = value_of_weakest_attacker(a2);
1913 short piece_value = (*value)[stage][piece];
1915 if (attack_value < piece_value)
1917 /* attacked by a weaker piece */
1918 s += AttackedPieceValue(sq, side) / 2;
1920 else if (abs(attack_value - piece_value) < 10)
1922 /* opponent has the option to exchange equal pieces */
1923 s += (ds = -fv1[ATTACKED]);
1933 /* piece is defended */
1934 s += (ds = (a1 & CNT_MASK) * fv1[PROTECT]);
1939 s += (ds = fv1[SEED]);
1944 s += (ds = -fv1[LOOSE]);
1947 if (starget[c1][sq])
1951 s -= (ds = -fv1[ATTACKED]/2);
1953 else if (piece == pawn)
1955 s += (ds = fv1[ATTACKED]);
1959 if (starget[c2][sq])
1963 s -= (ds = -fv1[ATTACKED] / 3);
1967 s += (ds = fv1[ATTACKED]);
1971 if (Kdist[c1][sq] == 1)
1973 s += (ds = fv1[KSFTY]);
1980 s += PawnValue(sq, side);
1985 s += LanceValue(sq, side);
1989 s += KnightValue(sq, side);
1994 s += SilverValue(sq, side);
1998 s += GoldValue(sq, side);
2002 s += BishopValue(sq, side);
2006 s += RookValue(sq, side);
2010 s += KingValue(sq, side);
2014 s += PPawnValue(sq, side);
2019 s += PLanceValue(sq, side);
2023 s += PKnightValue(sq, side);
2028 s += PSilverValue(sq, side);
2032 s += PBishopValue(sq, side);
2036 s += PRookValue(sq, side);
2046 * Score distance to pattern regarding the game type which side plays.
2050 ScorePatternDistance(short c1)
2053 small_short *fv1 = fvalue[c1];
2056 if ((MAXCDIST > 0) && (fv1[PCASTLE] != 0)
2057 && ((os = castle_opening_sequence[c1]) >= 0))
2059 ds = board_to_pattern_distance(c1, os, MAXCDIST, GameCnt);
2063 s += (ds *= fv1[PCASTLE]);
2067 if ((MAXADIST > 0) && (fv1[PATTACK] != 0)
2068 && ((os = attack_opening_sequence[c1]) >= 0))
2070 ds = board_to_pattern_distance(c1, os, MAXADIST, GameCnt);
2074 s += (ds *= fv1[PATTACK]);
2085 * Determine castle and attack pattern which should be reached next.
2086 * Only patterns are considered, which have not been reached yet.
2090 UpdatePatterns(short side, short GameCnt)
2093 short xside = side ^ 1;
2097 strcpy(s, "CASTLE_?_?");
2098 s[7] = GameType[side];
2099 s[9] = GameType[xside];
2100 castle_opening_sequence[side] = os
2101 = locate_opening_sequence(side, s, GameCnt);
2103 if (flag.post && (os != END_OF_SEQUENCES))
2105 for (j = 0; j < MAX_SEQUENCE; j++)
2107 for (k = OpeningSequence[os].first_pattern[j];
2108 k != END_OF_PATTERNS;
2109 k = Pattern[k].next_pattern)
2111 if (Pattern[k].distance[side] >= 0)
2117 if (os != END_OF_SEQUENCES)
2118 update_advance_bonus(side, os);
2120 strcpy(s, "ATTACK_?_?");
2121 s[7] = GameType[side];
2122 s[9] = GameType[xside];
2123 attack_opening_sequence[side] = os
2124 = locate_opening_sequence(side, s, GameCnt);
2126 if (flag.post && (os != END_OF_SEQUENCES))
2128 for (j = 0; j < MAX_SEQUENCE; j++)
2130 for (k = OpeningSequence[os].first_pattern[j];
2131 k != END_OF_PATTERNS;
2132 k = Pattern[k].next_pattern)
2134 if (Pattern[k].distance[side] >= 0)
2141 ShowPatternCount(side, n);
2143 if (os != END_OF_SEQUENCES)
2144 update_advance_bonus(side, os);
2152 short ds, col, n, m, piece;
2154 if ((n = Captured[c1][pawn]))
2158 for (col = 0; col < NO_COLS; col++)
2167 /* FIXME! another great obfuscated line... */
2168 pscore[c1] += (ds *= ((n > 2) ? 3 : n));
2173 for (piece = pawn+1, n = 0; piece <= rook; piece++)
2175 if (Captured[c1][piece])
2179 pscore[c1] += (ds = m * fv1[DFFDROP]);
2182 for (piece = pawn, n = 0; piece <= rook; piece++)
2184 if (Captured[c1][piece])
2189 ds = BMBLTY[MAX_BMBLTY - 1];
2193 ds = RMBLTY[MAX_RMBLTY - 1];
2198 ds = LMBLTY[MAX_LMBLTY - 1];
2208 if (!Captured[c2][piece])
2209 n += relative_value[piece];
2215 pscore[c1] += (ds = -n * fv1[OPPDROP] / 2);
2224 * Perform normal static evaluation of board position. A score is generated
2225 * for each piece and these are summed to get a score for each side.
2229 ScorePosition(short side)
2238 UpdateWeights(side);
2240 hung[black] = hung[white] = pscore[black] = pscore[white] = 0;
2242 array_zero(shung, sizeof(shung));
2245 if (!(use_etable && ProbeEETable(side, &s)))
2248 for (c1 = black; c1 <= white; c1++)
2252 /* atk1 is array of attacks on squares by my side */
2255 /* atk2 is array of attacks on squares by other side */
2258 /* same for PC1 and PC2 */
2262 /* same for fv1 and fv2 */
2265 for (i = PieceCnt[c1]; i >= 0; i--)
2267 sq = PieceList[c1][i];
2269 #if defined SAVE_SVALUE
2270 pscore[c1] += PieceValue(sq, side);
2272 pscore[c1] += (svalue[sq] = PieceValue(sq, side));
2280 # define BLACKHOME_START 0
2281 # define BLACKHOME_END 26
2282 # define MIDDLEROW_START 36
2283 # define MIDDLEROW_END 44
2284 # define WHITEHOME_START 54
2285 # define WHITEHOME_END 80
2287 # define BLACKHOME_START 0
2288 # define BLACKHOME_END 4
2289 # define MIDDLEROW_START 10
2290 # define MIDDLEROW_END 14
2291 # define WHITEHOME_START 19
2292 # define WHITEHOME_END 24
2294 for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
2300 /* Score fifth rank */
2301 for (sq = MIDDLEROW_START, n = 0; sq <= MIDDLEROW_END; sq++)
2303 if ((color[sq] == c1) || (attack[c1][sq] != 0))
2309 pscore[c1] += (ds = n * fv1[CNTRL5TH]);
2313 for (sq = ((c1 == black) ? BLACKHOME_START : WHITEHOME_START), n = 0;
2314 sq <= ((c1 == black) ? BLACKHOME_END : WHITEHOME_END);
2317 if (board[sq] == no_piece && attack[c1][sq] == 0)
2323 pscore[c1] += (ds = -n * fv1[HOLES]);
2328 pscore[c1] += (ds = -fv1[HUNGX]);
2331 /* Score opening features and
2332 * castle/attack pattern distances */
2334 if (in_opening_stage)
2336 pscore[c1] += (ds = ScoreKingOpeningFeatures());
2337 pscore[c1] += (ds = ScorePatternDistance(c1));
2341 score = mtl[side] - mtl[xside]
2342 + pscore[side] - pscore[xside] + 10;
2345 if (use_etable && PUTVAR)
2346 PutInEETable(side, score);
2350 #ifdef CACHE /* CHECKME: this looks bad */
2360 * Try to determine the game type of "side".
2365 GuessGameType(short side_to_move)
2368 short StaticRook[2] = { 0, 0 };
2369 short RangingRook[2] = { 0, 0 };
2371 for (side = black; side <= white; side++)
2373 /* computer should not change its mind */
2375 extern int bookflag;
2377 gt = GameType[side];
2379 if (!bookflag && (side == side_to_move))
2381 if (gt == STATIC_ROOK)
2382 StaticRook[side] += 4;
2383 else if (gt == RANGING_ROOK)
2384 RangingRook[side] += 4;
2387 /* static rook conditions */
2389 if (on_column(side, rook, 7))
2390 StaticRook[side] += 3;
2392 if (on_csquare(side, pawn, 34))
2393 StaticRook[side] += 6;
2394 else if (on_csquare(side, pawn, 43))
2395 StaticRook[side] += 4;
2396 else if (!on_column(side, pawn, 7))
2397 StaticRook[side] += 5;
2399 if (empty_csquare(side, 5) || empty_csquare(side, 6))
2400 StaticRook[side] += 2;
2402 if (on_left_side(side, king))
2403 StaticRook[side] += 2;
2405 /* ranging rook conditions */
2407 if (on_left_side(side, rook))
2408 RangingRook[side] += 5;
2409 else if (!on_column(side, rook, 7))
2410 RangingRook[side] += 3;
2412 if (on_csquare(side, pawn, 25))
2413 RangingRook[side] += 1;
2415 if (on_csquare(side, pawn, 30))
2416 RangingRook[side] += 1;
2418 RangingRook[side] -= 2;
2420 if (!on_right_side(side, rook))
2421 RangingRook[side] += 4;
2423 if (on_right_side(side, king))
2424 RangingRook[side] += 4;
2426 if (on_csquare(side, bishop, 20))
2428 if (on_csquare(side, silver, 11)
2429 || on_csquare(side, silver, 12)
2430 || on_csquare(side, silver, 21))
2432 RangingRook[side] += 3;
2436 if ((StaticRook[side] > 5) || (RangingRook[side] > 5))
2438 GameType[side] = (StaticRook[side] > RangingRook[side])
2439 ? STATIC_ROOK : RANGING_ROOK;
2443 GameType[side] = UNKNOWN;
2447 if ((GameType[black] == UNKNOWN) || (GameType[white] == UNKNOWN))
2449 for (side = black; side <= white; side++)
2451 if ((side == computer) && (GameType[side] == UNKNOWN))
2454 * Game type is UNKNOWN.
2455 * Make a decision what type of game to play.
2456 * To make computer games more interesting, make a
2460 if (!on_csquare(side, pawn, 25))
2462 /* Play static rook if rook pawn has been pushed! */
2463 GameType[side] = STATIC_ROOK;
2467 unsigned int random = urand() % 100;
2468 short d = StaticRook[side] - RangingRook[side];
2470 switch (GameType[side ^ 1])
2473 if (random < 35 + d)
2474 GameType[side] = STATIC_ROOK;
2475 else if (random < 95)
2476 GameType[side] = RANGING_ROOK;
2480 if (random < 75 + d)
2481 GameType[side] = STATIC_ROOK;
2482 else if (random < 95)
2483 GameType[side] = RANGING_ROOK;
2487 if (random < 33 + d)
2488 GameType[side] = STATIC_ROOK;
2489 else if (random < 66)
2490 GameType[side] = RANGING_ROOK;
2503 DetermineGameType(short side_to_move)
2508 /* FIXME: calculations below are wrong for minishogi, all done for 9x9 */
2509 GuessGameType(side_to_move);
2511 GameType[black] = UNKNOWN;
2512 GameType[white] = UNKNOWN;
2515 array_zero(Mpawn, sizeof(Mpawn));
2517 array_zero(Mlance, sizeof(Mlance));
2518 array_zero(Mknight, sizeof(Mknight));
2520 array_zero(Msilver, sizeof(Msilver));
2521 array_zero(Mgold, sizeof(Mgold));
2522 array_zero(Mbishop, sizeof(Mbishop));
2523 array_zero(Mrook, sizeof(Mrook));
2524 array_zero(Mking, sizeof(Mking));
2526 if (in_opening_stage)
2528 for (side = black; side <= white; side++)
2529 UpdatePatterns(side, GameCnt);
2533 ShowPatternCount(black, -1);
2534 ShowPatternCount(white, -1);
2541 * This is done one time before the search is started. Set up arrays
2542 * Mwpawn, Mbpawn, Mknight, Mbishop, Mking which are used in the SqValue()
2543 * function to determine the positional value of each piece.
2547 ExaminePosition(short side)
2549 short c1, piece, sq, i, bsq, wsq;
2551 /* Build enemy king distance tables. */
2553 for (sq = 0, bsq = BlackKing, wsq = WhiteKing; sq < NO_SQUARES; sq++)
2555 Kdist[black][sq] = distance(sq, bsq);
2556 Kdist[white][sq] = distance(sq, wsq);
2564 DetermineGameType(side);
2565 DetermineStage(side);
2567 UpdateWeights(side);
2569 array_zero(HasPiece, sizeof(HasPiece));
2571 for (c1 = black; c1 <= white; c1++)
2573 for (i = PieceCnt[c1]; i >= 0; i--)
2575 ++HasPiece[c1][piece = board[sq = PieceList[c1][i]]];
2582 /* FIXME: calculations below are wrong for minishogi, all done for 9x9 */
2584 DetermineStage(short side)
2586 short xside = side ^ 1, ds, db1, c1, c2, feature;
2588 /* Determine initial stage */
2590 balance[side] = balance[xside] = 50;
2592 if ((GameType[side] == STATIC_ROOK)
2593 && (GameType[xside] == STATIC_ROOK))
2597 else if (GameCnt < 60)
2599 else if (GameCnt < 80)
2604 else if ((GameType[side] == RANGING_ROOK)
2605 || (GameType[xside] == RANGING_ROOK))
2609 else if (GameCnt < 50)
2611 else if (GameCnt < 70)
2620 else if (GameCnt < 55)
2622 else if (GameCnt < 75)
2629 /* Update stage depending on board features and attack balance value */
2631 if (abs(ds = (mtl[side] - mtl[xside]))
2632 > (db1 = (*value)[stage][lance]))
2634 db1 = abs(4 * ds / db1);
2637 balance[side] += db1;
2639 balance[xside] += db1;
2641 stage += (ds = db1);
2645 for (c1 = black, c2 = white; c1 <= white; c1++, c2--)
2647 if ((ds = seed[c1]) > 2)
2649 balance[c1] += (db1 = ds * 2);
2658 if ((db1 = hung[c1]) > 2)
2660 balance[c1] -= (db1 *= 2);
2664 if ((db1 = loose[c1]) > 4)
2666 balance[c1] -= (db1 /= 2);
2671 if ((ds = hole[c1]))
2673 balance[c1] -= (db1 = ds);
2678 if ((db1 = target[c1]) > 3)
2680 balance[c1] += (db1 /= 3);
2682 stage += (ds = db1 / 4);
2685 stage += (ds = captured[c1] / 2);
2687 if ((db1 = captured[c1]) > 4)
2689 balance[c1] += (db1 /= 2);
2693 if ((db1 = dcaptured[c1]) > 3)
2699 if (balance[c1] > 99)
2701 else if (balance[c1] < 0)
2713 /* Determine stage dependant weights */
2715 ADVNCM[pawn] = 1; /* advanced pawn bonus increment */
2720 ADVNCM[silver] = 1; /* advanced silver bonus increment */
2721 ADVNCM[gold] = 1; /* advanced gold bonus increment */
2724 ADVNCM[king] = 1; /* advanced king bonus increment */
2726 MAXCDIST = (stage < 33) ? (33 - stage) / 4 : 0;
2727 MAXADIST = (stage < 30) ? (30 - stage) / 4 : 0;
2729 for (c1 = black; c1 <= white; c1++)
2731 for (feature = 0; feature < NO_FEATURES; feature++)
2733 fvalue[c1][feature] =
2734 ((((*fscore)[stage][feature][0] * (99 - balance[c1])) + 50)
2736 + ((((*fscore)[stage][feature][1] * balance[c1]) + 50)
2745 UpdateWeights(short stage)
2752 * Compute stage dependent relative material values assuming
2753 * linearity between the main stages:
2755 * minstage < stage < maxstage =>
2756 * stage - minstage value - minvalue
2757 * ------------------- = -------------------
2758 * maxstage - minstage maxvalue - minvalue
2763 linear_piece_value(short piece, short stage, short i, short j)
2765 short minvalue, maxvalue, minstage, maxstage;
2767 minstage = ispvalue[0][i];
2768 maxstage = ispvalue[0][j];
2769 minvalue = ispvalue[piece][i];
2770 maxvalue = ispvalue[piece][j];
2772 /* FIXME: set a variable then return it, puh-leeze! */
2773 return ((stage - minstage) * (maxvalue - minvalue)
2774 / (maxstage - minstage)) + minvalue;
2780 linear_feature_value(short feature, short stage, short i, short j)
2782 short minvalue, maxvalue, minstage, maxstage;
2784 minstage = ispvalue[0][i];
2785 maxstage = ispvalue[0][j];
2786 minvalue = weight[feature][i];
2787 maxvalue = weight[feature][j];
2789 /* FIXME: set a variable then return it, puh-leeze! */
2790 return ((stage - minstage) * (maxvalue - minvalue)
2791 / (maxstage - minstage)) + minvalue;
2796 * matweight = percentage_of_max_value * max_value(stage) / 100
2797 * max_value(0) = MAX_VALUE; max_value(100) = MIN_VALUE
2798 * => max_value(stage) = a * stage + b; b = MAX_VALUE,
2799 * a = (MIN_VALUE - MAX_VALUE)/100
2802 #define MIN_VALUE 300
2803 #define MAX_VALUE 1000
2805 #define max_value(stage) \
2806 ((long)(MIN_VALUE - MAX_VALUE) * stage + (long)100 * MAX_VALUE)
2807 #define matweight(value, stage) \
2808 ((long)max_value(stage) * value / 10000)
2813 Initialize_eval(void)
2815 short stage, piece, feature, i;
2817 for (stage = 0; stage < NO_STAGES; stage++)
2819 for (i = 0; i < MAIN_STAGES; i++)
2821 if (stage == ispvalue[0][i])
2823 for (piece = 0; piece < NO_PIECES; piece++)
2825 (*value)[stage][piece] =
2826 matweight(ispvalue[piece][i], stage);
2829 for (feature = 0; feature < NO_FEATURES; feature++)
2831 (*fscore)[stage][feature][0] =
2833 * weight[feature][MAIN_STAGES] + 50) / 100;
2835 (*fscore)[stage][feature][1] =
2837 * weight[feature][MAIN_STAGES + 1] + 50) / 100;
2843 if (stage < ispvalue[0][i + 1])
2845 for (piece = 0; piece < NO_PIECES; piece++)
2847 (*value)[stage][piece] =
2849 linear_piece_value(piece, stage, i, i + 1),
2853 for (feature = 0; feature < NO_FEATURES; feature++)
2855 (*fscore)[stage][feature][0] =
2856 (linear_feature_value(feature, stage, i, i + 1)
2857 * weight[feature][MAIN_STAGES] + 50) / 100;
2859 (*fscore)[stage][feature][1] =
2860 (linear_feature_value(feature, stage, i, i + 1)
2861 * weight[feature][MAIN_STAGES + 1] + 50) / 100;