src/main.c

Wed, 29 Aug 2018 13:45:13 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 13:45:13 +0200
changeset 63
611332453da0
parent 55
54ea19938d57
child 69
c8f2c280cff7
permissions
-rw-r--r--

move log has now three columns and starts scrolling after 45 moves

     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * Copyright 2016 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:v")) != -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 'v':
    96             printf("terminal-chess : Version %s (Netcode Version %d)\n",
    97                 PROGRAM_VERSION, NETCODE_VERSION);
    98             exit(0);
    99         case 'h':
   100         case '?':
   101             printf(
   102 "Usage: terminal-chess [OPTION]... [HOST]\n"
   103 "Starts/joins a network chess game\n"
   104 "\nGeneral options\n"
   105 "  -c <PGN file> Continue the specified game\n"
   106 "  -h            This help page\n"
   107 "  -p            TCP port to use (default: 27015)\n"
   108 // TODO: implement and activate feature
   109 //"  -S <PGN file> Compute and print statistics for the specified game\n"
   110 "  -v            Print version information and exists\n"
   111 "\nServer options\n"
   112 "  -a <time>     Specifies the time to add after each move\n"
   113 "  -b            Server plays black pieces (default: white)\n"
   114 "  -r            Distribute color randomly\n"
   115 "  -s            Single machine mode\n"
   116 "  -t <time>     Specifies time limit (default: no limit)\n"
   117 "\nNotes\n"
   118 "The time unit for -a is seconds and for -t minutes by default. To "
   119 "specify\nseconds for the -t option, use the s suffix.\n"
   120 "Example: -t 150s\n\n"
   121 "Use '-' for PGN files to read PGN data from standard input\n"
   122             );
   123             exit(0);
   124         }
   125     }
   127     if (optind == argc - 1) {
   128         settings->serverhost = argv[optind];
   129     } else if (optind < argc - 1) {
   130         fprintf(stderr, "Too many arguments\n");
   131         return 1;
   132     }
   135     if (settings->continuepgn) {
   136         if (settings->serverhost) {
   137             fprintf(stderr, "Can't continue a game when joining a server.\n");
   138             return 1;
   139         }
   140         if (settings->analyzepgn) {
   141             fprintf(stderr, "The options -c and -S are mutually exclusive\n");
   142             return 1;
   143         }
   144     }
   146     return 0;
   147 }
   149 static Settings default_settings() {
   150     Settings settings;
   151     memset(&settings, 0, sizeof(Settings));
   152     settings.gameinfo.servercolor = WHITE;
   153     settings.port = "27015";
   154     return settings;
   155 }
   157 void dump_gameinfo(GameInfo *gameinfo) {
   158     int serverwhite = gameinfo->servercolor == WHITE;
   159     attron(A_UNDERLINE);
   160     printw("Game details\n");
   161     attroff(A_UNDERLINE);
   162     printw("  Server:     %s\n  Client:     %s\n",
   163         serverwhite?"White":"Black", serverwhite?"Black":"White"
   164     );
   165     if (gameinfo->timecontrol) {
   166         if (gameinfo->time % 60) {
   167             printw("  Time limit: %ds + %ds\n",
   168                 gameinfo->time, gameinfo->addtime);
   169         } else {
   170             printw("  Time limit: %dm + %ds\n",
   171                 gameinfo->time/60, gameinfo->addtime);
   172         }
   173     } else {
   174         printw("  No time limit\n");
   175     }
   176     refresh();
   177 }
   179 void dump_moveinfo(GameState *gamestate) {
   180     int i = 1;
   181     for (MoveList *movelist = gamestate->movelist ;
   182         movelist ; movelist = movelist->next) {        
   183         if (++i % 2 == 0) {
   184             printw("%d. %s", i/2, movelist->move.string);
   185         } else {
   186             printw(" %s", movelist->move.string);
   187         }
   188         if (i % 20)  {
   189             addch(' ');
   190         } else {
   191             addch('\n');
   192         }
   193     }
   194     refresh();
   195 }
   197 int main(int argc, char **argv) {
   198     srand(time(NULL));
   200     Settings settings = default_settings();
   201     if (get_settings(argc, argv, &settings)) {
   202         return 1;
   203     }
   205     initscr();
   206     halfdelay(1);
   207     keypad(stdscr, TRUE);
   208     if (has_colors()) {
   209         start_color();
   210         init_colorpairs();
   211         bkgd(COLOR_PAIR(COL_APP));
   212     } else {
   213         fprintf(stderr, "Non-colored terminals are not supported yet.");
   214         endwin();
   215         return EXIT_FAILURE;
   216     }
   217     atexit((void(*)())endwin);
   219     int exitcode;
   220     if (settings.singlemachine) {
   221         game_start_singlemachine(&settings);
   222         exitcode = EXIT_SUCCESS;
   223     } else if (settings.analyzepgn) {
   224         printw("Not implemented yet.\n");
   225         exitcode = EXIT_SUCCESS;
   226     } else {
   227         exitcode = is_server(&settings) ?
   228             server_run(&settings) : client_run(&settings);
   229     }
   231     mvaddstr(getmaxy(stdscr)-1, 0,
   232         "Game has ended. Press any key to leave...");
   233     clrtoeol();
   234     refresh();
   235     cbreak();
   236     flushinp();
   237     getch();
   239     return exitcode;
   240 }

mercurial