src/main.c

Tue, 01 Apr 2014 14:04:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 01 Apr 2014 14:04:00 +0200
changeset 26
e0a76ee1bb2b
parent 23
824c9522ce66
child 29
c6a1ad6cf749
permissions
-rw-r--r--

introduced single machine mode

     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 <string.h>
    34 #include <time.h>
    35 #include <getopt.h>
    37 int get_settings(int argc, char **argv, Settings *settings) {
    38     char *valid;
    39     unsigned long int time, port;
    40     uint8_t timeunit = 60;
    41     size_t len;
    43     for (char opt ; (opt = getopt(argc, argv, "a:bhp:rst:")) != -1 ;) {
    44         switch (opt) {
    45         case 'b':
    46             settings->gameinfo.servercolor = BLACK;
    47             break;
    48         case 'r':
    49             settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK;
    50             break;
    51         case 's':
    52             settings->singlemachine = 1;
    53             break;
    54         case 't':
    55         case 'a':
    56             len = strlen(optarg);
    57             if (optarg[len-1] == 's') {
    58                 optarg[len-1] = '\0';
    59                 timeunit = 1;
    60             }
    62             if ((time = strtoul(optarg, &valid, 10)) > TIME_MAX
    63                 || *valid != '\0') {
    64                 fprintf(stderr, "Specified time is invalid (%s)\n", optarg);
    65                 return 1;
    66             } else {
    67                 if (opt=='t') {
    68                     settings->gameinfo.time = timeunit * time;
    69                 } else {
    70                     settings->gameinfo.addtime = time;
    71                 }
    72             }
    73             break;
    74         case 'p':
    75             port = strtol(optarg, &valid, 10);
    76             if (port < 1025 || port > 65535 || *valid != '\0') {
    77                 fprintf(stderr,
    78                     "Invalid port number (%s) - choose a number between "
    79                     "1025 and 65535\n",
    80                     optarg);
    81                 return 1;
    82             } else {
    83                 settings->port = optarg;
    84             }
    85             break;
    86         case 'h':
    87         case '?':
    88             settings->printhelp = 1;
    89             break;
    90         }
    91     }
    93     if (optind == argc - 1) {
    94         settings->serverhost = argv[optind];
    95     } else if (optind < argc - 1) {
    96         fprintf(stderr, "Too many arguments\n");
    97         return 1;
    98     }
   100     return 0;
   101 }
   103 Settings default_settings() {
   104     Settings settings;
   105     memset(&settings, 0, sizeof(Settings));
   106     settings.gameinfo.servercolor = WHITE;
   107     settings.port = "27015";
   108     return settings;
   109 }
   111 void dump_gameinfo(Gameinfo *gameinfo) {
   112     int serverwhite = gameinfo->servercolor == WHITE;
   113     attron(A_UNDERLINE);
   114     printw("Game details\n");
   115     attroff(A_UNDERLINE);
   116     printw("  Server:     %s\n  Client:     %s\n",
   117         serverwhite?"white":"black", serverwhite?"black":"White"
   118     );
   119     if (gameinfo->time > 0) {
   120         if (gameinfo->time % 60) {
   121             printw("  Time limit: %ds + %ds\n",
   122                 gameinfo->time, gameinfo->addtime);
   123         } else {
   124             printw("  Time limit: %dm + %ds\n",
   125                 gameinfo->time/60, gameinfo->addtime);
   126         }
   127     } else {
   128         printw("  No time limit\n");
   129     }
   130     refresh();
   131 }
   133 void leavescr() {
   134     endwin();
   135 }
   137 int main(int argc, char **argv) {
   138     srand(time(NULL));
   140     Settings settings = default_settings();
   141     if (get_settings(argc, argv, &settings)) {
   142         return 1;
   143     }
   145     if (settings.printhelp) {
   146         printf(
   147             "Usage: terminal-chess [OPTION]... [HOST]\n"
   148             "Starts/joins a network chess game\n"
   149             "\nGeneral options\n"
   150             "  -h            This help page\n"
   151             "  -p            TCP port to use (default: 27015)\n"
   152             "\nServer options\n"
   153             "  -a <time>     Specifies the time to add after each move\n"
   154             "  -b            Server plays black pieces (default: white)\n"
   155             "  -r            Distribute color randomly\n"
   156             "  -s            Single machine mode\n"
   157             "  -t <time>     Specifies time limit (default: no limit)\n"
   158             "\nNotes\n"
   159             "The time unit for -a is seconds and for -t minutes by default. To "
   160             "specify\nseconds for the -t option, use the s suffix.\n"
   161             "Example: -t 150s\n"
   162         );
   163         return EXIT_SUCCESS;
   164     }    
   165     initscr();
   166     cbreak();
   167     if (has_colors()) {
   168         start_color();
   169         init_colorpairs();
   170         bkgd(COLOR_PAIR(COL_YB));
   171     } else {
   172         fprintf(stderr, "Non-colored terminals are not supported yet.");
   173         endwin();
   174         return EXIT_FAILURE;
   175     }
   176     atexit(leavescr);
   178     if (settings.singlemachine) {
   179         game_start_singlemachine(&settings);
   180     } else {
   181         return is_server(&settings) ?
   182             server_run(&settings) : client_run(&settings);
   183     }
   184 }

mercurial