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