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

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

mercurial