Generate patterns.inc at build time.
authorYann Dirson <ydirson@altern.org>
Mon, 11 Feb 2008 00:47:01 +0000 (01:47 +0100)
committerYann Dirson <ydirson@free.fr>
Mon, 4 Nov 2013 21:55:36 +0000 (22:55 +0100)
TODO
gnushogi/Makefile.in
gnushogi/init-common.c [new file with mode: 0644]
gnushogi/init.c
gnushogi/makepattern.c
gnushogi/pat2inc.c
gnushogi/pattern-common.c [new file with mode: 0644]
gnushogi/pattern.c
gnushogi/pattern.inc [deleted file]
misc/gnuminishogi.pat [new file with mode: 0644]

diff --git a/TODO b/TODO
index 12b25b5..9489313 100644 (file)
--- a/TODO
+++ b/TODO
@@ -4,7 +4,6 @@ Generic cleanups
   - position of captured pieces in curses mode
   - compile with bound-checker
 - add autosave mode to ease hunt for segfaults ?
-- generate patterns.inc at build-time
 - use 2D array for the board, get rid of the (i)nunmap stuff
 - fixup build procedure to support parallel make runs
 - investigate those preexisting "overflow in implicit constant
@@ -20,9 +19,3 @@ Minishogi-related stuff
   * OPENING_HINT
 - add minishogi stuff to the doc
 - use valid minishogi moves in online help
-
-- segfault (on level 1) following 2e1d 1d4a
-       => field.piece can reach 14 in pattern.c::update_advance_bonus
-          => probably related to using patterns for standard shogi
-          => must break the circular pattern.o <-> pattern.inc dep to
-               be able to generated pattern.inc as needed
index 1596717..05168ad 100644 (file)
@@ -70,7 +70,7 @@ LCURSES = @LIBCURSES@
 CC          = @CC@
 CWARNINGS   = @WARNINGS@
 CEXTRAFLAGS = @CEXTRAFLAGS@
-CFLAGS      = @CFLAGS@ $(CEXTRAFLAGS) $(CWARNINGS) -I$(ROOT) -I@top_builddir@
+CFLAGS      = @CFLAGS@ $(CEXTRAFLAGS) $(CWARNINGS) -I$(ROOT) -I@top_builddir@ -I.
 LDFLAGS     = @LDFLAGS@
 
 # The hashfile is a record of positions seen. It is used by
@@ -91,7 +91,8 @@ BINBOOK    = -DBINBOOK=\"$(LIBDIR)/gnushogi.bbk\"
 # obtain such a pattern in the opening stage. Sequences of opening
 # patterns may be described in order to support the correct order of moves.
 
-PATTERN    = -DPATTERNFILE=\"$(ROOT)/misc/gnushogi.pat\"
+PATTERNFILE=$(ROOT)/misc/gnushogi.pat
+PATTERN    = -DPATTERNFILE=\"$(PATTERNFILE)\"
 
 
 all:
@@ -103,6 +104,11 @@ all:
 #
 
 COMMONFILES = \
+                globals.o      \
+                init-common.o    \
+                pattern-common.o
+
+NOTCOMMONFILES = \
                 attacks.o      \
                 book.o         \
                 commondsp.o    \
@@ -110,7 +116,6 @@ COMMONFILES = \
                 dspwrappers.o  \
                 eval.o         \
                 genmove.o      \
-                globals.o      \
                 init.o         \
                 pattern.o      \
                 rawdsp.o       \
@@ -118,7 +123,7 @@ COMMONFILES = \
                 tcontrl.o      \
                 util.o
 
-GNUSHOGI_FILES  = $(COMMONFILES) main.o      
+GNUSHOGI_FILES  = $(COMMONFILES) $(NOTCOMMONFILES) main.o
 
 
 PAT2INCFILES    = $(COMMONFILES) makepattern.o pat2inc.o
@@ -173,6 +178,9 @@ eval.o: eval.c
 init.o: init.c
        $(CC)  $(CFLAGS) -c $<
 
+init-common.o: init-common.c
+       $(CC)  $(CFLAGS) $(LANGF) -c $<
+
 main.o: main.c
        $(CC) $(CFLAGS) $(BINBOOK) -c $<
 
@@ -185,6 +193,9 @@ makepattern.o: makepattern.c
 pattern.o: pattern.c
        $(CC)  $(CFLAGS) -c $<
 
+pattern-common.o: pattern-common.c
+       $(CC)  $(CFLAGS) -c $<
+
 rawdsp.o: rawdsp.c
        $(CC) $(CFLAGS) -c $<
 
@@ -206,6 +217,8 @@ pat2inc.o: pat2inc.c
 sizetest.o: sizetest.c
        $(CC) $(CFLAGS) -c $<
 
+pattern.inc: $(PATTERNFILE) pat2inc
+       ./pat2inc
 
 #
 # Other targets.
@@ -266,7 +279,7 @@ eval.o:        eval.c eval.h gnushogi.h $(SRCDIR)/pattern.h
 init.o:        init.c gnushogi.h $(SRCDIR)/pattern.h
 main.o:        main.c gnushogi.h
 makepattern.o: pattern.c gnushogi.h $(SRCDIR)/pattern.h
-pattern.o:     pattern.c gnushogi.h $(SRCDIR)/pattern.h $(SRCDIR)/pattern.inc
+pattern.o:     pattern.c gnushogi.h $(SRCDIR)/pattern.h pattern.inc
 rawdsp.o:      rawdsp.c gnushogi.h
 search.o:      search.c gnushogi.h
 tcontrl.o:     tcontrl.c gnushogi.h
