src/main.c

Tue, 01 Apr 2014 14:04:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 01 Apr 2014 14:04:00 +0200
changeset 26
e0a76ee1bb2b
parent 23
824c9522ce66
child 29
c6a1ad6cf749
permissions
-rw-r--r--

introduced single machine mode

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@1 33 #include <string.h>
universe@2 34 #include <time.h>
universe@7 35 #include <getopt.h>
universe@1 36
universe@1 37 int get_settings(int argc, char **argv, Settings *settings) {
universe@1 38 char *valid;
universe@2 39 unsigned long int time, port;
universe@2 40 uint8_t timeunit = 60;
universe@2 41 size_t len;
universe@1 42
universe@26 43 for (char opt ; (opt = getopt(argc, argv, "a:bhp:rst:")) != -1 ;) {
universe@1 44 switch (opt) {
universe@2 45 case 'b':
universe@2 46 settings->gameinfo.servercolor = BLACK;
universe@2 47 break;
universe@2 48 case 'r':
universe@7 49 settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK;
universe@2 50 break;
universe@26 51 case 's':
universe@26 52 settings->singlemachine = 1;
universe@26 53 break;
universe@2 54 case 't':
universe@2 55 case 'a':
universe@2 56 len = strlen(optarg);
universe@2 57 if (optarg[len-1] == 's') {
universe@2 58 optarg[len-1] = '\0';
universe@2 59 timeunit = 1;
universe@2 60 }
universe@2 61
universe@2 62 if ((time = strtoul(optarg, &valid, 10)) > TIME_MAX
universe@2 63 || *valid != '\0') {
universe@2 64 fprintf(stderr, "Specified time is invalid (%s)\n", optarg);
universe@2 65 return 1;
universe@2 66 } else {
universe@2 67 if (opt=='t') {
universe@2 68 settings->gameinfo.time = timeunit * time;
universe@1 69 } else {
universe@2 70 settings->gameinfo.addtime = time;
universe@1 71 }
universe@2 72 }
universe@2 73 break;
universe@2 74 case 'p':
universe@2 75 port = strtol(optarg, &valid, 10);
universe@2 76 if (port < 1025 || port > 65535 || *valid != '\0') {
universe@2 77 fprintf(stderr,
universe@2 78 "Invalid port number (%s) - choose a number between "
universe@2 79 "1025 and 65535\n",
universe@2 80 optarg);
universe@2 81 return 1;
universe@2 82 } else {
universe@2 83 settings->port = optarg;
universe@2 84 }
universe@2 85 break;
universe@2 86 case 'h':
universe@2 87 case '?':
universe@2 88 settings->printhelp = 1;
universe@2 89 break;
universe@1 90 }
universe@1 91 }
universe@1 92
universe@1 93 if (optind == argc - 1) {
universe@1 94 settings->serverhost = argv[optind];
universe@1 95 } else if (optind < argc - 1) {
universe@1 96 fprintf(stderr, "Too many arguments\n");
universe@1 97 return 1;
universe@1 98 }
universe@1 99
universe@1 100 return 0;
universe@1 101 }
universe@1 102
universe@1 103 Settings default_settings() {
universe@1 104 Settings settings;
universe@1 105 memset(&settings, 0, sizeof(Settings));
universe@2 106 settings.gameinfo.servercolor = WHITE;
universe@1 107 settings.port = "27015";
universe@1 108 return settings;
universe@1 109 }
universe@1 110
universe@2 111 void dump_gameinfo(Gameinfo *gameinfo) {
universe@2 112 int serverwhite = gameinfo->servercolor == WHITE;
universe@3 113 attron(A_UNDERLINE);
universe@3 114 printw("Game details\n");
universe@3 115 attroff(A_UNDERLINE);
universe@3 116 printw(" Server: %s\n Client: %s\n",
universe@2 117 serverwhite?"white":"black", serverwhite?"black":"White"
universe@2 118 );
universe@2 119 if (gameinfo->time > 0) {
universe@2 120 if (gameinfo->time % 60) {
universe@3 121 printw(" Time limit: %ds + %ds\n",
universe@2 122 gameinfo->time, gameinfo->addtime);
universe@2 123 } else {
universe@3 124 printw(" Time limit: %dm + %ds\n",
universe@2 125 gameinfo->time/60, gameinfo->addtime);
universe@2 126 }
universe@2 127 } else {
universe@3 128 printw(" No time limit\n");
universe@2 129 }
universe@3 130 refresh();
universe@2 131 }
universe@2 132
universe@4 133 void leavescr() {
universe@4 134 endwin();
universe@4 135 }
universe@4 136
universe@0 137 int main(int argc, char **argv) {
universe@2 138 srand(time(NULL));
universe@1 139
universe@1 140 Settings settings = default_settings();
universe@2 141 if (get_settings(argc, argv, &settings)) {
universe@2 142 return 1;
universe@2 143 }
universe@1 144
universe@1 145 if (settings.printhelp) {
universe@1 146 printf(
universe@2 147 "Usage: terminal-chess [OPTION]... [HOST]\n"
universe@2 148 "Starts/joins a network chess game\n"
universe@2 149 "\nGeneral options\n"
universe@1 150 " -h This help page\n"
universe@2 151 " -p TCP port to use (default: 27015)\n"
universe@2 152 "\nServer options\n"
universe@2 153 " -a <time> Specifies the time to add after each move\n"
universe@2 154 " -b Server plays black pieces (default: white)\n"
universe@2 155 " -r Distribute color randomly\n"
universe@26 156 " -s Single machine mode\n"
universe@2 157 " -t <time> Specifies time limit (default: no limit)\n"
universe@2 158 "\nNotes\n"
universe@2 159 "The time unit for -a is seconds and for -t minutes by default. To "
universe@2 160 "specify\nseconds for the -t option, use the s suffix.\n"
universe@2 161 "Example: -t 150s\n"
universe@2 162 );
universe@1 163 return EXIT_SUCCESS;
universe@16 164 }
universe@23 165 initscr();
universe@3 166 cbreak();
universe@7 167 if (has_colors()) {
universe@7 168 start_color();
universe@7 169 init_colorpairs();
universe@7 170 bkgd(COLOR_PAIR(COL_YB));
universe@7 171 } else {
universe@7 172 fprintf(stderr, "Non-colored terminals are not supported yet.");
universe@7 173 endwin();
universe@7 174 return EXIT_FAILURE;
universe@7 175 }
universe@4 176 atexit(leavescr);
universe@3 177
universe@26 178 if (settings.singlemachine) {
universe@26 179 game_start_singlemachine(&settings);
universe@26 180 } else {
universe@26 181 return is_server(&settings) ?
universe@26 182 server_run(&settings) : client_run(&settings);
universe@26 183 }
universe@0 184 }
universe@0 185

mercurial