src/main.c

Wed, 09 Apr 2014 18:11:51 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 09 Apr 2014 18:11:51 +0200
changeset 35
6c64b7a073af
parent 34
c4d4b8a8f902
child 46
4dcfb4c58b6d
permissions
-rw-r--r--

tried to improve colors

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

mercurial