diff --git a/gnushogi/init-common.c b/gnushogi/init-common.c
new file mode 100644 (file)
index 0000000..bd571ce
--- /dev/null
@@ -0,0 +1,492 @@
+#include "gnushogi.h"
+#include "pattern.h"
+
+unsigned int ttbllimit;
+
+/*
+ * ptype is used to separate black and white pawns, like this; ptyp =
+ * ptype[side][piece] piece can be used directly in nextpos/nextdir when
+ * generating moves for pieces that are not white pawns.
+ */
+
+const small_short ptype[2][NO_PIECES] =
+{
+    {
+        ptype_no_piece, ptype_pawn,
+#ifndef MINISHOGI
+       ptype_lance,  ptype_knight,
+#endif
+        ptype_silver,   ptype_gold,  ptype_bishop, ptype_rook,
+        ptype_gold,
+#ifndef MINISHOGI
+       ptype_gold,  ptype_gold,
+#endif
+       ptype_gold,
+        ptype_pbishop,  ptype_prook, ptype_king
+    },
+    {
+        ptype_no_piece, ptype_wpawn,
+#ifndef MINISHOGI
+       ptype_wlance, ptype_wknight,
+#endif
+        ptype_wsilver,  ptype_wgold, ptype_bishop, ptype_rook,
+        ptype_wgold,
+#ifndef MINISHOGI
+       ptype_wgold, ptype_wgold,
+#endif
+       ptype_wgold,
+        ptype_pbishop,  ptype_prook, ptype_king
+    },
+};
+
+const small_short promoted[NO_PIECES] =
+{
+    no_piece, ppawn,
+#ifndef MINISHOGI
+    plance, pknight,
+#endif
+    psilver, gold, pbishop, prook,
+    ppawn,
+#ifndef MINISHOGI
+    plance, pknight,
+#endif
+    psilver, pbishop, prook, king
+};
+
+const small_short unpromoted[NO_PIECES] =
+{
+    no_piece, pawn,
+#ifndef MINISHOGI
+    lance, knight,
+#endif
+    silver, gold, bishop, rook,
+    pawn,
+#ifndef MINISHOGI
+    lance, knight,
+#endif
+    silver, bishop, rook, king
+};
+
+
+/* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
+
+#ifndef WIN32
+#define max(a, b) (((a) < (b))?(b):(a))
+#endif
+#define odd(a) ((a) & 1)
+
+const small_short piece_of_ptype[NO_PTYPE_PIECES] =
+{
+    pawn,
+#ifndef MINISHOGI
+    lance, knight,
+#endif
+    silver, gold, bishop, rook, pbishop, prook, king,
+    pawn,
+#ifndef MINISHOGI
+    lance, knight,
+#endif
+    silver, gold
+};
+
+
+/* FIXME: all bishops and rooks are black ? */
+const small_short side_of_ptype[NO_PTYPE_PIECES] =
+{
+    black,
+#ifndef MINISHOGI
+    black, black,
+#endif
+    black, black, black, black, black, black, black,
+    white,
+#ifndef MINISHOGI
+    white, white,
+#endif
+    white, white
+};
+
+
+int
+Initialize_data(void)
+{
+    size_t n;
+    int i;
+    char buffer[60];
+    int doit = true;
+
+    {
+        small_short x = -1;
+
+        if (x >= 0)
+        {
+            ShowMessage("datatype 'small_short' is unsigned; "
+                        "check gnushogi.h\n");
+            return 1;
+        }
+    }
+
+    n = sizeof(struct leaf) * (size_t)TREE;
+    Tree = malloc(n);
+
+    if (!Tree)
+    {
+        sprintf(buffer, "Cannot allocate %ld bytes for search tree",
+                (long)n);
+        ShowMessage(buffer);
+        return 1;
+    }
+
+    n = sizeof(hashcode_array);
+    hashcode = malloc(n);
+
+    if (!hashcode)
+    {
+        sprintf(buffer, "Cannot allocate %ld bytes for hashcode", (long)n);
+        ShowMessage(buffer);
+        return 1;
+    }
+
+    n = sizeof(drop_hashcode_array);
+    drop_hashcode = malloc(n);
+
+    if (!drop_hashcode)
+    {
+        sprintf(buffer,
+                "Cannot allocate %ld bytes for drop_hashcode",
+                (long)n);
+        ShowMessage(buffer);
+        return 1;
+    }
+
+    n = sizeof(struct GameRec) * (size_t)(MAXMOVES + MAXDEPTH);
+    GameList = malloc(n);
+
+    if (!GameList)
+    {
+        sprintf(buffer,
+                "Cannot allocate %ld bytes for game record",
+                (long)n);
+        ShowMessage(buffer);
+        return 1;
+    }
+
+#if !defined SAVE_NEXTPOS
+    n = sizeof(next_array);
+
+    for (i = 0; i < NO_PTYPE_PIECES; i++)
+    {
+        nextdir[i] = use_nextpos ? malloc(n) : NULL;
+
+        if (!nextdir[i])
+        {
+            if (use_nextpos)
+            {
+                sprintf(buffer, "cannot allocate %ld space for nextdir %d",
+                        (long)(n), i);
+                ShowMessage(buffer);
+            }
+
+            nextdir[i] = NULL;
+            use_nextpos = false;
+        }
+
+        nextpos[i] = use_nextpos ? malloc(n) : NULL;
+
+        if (!nextpos[i])
+        {
+            if (use_nextpos)
+            {
+                sprintf(buffer, "cannot allocate %ld space for nextpos %d",
+                        (long)(n), i);
+                ShowMessage(buffer);
+            }
+
+            use_nextpos = false;
+        }
+    }
+
+    if (!use_nextpos)
+    {
+        return 1;
+    }
+#endif
+
+    n = sizeof(value_array);
+    value = malloc(n);
+
+    if (!value)
+    {
+        ShowMessage("cannot allocate value space");
+        return 1;
+    }
+
+    n = sizeof(fscore_array);
+    fscore = malloc(n);
+
+    if (!fscore)
+    {
+        ShowMessage("cannot allocate fscore space");
+        return 1;
+    }
+
+#if defined HISTORY
+    n = sizeof_history;
+    history = malloc(n);
+
+    if (!history)
+    {
+        sprintf(buffer, "Cannot allocate %ld bytes for history table",
+                (long)sizeof_history);
+        ShowMessage(buffer);
+        use_history = false;
+    }
+#endif
+
+#if defined CACHE
+    n = sizeof(struct etable) * (size_t)ETABLE;
+
+    for (i = 0; i < 2; i++)
+    {
+        etab[i] = use_etable ? malloc(n) : 0;
+
+        if (!etab[i])
+        {
+            sprintf(buffer, "Cannot allocate %ld bytes for cache table %ld",
+                    (long)n, (long)i);
+            ShowMessage(buffer);
+            use_etable = false;
+        }
+    }
+#endif
+
+#if ttblsz
+
+    if (rehash < 0)
+        rehash = MAXrehash;
+
+    n = sizeof(struct hashentry)*(ttblsize + rehash);
+
+    while (doit && ttblsize > MINTTABLE)
+    {
+        ttable[0] = malloc(n);  /* FIXME: cast to the correct type. */
+        ttable[1] = ttable[0] ? malloc(n) : NULL;
+
+        if (!ttable[0] || !ttable[1])
+        {
+            if (!ttable[0])
+                free(ttable[0]);
+
+            if (!ttable[1])
+                free(ttable[1]);
+
+            ttblsize = ttblsize >> 1;
+            n = sizeof(struct hashentry) * (ttblsize + rehash);
+        }
+        else
+        {
+            doit = false;
+        }
+    }
+
+    if (ttblsize <= MINTTABLE)
+    {
+        use_ttable = false;
+    }
+
+    if (use_ttable)
+    {
+        /* CHECKME: is the precedence here correct? */
+        /* ttbllimit = ttblsize << 1 - ttblsize >> 2; */
+        ttbllimit = (ttblsize << 1) - (ttblsize >> 2);
+    }
+    else
+    {
+        sprintf(buffer, "Cannot allocate %ld bytes for transposition table",
+                (long)(2 * n));
+        ShowMessage(buffer);
+        ttable[0] = ttable[1] = NULL;
+    }
+#endif /* ttblsz */
+
+#if !defined SAVE_DISTDATA
+    n = sizeof(distdata_array);
+    distdata = malloc(n);
+
+    if (!distdata)
+    {
+        ShowMessage("cannot allocate distdata space...");
+        use_distdata = false;
+    }
+#endif
+
+#if !defined SAVE_PTYPE_DISTDATA
+    n = sizeof(distdata_array);
+
+    for (i = 0; i < NO_PTYPE_PIECES; i++)
+    {
+        ptype_distdata[i] = use_ptype_distdata ? malloc(n) : 0;
+
+        if (!ptype_distdata[i])
+        {
+            sprintf(buffer,
+                    "cannot allocate %ld bytes for ptype_distdata %d...",
+                    (long)n, i);
+            use_ptype_distdata = false;
+        }
+    }
+#endif
+
+    return 0;
+}
+
+
+#ifdef SAVE_PTYPE_DISTDATA
+short
+piece_distance(short side, short piece, short f, short t)
+{
+    return ((f > NO_SQUARES)
+            ? (short)1
+            : (short)ptype_distance(ptype[side][piece], f, t));
+}
+#else
+short
+piece_distance(short side, short piece, short f, short t)
+{
+    return ((f > NO_SQUARES)
+            ? (short)1
+            : (use_ptype_distdata
+               ? (short)(*ptype_distdata[ptype[side][piece]])[f][t]
+               : (short)ptype_distance(ptype[side][piece], f, t)));
+}
+#endif
+
+
+/*
+ * Determine the minimum number of moves for a piece from
+ * square "f" to square "t". If the piece cannot reach "t",
+ * the count is set to CANNOT_REACH.
+ */
+
+#define csquare(sq) ((side == black) ? sq : (NO_SQUARES - 1 - sq))
+#define crow(sq) row(csquare(sq))
+#define ccol(sq) column(csquare(sq))
+
+short
+ptype_distance(short ptyp, short f, short t)
+{
+    short side, piece;
+    short colf, colt, rowf, rowt, dcol, drow;
+
+    if (f == t)
+        return 0;
+
+    piece = piece_of_ptype[ptyp];
+    side  = side_of_ptype[ptyp];
+
+    dcol = (colt = ccol(t)) - (colf = ccol(f));
+    drow = (rowt = crow(t)) - (rowf = crow(f));
+
+    switch (piece)
+    {
+    case pawn:
+        if ((dcol != 0) || (drow < 1))
+            return CANNOT_REACH;
+        else
+            return drow;
+
+#ifndef MINISHOGI
+    case lance:
+        if ((dcol != 0) || (drow < 1))
+            return CANNOT_REACH;
+        else
+            return 1;
+
+    case knight:
+        if (odd(drow) || (odd(drow / 2) != odd(dcol)))
+            return CANNOT_REACH;
+        else if ((drow == 0) || ((drow / 2) < abs(dcol)))
+            return CANNOT_REACH;
+        else
+            return (drow / 2);
+#endif
+
+    case silver:
+        if (drow > 0)
+        {
+            if (odd(drow) == odd(dcol))
+            {
+                return max(abs(drow), abs(dcol));
+            }
+            else
+            {
+                if (abs(dcol) <= drow)
+                    return drow;
+                else
+                    return (max(abs(drow), abs(dcol)) + 1);
+            }
+        }
+        else
+        {
+            if (odd(drow) == odd(dcol))
+                return (max(abs(drow), abs(dcol)));
+            else
+                return (max(abs(drow) + 1, abs(dcol)) + 1);
+        };
+
+    case gold:
+    case ppawn:
+#ifndef MINISHOGI
+    case pknight:
+    case plance:
+#endif
+    case psilver:
+        if (abs(dcol) == 0)
+            return (abs(drow));
+        else if (drow >= 0)
+            return max(drow, abs(dcol));
+        else
+            return (abs(dcol) - drow);
+
+    case bishop:
+        if (odd(dcol) != odd(drow))
+            return CANNOT_REACH;
+        else
+            return ((abs(dcol) == abs(drow)) ? 1 : 2);
+
+    case pbishop:
+        if (odd(dcol) != odd(drow))
+        {
+            if ((abs(dcol) <= 1) && (abs(drow) <= 1))
+                return 1;
+            else if (abs(abs(dcol) - abs(drow)) == 1)
+                return 2;
+            else
+                return 3;
+        }
+        else
+        {
+            return ((abs(dcol) == abs(drow)) ? 1 : 2);
+        }
+
+    case rook:
+        if ((dcol == 0) || (drow == 0))
+            return 1;
+        else
+            return 2;
+
+    case prook:
+        if ((dcol == 0) || (drow == 0))
+            return 1;
+        else if ((abs(dcol) == 1) && (abs(drow) == 1))
+            return 1;
+        else
+            return 2;
+
+    case king:
+        return max(abs(drow), abs(dcol));
+
+    default:
+        /* should never occur */
+        return (CANNOT_REACH);
+    }
+}
index 819e738..e6b495f 100644 (file)
@@ -64,47 +64,8 @@ short nolist          = 1;  /* Don't list the game after exit. */
 
 display_t display_type = DISPLAY_X;
 
