Mon, 31 Mar 2014 14:00:58 +0200
implemented rook + some fixes
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
30 #include "terminal-chess.h"
31 #include "game.h"
32 #include "input.h"
33 #include <string.h>
34 #include <time.h>
35 #include <getopt.h>
37 int get_settings(int argc, char **argv, Settings *settings) {
38 char *valid;
39 unsigned long int time, port;
40 uint8_t timeunit = 60;
41 size_t len;
43 for (char opt ; (opt = getopt(argc, argv, "a:bhp:rt:")) != -1 ;) {
44 switch (opt) {
45 case 'b':
46 settings->gameinfo.servercolor = BLACK;
47 break;
48 case 'r':
49 settings->gameinfo.servercolor = rand() & 1 ? WHITE : BLACK;
50 break;
51 case 't':
52 case 'a':
53 len = strlen(optarg);
54 if (optarg[len-1] == 's') {
55 optarg[len-1] = '\0';
56 timeunit = 1;
57 }
59 if ((time = strtoul(optarg, &valid, 10)) > TIME_MAX
60 || *valid != '\0') {
61 fprintf(stderr, "Specified time is invalid (%s)\n", optarg);
62 return 1;
63 } else {
64 if (opt=='t') {
65 settings->gameinfo.time = timeunit * time;
66 } else {
67 settings->gameinfo.addtime = time;
68 }
69 }
70 break;
71 case 'p':
72 port = strtol(optarg, &valid, 10);
73 if (port < 1025 || port > 65535 || *valid != '\0') {
74 fprintf(stderr,
75 "Invalid port number (%s) - choose a number between "
76 "1025 and 65535\n",
77 optarg);
78 return 1;
79 } else {
80 settings->port = optarg;
81 }
82 break;
83 case 'h':
84 case '?':
85 settings->printhelp = 1;
86 break;
87 }
88 }
90 if (optind == argc - 1) {
91 settings->serverhost = argv[optind];
92 } else if (optind < argc - 1) {
93 fprintf(stderr, "Too many arguments\n");
94 return 1;
95 }
97 return 0;
98 }
100 Settings default_settings() {
101 Settings settings;
102 memset(&settings, 0, sizeof(Settings));
103 settings.gameinfo.servercolor = WHITE;
104 settings.port = "27015";
105 return settings;
106 }
108 void dump_gameinfo(Gameinfo *gameinfo) {
109 int serverwhite = gameinfo->servercolor == WHITE;
110 attron(A_UNDERLINE);
111 printw("Game details\n");
112 attroff(A_UNDERLINE);
113 printw(" Server: %s\n Client: %s\n",
114 serverwhite?"white":"black", serverwhite?"black":"White"
115 );
116 if (gameinfo->time > 0) {
117 if (gameinfo->time % 60) {
118 printw(" Time limit: %ds + %ds\n",
119 gameinfo->time, gameinfo->addtime);
120 } else {
121 printw(" Time limit: %dm + %ds\n",
122 gameinfo->time/60, gameinfo->addtime);
123 }
124 } else {
125 printw(" No time limit\n");
126 }
127 refresh();
128 }
130 void leavescr() {
131 endwin();
132 }
134 int main(int argc, char **argv) {
135 srand(time(NULL));
137 Settings settings = default_settings();
138 if (get_settings(argc, argv, &settings)) {
139 return 1;
140 }
142 if (settings.printhelp) {
143 printf(
144 "Usage: terminal-chess [OPTION]... [HOST]\n"
145 "Starts/joins a network chess game\n"
146 "\nGeneral options\n"
147 " -h This help page\n"
148 " -p TCP port to use (default: 27015)\n"
149 "\nServer options\n"
150 " -a <time> Specifies the time to add after each move\n"
151 " -b Server plays black pieces (default: white)\n"
152 " -r Distribute color randomly\n"
153 " -t <time> Specifies time limit (default: no limit)\n"
154 "\nNotes\n"
155 "White pieces are displayed as uppercase and black pieces as "
156 "lowercase letters.\n"
157 "The time unit for -a is seconds and for -t minutes by default. To "
158 "specify\nseconds for the -t option, use the s suffix.\n"
159 "Example: -t 150s\n"
160 );
161 return EXIT_SUCCESS;
162 }
163 tchess_window = initscr();
164 cbreak();
165 if (has_colors()) {
166 start_color();
167 init_colorpairs();
168 bkgd(COLOR_PAIR(COL_YB));
169 } else {
170 fprintf(stderr, "Non-colored terminals are not supported yet.");
171 endwin();
172 return EXIT_FAILURE;
173 }
174 atexit(leavescr);
176 return is_server(&settings) ? server_run(&settings) : client_run(&settings);
177 }