src/main.c

Mon, 16 Jun 2014 13:45:31 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 16 Jun 2014 13:45:31 +0200
changeset 50
41017d0a72c5
parent 47
d726e4b46c33
child 51
84f2e380a434
permissions
-rw-r--r--

added pgn parser and writer (without comment support yet) + minor refactorings

     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         // TODO: implement
   120         if (!settings->singlemachine) {
   121             fprintf(stderr, "Game continuation currently not supported for "
   122                 "network games.\n");
   123             return 1;
   124         }
   125     }
   127     return 0;
   128 }
   130 Settings default_settings() {
   131     Settings settings;
   132     memset(&settings, 0, sizeof(Settings));
   133     settings.gameinfo.servercolor = WHITE;
   134     settings.port = "27015";
   135     return settings;
   136 }
   138 void dump_gameinfo(GameInfo *gameinfo) {
   139     int serverwhite = gameinfo->servercolor == WHITE;
   140     attron(A_UNDERLINE);
   141     printw("Game details\n");
   142     attroff(A_UNDERLINE);
   143     printw("  Server:     %s\n  Client:     %s\n",
   144         serverwhite?"White":"Black", serverwhite?"Black":"White"
   145     );
   146     if (gameinfo->timecontrol) {
   147         if (gameinfo->time % 60) {
   148             printw("  Time limit: %ds + %ds\n",
   149                 gameinfo->time, gameinfo->addtime);
   150         } else {
   151             printw("  Time limit: %dm + %ds\n",
   152                 gameinfo->time/60, gameinfo->addtime);
   153         }
   154     } else {
   155         printw("  No time limit\n");
   156     }
   157     refresh();
   158 }
   160 void leavescr() {
   161     endwin();
   162 }
   164 int main(int argc, char **argv) {
   165     srand(time(NULL));
   167     Settings settings = default_settings();
   168     if (get_settings(argc, argv, &settings)) {
   169         return 1;
   170     }
   172     if (settings.printhelp) {
   173         printf(
   174 "Usage: terminal-chess [OPTION]... [HOST]\n"
   175 "Starts/joins a network chess game\n"
   176 "\nGeneral options\n"
   177 "  -c <PGN file> Continue the specified game\n"
   178 "  -h            This help page\n"
   179 "  -p            TCP port to use (default: 27015)\n"
   180 // TODO: implement and activate feature
   181 //"  -S <PGN file> Compute and print statistics for the specified game\n"
   182 "\nServer options\n"
   183 "  -a <time>     Specifies the time to add after each move\n"
   184 "  -b            Server plays black pieces (default: white)\n"
   185 "  -r            Distribute color randomly\n"
   186 "  -s            Single machine mode\n"
   187 "  -t <time>     Specifies time limit (default: no limit)\n"
   188 "\nNotes\n"
   189 "The time unit for -a is seconds and for -t minutes by default. To "
   190 "specify\nseconds for the -t option, use the s suffix.\n"
   191 "Example: -t 150s\n\n"
   192 "Use '-' for PGN files to read PGN data from standard input\n"
   193         );
   194         return EXIT_SUCCESS;
   195     }    
   196     initscr();
   197     halfdelay(1);
   198     keypad(stdscr, TRUE);
   199     if (has_colors()) {
   200         start_color();
   201         init_colorpairs();
   202         bkgd(COLOR_PAIR(COL_APP));
   203     } else {
   204         fprintf(stderr, "Non-colored terminals are not supported yet.");
   205         endwin();
   206         return EXIT_FAILURE;
   207     }
   208     atexit(leavescr);
   210     int exitcode;
   211     if (settings.singlemachine) {
   212         game_start_singlemachine(&settings);
   213         exitcode = EXIT_SUCCESS;
   214     } else if (settings.analyzepgn) {
   215         printw("Not implemented yet.\n");
   216         exitcode = EXIT_SUCCESS;
   217     } else {
   218         exitcode = is_server(&settings) ?
   219             server_run(&settings) : client_run(&settings);
   220     }
   222     mvaddstr(getmaxy(stdscr)-1, 0,
   223         "Game has ended. Press any key to leave...");
   224     clrtoeol();
   225     refresh();
   226     cbreak();
   227     flushinp();
   228     getch();
   230     return exitcode;
   231 }

mercurial