-unsigned int ttbllimit;
-
 /* .... MOVE GENERATION VARIABLES AND INITIALIZATIONS .... */
 
-
-#ifndef WIN32
-#define max(a, b) (((a) < (b))?(b):(a))
-#endif
-#define odd(a) ((a) & 1)
-
-
-const small_short piece_of_ptype[NO_PTYPE_PIECES] =
-{
-    pawn,
-#ifndef MINISHOGI
-    lance, knight,
-#endif
-    silver, gold, bishop, rook, pbishop, prook, king,
-    pawn,
-#ifndef MINISHOGI
-    lance, knight,
-#endif
-    silver, gold
-};
-
-
-/* FIXME: all bishops and rooks are black ? */
-const small_short side_of_ptype[NO_PTYPE_PIECES] =
-{
-    black,
-#ifndef MINISHOGI
-    black, black,
-#endif
-    black, black, black, black, black, black, black,
-    white,
-#ifndef MINISHOGI
-    white, white,
-#endif
-    white, white
-};
-
 #ifdef SAVE_NEXTPOS
 const small_short psweep[NO_PTYPE_PIECES] =
 {
@@ -128,137 +89,6 @@ const small_short sweep[NO_PIECES] =
 };
 
 
-/*
- * Determine the minimum number of moves for a piece from
- * square "f" to square "t". If the piece cannot reach "t",
- * the count is set to CANNOT_REACH.
- */
-
-#define csquare(sq) ((side == black) ? sq : (NO_SQUARES - 1 - sq))
-#define crow(sq) row(csquare(sq))
-#define ccol(sq) column(csquare(sq))
-
-short
-ptype_distance(short ptyp, short f, short t)
-{
-    short side, piece;
-    short colf, colt, rowf, rowt, dcol, drow;
-
-    if (f == t)
-        return 0;
-
-    piece = piece_of_ptype[ptyp];
-    side  = side_of_ptype[ptyp];
-
-    dcol = (colt = ccol(t)) - (colf = ccol(f));
-    drow = (rowt = crow(t)) - (rowf = crow(f));
-
-    switch (piece)
-    {
-    case pawn:
-        if ((dcol != 0) || (drow < 1))
-            return CANNOT_REACH;
-        else
-            return drow;
-
-#ifndef MINISHOGI
-    case lance:
-        if ((dcol != 0) || (drow < 1))
-            return CANNOT_REACH;
-        else
-            return 1;
-
-    case knight:
-        if (odd(drow) || (odd(drow / 2) != odd(dcol)))
-            return CANNOT_REACH;
-        else if ((drow == 0) || ((drow / 2) < abs(dcol)))
-            return CANNOT_REACH;
-        else
-            return (drow / 2);
-#endif
-
-    case silver:
-        if (drow > 0)
-        {
-            if (odd(drow) == odd(dcol))
-            {
-                return max(abs(drow), abs(dcol));
-            }
-            else
-            {
-                if (abs(dcol) <= drow)
-                    return drow;
-                else
-                    return (max(abs(drow), abs(dcol)) + 1);
-            }
-        }
-        else
-        {
-            if (odd(drow) == odd(dcol))
-                return (max(abs(drow), abs(dcol)));
-            else
-                return (max(abs(drow) + 1, abs(dcol)) + 1);
-        };
-
-    case gold:
-    case ppawn:
-#ifndef MINISHOGI
-    case pknight:
-    case plance:
-#endif
-    case psilver:
-        if (abs(dcol) == 0)
-            return (abs(drow));
-        else if (drow >= 0)
-            return max(drow, abs(dcol));
-        else
-            return (abs(dcol) - drow);
-
-    case bishop:
-        if (odd(dcol) != odd(drow))
-            return CANNOT_REACH;
-        else
-            return ((abs(dcol) == abs(drow)) ? 1 : 2);
-
-    case pbishop:
-        if (odd(dcol) != odd(drow))
-        {
-            if ((abs(dcol) <= 1) && (abs(drow) <= 1))
-                return 1;
-            else if (abs(abs(dcol) - abs(drow)) == 1)
-                return 2;
-            else
-                return 3;
-        }
-        else
-        {
-            return ((abs(dcol) == abs(drow)) ? 1 : 2);
-        }
-
-    case rook:
-        if ((dcol == 0) || (drow == 0))
-            return 1;
-        else
-            return 2;
-
-    case prook:
-        if ((dcol == 0) || (drow == 0))
-            return 1;
-        else if ((abs(dcol) == 1) && (abs(drow) == 1))
-            return 1;
-        else
-            return 2;
-
-    case king:
-        return max(abs(drow), abs(dcol));
-
-    default:
-        /* should never occur */
-        return (CANNOT_REACH);
-    }
-}
-
-
 #ifdef SAVE_DISTDATA
 short
 distance(short a, short b)
