f9cb6dcf25da8873e9d01d382d10958674f314ed
[gnushogi.git] / src / main.c
1 /*
2  * main.c - C source for GNU SHOGI based on GNU CHESS
3  *
4  * Copyright (c) 1988,1989,1990 John Stanback (GNU Chess)
5  * Copyright (c) 1992 Free Software Foundation 
6  * Copyright (c) 1993,1994,1995 Matthias Mutz (GNU Shogi)
7  *
8  * This file is part of GNU SHOGI.
9  *
10  * GNU Shogi is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 1, or (at your option)
13  * any later version.
14  *
15  * GNU Shogi is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with GNU Shogi; see the file COPYING.  If not, write to
22  * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25
26 #include "version.h"
27 #include "gnushogi.h"
28
29 #include <signal.h>
30
31
32 #if defined THINK_C
33 #include <console.h>
34 #include <time.h>
35 #endif
36
37
38
39
40 #ifdef DEBUG_INITS
41
42 /* print all possible moves for all pieces from all squares */
43
44 void DebugInits ()
45 {                    
46    register short u, sq;
47    register unsigned char *ppos, *pdir;
48    char s[10];
49    short piece, ptyp;
50
51    for ( piece = 0; piece < NO_PIECES; piece++ ) {
52      printf("move list for piece %i\n",piece);
53      for ( sq = 0; sq < NO_SQUARES; sq++ ) {
54        printf("  from square %i to ",sq);
55        ptyp = ptype[black][piece];
56        ppos = (*nextpos)[ptyp][sq];
57        u = ppos[sq]; 
58        do {
59           printf("%i",u);
60           u = ppos[u];
61           if (u != sq) printf(", ");
62        } while (u != sq); 
63        printf("\n");
64      };
65      /* pdir = (*nextdir)[ptyp][sq]; */
66      printf("\n");
67      scanf("%s",s);
68      if ( strcmp(s,"exit") == 0 )
69        exit(0);
70    };
71 }
72
73 #endif  
74
75
76
77 int
78 main (int argc, char **argv)
79 {
80
81 #ifdef THINK_C
82   console_options.ncols = 100;
83   cshow(stdout);
84 #ifdef NONDSP
85   ccommand(&argv);
86 #endif
87 #endif    
88
89   if (argc > 2)
90     {
91       if (argv[1][0] == '-' && argv[1][1] == 'L')
92         {
93           Lang = argv[2];
94           argv += 2;
95           argc -= 2;
96         }
97     }
98     
99   while (argc > 1 && ((argv[1][0] == '-') || (argv[1][0] == '+')))
100     {
101       switch (argv[1][1])
102         {
103         case 'a':
104           ahead = ((argv[1][0] == '-') ? false : true);
105           break;
106         case 'b':
107           argv++;
108           argc--;
109           if (argc > 1)
110             {
111               bookfile = argv[1];
112 #ifdef BINBOOK
113               binbookfile = NULL;
114 #endif
115             }
116           break;
117 #ifdef BINBOOK
118         case 'B':
119           argv++;
120           argc--;
121           if (argc > 1)
122             binbookfile = argv[1];
123           break;
124 #endif
125         case 'h':
126           hash = ((argv[1][0] == '-') ? false : true);
127           break;
128         case 's':
129           argc--;
130           argv++;
131           if (argc > 1)
132             strcpy (savefile, argv[1]);
133           break; 
134         case 'l':
135           argc--;
136           argv++;
137           if (argc > 1)
138             strcpy (listfile, argv[1]);
139           break;
140         case 'S':
141           argc--;
142           argv++;
143           if(argc > 1)booksize = atoi(argv[1]);
144           break;
145         case 'P':
146           argc--;
147           argv++;
148           if(argc > 1)bookmaxply = atoi(argv[1]);
149           break;
150
151 #if ttblsz
152         case 'r':
153           if (argc > 2)
154             rehash = atoi (argv[2]);
155           argc--;
156           argv++;
157           if (rehash > MAXrehash)
158             rehash = MAXrehash;
159           break;
160         case 'T':
161           if (argc > 2)
162             ttblsize = atoi (argv[2]);
163           argc--;
164           argv++;
165           if ((ttblsize <= MINTTABLE)) ttblsize = (MINTTABLE)+1;
166           break;
167 #ifdef HASHFILE
168         case 't':       /* create or test persistent transposition
169                                  * table */
170           hashfile = fopen (HASHFILE, RWA_ACC);
171           if (hashfile)
172             {
173               fseek (hashfile, 0L, SEEK_END);
174               filesz = (ftell (hashfile) / sizeof (struct fileentry)) - 1;
175             }
176           if (hashfile != NULL)
177             {
178               long i, j;
179               int nr[MAXDEPTH];
180               struct fileentry n;
181
182               printf (CP[49]);
183               for (i = 0; i < MAXDEPTH; i++)
184                 nr[i] = 0;
185               fseek (hashfile, 0L, SEEK_END);
186               i = ftell (hashfile) / sizeof (struct fileentry);
187               fseek (hashfile, 0L, SEEK_SET);
188               for (j = 0; j < i + 1; j++)
189                 {
190                   fread (&n, sizeof (struct fileentry), 1, hashfile);
191 if(n.depth >MAXDEPTH) {printf("ERROR\n");exit(1);}
192                   if (n.depth)
193                     {
194                       nr[n.depth]++;
195                       nr[0]++;
196                     }
197                 }
198               printf (CP[109],
199                       nr[0], i);
200               for (j = 1; j < MAXDEPTH; j++)
201                 printf ("%d ", nr[j]);
202               printf ("\n");
203             }
204           return 0;
205         case 'c':               /* create or test persistent transposition
206                                  * table */
207           if (argc > 2)
208             filesz = atoi (argv[2]);
209           else
210             filesz = vfilesz;
211           if (filesz > 0 && filesz < 24)
212             filesz = (1 << filesz) - 1 + MAXrehash;
213           else
214             filesz = filesz + MAXrehash;
215 #ifdef HASHFILE
216           if ((hashfile = fopen (HASHFILE, RWA_ACC)) == NULL)
217             hashfile = fopen (HASHFILE, WA_ACC);
218           if (hashfile != NULL)
219             {
220               long j;
221               struct fileentry n;
222
223               printf (CP[66]);
224               n.f = n.t = 0;
225               n.flags = 0;
226               n.depth = 0;
227               n.sh = n.sl = 0;
228               for (j = 0; j < filesz + 1; j++)
229                 fwrite (&n, sizeof (struct fileentry), 1, hashfile);
230               fclose (hashfile);
231             }
232           else
233             printf (CP[50], HASHFILE);
234 #endif
235           return (0);
236 #endif /* HASHFILE */
237 #endif /* ttblsz */
238         case 'x':
239           xwin = &argv[1][2];
240           break;
241         case 'v':
242           fprintf (stderr, CP[102], version, patchlevel);
243           exit (1);
244         default:
245           fprintf (stderr, CP[113]);
246           exit (1);
247         }
248       argv++;
249       argc--;
250     }
251     
252   if (argc == 2)
253     {
254       char *p;
255       
256       MaxResponseTime = 100L * strtol (argv[1], &p, 10);
257       if (*p == ':')
258         MaxResponseTime = 60L * MaxResponseTime +
259           100L * strtol (++p, (char **) NULL, 10);
260       TCflag = false;
261       TCmoves = 0;
262       TCminutes = 0;
263       TCseconds = 0;
264     }
265     
266   if (argc >= 3)
267     {
268       char *p;
269       if (argc > 9)
270         {
271           printf ("%s\n", CP[220]);
272           exit (1);
273         }
274       TCmoves = atoi (argv[1]);
275       TCminutes = (short)strtol (argv[2], &p, 10);
276       if (*p == ':')
277         TCseconds = (short)strtol (p + 1, (char **) NULL, 10);
278       else
279         TCseconds = 0;
280       TCflag = true;
281       argc -= 3;
282       argv += 3;
283       while (argc > 1)
284         {
285           XCmoves[XC] = atoi (argv[0]);
286           XCminutes[XC] = (short)strtol (argv[1], &p, 10);
287           if (*p == ':')
288             XCseconds[XC] = (short)strtol (p + 1, (char **) NULL, 10);
289           else
290             XCseconds[XC] = 0;
291           if (XCmoves[XC] && (XCminutes[XC] || XCseconds[XC]))
292             XC++;
293           else
294             {
295               printf (CP[220]);
296               exit (1);
297             }
298           argc -= 2;
299           argv += 2;
300         }
301       if (argc)
302         {
303           printf ("%s\n", CP[220]);
304           exit (1);
305         }
306     }
307     
308   if ( InitMain() != 0 )
309     exit(1);
310   
311 #ifdef DEBUG_INITS
312   DebugInits ();
313 #endif
314
315   while (!(flag.quit))
316     {
317       oppptr = (oppptr + 1) % MINGAMEIN;
318       if (flag.bothsides && !flag.mate) {
319         SelectMove (opponent, FOREGROUND_MODE);
320       } else
321         InputCommand (NULL);
322       if (opponent == white)
323         if (flag.gamein || TCadd)
324           {
325             TimeCalc ();
326           }
327         else if (TimeControl.moves[opponent] == 0)
328           {
329             if (XC)
330               if (XCmore < XC)
331                 {
332                   TCmoves = XCmoves[XCmore];
333                   TCminutes = XCminutes[XCmore];
334                   TCseconds = XCseconds[XCmore];
335                   XCmore++;
336                 }
337             SetTimeControl ();
338           }
339
340       compptr = (compptr + 1) % MINGAMEIN;
341       if (!(flag.quit || flag.mate || flag.force))
342         { 
343 #ifdef INTTERRUPT_TEST
344           printf("starting search...\n");
345 #endif
346           SelectMove (computer, FOREGROUND_MODE);
347           if (computer == white)
348             if (flag.gamein)
349               {
350                 TimeCalc ();
351               }
352             else if (TimeControl.moves[computer] == 0)
353               {
354                 if (XC)
355                   if (XCmore < XC)
356                     {
357                       TCmoves = XCmoves[XCmore];
358                       TCminutes = XCminutes[XCmore];
359                       TCseconds = XCseconds[XCmore];
360                       XCmore++;
361                     }
362                 SetTimeControl ();
363               }
364         }
365     }
366     
367   ExitMain ();
368   
369   return (0);
370 }
371
372