Curses: fix inverted column numbers display for minishogi.
[gnushogi.git] / gnushogi / main.c
blob7551512c497e67125826f5fdb08c6ec5ced6b8ad
1 /*
2 * FILE: main.c
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
24 * for more details.
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 * ----------------------------------------------------------------------
33 #include "gnushogi.h"
35 #include <signal.h>
38 void print_arglist(int argc, char **argv)
40 int i;
42 for (i = 0; i < argc; i++)
43 printf("argv[%d] = %s\n", i, argv[i]);
45 printf("\n");
49 int
50 main (int argc, char **argv)
53 * Process command-line arguments.
56 /* Get rid of the program name. */
58 argc--;
59 argv++;
61 /* CHECKME: get rid of the '+' syntax? */
63 while ((argc > 0) && ((argv[0][0] == '-') || (argv[0][0] == '+')))
65 switch (argv[0][1])
67 case 'a':
68 /* Need the "+" syntax here... */
69 ahead = ((argv[0][0] == '-') ? false : true);
70 break;
73 case 'b':
74 argc--;
75 argv++;
77 if (argc > 0)
79 bookfile = argv[0];
80 #ifdef BINBOOK
81 binbookfile = NULL;
82 #endif
85 break;
87 #ifdef BINBOOK
88 case 'B':
89 argc--;
90 argv++;
92 if (argc > 0)
93 binbookfile = argv[0];
95 break;
96 #endif
98 #ifdef HAVE_LIBCURSES
99 case 'C':
100 /* Curses interface. */
101 display_type = DISPLAY_CURSES;
103 break;
104 #endif
106 case 'h':
107 /* Need the "+" syntax here... */
108 hash = ((argv[0][0] == '-') ? false : true);
109 break;
112 case 'l':
113 argc--;
114 argv++;
116 if (argc > 0)
117 Lang = argv[0];
119 break;
122 case 'L':
123 argc--;
124 argv++;
126 if (argc > 0)
127 strcpy(listfile, argv[0]);
128 break;
131 case 's':
132 argc--;
133 argv++;
135 if (argc > 0)
136 strcpy(savefile, argv[0]);
138 break;
141 case 'P':
142 argc--;
143 argv++;
145 if (argc > 0)
146 bookmaxply = atoi(argv[0]);
148 break;
151 case 'R':
152 /* Raw text interface. */
153 display_type = DISPLAY_RAW;
155 break;
158 case 'S':
159 argc--;
160 argv++;
162 if (argc > 0)
163 booksize = atoi(argv[0]);
164 break;
166 #if ttblsz
167 case 'r':
168 argc--;
169 argv++;
171 if (argc > 0)
172 rehash = atoi(argv[0]);
174 if (rehash > MAXrehash)
175 rehash = MAXrehash;
177 break;
180 case 'T':
181 argc--;
182 argv++;
184 if (argc > 0)
185 ttblsize = atoi(argv[0]);
187 if ((ttblsize <= MINTTABLE))
188 ttblsize = (MINTTABLE) + 1;
190 break;
192 #ifdef HASHFILE
193 case 'c': /* Create or test persistent transposition table. */
194 argc--;
195 argv++;
197 if (argc > 0)
198 filesz = atoi(argv[0]);
199 else
200 filesz = vfilesz;
202 if ((filesz > 0) && (filesz < 24))
203 filesz = (1 << filesz) - 1 + MAXrehash;
204 else
205 filesz = filesz + MAXrehash;
207 if ((hashfile = fopen(HASHFILE, RWA_ACC)) == NULL)
208 hashfile = fopen(HASHFILE, WA_ACC);
210 if (hashfile != NULL)
212 long j;
213 struct fileentry n;
215 fputs("Filling transposition file, wait!\n", stdout);
216 n.f = n.t = 0;
217 n.flags = 0;
218 n.depth = 0;
219 n.sh = n.sl = 0;
221 for (j = 0; j < filesz + 1; j++)
222 fwrite(&n, sizeof(struct fileentry), 1, hashfile);
224 fclose(hashfile);
226 else
228 printf("Create failed for %s\n", HASHFILE);
231 return 0;
234 case 't': /* Create or test persistent transposition table. */
235 hashfile = fopen(HASHFILE, RWA_ACC);
237 if (hashfile)
239 fseek(hashfile, 0L, SEEK_END);
240 filesz = (ftell(hashfile) / (sizeof(struct fileentry))) - 1;
243 if (hashfile != NULL)
245 long i, j;
246 int nr[MAXDEPTH];
247 struct fileentry n;
249 fputs("Counting transposition file entries, wait!\n", stdout);
251 for (i = 0; i < MAXDEPTH; i++)
252 nr[i] = 0;
254 fseek(hashfile, 0L, SEEK_END);
255 i = ftell(hashfile) / (sizeof(struct fileentry));
256 fseek(hashfile, 0L, SEEK_SET);
258 for (j = 0; j < i + 1; j++)
260 fread(&n, sizeof(struct fileentry), 1, hashfile);
262 if (n.depth > MAXDEPTH)
264 printf("ERROR\n");
265 exit(1);
268 if (n.depth)
270 nr[n.depth]++;
271 nr[0]++;
275 printf("The file contains %d entries out of max %d\n", nr[0], i);
277 for (j = 1; j < MAXDEPTH; j++)
278 printf("%d ", nr[j]);
280 printf("\n");
283 return 0;
286 #endif /* HASHFILE */
287 #endif /* ttblsz */
289 case 'v':
290 fprintf(stderr, "gnushogi version %s\n", PACKAGE_VERSION);
291 exit(1);
294 case 'X':
295 /* X interface. */
296 display_type = DISPLAY_X;
298 break;
301 case 'x':
302 argc--;
303 argv++;
305 if (argc > 0)
306 xwin = argv[0];
308 break;
311 default:
312 fputs("Usage: gnushogi [-a] [-t] [-c size] [-s savefile][-l listfile] [-x xwndw]\n", stderr);
313 exit(1);
316 argc--;
317 argv++;
320 if (argc == 2)
322 char *p;
324 MaxResponseTime = 100L * strtol(argv[1], &p, 10);
326 if (*p == ':')
328 MaxResponseTime = 60L * MaxResponseTime +
329 100L * strtol(++p, (char **) NULL, 10);
332 TCflag = false;
333 TCmoves = 0;
334 TCminutes = 0;
335 TCseconds = 0;
338 if (argc >= 3)
340 char *p;
342 if (argc > 9)
344 printf("Time Control Error\n");
345 exit(1);
348 TCmoves = atoi(argv[1]);
349 TCminutes = (short)strtol(argv[2], &p, 10);
351 if (*p == ':')
352 TCseconds = (short)strtol(p + 1, (char **) NULL, 10);
353 else
354 TCseconds = 0;
356 TCflag = true;
357 argc -= 3;
358 argv += 3;
360 while (argc > 1)
362 XCmoves[XC] = atoi(argv[0]);
363 XCminutes[XC] = (short)strtol(argv[1], &p, 10);
365 if (*p == ':')
366 XCseconds[XC] = (short)strtol(p + 1, (char **) NULL, 10);
367 else
368 XCseconds[XC] = 0;
370 if (XCmoves[XC] && (XCminutes[XC] || XCseconds[XC]))
371 XC++;
372 else
374 printf("Time Control Error\n");
375 exit(1);
378 argc -= 2;
379 argv += 2;
382 if (argc)
385 * If we got here, there are unknown arguments, so issue
386 * an error message and quit.
389 printf("Invalid command-line arguments:\n");
390 print_arglist(argc, argv);
391 exit(1);
395 if (InitMain() != 0)
396 exit(1);
398 while (!flag.quit)
400 oppptr = (oppptr + 1) % MINGAMEIN;
402 if (flag.bothsides && !flag.mate)
403 SelectMove(opponent, FOREGROUND_MODE);
404 else
405 InputCommand(NULL);
407 if (opponent == white)
409 if (flag.gamein || TCadd)
411 TimeCalc();
413 else if (TimeControl.moves[opponent] == 0)
415 if (XC)
417 if (XCmore < XC)
419 TCmoves = XCmoves[XCmore];
420 TCminutes = XCminutes[XCmore];
421 TCseconds = XCseconds[XCmore];
422 XCmore++;
426 SetTimeControl();
430 compptr = (compptr + 1) % MINGAMEIN;
432 if (!(flag.quit || flag.mate || flag.force))
434 #ifdef INTERRUPT_TEST
435 printf("starting search...\n");
436 #endif
437 SelectMove(computer, FOREGROUND_MODE);
439 if (computer == white)
441 if (flag.gamein)
443 TimeCalc();
445 else if (TimeControl.moves[computer] == 0)
447 if (XC)
449 if (XCmore < XC)
451 TCmoves = XCmoves[XCmore];
452 TCminutes = XCminutes[XCmore];
453 TCseconds = XCseconds[XCmore];
454 XCmore++;
458 SetTimeControl();
464 ExitMain();
466 return 0;