@@ -276,27 +106,6 @@ distance(short a, short b)
 #endif
 
 
-#ifdef SAVE_PTYPE_DISTDATA
-short
-piece_distance(short side, short piece, short f, short t)
-{
-    return ((f > NO_SQUARES)
-            ? (short)1
-            : (short)ptype_distance(ptype[side][piece], f, t));
-}
-#else
-short
-piece_distance(short side, short piece, short f, short t)
-{
-    return ((f > NO_SQUARES)
-            ? (short)1
-            : (use_ptype_distdata
-               ? (short)(*ptype_distdata[ptype[side][piece]])[f][t]
-               : (short)ptype_distance(ptype[side][piece], f, t)));
-}
-#endif
-
-
 void
 Initialize_dist(void)
 {
@@ -348,70 +157,6 @@ Initialize_dist(void)
  */
 
 
-/*
- * ptype is used to separate black and white pawns, like this; ptyp =
- * ptype[side][piece] piece can be used directly in nextpos/nextdir when
- * generating moves for pieces that are not white pawns.
- */
-
-const small_short ptype[2][NO_PIECES] =
-{
-    {
-        ptype_no_piece, ptype_pawn,
-#ifndef MINISHOGI
-       ptype_lance,  ptype_knight,
-#endif
-        ptype_silver,   ptype_gold,  ptype_bishop, ptype_rook,
-        ptype_gold,
-#ifndef MINISHOGI
-       ptype_gold,  ptype_gold,
-#endif
-       ptype_gold,
-        ptype_pbishop,  ptype_prook, ptype_king
-    },
-    {
-        ptype_no_piece, ptype_wpawn,
-#ifndef MINISHOGI
-       ptype_wlance, ptype_wknight,
-#endif
-        ptype_wsilver,  ptype_wgold, ptype_bishop, ptype_rook,
-        ptype_wgold,
-#ifndef MINISHOGI
-       ptype_wgold, ptype_wgold,
-#endif
-       ptype_wgold,
-        ptype_pbishop,  ptype_prook, ptype_king
-    },
-};
-
-const small_short promoted[NO_PIECES] =
-{
-    no_piece, ppawn,
-#ifndef MINISHOGI
-    plance, pknight,
-#endif
-    psilver, gold, pbishop, prook,
-    ppawn,
-#ifndef MINISHOGI
-    plance, pknight,
-#endif
-    psilver, pbishop, prook, king
-};
-
-const small_short unpromoted[NO_PIECES] =
-{
-    no_piece, pawn,
-#ifndef MINISHOGI
-    lance, knight,
-#endif
-    silver, gold, bishop, rook,
-    pawn,
-#ifndef MINISHOGI
-    lance, knight,
-#endif
-    silver, bishop, rook, king
-};
-
 const small_short is_promoted[NO_PIECES] =
 {
     false, false,
@@ -892,239 +637,6 @@ NewGame(void)
 
 
 
-int
-Initialize_data(void)
-{
-    size_t n;
-    int i;
-    char buffer[60];
-    int doit = true;
-
-    {
-        small_short x = -1;
-
-        if (x >= 0)
-        {
-            ShowMessage("datatype 'small_short' is unsigned; "
-                        "check gnushogi.h\n");
-            return 1;
-        }
-    }
-
-    n = sizeof(struct leaf) * (size_t)TREE;
-    Tree = malloc(n);
-
-    if (!Tree)
-    {
-        sprintf(buffer, "Cannot allocate %ld bytes for search tree",
-                (long)n);
-        ShowMessage(buffer);
-        return 1;
-    }
-
-    n = sizeof(hashcode_array);
-    hashcode = malloc(n);
-
-    if (!hashcode)
-    {
-        sprintf(buffer, "Cannot allocate %ld bytes for hashcode", (long)n);
-        ShowMessage(buffer);
-        return 1;
-    }
-
-    n = sizeof(drop_hashcode_array);
-    drop_hashcode = malloc(n);
-
-    if (!drop_hashcode)
-    {
-        sprintf(buffer,
-                "Cannot allocate %ld bytes for drop_hashcode",
-                (long)n);
-        ShowMessage(buffer);
-        return 1;
-    }
-
-    n = sizeof(struct GameRec) * (size_t)(MAXMOVES + MAXDEPTH);
-    GameList = malloc(n);
-
-    if (!GameList)
-    {
-        sprintf(buffer,
-                "Cannot allocate %ld bytes for game record",
-                (long)n);
-        ShowMessage(buffer);
-        return 1;
-    }
-
-#if !defined SAVE_NEXTPOS
-    n = sizeof(next_array);
-
-    for (i = 0; i < NO_PTYPE_PIECES; i++)
-    {
-        nextdir[i] = use_nextpos ? malloc(n) : NULL;
-
-        if (!nextdir[i])
-        {
-            if (use_nextpos)
-            {
-                sprintf(buffer, "cannot allocate %ld space for nextdir %d",
-                        (long)(n), i);
-                ShowMessage(buffer);
-            }
-
-            nextdir[i] = NULL;
-            use_nextpos = false;
-        }
-
-        nextpos[i] = use_nextpos ? malloc(n) : NULL;
-
-        if (!nextpos[i])
-        {
-            if (use_nextpos)
-            {
-                sprintf(buffer, "cannot allocate %ld space for nextpos %d",
-                        (long)(n), i);
-                ShowMessage(buffer);
-            }
-
-            use_nextpos = false;
-        }
-    }
-
-    if (!use_nextpos)
-    {
-        return 1;
-    }
-#endif
-
-    n = sizeof(value_array);
-    value = malloc(n);
-
-    if (!value)
-    {
-        ShowMessage("cannot allocate value space");
-        return 1;
-    }
-
-    n = sizeof(fscore_array);
-    fscore = malloc(n);
-
-    if (!fscore)
-    {
-        ShowMessage("cannot allocate fscore space");
-        return 1;
-    }
-
-#if defined HISTORY
-    n = sizeof_history;
-    history = malloc(n);
-
-    if (!history)
-    {
-        sprintf(buffer, "Cannot allocate %ld bytes for history table",
-                (long)sizeof_history);
-        ShowMessage(buffer);
-        use_history = false;
-    }
-#endif
-
-#if defined CACHE
-    n = sizeof(struct etable) * (size_t)ETABLE;
-
-    for (i = 0; i < 2; i++)
-    {
-        etab[i] = use_etable ? malloc(n) : 0;
-
-        if (!etab[i])
-        {
-            sprintf(buffer, "Cannot allocate %ld bytes for cache table %ld",
-                    (long)n, (long)i);
-            ShowMessage(buffer);
-            use_etable = false;
-        }
-    }
-#endif
-
-#if ttblsz
-
-    if (rehash < 0)
-        rehash = MAXrehash;
-
-    n = sizeof(struct hashentry)*(ttblsize + rehash);
-
-    while (doit && ttblsize > MINTTABLE)
-    {
-        ttable[0] = malloc(n);  /* FIXME: cast to the correct type. */
-        ttable[1] = ttable[0] ? malloc(n) : NULL;
-
-        if (!ttable[0] || !ttable[1])
-        {
-            if (!ttable[0])
-                free(ttable[0]);
-
-            if (!ttable[1])
-                free(ttable[1]);
-
-            ttblsize = ttblsize >> 1;
-            n = sizeof(struct hashentry) * (ttblsize + rehash);
-        }
-        else
-        {
-            doit = false;
-        }
-    }
-
-    if (ttblsize <= MINTTABLE)
-    {
-        use_ttable = false;
-    }
-
-    if (use_ttable)
-    {
-        /* CHECKME: is the precedence here correct? */
-        /* ttbllimit = ttblsize << 1 - ttblsize >> 2; */
-        ttbllimit = (ttblsize << 1) - (ttblsize >> 2);
-    }
-    else
-    {
-        sprintf(buffer, "Cannot allocate %ld bytes for transposition table",
-                (long)(2 * n));
-        ShowMessage(buffer);
-        ttable[0] = ttable[1] = NULL;
-    }
-#endif /* ttblsz */
-
-#if !defined SAVE_DISTDATA
-    n = sizeof(distdata_array);
-    distdata = malloc(n);
-
-    if (!distdata)
-    {
-        ShowMessage("cannot allocate distdata space...");
-        use_distdata = false;
-    }
-#endif
-
-#if !defined SAVE_PTYPE_DISTDATA
-    n = sizeof(distdata_array);
-
-    for (i = 0; i < NO_PTYPE_PIECES; i++)
-    {
-        ptype_distdata[i] = use_ptype_distdata ? malloc(n) : 0;
-
-        if (!ptype_distdata[i])
-        {
-            sprintf(buffer,
-                    "cannot allocate %ld bytes for ptype_distdata %d...",
-                    (long)n, i);
-            use_ptype_distdata = false;
-        }
-    }
-#endif
-
-    return 0;
-}
-
 
 int
 InitMain(void)
index 067c758..34053d8 100644 (file)
 #define MAX_PATTERN          200
 
 char *patternfile = PATTERNFILE;
+small_short pattern_data[MAX_PATTERN_DATA];
+
+/* minimal ShowMessage to avoid dependency on extraneous display code */
+void
+ShowMessage(char *s)
+{
+    printf("%s\n", s);
+}
 
 #define is_digit(c) (((c) >= '0') && ((c) <= '9'))
 #define is_alpha(c) ((((c) >= 'a') && ((c) <= 'z')) \
index 956f5c2..00d505e 100644 (file)
@@ -43,12 +43,11 @@ extern void WriteOpeningSequences(short pindex);
 small_short PieceCnt[2];
 small_short PieceList[2][NO_SQUARES];
 small_short PieceIndex[NO_SQUARES];
+*/
 
 small_short board[NO_SQUARES];
 small_short color[NO_SQUARES];
 
-*/
-
 void
 test_distance(void)
 {
@@ -95,8 +94,6 @@ main(int argc, char **argv)
     char  s[80];
 #endif
 
-    display_type = DISPLAY_RAW;
-
     Initialize_data();
 
     for (sq = 0; sq < NO_SQUARES; sq++)
diff --git a/gnushogi/pattern-common.c b/gnushogi/pattern-common.c
new file mode 100644 (file)
index 0000000..4cf1a12
--- /dev/null
@@ -0,0 +1,49 @@
+#include "gnushogi.h"
+#include "pattern.h"
+
+short
+ValueOfOpeningName (char *name)
+{
+    short i;
+    i = (name[0] == 'C') ? 0 : 100;
+
+    switch (name[7])
+    {
+    case 'S':
+        i += 10;
+        break;
+
+    case 'R':
+        i += 20;
+        break;
+
+    case 'U':
+        i += 30;
+        break;
+
+    default:
+        i += 40;
+        break;
+    }
+
+    switch (name[9])
+    {
+    case 'S':
+        i += 1;
+        break;
+
+    case 'R':
+        i += 2;
+        break;
+
+    case 'U':
+        i += 3;
+        break;
+
+    default:
+        i += 4;
+        break;
+    }
+
+    return i;
+}
index c53b86c..f5209d7 100644 (file)
@@ -41,54 +41,6 @@ struct OpeningSequence_rec OpeningSequence[MAX_OPENING_SEQUENCE];
 small_short pattern_data[MAX_PATTERN_DATA];
 
 
-short
-ValueOfOpeningName (char *name)
-{
-    short i;
-    i = (name[0] == 'C') ? 0 : 100;
-
-    switch (name[7])
-    {
-    case 'S':
-        i += 10;
-        break;
-
-    case 'R':
-        i += 20;
-        break;
-
-    case 'U':
-        i += 30;
-        break;
-
-    default:
-        i += 40;
-        break;
-    }
-
-    switch (name[9])
-    {
-    case 'S':
-        i += 1;
-        break;
-
-    case 'R':
-        i += 2;
-        break;
-
-    case 'U':
-        i += 3;
-        break;
-
-    default:
-        i += 4;
-        break;
-    }
-
-    return i;
-}
-
-
 static void
 NameOfOpeningValue (short i, char *name)
 {
diff --git a/gnushogi/pattern.inc b/gnushogi/pattern.inc
deleted file mode 100644 (file)
index 5613075..0000000
+++ /dev/null
@@ -1,166 +0,0 @@
-#define MAX_PATTERN_DATA 2102
-
-small_short pattern_data[MAX_PATTERN_DATA] =
-{
-  11,
-    5, -3, 1,29,1,19,6,10,4,12,5,3,1,-46,1,-60,-4,
-    6, -3, 1,19,1,29,4,10,6,20,1,-37,1,-51,-4,
-    8, -3, 1,19,1,29,4,20,1,21,1,22,5,11,14,12,5,13,-4,
-    5, -3, 6,10,1,19,1,29,4,20,5,11,14,3,-4,
-    5, -3, 14,4,5,3,5,21,4,11,6,10,1,19,1,29,1,30,-4,
-    8, -3, 14,3,6,2,5,11,4,20,1,19,1,29,-4,
-    7, -3, 14,3,4,10,5,11,5,13,1,19,6,20,1,29,1,30,1,31,-4,
-    8, -3, 14,3,5,11,6,12,5,13,1,19,4,20,1,29,1,30,1,31,-4,
-    9, 10, -3, 14,2,5,11,1,19,1,29,4,20,1,30,1,31,5,21,-4,
-    11, -3, 14,10,5,11,1,27,1,19,1,29,1,30,4,20,5,21,1,-45,-4,
-    -3, 14,2,5,10,1,18,1,19,4,20,5,21,1,29,1,30,1,-36,-4,
-    -3, 2,0,3,1,14,10,5,11,4,20,1,19,1,27,1,29,-4,
-    13, -3, 6,20,4,-65,-4,
-    6, -3, 6,20,4,10,5,11,4,-46,-4,
-    2, -3, 4,20,5,11,14,12,5,13,-4,
-    8, -3, 14,2,5,11,5,21,-4,
-    7, -3, 14,3,5,11,6,12,5,13,4,20,-4,
-    11, -3, 2,0,3,1,14,10,5,11,4,20,-4,
-    6, -3, 14,3,4,10,5,11,5,13,1,19,6,20,-4,
-    -2,
-  111,
-    1, 2, -3, 1,22,1,23,1,24,1,34,1,26,7,16,4,6,-4,
-    3, -3, 1,22,1,23,1,24,1,34,4,14,7,16,3,7,-4,
-    4, -3, 4,15,1,23,1,24,1,34,7,16,-4,
-    5, 6, 7, -3, 4,14,1,23,1,33,1,34,1,26,7,16,3,7,-4,
-    12, 14, 15, 17, -3, 1,33,1,43,4,34,1,35,7,16,3,7,2,8,-4,
-    12, -3, 3,7,7,16,4,24,1,33,1,43,1,44,1,-53,-4,
-    9, 11, -3, 2,8,7,16,4,23,3,24,1,32,1,33,1,34,1,44,1,-62,-4,
-    8, -3, 4,14,1,31,1,23,3,24,1,33,1,34,7,16,2,26,-4,
-    -3, 4,14,1,31,1,23,3,24,1,33,1,34,7,17,2,26,-4,
-    10, -3, 4,23,1,32,3,24,3,33,7,16,2,26,1,44,-4,
-    -3, 4,23,1,32,3,24,3,33,7,17,2,26,1,44,-4,
-    -3, 4,31,1,22,1,32,1,33,3,24,7,16,1,43,1,35,2,8,-4,
-    13, -3, 3,7,4,24,1,33,1,43,2,26,7,16,-4,
-    -3, 3,7,4,24,1,33,1,43,2,26,7,17,-4,
-    16, -3, 3,7,2,8,7,16,1,43,1,33,4,42,-4,
-    16, -3, 3,7,2,8,7,16,1,43,1,42,4,34,-4,
-    -3, 4,42,1,52,7,16,-4,
-    -3, 4,44,1,52,7,16,-4,
-    -2,
-  12,
-    1, 2, -3, 5,3,5,13,14,4,6,10,1,29,4,2,3,1,2,0,-4,
-    3, -3, 1,27,2,0,3,1,6,10,14,11,5,3,5,13,1,19,1,29,1,21,1,31,-4,
-    4, -3, 2,0,3,1,4,2,5,3,5,13,14,12,6,10,1,19,1,21,1,29,-4,
-    7, 8, -3, 2,0,3,1,5,3,6,10,14,11,4,12,5,13,1,27,1,19,1,29,1,21,1,31,-4,
-    5, -3, 2,0,3,1,4,2,5,3,5,13,14,11,6,10,1,19,1,21,1,29,1,22,-4,
-    6, -3, 2,9,14,10,3,1,4,2,5,3,6,20,1,18,1,19,1,29,-4,
-    -3, 14,0,2,9,4,10,5,11,5,2,1,19,6,20,1,29,1,21,-4,
-    -3, 2,0,3,1,14,10,5,11,1,27,1,19,6,20,5,21,1,29,1,30,-4,
-    9, -3, 2,0,3,1,6,10,14,11,5,3,5,13,4,20,1,38,1,21,4,22,1,31,1,-56,-4,
-    -3, 2,0,3,1,14,10,5,11,6,20,5,21,4,22,4,29,1,30,1,31,1,37,1,38,-4,
-    1, -3, 2,0,3,1,6,10,14,11,5,3,5,13,-4,
-    3, -3, 2,0,3,1,5,3,6,10,14,11,4,12,5,13,-4,
-    2, -3, 2,0,3,1,4,2,5,3,5,13,14,12,6,10,-4,
-    -2,
-  112,
-    1, 2, 6, -3, 7,16,1,43,-4,
-    2, 3, 5, 6, 13, -3, 4,14,7,16,3,7,2,8,1,31,1,33,-4,
-    13, -3, 2,8,4,14,7,16,3,24,1,32,1,33,-4,
-    4, -3, 2,8,3,7,7,16,4,12,1,24,1,23,4,22,1,40,1,-58,1,-50,-4,
-    -3, 2,8,3,7,7,16,4,12,1,24,1,23,4,31,1,40,1,-58,-4,
-    -3, 2,8,6,10,4,20,4,22,1,38,1,-56,-4,
-    7, -3, 1,43,1,35,7,16,4,15,-4,
-    8, 10, -3, 1,43,1,33,1,35,4,34,7,16,-4,
-    9, -3, 1,42,1,43,1,35,4,34,7,16,-4,
-    -3, 4,42,1,43,1,35,7,16,-4,
-    -3, 1,43,4,44,1,35,7,16,-4,
-    12, -3, 2,26,7,16,-4,
-    -3, 2,26,7,17,1,53,-4,
-    11, -3, 4,23,3,24,7,16,2,8,1,32,1,33,-4,
-    -2,
-  21,
-    1, -3, 5,3,5,5,4,6,3,7,2,8,14,14,1,23,1,24,1,25,1,26,-4,
-    2, 4, -3, 5,3,5,5,4,6,3,7,2,8,14,16,1,23,1,24,1,25,1,26,-4,
-    3, -3, 5,5,4,6,3,7,14,8,5,13,2,17,1,23,1,24,1,25,1,26,-4,
-    12, -3, 5,6,3,7,14,8,5,15,4,16,2,17,1,23,1,24,1,25,1,26,-4,
-    5, 8, -3, 5,5,3,7,2,8,4,15,14,16,1,24,1,25,1,35,-4,
-    6, 9, -3, 5,5,3,7,2,8,5,13,4,15,14,16,1,23,1,24,1,25,1,35,-4,
-    7, 10, -3, 5,5,3,7,2,8,4,15,14,16,5,23,1,25,1,32,1,33,1,35,-4,
-    11, -3, 3,7,2,8,5,15,14,16,5,23,4,25,1,32,1,33,1,34,1,35,-4,
-    5, -3, 5,5,3,7,2,8,4,15,14,16,1,24,1,25,1,35,-4,
-    6, -3, 5,5,3,7,2,8,5,13,4,15,14,16,1,23,1,24,1,25,1,35,-4,
-    7, -3, 5,5,3,7,2,8,4,15,14,16,5,23,-4,
-    -3, 3,7,2,8,5,15,14,16,5,23,4,25,-4,
-    -3, 5,6,3,7,14,8,5,15,4,16,2,17,-4,
-    -2,
-  22,
-    1, -3, 2,8,4,6,5,5,5,3,14,15,1,25,1,24,1,23,-4,
-    2, -3, 2,8,5,5,4,16,14,15,5,13,1,25,1,24,1,23,-4,
-    -3, 4,16,14,15,5,14,5,13,1,25,1,24,1,23,-4,
-    0, -3, 2,8,4,6,5,5,5,3,14,15,-4,
-    1, -3, 2,8,5,5,4,16,14,15,5,13,-4,
-    2, -3, 4,16,14,15,5,14,5,13,-4,
-    -2,
-  13,
-    -3, 1,26,1,43,1,24,1,23,1,22,1,21,1,29,1,19,1,18,3,1,4,2,5,3,14,4,5,5,4,6,3,7,-4,
-    -3, 1,19,1,29,1,21,5,3,5,13,1,31,-4,
-    -2,
-  113,
-    1, 2, -3, 1,34,7,16,-4,
-    3, -3, 1,34,4,14,-4,
-    6, -3, 1,34,4,15,-4,
-    4, 5, -3, 1,34,4,14,1,33,-4,
-    -3, 1,34,1,32,1,33,4,23,3,24,-4,
-    -3, 1,43,1,33,4,24,1,44,-4,
-    7, -3, 1,43,4,25,1,24,-4,
-    -3, 1,43,4,34,1,35,1,33,-4,
-    -2,
-  23,
-    4, -3, 7,13,14,4,5,5,4,6,-4,
-    4, -3, 7,12,14,4,5,5,4,6,-4,
-    4, -3, 7,11,14,4,5,5,4,6,-4,
-    4, -3, 7,10,14,4,5,5,4,6,-4,
-    -3, 14,15,5,5,4,6,-4,
-    -2,
-  123,
-    1, 2, 3, 4, -3, 1,29,1,30,6,20,-4,
-    -3, 7,10,1,29,1,30,6,20,4,21,-4,
-    -3, 7,11,1,29,1,30,6,20,-4,
-    -3, 7,12,1,29,1,30,6,20,4,21,-4,
-    -3, 7,13,1,29,1,30,6,20,4,21,-4,
-    -2,
-  33,
-    1, -3, 1,24,1,23,1,21,1,29,3,1,4,2,5,3,14,4,5,5,4,6,3,7,-4,
-    2, 3, -3, 1,24,1,23,1,30,1,29,6,10,4,2,5,3,14,4,5,5,4,6,-4,
-    4, -3, 1,30,1,29,4,11,6,10,-4,
-    4, -3, 1,30,1,29,4,12,6,10,-4,
-    -3, 1,30,1,29,4,12,6,10,1,44,-4,
-    -2,
-  -123,
-    -3, 1,29,-4,
-    -2,
-  124,
-    1, 5, 8, 10, -3, 6,20,1,29,1,30,14,4,5,5,4,6,1,25,-4,
-    2, -3, 7,10,6,20,1,29,1,30,14,4,5,5,4,6,-4,
-    3, 4, -3, 7,10,3,1,5,3,6,20,4,21,1,29,1,30,-4,
-    -3, 7,10,3,1,5,11,6,20,4,21,1,29,1,30,-4,
-    -3, 7,10,3,1,5,13,6,20,4,21,1,29,1,30,-4,
-    6, 7, -3, 7,11,6,20,1,29,1,30,14,4,5,5,4,6,-4,
-    -3, 7,11,4,21,5,13,1,19,6,20,1,29,1,30,-4,
-    -3, 7,11,4,21,1,19,6,20,1,29,1,30,1,31,-4,
-    9, -3, 7,12,6,20,4,21,1,29,1,30,14,4,5,5,4,6,-4,
-    -3, 7,12,3,1,5,3,1,19,6,20,4,21,1,29,1,30,-4,
-    11, 13, -3, 7,13,1,19,6,20,4,21,14,4,5,5,4,6,-4,
-    12, -3, 7,13,3,1,5,11,1,19,6,20,4,21,1,29,1,30,-4,
-    -3, 7,4,3,1,1,19,6,20,4,21,4,23,1,29,1,30,-4,
-    -3, 7,13,3,1,1,19,6,20,4,21,5,23,1,29,1,30,-4,
-    -2,
-  34,
-    1, -3, 1,24,1,23,1,21,1,29,3,1,4,2,5,3,14,4,5,5,4,6,3,7,-4,
-    2, 3, -3, 1,24,1,23,1,30,1,29,6,10,4,2,5,3,14,4,5,5,4,6,-4,
-    4, -3, 1,30,1,29,4,11,6,10,-4,
-    4, -3, 1,30,1,29,4,12,6,10,-4,
-    -3, 1,30,1,29,4,12,6,10,1,44,-4,
-    -2,
-  -1
-}; 
-
-#define MAX_OPENING_SEQUENCE 14
-
-#define MAX_PATTERN 128
diff --git a/misc/gnuminishogi.pat b/misc/gnuminishogi.pat
new file mode 100644 (file)
index 0000000..946febc
--- /dev/null
@@ -0,0 +1,5 @@
+# This is a dummy file for lack of documentation.  The single entry is
+# just here to keep the code happy, it is even not guaranted to be
+# smart,or even to be in a section with the right name.
+ATTACK_U_U
+[0]          b1d