Make piece-type testable by bitmask
authorH.G.Muller <hgm@hgm-xboard.(none)>
Wed, 5 Mar 2014 09:45:19 +0000 (10:45 +0100)
committerH.G.Muller <hgm@hgm-xboard.(none)>
Wed, 5 Mar 2014 09:45:19 +0000 (10:45 +0100)
This allows faster testing if a given piece belongs to a group of types.

gnushogi/genmove.c
gnushogi/gnushogi.h
gnushogi/init.c

index 208966d..a2c1c09 100644 (file)
@@ -323,21 +323,7 @@ PromotionPossible(short color, short f, short t, short p)
             return false;
     }
 
-    /* FIXME: this can be simplified... */
-    switch (p)
-    {
-    case pawn:
-#ifndef MINISHOGI
-    case lance:
-    case knight:
-#endif
-    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;
 }
 
 
@@ -538,10 +524,7 @@ field_bonus(short side, short piece,
 
                     /* CHECKME: is this right? */
                     if (((rvupiece == rvuboard) && (upiece == pawn))
-                        || (upiece == bishop)
-#ifndef MINISHOGI
-                       || (upiece == knight)
-#endif
+                        || typeMask[upiece] & (T_BISHOP | T_KNIGHT)
                        )
                     {
                         s++; /* The opposing pawn (piece) */
@@ -781,11 +764,7 @@ LinkMove(short ply, short f,
             {
 #ifdef TESUJIBONUS
                 /* Look at non-promoting silver or knight */
-                if (piece == silver
-#ifndef MINISHOGI
-                   || piece == knight
-#endif
-                   )
+                if (typeMask[piece] & (T_SILVER | T_KNIGHT))
                 {
                     local_flag |= tesuji; /* Non-promotion */
                     s++;
@@ -1127,11 +1106,7 @@ LinkPreventCheckDrops(short side, short xside, short ply)
 
     for (piece = pawn+1; piece <= rook; piece++)
     {
-        if (
-#ifndef MINISHOGI
-           piece == lance ||
-#endif
-           piece == bishop || piece == rook)
+        if (typeMask[piece] & (T_LANCE | T_BISHOP | T_ROOK))
         {
             /* check for threat of xside piece */
             ptyp = ptype[side][piece];
index d40c3ac..c993804 100644 (file)
@@ -281,6 +281,33 @@ enum {
     king
 };
 
+/* piece mask defines */
+enum {
+    T_PAWN    = 1<<pawn,
+#ifndef MINISHOGI
+    T_LANCE   = 1<<lance,
+    T_KNIGHT  = 1<<knight,
+#endif
+    /* start of pieces that can be dropped at any square */
+    T_SILVER  = 1<<silver,
+    T_GOLD    = 1<<gold,
+    T_BISHOP  = 1<<bishop,
+    T_ROOK    = 1<<rook,
+    T_PPAWN   = 1<<ppawn,
+#ifndef MINISHOGI
+    T_PLANCE  = 1<<plance,
+    T_PKNIGHT = 1<<pknight,
+#endif
+    T_PSILVER = 1<<psilver,
+    T_PBISHOP = 1<<pbishop,
+    T_PROOK   = 1<<prook,
+    T_KING    = 1<<king
+#ifdef MINISHOGI
+    T_LANCE   = 1<<king+1,
+    T_KNIGHT  = 1<<king+2,
+#endif
+};
+
 /* move types */
 enum {
     ptype_no_piece = 0,
index 1c034d0..ac870a5 100644 (file)
@@ -87,6 +87,20 @@ const small_short sweep[NO_PIECES] =
     false, true, true, false
 };
 
+const int typeMask[NO_PIECES] =
+{
+    0, T_PAWN,
+#ifndef MINISHOGI
+    T_LANCE, T_KNIGHT,
+#endif
+    T_SILVER, T_GOLD, T_BISHOP, T_ROOK,
+    T_PPAWN,
+#ifndef MINISHOGI
+    T_PLANCE, T_PKNIGHT,
+#endif
+    T_PSILVER, T_PBISHOP, T_PROOK, T_KING
+};
+
 
 #ifdef SAVE_DISTDATA
 short