Make piece-type testable by bitmask
[gnushogi.git] / gnushogi / genmove.c
index 4c20a5f..a2c1c09 100644 (file)
@@ -2,11 +2,15 @@
  * FILE: genmove.c
  *
  * ----------------------------------------------------------------------
- *
- * Copyright (c) 2012 Free Software Foundation
+ * Copyright (c) 1993, 1994, 1995 Matthias Mutz
+ * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
+ * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
  *
  * GNU SHOGI is based on GNU CHESS
  *
+ * Copyright (c) 1988, 1989, 1990 John Stanback
+ * Copyright (c) 1992 Free Software Foundation
+ *
  * This file is part of GNU SHOGI.
  *
  * GNU Shogi is free software; you can redistribute it and/or modify it
@@ -242,12 +246,12 @@ gives_check_flag(unsigned short *flags, short side, short f, short t)
 
 
 inline static void
-Link(short side, short piece,
+Link(short side,
      short from, short to, unsigned short local_flag, short s)
 {
     if (*TrP == TREE)
     {
-        ShowMessage("TREE overflow\n");
+        dsp->ShowMessage("TREE overflow\n");
     }
     else
     {
@@ -310,33 +314,21 @@ PromotionPossible(short color, short f, short t, short p)
 {
     if (color == black)
     {
-        if ((f < 54) && (t < 54))
+       if ((!InWhiteCamp(f)) && (!InWhiteCamp(t)))
             return false;
     }
     else
     {
-        if ((f > 26) && (t > 26))
+       if ((!InBlackCamp(f)) && (!InBlackCamp(t)))
             return false;
     }
 
-    /* FIXME: this can be simplified... */
-    switch (p)
-    {
-    case pawn:
-    case lance:
-    case knight:
-    case silver:
-    case bishop:
-    case rook:
-        return true;
-    };
-
-    return false;
+    return (typeMask[p] & (T_PAWN|T_LANCE|T_KNIGHT|T_SILVER|T_BISHOP|T_ROOK)) != 0;
 }
 
 
-inline int
-NonPromotionPossible(short color, short f,
+static inline int
+NonPromotionPossible(short color,
                      short t, short p)
 {
     switch (p)
@@ -355,6 +347,7 @@ NonPromotionPossible(short color, short f,
                     : (generate_move_flags ? ILLEGAL_TRAPPED : false));
         }
 
+#ifndef MINISHOGI
     case lance:
         if (color == black)
         {
@@ -382,6 +375,7 @@ NonPromotionPossible(short color, short f,
                     ? true
                     : (generate_move_flags ? ILLEGAL_TRAPPED : false));
         }
+#endif
     }
 
     return true;
@@ -393,7 +387,7 @@ NonPromotionPossible(short color, short f,
 /* bonus for possible next moves */
 
 inline static short
-field_bonus(short ply, short side, short piece,
+field_bonus(short side, short piece,
             short f, short t, unsigned short *local_flag)
 {
     short s, u, ptyp;
@@ -530,7 +524,8 @@ field_bonus(short ply, short side, short piece,
 
                     /* CHECKME: is this right? */
                     if (((rvupiece == rvuboard) && (upiece == pawn))
-                        || (upiece == bishop) || (upiece == knight))
+                        || typeMask[upiece] & (T_BISHOP | T_KNIGHT)
+                       )
                     {
                         s++; /* The opposing pawn (piece) */
 
@@ -609,7 +604,7 @@ LinkMove(short ply, short f,
     if (score_if_impossible < 0)
     {
         /* The move is flagged as illegal. */
-        Link(side, piece,
+        Link(side,
              f, t, local_flag, score_if_impossible);
 
         return;
@@ -718,7 +713,7 @@ LinkMove(short ply, short f,
             s -= 16 / Captured[side][silver];
 
 #if defined DROPBONUS
-        s += field_bonus(ply, side, piece, f, t, &local_flag);
+        s += field_bonus(side, piece, f, t, &local_flag);
 
         if (s == 10 && piece != pawn)
             local_flag |= questionable;
@@ -769,7 +764,7 @@ LinkMove(short ply, short f,
             {
 #ifdef TESUJIBONUS
                 /* Look at non-promoting silver or knight */
-                if (piece == silver || piece == knight)
+                if (typeMask[piece] & (T_SILVER | T_KNIGHT))
                 {
                     local_flag |= tesuji; /* Non-promotion */
                     s++;
@@ -803,7 +798,7 @@ LinkMove(short ply, short f,
         else
         {
 #if defined FIELDBONUS
-            s += field_bonus(ply, side, piece, f, t, &local_flag);
+            s += field_bonus(side, piece, f, t, &local_flag);
 #endif
         }
     }
@@ -855,7 +850,7 @@ LinkMove(short ply, short f,
 
     if (try_link || GenerateAllMoves)
     {
-        Link(side, piece, f, t, local_flag,
+        Link(side, f, t, local_flag,
              s - ((SCORE_LIMIT + 1000) * 2));
     }
 
@@ -905,6 +900,7 @@ DropPossible(short piece, short side, short sq)
             GenUnmakeMove(side, f, sq, tempb, tempc, false);
         }
     }
+#ifndef MINISHOGI
     else if (piece == lance)
     {
         if ((side == black) && (r == 8))
@@ -919,6 +915,7 @@ DropPossible(short piece, short side, short sq)
         else if ((side == white) && (r <= 1))
             possible = (generate_move_flags ? ILLEGAL_TRAPPED : false);
     }
+#endif
 
     return possible;
 }
@@ -1023,7 +1020,7 @@ GenMoves(short ply, short sq, short side,
                 LinkMove(ply, sq, u, local_flag | promote, xside, true);
 
                 if ((possible
-                     = NonPromotionPossible(color[sq], sq, u, piece)))
+                     = NonPromotionPossible(color[sq], u, piece)))
                 {
                     LinkMove(ply, sq, u, local_flag, xside, possible);
                 }
@@ -1107,9 +1104,9 @@ LinkPreventCheckDrops(short side, short xside, short ply)
     if (board[square = PieceList[side][0]] != king)
         return;
 
-    for (piece = lance; piece <= rook; piece++)
+    for (piece = pawn+1; piece <= rook; piece++)
     {
-        if (piece == lance || piece == bishop || piece == rook)
+        if (typeMask[piece] & (T_LANCE | T_BISHOP | T_ROOK))
         {
             /* check for threat of xside piece */
             ptyp = ptype[side][piece];
@@ -1475,7 +1472,7 @@ CaptureList(short side, short ply,
 
                     if ((PP = PromotionPossible(color[sq], sq, u, piece)))
                     {
-                        Link(side, piece,
+                        Link(side,
                              sq, u, capture | promote,
                              (*value)[stage][board[u]]
 #if !defined SAVE_SVALUE
@@ -1486,7 +1483,7 @@ CaptureList(short side, short ply,
 
                     if (!PP || flag.tsume)
                     {
-                        Link(side, piece,
+                        Link(side,
                              sq, u, capture,
                              (*value)[stage][board[u]]
 #if !defined SAVE_SVALUE
@@ -1636,7 +1633,7 @@ IsCheckmate(short side, short in_check, short blockable)
                  * Drops are restricted for pawns, lances, and knights.
                  */
 
-                if (piece > knight)
+                if (piece >= silver)
                     break;
             }
         }