4 * ----------------------------------------------------------------------
5 * Copyright (c) 1993, 1994, 1995 Matthias Mutz
6 * Copyright (c) 1999 Michael Vanier and the Free Software Foundation
7 * Copyright (c) 2008, 2013, 2014 Yann Dirson and the Free Software Foundation
9 * GNU SHOGI is based on GNU CHESS
11 * Copyright (c) 1988, 1989, 1990 John Stanback
12 * Copyright (c) 1992 Free Software Foundation
14 * This file is part of GNU SHOGI.
16 * GNU Shogi is free software; you can redistribute it and/or modify it
17 * under the terms of the GNU General Public License as published by the
18 * Free Software Foundation; either version 3 of the License,
19 * or (at your option) any later version.
21 * GNU Shogi is distributed in the hope that it will be useful, but WITHOUT
22 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
23 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26 * You should have received a copy of the GNU General Public License along
27 * with GNU Shogi; see the file COPYING. If not, see
28 * <http://www.gnu.org/licenses/>.
29 * ----------------------------------------------------------------------
38 /* Declarations of read(), write(), close(), and lseek(). */
48 unsigned booksize
= BOOKSIZE
;
49 unsigned short bookmaxply
= BOOKMAXPLY
;
50 unsigned bookcount
= 0;
53 char *bookfile
= BOOK
;
55 char *bookfile
= NULL
;
59 char *binbookfile
= BINBOOK
;
61 char *binbookfile
= NULL
;
64 static char bmvstr
[3][7];
67 static ULONG bhashkey
;
73 * Generate move strings in different formats.
77 Balgbr(short f
, short t
, short flag
)
79 short promoted
= false;
90 piece
= f
- NO_SQUARES
;
92 if (f
> (NO_SQUARES
+ NO_PIECES
))
95 flag
= (dropmask
| piece
);
104 if ((f
== t
) && ((f
!= 0) || (t
!= 0)))
107 * error in algbr: FROM=TO=t
110 bmvstr
[0][0] = bmvstr
[1][0] = bmvstr
[2][0] = '\0';
114 if ((flag
& dropmask
) != 0)
116 /* bmvstr[0]: P*3c bmvstr[1]: P'3c */
117 short piece
= flag
& pmask
;
118 bmvstr
[0][0] = pxx
[piece
];
120 bmvstr
[0][2] = cxx
[column(t
)];
121 bmvstr
[0][3] = rxx
[row(t
)];
122 bmvstr
[0][4] = bmvstr
[2][0] = '\0';
123 strcpy(bmvstr
[1], bmvstr
[0]);
128 if ((f
!= 0) || (t
!= 0))
130 /* algebraic notation */
131 /* bmvstr[0]: 7g7f bmvstr[1]:
132 * (+)P7g7f(+) bmvstr[2]: (+)P7f(+) */
133 bmvstr
[0][0] = cxx
[column(f
)];
134 bmvstr
[0][1] = rxx
[row(f
)];
135 bmvstr
[0][2] = cxx
[column(t
)];
136 bmvstr
[0][3] = rxx
[row(t
)];
141 bmvstr
[1][0] = bmvstr
[2][0] = '+';
142 bmvstr
[1][1] = bmvstr
[2][1] = pxx
[board
[f
]];
143 strcpy(&bmvstr
[1][2], &bmvstr
[0][0]);
144 strcpy(&bmvstr
[2][2], &bmvstr
[0][2]);
148 bmvstr
[1][0] = bmvstr
[2][0] = pxx
[board
[f
]];
149 strcpy(&bmvstr
[1][1], &bmvstr
[0][0]);
150 strcpy(&bmvstr
[2][1], &bmvstr
[0][2]);
155 strcat(bmvstr
[0], "+");
156 strcat(bmvstr
[1], "+");
157 strcat(bmvstr
[2], "+");
162 bmvstr
[0][0] = bmvstr
[1][0] = bmvstr
[2][0] = '\0';
173 bkdisplay(char *s
, int cnt
, int moveno
)
180 printf("matches = %d\n", cnt
);
181 printf("inout move is :%s: move number %d side %s\n",
182 s
, moveno
/ 2 + 1, (moveno
& 1) ? "white" : "black");
184 #ifndef SEMIQUIETBOOKGEN
185 printf("legal moves are \n");
187 while (pnt
< TrPnt
[3])
191 if (is_promoted
[board
[node
->f
]] )
192 Balgbr(node
->f
| 0x80, node
->t
, (short) node
->flags
);
194 Balgbr(node
->f
, node
->t
, (short) node
->flags
);
197 bmvstr
[0], bmvstr
[1], bmvstr
[2]);
200 printf("\n current board is\n");
202 for (r
= (NO_ROWS
- 1); r
>= 0; r
--)
204 for (c
= 0; c
<= (NO_COLS
- 1); c
++)
209 pc
= (is_promoted
[board
[l
]] ? '+' : ' ');
211 if (color
[l
] == neutral
)
213 else if (color
[l
] == black
)
214 printf("%c%c", pc
, qxx
[board
[l
]]);
216 printf("%c%c", pc
, pxx
[board
[l
]]);
226 for (color
= black
; color
<= white
; color
++)
230 printf((color
== black
) ? "black " : "white ");
232 for (piece
= pawn
; piece
<= king
; piece
++)
234 if ((c
= Captured
[color
][piece
]))
235 printf("%i%c ", c
, pxx
[piece
]);
241 #endif /* SEMIQUIETBOOKGEN */
244 #endif /* QUIETBOOKGEN */
249 * BVerifyMove(s, mv, moveno)
251 * Compare the string 's' to the list of legal moves available for the
252 * opponent. If a match is found, make the move on the board.
256 BVerifyMove(char *s
, unsigned short *mv
, int moveno
)
258 static short pnt
, tempb
, tempc
, tempsf
, tempst
, cnt
;
259 static struct leaf xnode
;
264 MoveList(opponent
, 2, -2, true);
267 while (pnt
< TrPnt
[3])
271 if (is_promoted
[board
[node
->f
]] )
272 Balgbr(node
->f
| 0x80, node
->t
, (short) node
->flags
);
274 Balgbr(node
->f
, node
->t
, (short) node
->flags
);
276 if (strcmp(s
, bmvstr
[0]) == 0 || strcmp(s
, bmvstr
[1]) == 0 ||
277 strcmp(s
, bmvstr
[2]) == 0)
288 MakeMove(opponent
, &xnode
, &tempb
,
289 &tempc
, &tempsf
, &tempst
, &INCscore
);
291 if (SqAttacked(PieceList
[opponent
][0], computer
, &blockable
))
293 UnmakeMove(opponent
, &xnode
, &tempb
, &tempc
, &tempsf
, &tempst
);
294 /* Illegal move in check */
295 #if !defined QUIETBOOKGEN
296 puts("Illegal move (in check) %s");
297 bkdisplay(s
, cnt
, moveno
);
303 *mv
= (xnode
.f
<< 8) | xnode
.t
;
305 if (is_promoted
[board
[xnode
.t
]] )
306 Balgbr(xnode
.f
| 0x80, xnode
.t
, false);
308 Balgbr(xnode
.f
, xnode
.t
, false);
315 #if !defined QUIETBOOKGEN
316 printf("Illegal move (no match) %s\n", s
);
317 bkdisplay(s
, cnt
, moveno
);
328 * Reset the board and other variables to start a new game.
337 flag
.illegal
= flag
.mate
= flag
.post
= flag
.quit
338 = flag
.reverse
= flag
.bothsides
= flag
.onemove
= flag
.force
341 flag
.material
= flag
.coords
= flag
.hash
= flag
.easy
342 = flag
.beep
= flag
.rcptr
345 flag
.stars
= flag
.shade
= flag
.back
= flag
.musttimeout
= false;
349 CptrFlag
[0] = TesujiFlag
[0] = false;
353 for (l
= 0; l
< NO_SQUARES
; l
++)
355 board
[l
] = Stboard
[l
];
356 color
[l
] = Stcolor
[l
];
362 hashbd
= hashkey
= 0;
369 Vparse (FILE * fd
, USHORT
*mv
, USHORT
*flags
, USHORT side
, int moveno
)
378 while (((c
= getc(fd
)) == ' ')
379 || (c
== '!') || (c
== '/') || (c
== '\n'));
383 /* amount of time spent for the last move */
384 while (((c
= getc(fd
)) != ')') && (c
!= EOF
));
388 while (((c
= getc(fd
)) == ' ') || (c
== '\n'));
394 /* comment for the actual game */
395 while (((c
= getc(fd
))) != ']' && (c
!= EOF
));
399 while (((c
= getc(fd
))) == ' ' || (c
== '\n'));
415 /* goes to end of line */
428 while ((c
>= '0') && (c
<= '9'))
436 while (((c
= getc(fd
)) == ' ') || (c
== '.') || (c
== '\n'));
440 while (((c
= getc(fd
)) != '?') && (c
!= '!') && (c
!= ' ')
441 && (c
!= '(') && (c
!= '\n') && (c
!= '\t') && (c
!= EOF
))
446 if ((c
!= 'x') && (c
!= '-') && (c
!= ',')
447 && (c
!= ';') && (c
!= '='))
457 while (((c
= getc(fd
)) != ')') && (c
!= EOF
));
468 while ((c
!= '\n') && (c
!= EOF
))
477 if (strcmp(s
, "draw") == 0)
479 else if (strcmp(s
, "1-0") == 0)
481 else if (strcmp(s
, "0-1") == 0)
483 else if (strcmp(s
, "Resigns") == 0)
485 else if (strcmp(s
, "Resigns.") == 0)
487 else if (strcmp(s
, "Sennichite") == 0)
489 else if (strcmp(s
, "Sennichite.") == 0)
491 else if (strcmp(s
, "Jishogi") == 0)
493 else if (strcmp(s
, "Jishogi.") == 0)
499 i
= BVerifyMove(s
, mv
, moveno
);
503 /* Bad move, not for the program to play */
504 *flags
|= BADMOVE
; /* Flag it ! */
505 while (((c
= getc(fd
)) == '?') || (c
== '!') || (c
== '/'));
510 /* Do not use by computer */
511 *flags
|= BADMOVE
; /* Flag it ! */
513 while (((c
= getc(fd
)) == '?') || (c
== '!') || (c
== '/'));
519 *flags
|= GOODMOVE
; /* Flag it ! */
521 while (((c
= getc(fd
)) == '?') || (c
== '!') || (c
== '/'));
529 while (((c
= getc(fd
)) != ')') && (c
!= EOF
));
533 /* flush to start of next */
534 while (((c
= getc(fd
)) != '#') && (c
!= EOF
));
552 static struct gdxadmin ADMIN
;
554 static struct gdxdata DATA
;
556 /* lts(l) returns most significant 16 bits of l */
558 #if SIZEOF_LONG == 8 /* 64-bit long i.e. 8 bytes */
559 # define lts(x) (USHORT)(((x >> 48) & 0xfffe) | side)
561 # if defined USE_LTSIMP
562 static USHORT
ltsimp(long x
)
565 n
= (((x
>> 16) & 0xfffe));
568 # define lts(x) (USHORT)(ltsimp(x) | side)
570 # define lts(x) (USHORT)(((x >> 16)&0xfffe) | side)
575 /* #define HashValue(l) lts(l) */
576 #define HashValue(l) (USHORT)(l & 0xffff)
580 static ULONG currentoffset
;
583 #define MAXOFFSET(B) ((B.booksize - 1) * sizeof_gdxdata + sizeof_gdxadmin)
585 #define HashOffset(hashkey, B) \
587 currentoffset = ((ULONG)hashkey % B.booksize) \
588 * sizeof_gdxdata + sizeof_gdxadmin; \
592 #define NextOffset(B) \
594 currentoffset += sizeof_gdxdata; \
595 if (currentoffset > B.maxoffset) \
596 currentoffset = sizeof_gdxadmin; \
600 #define WriteAdmin() \
603 write(gfd, (char *)&ADMIN, sizeof_gdxadmin); \
606 #define WriteData() \
610 lseek(gfd, currentoffset, 0); \
611 write(gfd, (char *)&DATA, sizeof_gdxdata); \
616 static int ReadAdmin(void)
619 return (sizeof_gdxadmin
== read(gfd
, (char *)&ADMIN
, sizeof_gdxadmin
));
622 static int ReadData(struct gdxdata
*DATA
)
624 lseek(gfd
, currentoffset
, 0);
625 return (sizeof_gdxdata
== read(gfd
, (char *)DATA
, sizeof_gdxdata
));
632 * CHECKME: is this still valid esp. wrt gnushogi.book?
634 * Read in the Opening Book file and parse the algebraic notation for a move
635 * into an unsigned integer format indicating the from and to square. Create
636 * a linked list of opening lines of play, with entry->next pointing to the
637 * next line and entry->move pointing to a chunk of memory containing the
638 * moves. More Opening lines of up to 100 half moves may be added to
639 * gnushogi.book. But now it's a hashed table by position which yields a move
640 * or moves for each position. It no longer knows about openings per se only
641 * positions and recommended moves in those positions.
649 int mustwrite
= false, first
;
650 unsigned short xside
, side
;
654 unsigned int games
= 0;
660 if ((fd
= fopen(bookfile
, "r")) == NULL
)
661 fd
= fopen("gnushogi.tbk", "r");
665 /* yes add to book */
666 /* open book as writer */
667 gfd
= open(binbookfile
, O_RDONLY
| O_BINARY
);
673 B
.bookcount
= ADMIN
.bookcount
;
674 B
.booksize
= ADMIN
.booksize
;
675 B
.maxoffset
= ADMIN
.maxoffset
;
677 if (B
.booksize
&& !(B
.maxoffset
== MAXOFFSET(B
)))
679 printf("bad format %s\n", binbookfile
);
685 printf("bad format %s\n", binbookfile
);
689 gfd
= open(binbookfile
, O_RDWR
| O_BINARY
);
694 gfd
= open(binbookfile
, O_RDWR
| O_CREAT
| O_BINARY
, 0644);
696 ADMIN
.bookcount
= B
.bookcount
= 0;
697 ADMIN
.booksize
= B
.booksize
= booksize
;
698 B
.maxoffset
= ADMIN
.maxoffset
= MAXOFFSET(B
);
705 write(gfd
, (char *)&ADMIN
, sizeof_gdxadmin
);
706 printf("creating bookfile %s %ld %ld\n",
707 binbookfile
, B
.maxoffset
, B
.booksize
);
709 for (x
= 0; x
< B
.booksize
; x
++)
711 write(gfd
, (char *)&DATA
, sizeof_gdxdata
);
717 /* setvbuf(fd, buffr, _IOFBF, 2048); */
720 hashbd
= hashkey
= 0;
723 while ((c
= Vparse(fd
, &mv
, &flags
, side
, i
)) >= 0)
728 * If this is not the first move of an opening and
729 * if it's the first time we have seen it then
730 * save the next move as a hint.
734 if (i
< bookmaxply
+ 2)
736 if (i
> 1 && !(flags
& BADMOVE
))
739 if (i
< bookmaxply
+ 1)
742 * See if this position and move already
743 * exist from some other opening.
747 HashOffset(bhashkey
, B
);
752 if (!ReadData(&DATA
))
753 break; /* corrupted binbook file */
756 break; /* free entry */
758 if (DATA
.hashkey
== HashValue(bhashkey
)
759 && DATA
.hashbd
== bhashbd
)
761 if (DATA
.bmove
== mv
)
764 * Yes, so just bump count - count
765 * is used to choose the opening
766 * move in proportion to its
767 * presence in the book.
780 if (DATA
.flags
& LASTMOVE
)
782 DATA
.flags
&= (~LASTMOVE
);
794 * Doesn't exist so add it to the book.
801 if ((B
.bookcount
% 1000) == 0)
803 /* CHECKME: may want to get rid of this,
804 * especially for xshogi. */
805 printf("%ld rec %d openings "
810 /* initialize a record */
811 DATA
.hashbd
= bhashbd
;
812 DATA
.hashkey
= HashValue(bhashkey
);
814 DATA
.flags
= flags
| LASTMOVE
;
823 opponent
= computer
^ 1;
830 /* reset for next opening */
843 /* write admin rec with counts */
844 ADMIN
.bookcount
= B
.bookcount
;
851 if (binbookfile
!= NULL
)
853 /* open book as reader */
854 gfd
= open(binbookfile
, O_RDONLY
| O_BINARY
);
858 if (ReadAdmin() && (!ADMIN
.booksize
859 || (ADMIN
.maxoffset
== MAXOFFSET(ADMIN
))))
861 B
.bookcount
= ADMIN
.bookcount
;
862 B
.booksize
= ADMIN
.booksize
;
863 B
.maxoffset
= ADMIN
.maxoffset
;
867 printf("bad format %s\n", binbookfile
);
875 B
.booksize
= booksize
;
879 sprintf(msg
, "Book used %d(%d).", B
.bookcount
, B
.booksize
);
883 /* Set everything back to start the game. */
887 /* Now get ready to play .*/
890 ShowMessage("Can't find book.");
898 * OpeningBook(hint, side)
900 * Go through each of the opening lines of play and check for a match with
901 * the current game listing. If a match occurs, generate a random
902 * number. If this number is the largest generated so far then the next
903 * move in this line becomes the current "candidate". After all lines are
904 * checked, the candidate move is put at the top of the Tree[] array and
905 * will be played by the program. Note that the program does not handle
906 * book transpositions.
910 OpeningBook(unsigned short *hint
, short side
)
913 int possibles
= TrPnt
[2] - TrPnt
[1];
915 gsrand((unsigned int) time((long *) 0));
919 * Find all the moves for this position - count them and get their
928 struct gdxdata OBB
[128];
930 if (B
.bookcount
== 0)
937 HashOffset(hashkey
, B
);
939 printf("looking for book move, bhashbd = 0x%lx bhashkey = 0x%x\n",
940 (ULONG
)hashbd
, HashValue(hashkey
));
944 if (!ReadData(&OBB
[x
]))
947 if (OBB
[x
].bmove
== 0)
951 printf("compare with bhashbd = 0x%lx bhashkey = 0x%x\n",
952 OBB
[x
].hashbd
, OBB
[x
].hashkey
);
954 if ((OBB
[x
].hashkey
== HashValue(hashkey
))
955 && (OBB
[x
].hashbd
== (ULONG
)hashbd
))
959 if (OBB
[x
-1].flags
& LASTMOVE
)
967 printf("%d book move(s) found.\n", x
);
976 for (i
= 0; i
< x
; i
++)
978 if (OBB
[i
].flags
& BADMOVE
)
982 /* Is the move in the MoveList? */
983 for (b
= TrPnt
[1]; b
< (unsigned) TrPnt
[2]; b
++)
985 if (((Tree
[b
].f
<< 8) | Tree
[b
].t
) == m
)
988 Tree
[b
].score
= DONTUSE
;
997 movealgbr(m
= OBB
[i
].bmove
, s
);
998 printf("finding book move: %s\n", s
);
1000 summ
+= OBB
[i
].count
;
1010 r
= (urand() % summ
);
1012 for (i
= 0; i
< x
; i
++)
1014 if (!(OBB
[i
].flags
& BADMOVE
))
1016 if (r
< OBB
[i
].count
)
1031 /* Make sure the move is in the MoveList. */
1032 for (b
= TrPnt
[1]; b
< (unsigned) TrPnt
[2]; b
++)
1034 if (((Tree
[b
].f
<< 8) | Tree
[b
].t
) == m
)
1036 Tree
[b
].flags
|= book
;
1042 /* Make sure it's the best. */
1044 pick(TrPnt
[1], TrPnt
[2] - 1);
1046 if (Tree
[TrPnt
[1]].score
)
1053 /* Ok, pick up the hint and go. */