src/main.c

Wed, 28 May 2014 15:47:57 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 28 May 2014 15:47:57 +0200
changeset 47
d726e4b46c33
parent 46
4dcfb4c58b6d
child 50
41017d0a72c5
permissions
-rw-r--r--

refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)

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@47 44 for (int opt ; (opt = getopt(argc, argv, "a:bc:hp:rst:")) != -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@2 58 case 't':
universe@2 59 case 'a':
universe@2 60 len = strlen(optarg);
universe@2 61 if (optarg[len-1] == 's') {
universe@2 62 optarg[len-1] = '\0';
universe@2 63 timeunit = 1;
universe@2 64 }
universe@2 65
universe@30 66 if ((time = strtoul(optarg, &valid, 10))*timeunit > UINT16_MAX
universe@2 67 || *valid != '\0') {
universe@30 68 fprintf(stderr, "Specified time is invalid (%s)"
universe@30 69 "- Maximum: 65535 seconds (1092 minutes)\n", optarg);
universe@2 70 return 1;
universe@2 71 } else {
universe@30 72 settings->gameinfo.timecontrol = 1;
universe@2 73 if (opt=='t') {
universe@2 74 settings->gameinfo.time = timeunit * time;
universe@1 75 } else {
universe@2 76 settings->gameinfo.addtime = time;
universe@1 77 }
universe@2 78 }
universe@2 79 break;
universe@2 80 case 'p':
universe@2 81 port = strtol(optarg, &valid, 10);
universe@2 82 if (port < 1025 || port > 65535 || *valid != '\0') {
universe@2 83 fprintf(stderr,
universe@2 84 "Invalid port number (%s) - choose a number between "
universe@2 85 "1025 and 65535\n",
universe@2 86 optarg);
universe@2 87 return 1;
universe@2 88 } else {
universe@2 89 settings->port = optarg;
universe@2 90 }
universe@2 91 break;
universe@2 92 case 'h':
universe@2 93 case '?':
universe@2 94 settings->printhelp = 1;
universe@2 95 break;
universe@1 96 }
universe@1 97 }
universe@1 98
universe@1 99 if (optind == argc - 1) {
universe@1 100 settings->serverhost = argv[optind];
universe@1 101 } else if (optind < argc - 1) {
universe@1 102 fprintf(stderr, "Too many arguments\n");
universe@1 103 return 1;
universe@1 104 }
universe@1 105
universe@1 106 return 0;
universe@1 107 }
universe@1 108
universe@1 109 Settings default_settings() {
universe@1 110 Settings settings;
universe@1 111 memset(&settings, 0, sizeof(Settings));
universe@2 112 settings.gameinfo.servercolor = WHITE;
universe@1 113 settings.port = "27015";
universe@47 114 settings.continuepgn = NULL;
universe@1 115 return settings;
universe@1 116 }
universe@1 117
universe@30 118 void dump_gameinfo(GameInfo *gameinfo) {
universe@2 119 int serverwhite = gameinfo->servercolor == WHITE;
universe@3 120 attron(A_UNDERLINE);
universe@3 121 printw("Game details\n");
universe@3 122 attroff(A_UNDERLINE);
universe@3 123 printw(" Server: %s\n Client: %s\n",
universe@33 124 serverwhite?"White":"Black", serverwhite?"Black":"White"
universe@2 125 );
universe@30 126 if (gameinfo->timecontrol) {
universe@2 127 if (gameinfo->time % 60) {
universe@3 128 printw(" Time limit: %ds + %ds\n",
universe@2 129 gameinfo->time, gameinfo->addtime);
universe@2 130 } else {
universe@3 131 printw(" Time limit: %dm + %ds\n",
universe@2 132 gameinfo->time/60, gameinfo->addtime);
universe@2 133 }
universe@2 134 } else {
universe@3 135 printw(" No time limit\n");
universe@2 136 }
universe@3 137 refresh();
universe@2 138 }
universe@2 139
universe@4 140 void leavescr() {
universe@4 141 endwin();
universe@4 142 }
universe@4 143
universe@0 144 int main(int argc, char **argv) {
universe@2 145 srand(time(NULL));
universe@1 146
universe@1 147 Settings settings = default_settings();
universe@2 148 if (get_settings(argc, argv, &settings)) {
universe@2 149 return 1;
universe@2 150 }
universe@1 151
universe@1 152 if (settings.printhelp) {
universe@1 153 printf(
universe@47 154 "Usage: terminal-chess [OPTION]... [HOST]\n"
universe@47 155 "Starts/joins a network chess game\n"
universe@47 156 "\nGeneral options\n"
universe@47 157 " -h This help page\n"
universe@47 158 " -p TCP port to use (default: 27015)\n"
universe@47 159 "\nServer options\n"
universe@47 160 " -a <time> Specifies the time to add after each move\n"
universe@47 161 " -b Server plays black pieces (default: white)\n"
universe@47 162 // TODO: implement and activate feature
universe@47 163 //" -c <PGN file> Continue the specified game\n"
universe@47 164 " -r Distribute color randomly\n"
universe@47 165 " -s Single machine mode\n"
universe@47 166 // TODO: implement and activate feature
universe@47 167 //" -S <PGN file> Compute and print statistics for the specified game\n"
universe@47 168 " -t <time> Specifies time limit (default: no limit)\n"
universe@47 169 "\nNotes\n"
universe@47 170 "The time unit for -a is seconds and for -t minutes by default. To "
universe@47 171 "specify\nseconds for the -t option, use the s suffix.\n"
universe@47 172 "Example: -t 150s\n\n"
universe@47 173 "Use '-' for PGN files to read PGN data from standard input\n"
universe@2 174 );
universe@1 175 return EXIT_SUCCESS;
universe@16 176 }
universe@23 177 initscr();
universe@34 178 halfdelay(1);
universe@29 179 keypad(stdscr, TRUE);
universe@7 180 if (has_colors()) {
universe@7 181 start_color();
universe@7 182 init_colorpairs();
universe@35 183 bkgd(COLOR_PAIR(COL_APP));
universe@7 184 } else {
universe@7 185 fprintf(stderr, "Non-colored terminals are not supported yet.");
universe@7 186 endwin();
universe@7 187 return EXIT_FAILURE;
universe@7 188 }
universe@4 189 atexit(leavescr);
universe@3 190
universe@46 191 int exitcode;
universe@26 192 if (settings.singlemachine) {
universe@26 193 game_start_singlemachine(&settings);
universe@46 194 exitcode = EXIT_SUCCESS;
universe@26 195 } else {
universe@46 196 exitcode = is_server(&settings) ?
universe@26 197 server_run(&settings) : client_run(&settings);
universe@26 198 }
universe@46 199
universe@46 200 mvaddstr(getmaxy(stdscr)-1, 0,
universe@46 201 "Game has ended. Press any key to leave...");
universe@46 202 refresh();
universe@46 203 cbreak();
universe@46 204 flushinp();
universe@46 205 getch();
universe@46 206
universe@46 207 return exitcode;
universe@0 208 }
universe@0 209

mercurial