src/game.c

Wed, 09 Apr 2014 12:07:47 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 09 Apr 2014 12:07:47 +0200
changeset 34
c4d4b8a8f902
parent 33
866025982aa9
child 35
6c64b7a073af
permissions
-rw-r--r--

added nonblocking read for network games + minor build system fixes

universe@6 1 /*
universe@6 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@6 3 *
universe@6 4 * Copyright 2014 Mike Becker. All rights reserved.
universe@6 5 *
universe@6 6 * Redistribution and use in source and binary forms, with or without
universe@6 7 * modification, are permitted provided that the following conditions are met:
universe@6 8 *
universe@6 9 * 1. Redistributions of source code must retain the above copyright
universe@6 10 * notice, this list of conditions and the following disclaimer.
universe@6 11 *
universe@6 12 * 2. Redistributions in binary form must reproduce the above copyright
universe@6 13 * notice, this list of conditions and the following disclaimer in the
universe@6 14 * documentation and/or other materials provided with the distribution.
universe@6 15 *
universe@6 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
universe@6 17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
universe@6 18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
universe@6 19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
universe@6 20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
universe@6 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
universe@6 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
universe@6 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
universe@6 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
universe@6 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
universe@6 26 * POSSIBILITY OF SUCH DAMAGE.
universe@6 27 *
universe@6 28 */
universe@6 29
universe@6 30 #include "game.h"
universe@19 31 #include "network.h"
universe@7 32 #include "input.h"
universe@7 33 #include <ncurses.h>
universe@7 34 #include <string.h>
universe@30 35 #include <inttypes.h>
universe@34 36 #include <sys/select.h>
universe@7 37
universe@7 38 static const uint8_t boardx = 10, boardy = 10;
universe@33 39 static int inputy = 21; /* should be overridden on game startup */
universe@7 40
universe@33 41 static int timecontrol(GameState *gamestate, GameInfo *gameinfo) {
universe@30 42 if (gameinfo->timecontrol) {
universe@33 43 uint16_t white = remaining_movetime(gameinfo, gamestate, WHITE);
universe@33 44 uint16_t black = remaining_movetime(gameinfo, gamestate, BLACK);
universe@30 45 mvprintw(boardy+4, boardx-1,
universe@33 46 "White time: %4" PRIu16 ":%02" PRIu16,
universe@33 47 white / 60, white % 60);
universe@30 48 mvprintw(boardy+5, boardx-1,
universe@33 49 "Black time: %4" PRIu16 ":%02" PRIu16,
universe@33 50 black / 60, black % 60);
universe@33 51
universe@33 52 if (white == 0) {
universe@33 53 move(inputy, 0);
universe@33 54 printw("Time is over - Black wins!");
universe@33 55 clrtobot();
universe@33 56 refresh();
universe@33 57 return 1;
universe@33 58 }
universe@33 59 if (black == 0) {
universe@33 60 move(inputy, 0);
universe@33 61 printw("Time is over - White wins!");
universe@33 62 clrtobot();
universe@33 63 refresh();
universe@33 64 return 1;
universe@33 65 }
universe@30 66 }
universe@33 67
universe@33 68 return 0;
universe@30 69 }
universe@30 70
universe@23 71 static void draw_board(GameState *gamestate) {
universe@7 72 for (uint8_t y = 0 ; y < 8 ; y++) {
universe@7 73 for (uint8_t x = 0 ; x < 8 ; x++) {
universe@23 74 uint8_t col = gamestate->board[y][x] & COLOR_MASK;
universe@23 75 uint8_t piece = gamestate->board[y][x] & PIECE_MASK;
universe@18 76 char piecec;
universe@18 77 if (piece) {
universe@18 78 piecec = piece == PAWN ? 'P' : getpiecechr(piece);
universe@18 79 } else {
universe@18 80 piecec = ' ';
universe@7 81 }
universe@7 82
universe@7 83 attrset((col == WHITE ? A_BOLD : A_DIM) |
universe@7 84 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
universe@7 85
universe@23 86 int cy = gamestate->mycolor == WHITE ? boardy-y : boardy-7+y;
universe@23 87 int cx = gamestate->mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
universe@8 88 mvaddch(cy, cx, ' ');
universe@8 89 mvaddch(cy, cx+1, piecec);
universe@8 90 mvaddch(cy, cx+2, ' ');
universe@7 91 }
universe@7 92 }
universe@7 93
universe@7 94 attrset(A_NORMAL);
universe@7 95 for (uint8_t i = 0 ; i < 8 ; i++) {
universe@23 96 int x = gamestate->mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
universe@23 97 int y = gamestate->mycolor == WHITE ? boardy-i : boardy-7+i;
universe@8 98 mvaddch(boardy+1, x, 'a'+i);
universe@8 99 mvaddch(y, boardx-2, '1'+i);
universe@7 100 }
universe@18 101
universe@18 102 /* move log */
universe@18 103 // TODO: introduce window to avoid bugs with a long move log
universe@18 104 uint8_t logy = 0;
universe@18 105 const uint8_t logx = boardx + 30;
universe@18 106 int logi = 1;
universe@23 107 MoveList *logelem = gamestate->movelist;
universe@18 108
universe@18 109 while (logelem) {
universe@18 110 logi++;
universe@18 111 if (logi % 2 == 0) {
universe@18 112 if ((logi - 2) % 4 == 0) {
universe@18 113 logy++;
universe@23 114 move(logy, logx);
universe@18 115 }
universe@18 116 printw("%d. ", logi / 2);
universe@18 117 }
universe@18 118
universe@18 119 if (logelem) {
universe@18 120 Move move = logelem->move;
universe@25 121 if ((move.piece&PIECE_MASK) == KING &&
universe@25 122 abs(move.tofile-move.fromfile) == 2) {
universe@25 123 addstr(move.tofile==fileidx('c')?"O-O-O":"O-O");
universe@25 124 } else {
universe@25 125 char logstr[] = {
universe@25 126 getpiecechr(move.piece),
universe@25 127 filechr(move.fromfile), rowchr(move.fromrow),
universe@25 128 move.capture ? 'x':'\0',
universe@25 129 filechr(move.tofile), rowchr(move.torow),
universe@27 130 move.check ? '+' : (move.promotion ? '=' : '\0'),
universe@25 131 move.promotion ? getpiecechr(move.promotion) : '\0'
universe@25 132 };
universe@25 133 for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
universe@25 134 if (logstr[stri]) {
universe@25 135 addch(logstr[stri]);
universe@25 136 }
universe@18 137 }
universe@18 138 }
universe@27 139 if (!logelem->next) {
universe@27 140 if (gamestate->checkmate) {
universe@28 141 addstr("\b#");
universe@27 142 } else if (gamestate->stalemate) {
universe@27 143 addstr(" stalemate");
universe@27 144 }
universe@27 145 }
universe@25 146 addch(' ');
universe@18 147
universe@18 148 logelem = logelem->next;
universe@18 149 }
universe@18 150 }
universe@7 151 }
universe@7 152
universe@26 153 static void eval_move_failed_msg(int code) {
universe@26 154 switch (code) {
universe@26 155 case AMBIGUOUS_MOVE:
universe@26 156 printw("Ambiguous move - please specify the piece to move.");
universe@26 157 break;
universe@26 158 case INVALID_POSITION:
universe@26 159 printw("Cannot find the piece that shall be moved.");
universe@26 160 break;
universe@26 161 case NEED_PROMOTION:
universe@26 162 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
universe@26 163 break;
universe@26 164 default:
universe@26 165 printw("Can't interpret move - please use algebraic notation.");
universe@26 166 }
universe@26 167 }
universe@26 168
universe@30 169 static int domove_singlemachine(GameState *gamestate, GameInfo *gameinfo) {
universe@26 170
universe@26 171 const size_t buflen = 8;
universe@32 172 size_t bufpos = 0;
universe@26 173 char movestr[buflen];
universe@26 174
universe@33 175 flushinp();
universe@26 176 while (1) {
universe@33 177 if (timecontrol(gamestate, gameinfo)) {
universe@33 178 return 1;
universe@33 179 }
universe@33 180
universe@26 181 move(inputy, 0);
universe@26 182 printw(
universe@26 183 "Use chess notation to enter your move.\n"
universe@26 184 "Or type 'surr' to surrender or 'remis' to end with remis.\n\n"
universe@26 185 "Type your move: ");
universe@26 186 clrtoeol();
universe@30 187
universe@32 188 if (asyncgetnstr(movestr, &bufpos, buflen)) {
universe@30 189 if (strncmp(movestr, "surr", buflen) == 0) {
universe@30 190 printw("%s surrendered!",
universe@30 191 gamestate->mycolor==WHITE?"White":"Black");
universe@30 192 clrtoeol();
universe@30 193 refresh();
universe@30 194 return 1;
universe@30 195 } else if (strncmp(movestr, "remis", buflen) == 0) {
universe@30 196 printw("Game ends remis.");
universe@30 197 clrtoeol();
universe@30 198 refresh();
universe@30 199 return 1;
universe@30 200 } else {
universe@30 201 Move move;
universe@30 202 int eval_result = eval_move(gamestate, movestr, &move);
universe@30 203 switch (eval_result) {
universe@30 204 case VALID_MOVE_SYNTAX:
universe@30 205 if (validate_move(gamestate, &move)) {
universe@30 206 apply_move(gamestate, &move);
universe@30 207 if (gamestate->checkmate) {
universe@30 208 printw("Checkmate!");
universe@30 209 clrtoeol();
universe@30 210 return 1;
universe@30 211 } else if (gamestate->stalemate) {
universe@30 212 printw("Stalemate!");
universe@30 213 clrtoeol();
universe@30 214 return 1;
universe@30 215 } else {
universe@30 216 return 0;
universe@30 217 }
universe@26 218 } else {
universe@30 219 printw("Invalid move.");
universe@26 220 }
universe@30 221 break;
universe@30 222 default:
universe@30 223 eval_move_failed_msg(eval_result);
universe@26 224 }
universe@30 225 clrtoeol();
universe@26 226 }
universe@26 227 }
universe@26 228 }
universe@26 229 }
universe@8 230
universe@33 231 static int sendmove(GameState *gamestate, GameInfo *gameinfo, int opponent) {
universe@18 232
universe@7 233 const size_t buflen = 8;
universe@33 234 size_t bufpos = 0;
universe@8 235 char movestr[buflen];
universe@7 236 _Bool remisrejected = FALSE;
universe@11 237 uint8_t code;
universe@7 238
universe@33 239 flushinp();
universe@7 240 while (1) {
universe@33 241 if (timecontrol(gamestate, gameinfo)) {
universe@33 242 net_send_code(opponent, NETCODE_TIMEOVER);
universe@33 243 return 1;
universe@33 244 }
universe@33 245
universe@18 246 move(inputy, 0);
universe@7 247 if (remisrejected) {
universe@7 248 printw(
universe@7 249 "Use chess notation to enter your move.\n"
universe@7 250 "Remis offer rejected - type 'surr' to surrender. \n\n"
universe@7 251 "Type your move: ");
universe@7 252 } else {
universe@7 253 printw(
universe@7 254 "Use chess notation to enter your move.\n"
universe@7 255 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
universe@7 256 "Type your move: ");
universe@7 257 }
universe@7 258 clrtoeol();
universe@33 259
universe@33 260 if (asyncgetnstr(movestr, &bufpos, buflen)) {
universe@33 261 if (strncmp(movestr, "surr", buflen) == 0) {
universe@33 262 printw("You surrendered!");
universe@33 263 clrtoeol();
universe@7 264 refresh();
universe@33 265 net_send_code(opponent, NETCODE_SURRENDER);
universe@33 266 return 1;
universe@33 267 } else if (strncmp(movestr, "remis", buflen) == 0) {
universe@33 268 if (!remisrejected) {
universe@33 269 net_send_code(opponent, NETCODE_REMIS);
universe@33 270 printw("Remis offer sent - waiting for acceptance...");
universe@8 271 refresh();
universe@33 272 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
universe@33 273 printw("\rRemis accepted!");
universe@18 274 clrtoeol();
universe@33 275 refresh();
universe@26 276 return 1;
universe@14 277 } else {
universe@33 278 remisrejected = TRUE;
universe@11 279 }
universe@18 280 }
universe@33 281 } else {
universe@33 282 Move move;
universe@33 283 int eval_result = eval_move(gamestate, movestr, &move);
universe@33 284 switch (eval_result) {
universe@33 285 case VALID_MOVE_SYNTAX:
universe@33 286 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
universe@33 287 code = net_recieve_code(opponent);
universe@33 288 move.check = code == NETCODE_CHECK;
universe@33 289 gamestate->checkmate = code == NETCODE_CHECKMATE;
universe@33 290 gamestate->stalemate = code == NETCODE_STALEMATE;
universe@33 291 if (code == NETCODE_DECLINE) {
universe@33 292 printw("Invalid move.");
universe@33 293 } else {
universe@33 294 apply_move(gamestate, &move);
universe@33 295 if (gamestate->checkmate) {
universe@33 296 printw("Checkmate!");
universe@33 297 clrtoeol();
universe@33 298 return 1;
universe@33 299 } else if (gamestate->stalemate) {
universe@33 300 printw("Stalemate!");
universe@33 301 clrtoeol();
universe@33 302 return 1;
universe@33 303 } else {
universe@33 304 return 0;
universe@33 305 }
universe@33 306 }
universe@33 307 break;
universe@33 308 default:
universe@33 309 eval_move_failed_msg(eval_result);
universe@33 310 }
universe@33 311 clrtoeol();
universe@8 312 }
universe@7 313 }
universe@7 314 }
universe@7 315 }
universe@7 316
universe@33 317 static int recvmove(GameState *gamestate, GameInfo *gameinfo, int opponent) {
universe@7 318
universe@34 319 if (net_setnonblocking(opponent, 1)) {
universe@34 320 printw("Cannot setup nonblocking IO on network socket");
universe@34 321 cbreak(); getch();
universe@34 322 exit(EXIT_FAILURE);
universe@34 323 }
universe@34 324
universe@34 325 struct timeval timeout;
universe@7 326 while (1) {
universe@33 327 timecontrol(gamestate, gameinfo);
universe@33 328
universe@18 329 move(inputy, 0);
universe@7 330 printw("Awaiting opponent move...");
universe@7 331 clrtoeol();
universe@7 332 refresh();
universe@7 333
universe@34 334 fd_set readfds;
universe@8 335
universe@34 336 FD_ZERO(&readfds);
universe@34 337 FD_SET(opponent, &readfds);
universe@34 338 timeout.tv_sec = 0;
universe@34 339 timeout.tv_usec = 1e5;
universe@34 340
universe@34 341 int result = select(opponent+1, &readfds, NULL, NULL, &timeout);
universe@34 342 if (result == -1) {
universe@34 343 printw("\rCannot perform asynchronous network IO");
universe@34 344 cbreak(); getch();
universe@34 345 exit(EXIT_FAILURE);
universe@34 346 }
universe@34 347 if (result > 0) {
universe@34 348 uint32_t code = net_recieve_code(opponent);
universe@34 349
universe@34 350 Move move;
universe@34 351 switch (code) {
universe@34 352 case NETCODE_TIMEOVER:
universe@34 353 printw("\rYour opponent's time ran out - you win!");
universe@7 354 clrtoeol();
universe@7 355 return 1;
universe@34 356 case NETCODE_SURRENDER:
universe@34 357 printw("\rYour opponent surrendered!");
universe@34 358 clrtoeol();
universe@34 359 return 1;
universe@34 360 case NETCODE_REMIS:
universe@34 361 if (prompt_yesno(
universe@34 362 "\rYour opponent offers remis - do you accept")) {
universe@34 363 printw("\rRemis accepted!");
universe@7 364 clrtoeol();
universe@34 365 net_send_code(opponent, NETCODE_ACCEPT);
universe@7 366 return 1;
universe@7 367 } else {
universe@34 368 net_send_code(opponent, NETCODE_DECLINE);
universe@7 369 }
universe@34 370 break;
universe@34 371 case NETCODE_MOVE:
universe@34 372 net_recieve_data(opponent, &move, sizeof(Move));
universe@34 373 if (validate_move(gamestate, &move)) {
universe@34 374 apply_move(gamestate, &move);
universe@34 375 if (move.check) {
universe@34 376 net_send_code(opponent, NETCODE_CHECK);
universe@34 377 } else if (gamestate->checkmate) {
universe@34 378 net_send_code(opponent, NETCODE_CHECKMATE);
universe@34 379 printw("\rCheckmate!");
universe@34 380 clrtoeol();
universe@34 381 return 1;
universe@34 382 } else if (gamestate->stalemate) {
universe@34 383 net_send_code(opponent, NETCODE_STALEMATE);
universe@34 384 printw("\rStalemate!");
universe@34 385 clrtoeol();
universe@34 386 return 1;
universe@34 387 } else {
universe@34 388 net_send_code(opponent, NETCODE_ACCEPT);
universe@34 389 }
universe@34 390 return 0;
universe@34 391 } else {
universe@34 392 net_send_code(opponent, NETCODE_DECLINE);
universe@34 393 }
universe@33 394 }
universe@7 395 }
universe@7 396 }
universe@7 397 }
universe@6 398
universe@26 399 static void init_board(GameState *gamestate) {
universe@26 400 Board initboard = {
universe@26 401 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
universe@26 402 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
universe@26 403 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 404 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 405 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 406 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 407 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
universe@26 408 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
universe@26 409 };
universe@26 410 memcpy(gamestate->board, initboard, sizeof(Board));
universe@26 411 }
universe@26 412
universe@26 413 void game_start_singlemachine(Settings *settings) {
universe@33 414 inputy = getmaxy(stdscr) - 6;
universe@33 415
universe@26 416 GameState gamestate;
universe@28 417 memset(&gamestate, 0, sizeof(GameState));
universe@26 418 init_board(&gamestate);
universe@26 419 gamestate.mycolor = WHITE;
universe@30 420
universe@26 421 _Bool running;
universe@26 422 do {
universe@26 423 clear();
universe@26 424 draw_board(&gamestate);
universe@30 425 running = !domove_singlemachine(&gamestate, &(settings->gameinfo));
universe@26 426 gamestate.mycolor = opponent_color(gamestate.mycolor);
universe@26 427 } while (running);
universe@28 428 move(0,0);
universe@28 429 draw_board(&gamestate);
universe@26 430
universe@26 431 gamestate_cleanup(&gamestate);
universe@26 432
universe@26 433 mvaddstr(getmaxy(stdscr)-1, 0,
universe@26 434 "Game has ended. Press any key to leave...");
universe@26 435 refresh();
universe@26 436 getch();
universe@26 437 }
universe@26 438
universe@6 439 void game_start(Settings *settings, int opponent) {
universe@33 440 inputy = getmaxy(stdscr) - 6;
universe@33 441
universe@7 442 _Bool myturn = is_server(settings) ==
universe@7 443 (settings->gameinfo.servercolor == WHITE);
universe@8 444
universe@23 445 GameState gamestate;
universe@28 446 memset(&gamestate, 0, sizeof(GameState));
universe@26 447 init_board(&gamestate);
universe@23 448 gamestate.mycolor = myturn ? WHITE:BLACK;
universe@7 449
universe@23 450 _Bool running;
universe@7 451 do {
universe@7 452 clear();
universe@23 453 draw_board(&gamestate);
universe@7 454 if (myturn) {
universe@33 455 running = !sendmove(&gamestate, &(settings->gameinfo), opponent);
universe@7 456 } else {
universe@33 457 running = !recvmove(&gamestate, &(settings->gameinfo), opponent);
universe@7 458 }
universe@11 459 myturn ^= TRUE;
universe@7 460 } while (running);
universe@7 461
universe@28 462 move(0,0);
universe@28 463 draw_board(&gamestate);
universe@28 464
universe@23 465 gamestate_cleanup(&gamestate);
universe@18 466
universe@23 467 mvaddstr(getmaxy(stdscr)-1, 0,
universe@7 468 "Game has ended. Press any key to leave...");
universe@26 469 refresh();
universe@33 470 cbreak();
universe@33 471 flushinp();
universe@7 472 getch();
universe@6 473 }

mercurial