src/main.c

Mon, 16 Jun 2014 15:41:06 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 16 Jun 2014 15:41:06 +0200
changeset 51
84f2e380a434
parent 50
41017d0a72c5
child 52
26707039d5a6
permissions
-rw-r--r--

added support for game continuation over network + fixed major bug in checkmate anticipation when the king is attacked diagonally

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2014 Mike Becker. All rights reserved.
     5  *
     6  * Redistribution and use in source and binary forms, with or without
     7  * modification, are permitted provided that the following conditions are met:
     8  *
     9  *   1. Redistributions of source code must retain the above copyright
    10  *      notice, this list of conditions and the following disclaimer.
    11  *
    12  *   2. Redistributions in binary form must reproduce the above copyright
    13  *      notice, this list of conditions and the following disclaimer in the
    14  *      documentation and/or other materials provided with the distribution.
    15  *
    16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    17  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    19  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    20  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    21  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    22  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    23  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    24  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    25  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    26  * POSSIBILITY OF SUCH DAMAGE.
    27  *
    28  */
    30 #include "terminal-chess.h"
    31 #include "game.h"
    32 #include "input.h"
    33 #include "colors.h"
    34 #include <string.h>
    35 #include <time.h>
    36 #include <getopt.h>
    38 int get_settings(int argc, char **argv, Settings *settings) {
    39     char *valid;
    40     unsigned long int time, port;
    41     uint8_t timeunit = 60;
    42     size_t len;
    44     for (int opt ; (opt = getopt(argc, argv, "a:bc:hp:rsS:t:")) != -1 ;) {
    45         switch (opt) {
    46         case 'c':
    47             settings->continuepgn = optarg;
    48             break;
    49         case 'b':
    50             settings->gameinfo.servercolor = BLACK;
    51             break;
    52         case 'r':
    53             settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK;
    54             break;
    55         case 's':
    56             settings->singlemachine = 1;
    57             break;
    58         case 'S':
    59             settings->analyzepgn = optarg;
    60             break;
    61         case 't':
    62         case 'a':
    63             len = strlen(optarg);
    64             if (optarg[len-1] == 's') {
    65                 optarg[len-1] = '\0';
    66                 timeunit = 1;
    67             }
    69             if ((time = strtoul(optarg, &valid, 10))*timeunit > UINT16_MAX
    70                 || *valid != '\0') {
    71                 fprintf(stderr, "Specified time is invalid (%s)"
    72                     "- Maximum: 65535 seconds (1092 minutes)\n", optarg);
    73                 return 1;
    74             } else {
    75                 settings->gameinfo.timecontrol = 1;
    76                 if (opt=='t') {
    77                     settings->gameinfo.time = timeunit * time;
    78                 } else {
    79                     settings->gameinfo.addtime = time;
    80                 }
    81             }
    82             break;
    83         case 'p':
    84             port = strtol(optarg, &valid, 10);
    85             if (port < 1025 || port > 65535 || *valid != '\0') {
    86                 fprintf(stderr,
    87                     "Invalid port number (%s) - choose a number between "
    88                     "1025 and 65535\n",
    89                     optarg);
    90                 return 1;
    91             } else {
    92                 settings->port = optarg;
    93             }
    94             break;
    95         case 'h':
    96         case '?':
    97             settings->printhelp = 1;
    98             break;
    99         }
   100     }
   102     if (optind == argc - 1) {
   103         settings->serverhost = argv[optind];
   104     } else if (optind < argc - 1) {
   105         fprintf(stderr, "Too many arguments\n");
   106         return 1;
   107     }
   110     if (settings->continuepgn) {
   111         if (settings->serverhost) {
   112             fprintf(stderr, "Can't continue a game when joining a server.\n");
   113             return 1;
   114         }
   115         if (settings->analyzepgn) {
   116             fprintf(stderr, "The options -c and -S are mutually exclusive\n");
   117             return 1;
   118         }
   119     }
   121     return 0;
   122 }
   124 Settings default_settings() {
   125     Settings settings;
   126     memset(&settings, 0, sizeof(Settings));
   127     settings.gameinfo.servercolor = WHITE;
   128     settings.port = "27015";
   129     return settings;
   130 }
   132 void dump_gameinfo(GameInfo *gameinfo) {
   133     int serverwhite = gameinfo->servercolor == WHITE;
   134     attron(A_UNDERLINE);
   135     printw("Game details\n");
   136     attroff(A_UNDERLINE);
   137     printw("  Server:     %s\n  Client:     %s\n",
   138         serverwhite?"White":"Black", serverwhite?"Black":"White"
   139     );
   140     if (gameinfo->timecontrol) {
   141         if (gameinfo->time % 60) {
   142             printw("  Time limit: %ds + %ds\n",
   143                 gameinfo->time, gameinfo->addtime);
   144         } else {
   145             printw("  Time limit: %dm + %ds\n",
   146                 gameinfo->time/60, gameinfo->addtime);
   147         }
   148     } else {
   149         printw("  No time limit\n");
   150     }
   151     refresh();
   152 }
   154 void dump_moveinfo(GameState *gamestate) {
   155     int i = 1;
   156     for (MoveList *movelist = gamestate->movelist ;
   157         movelist ; movelist = movelist->next) {        
   158         if (++i % 2 == 0) {
   159             printw("%d. %s", i/2, movelist->move.string);
   160         } else {
   161             printw(" %s", movelist->move.string);
   162         }
   163         if (i % 20)  {
   164             addch(' ');
   165         } else {
   166             addch('\n');
   167         }
   168     }
   169     refresh();
   170 }
   172 void leavescr() {
   173     endwin();
   174 }
   176 int main(int argc, char **argv) {
   177     srand(time(NULL));
   179     Settings settings = default_settings();
   180     if (get_settings(argc, argv, &settings)) {
   181         return 1;
   182     }
   184     if (settings.printhelp) {
   185         printf(
   186 "Usage: terminal-chess [OPTION]... [HOST]\n"
   187 "Starts/joins a network chess game\n"
   188 "\nGeneral options\n"
   189 "  -c <PGN file> Continue the specified game\n"
   190 "  -h            This help page\n"
   191 "  -p            TCP port to use (default: 27015)\n"
   192 // TODO: implement and activate feature
   193 //"  -S <PGN file> Compute and print statistics for the specified game\n"
   194 "\nServer options\n"
   195 "  -a <time>     Specifies the time to add after each move\n"
   196 "  -b            Server plays black pieces (default: white)\n"
   197 "  -r            Distribute color randomly\n"
   198 "  -s            Single machine mode\n"
   199 "  -t <time>     Specifies time limit (default: no limit)\n"
   200 "\nNotes\n"
   201 "The time unit for -a is seconds and for -t minutes by default. To "
   202 "specify\nseconds for the -t option, use the s suffix.\n"
   203 "Example: -t 150s\n\n"
   204 "Use '-' for PGN files to read PGN data from standard input\n"
   205         );
   206         return EXIT_SUCCESS;
   207     }    
   208     initscr();
   209     halfdelay(1);
   210     keypad(stdscr, TRUE);
   211     if (has_colors()) {
   212         start_color();
   213         init_colorpairs();
   214         bkgd(COLOR_PAIR(COL_APP));
   215     } else {
   216         fprintf(stderr, "Non-colored terminals are not supported yet.");
   217         endwin();
   218         return EXIT_FAILURE;
   219     }
   220     atexit(leavescr);
   222     int exitcode;
   223     if (settings.singlemachine) {
   224         game_start_singlemachine(&settings);
   225         exitcode = EXIT_SUCCESS;
   226     } else if (settings.analyzepgn) {
   227         printw("Not implemented yet.\n");
   228         exitcode = EXIT_SUCCESS;
   229     } else {
   230         exitcode = is_server(&settings) ?
   231             server_run(&settings) : client_run(&settings);
   232     }
   234     mvaddstr(getmaxy(stdscr)-1, 0,
   235         "Game has ended. Press any key to leave...");
   236     clrtoeol();
   237     refresh();
   238     cbreak();
   239     flushinp();
   240     getch();
   242     return exitcode;
   243 }

mercurial