src/main.c

Wed, 29 Aug 2018 13:55:18 +0200

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

fixes castling not printed correctly to PGN

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

mercurial