Sat, 22 Mar 2014 16:25:49 +0100
implemented castling (without validation)
/* * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * Copyright 2014 Mike Becker. All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * */ #include "terminal-chess.h" #include "game.h" #include "input.h" #include <string.h> #include <time.h> #include <getopt.h> int get_settings(int argc, char **argv, Settings *settings) { char *valid; unsigned long int time, port; uint8_t timeunit = 60; size_t len; for (char opt ; (opt = getopt(argc, argv, "a:bhp:rt:")) != -1 ;) { switch (opt) { case 'b': settings->gameinfo.servercolor = BLACK; break; case 'r': settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK; break; case 't': case 'a': len = strlen(optarg); if (optarg[len-1] == 's') { optarg[len-1] = '\0'; timeunit = 1; } if ((time = strtoul(optarg, &valid, 10)) > TIME_MAX || *valid != '\0') { fprintf(stderr, "Specified time is invalid (%s)\n", optarg); return 1; } else { if (opt=='t') { settings->gameinfo.time = timeunit * time; } else { settings->gameinfo.addtime = time; } } break; case 'p': port = strtol(optarg, &valid, 10); if (port < 1025 || port > 65535 || *valid != '\0') { fprintf(stderr, "Invalid port number (%s) - choose a number between " "1025 and 65535\n", optarg); return 1; } else { settings->port = optarg; } break; case 'h': case '?': settings->printhelp = 1; break; } } if (optind == argc - 1) { settings->serverhost = argv[optind]; } else if (optind < argc - 1) { fprintf(stderr, "Too many arguments\n"); return 1; } return 0; } Settings default_settings() { Settings settings; memset(&settings, 0, sizeof(Settings)); settings.gameinfo.servercolor = WHITE; settings.port = "27015"; return settings; } void dump_gameinfo(Gameinfo *gameinfo) { int serverwhite = gameinfo->servercolor == WHITE; attron(A_UNDERLINE); printw("Game details\n"); attroff(A_UNDERLINE); printw(" Server: %s\n Client: %s\n", serverwhite?"white":"black", serverwhite?"black":"White" ); if (gameinfo->time > 0) { if (gameinfo->time % 60) { printw(" Time limit: %ds + %ds\n", gameinfo->time, gameinfo->addtime); } else { printw(" Time limit: %dm + %ds\n", gameinfo->time/60, gameinfo->addtime); } } else { printw(" No time limit\n"); } refresh(); } void leavescr() { endwin(); } int main(int argc, char **argv) { srand(time(NULL)); Settings settings = default_settings(); if (get_settings(argc, argv, &settings)) { return 1; } if (settings.printhelp) { printf( "Usage: terminal-chess [OPTION]... [HOST]\n" "Starts/joins a network chess game\n" "\nGeneral options\n" " -h This help page\n" " -p TCP port to use (default: 27015)\n" "\nServer options\n" " -a <time> Specifies the time to add after each move\n" " -b Server plays black pieces (default: white)\n" " -r Distribute color randomly\n" " -t <time> Specifies time limit (default: no limit)\n" "\nNotes\n" "White pieces are displayed as uppercase and black pieces as " "lowercase letters.\n" "The time unit for -a is seconds and for -t minutes by default. To " "specify\nseconds for the -t option, use the s suffix.\n" "Example: -t 150s\n" ); return EXIT_SUCCESS; } tchess_window = initscr(); cbreak(); if (has_colors()) { start_color(); init_colorpairs(); bkgd(COLOR_PAIR(COL_YB)); } else { fprintf(stderr, "Non-colored terminals are not supported yet."); endwin(); return EXIT_FAILURE; } atexit(leavescr); return is_server(&settings) ? server_run(&settings) : client_run(&settings); }