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