src/game.c

Wed, 29 Aug 2018 13:55:18 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 29 Aug 2018 13:55:18 +0200
changeset 64
4eda5df55f86
parent 63
611332453da0
child 68
b34de5ce7d0e
permissions
-rw-r--r--

fixes castling not printed correctly to PGN

universe@6 1 /*
universe@6 2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
universe@6 3 *
universe@55 4 * Copyright 2016 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@35 33 #include "colors.h"
universe@7 34 #include <ncurses.h>
universe@7 35 #include <string.h>
universe@30 36 #include <inttypes.h>
universe@34 37 #include <sys/select.h>
universe@50 38 #include <stdio.h>
universe@50 39 #include <errno.h>
universe@7 40
universe@63 41 static const uint8_t boardx = 4, boardy = 10;
universe@33 42 static int inputy = 21; /* should be overridden on game startup */
universe@7 43
universe@33 44 static int timecontrol(GameState *gamestate, GameInfo *gameinfo) {
universe@30 45 if (gameinfo->timecontrol) {
universe@33 46 uint16_t white = remaining_movetime(gameinfo, gamestate, WHITE);
universe@33 47 uint16_t black = remaining_movetime(gameinfo, gamestate, BLACK);
universe@30 48 mvprintw(boardy+4, boardx-1,
universe@33 49 "White time: %4" PRIu16 ":%02" PRIu16,
universe@33 50 white / 60, white % 60);
universe@30 51 mvprintw(boardy+5, boardx-1,
universe@33 52 "Black time: %4" PRIu16 ":%02" PRIu16,
universe@33 53 black / 60, black % 60);
universe@33 54
universe@33 55 if (white == 0) {
universe@33 56 move(inputy, 0);
universe@33 57 printw("Time is over - Black wins!");
universe@33 58 clrtobot();
universe@33 59 refresh();
universe@33 60 return 1;
universe@33 61 }
universe@33 62 if (black == 0) {
universe@33 63 move(inputy, 0);
universe@33 64 printw("Time is over - White wins!");
universe@33 65 clrtobot();
universe@33 66 refresh();
universe@33 67 return 1;
universe@33 68 }
universe@30 69 }
universe@33 70
universe@33 71 return 0;
universe@30 72 }
universe@30 73
universe@50 74 static void draw_board(GameState *gamestate, uint8_t perspective) {
universe@54 75 char fen[90];
universe@54 76 compute_fen(fen, gamestate);
universe@54 77 mvaddstr(0, 0, fen);
universe@54 78
universe@7 79 for (uint8_t y = 0 ; y < 8 ; y++) {
universe@7 80 for (uint8_t x = 0 ; x < 8 ; x++) {
universe@23 81 uint8_t col = gamestate->board[y][x] & COLOR_MASK;
universe@23 82 uint8_t piece = gamestate->board[y][x] & PIECE_MASK;
universe@18 83 char piecec;
universe@18 84 if (piece) {
universe@18 85 piecec = piece == PAWN ? 'P' : getpiecechr(piece);
universe@18 86 } else {
universe@18 87 piecec = ' ';
universe@7 88 }
universe@7 89
universe@35 90 _Bool boardblack = (y&1)==(x&1);
universe@35 91 attrset((col==WHITE ? A_BOLD : A_DIM)|
universe@35 92 COLOR_PAIR(col == WHITE ?
universe@35 93 (boardblack ? COL_WB : COL_WW) :
universe@35 94 (boardblack ? COL_BB : COL_BW)
universe@35 95 )
universe@35 96 );
universe@7 97
universe@50 98 int cy = perspective == WHITE ? boardy-y : boardy-7+y;
universe@50 99 int cx = perspective == WHITE ? boardx+x*3 : boardx+21-x*3;
universe@8 100 mvaddch(cy, cx, ' ');
universe@8 101 mvaddch(cy, cx+1, piecec);
universe@8 102 mvaddch(cy, cx+2, ' ');
universe@7 103 }
universe@7 104 }
universe@7 105
universe@7 106 attrset(A_NORMAL);
universe@7 107 for (uint8_t i = 0 ; i < 8 ; i++) {
universe@50 108 int x = perspective == WHITE ? boardx+i*3+1 : boardx+22-i*3;
universe@50 109 int y = perspective == WHITE ? boardy-i : boardy-7+i;
universe@8 110 mvaddch(boardy+1, x, 'a'+i);
universe@8 111 mvaddch(y, boardx-2, '1'+i);
universe@7 112 }
universe@18 113
universe@18 114 /* move log */
universe@63 115 uint8_t logy = 2;
universe@63 116 const uint8_t logx = boardx + 28;
universe@63 117 move(logy, logx);
universe@63 118
universe@63 119 unsigned int logi = 0;
universe@23 120 MoveList *logelem = gamestate->movelist;
universe@18 121
universe@63 122 /* wrap log after 45 moves */
universe@63 123 while (gamestate->movecount/6-logi/3 >= 15) {
universe@63 124 logelem = logelem->next->next;
universe@63 125 logi++;
universe@63 126 }
universe@63 127
universe@18 128 while (logelem) {
universe@63 129 _Bool iswhite = (logelem->move.piece & COLOR_MASK) == WHITE;
universe@63 130 if (iswhite) {
universe@63 131 logi++;
universe@63 132 printw("%d. ", logi);
universe@18 133 }
universe@18 134
universe@50 135 addstr(logelem->move.string);
universe@63 136 if (!iswhite && logi%3 == 0) {
universe@63 137 move(++logy, logx);
universe@63 138 } else {
universe@63 139 addch(' ');
universe@18 140 }
universe@50 141
universe@50 142 logelem = logelem->next;
universe@18 143 }
universe@7 144 }
universe@7 145
universe@26 146 static void eval_move_failed_msg(int code) {
universe@26 147 switch (code) {
universe@26 148 case AMBIGUOUS_MOVE:
universe@26 149 printw("Ambiguous move - please specify the piece to move.");
universe@26 150 break;
universe@26 151 case INVALID_POSITION:
universe@48 152 printw("No piece can be moved this way.");
universe@26 153 break;
universe@26 154 case NEED_PROMOTION:
universe@26 155 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
universe@26 156 break;
universe@47 157 case KING_IN_CHECK:
universe@47 158 printw("Your king is in check!");
universe@47 159 break;
universe@47 160 case PIECE_PINNED:
universe@47 161 printw("This piece is pinned!");
universe@47 162 break;
universe@47 163 case INVALID_MOVE_SYNTAX:
universe@47 164 printw("Can't interpret move - please use algebraic notation.");
universe@47 165 break;
universe@48 166 case RULES_VIOLATED:
universe@48 167 printw("Move does not comply chess rules.");
universe@48 168 break;
universe@48 169 case KING_MOVES_INTO_CHECK:
universe@48 170 printw("Can't move the king into a check position.");
universe@48 171 break;
universe@26 172 default:
universe@47 173 printw("Unknown move parser error.");
universe@26 174 }
universe@26 175 }
universe@26 176
universe@50 177 static void save_pgn(GameState *gamestate, GameInfo *gameinfo) {
universe@50 178 printw("Filename: ");
universe@50 179 clrtoeol();
universe@50 180 refresh();
universe@50 181
universe@50 182 char filename[64];
universe@50 183 int y = getcury(stdscr);
universe@51 184 if (getnstr(filename, 64) == OK && filename[0] != '\0') {
universe@50 185 move(y, 0);
universe@50 186 FILE *file = fopen(filename, "w");
universe@50 187 if (file) {
universe@50 188 write_pgn(file, gamestate, gameinfo);
universe@50 189 fclose(file);
universe@50 190 printw("File saved.");
universe@50 191 } else {
universe@50 192 printw("Can't write to file (%s).", strerror(errno));
universe@50 193 }
universe@50 194 clrtoeol();
universe@50 195 }
universe@50 196 }
universe@50 197
universe@50 198 #define MOVESTR_BUFLEN 10
universe@50 199 static int domove_singlemachine(GameState *gamestate,
universe@50 200 GameInfo *gameinfo, uint8_t curcolor) {
universe@26 201
universe@37 202
universe@32 203 size_t bufpos = 0;
universe@37 204 char movestr[MOVESTR_BUFLEN];
universe@26 205
universe@33 206 flushinp();
universe@26 207 while (1) {
universe@33 208 if (timecontrol(gamestate, gameinfo)) {
universe@33 209 return 1;
universe@33 210 }
universe@33 211
universe@26 212 move(inputy, 0);
universe@26 213 printw(
universe@26 214 "Use chess notation to enter your move.\n"
universe@50 215 "Or use a command: remis, resign, savepgn\n\n"
universe@26 216 "Type your move: ");
universe@26 217 clrtoeol();
universe@30 218
universe@37 219 if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) {
universe@44 220 if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) {
universe@50 221 gamestate->resign = 1;
universe@44 222 printw("%s resigned!",
universe@50 223 curcolor==WHITE?"White":"Black");
universe@50 224 clrtobot();
universe@30 225 refresh();
universe@30 226 return 1;
universe@37 227 } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) {
universe@50 228 gamestate->remis = 1;
universe@30 229 printw("Game ends remis.");
universe@50 230 clrtobot();
universe@30 231 refresh();
universe@30 232 return 1;
universe@50 233 } else if (strncmp(movestr, "savepgn", MOVESTR_BUFLEN) == 0) {
universe@50 234 save_pgn(gamestate, gameinfo);
universe@30 235 } else {
universe@30 236 Move move;
universe@50 237 int result = eval_move(gamestate, movestr, &move, curcolor);
universe@50 238 switch (result) {
universe@30 239 case VALID_MOVE_SYNTAX:
universe@50 240 result = validate_move(gamestate, &move);
universe@50 241 if (result == VALID_MOVE_SEMANTICS) {
universe@30 242 apply_move(gamestate, &move);
universe@30 243 if (gamestate->checkmate) {
universe@30 244 printw("Checkmate!");
universe@30 245 clrtoeol();
universe@30 246 return 1;
universe@30 247 } else if (gamestate->stalemate) {
universe@30 248 printw("Stalemate!");
universe@30 249 clrtoeol();
universe@30 250 return 1;
universe@30 251 } else {
universe@30 252 return 0;
universe@30 253 }
universe@26 254 } else {
universe@50 255 eval_move_failed_msg(result);
universe@26 256 }
universe@30 257 break;
universe@30 258 default:
universe@50 259 eval_move_failed_msg(result);
universe@26 260 }
universe@30 261 clrtoeol();
universe@26 262 }
universe@26 263 }
universe@26 264 }
universe@26 265 }
universe@8 266
universe@50 267 static int sendmove(GameState *gamestate, GameInfo *gameinfo,
universe@50 268 int opponent, uint8_t mycolor) {
universe@18 269
universe@33 270 size_t bufpos = 0;
universe@37 271 char movestr[MOVESTR_BUFLEN];
universe@7 272 _Bool remisrejected = FALSE;
universe@11 273 uint8_t code;
universe@7 274
universe@33 275 flushinp();
universe@7 276 while (1) {
universe@33 277 if (timecontrol(gamestate, gameinfo)) {
universe@33 278 net_send_code(opponent, NETCODE_TIMEOVER);
universe@33 279 return 1;
universe@33 280 }
universe@33 281
universe@18 282 move(inputy, 0);
universe@7 283 if (remisrejected) {
universe@7 284 printw(
universe@7 285 "Use chess notation to enter your move.\n"
universe@50 286 "Remis offer rejected \n\n"
universe@7 287 "Type your move: ");
universe@7 288 } else {
universe@7 289 printw(
universe@7 290 "Use chess notation to enter your move.\n"
universe@50 291 "Or use a command: remis, resign, savepgn\n\n"
universe@7 292 "Type your move: ");
universe@7 293 }
universe@7 294 clrtoeol();
universe@33 295
universe@37 296 if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) {
universe@44 297 if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) {
universe@50 298 gamestate->resign = 1;
universe@44 299 printw("You resigned!");
universe@33 300 clrtoeol();
universe@7 301 refresh();
universe@44 302 net_send_code(opponent, NETCODE_RESIGN);
universe@33 303 return 1;
universe@50 304 } else if (strncmp(movestr, "savepgn", MOVESTR_BUFLEN) == 0) {
universe@50 305 save_pgn(gamestate, gameinfo);
universe@37 306 } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) {
universe@33 307 if (!remisrejected) {
universe@33 308 net_send_code(opponent, NETCODE_REMIS);
universe@33 309 printw("Remis offer sent - waiting for acceptance...");
universe@8 310 refresh();
universe@46 311 code = net_recieve_code(opponent);
universe@46 312 if (code == NETCODE_ACCEPT) {
universe@50 313 gamestate->remis = 1;
universe@33 314 printw("\rRemis accepted!");
universe@18 315 clrtoeol();
universe@33 316 refresh();
universe@26 317 return 1;
universe@46 318 } else if (code == NETCODE_CONNLOST) {
universe@46 319 printw("\rYour opponent left the game.");
universe@46 320 clrtoeol();
universe@46 321 refresh();
universe@46 322 return 1;
universe@14 323 } else {
universe@33 324 remisrejected = TRUE;
universe@11 325 }
universe@18 326 }
universe@33 327 } else {
universe@33 328 Move move;
universe@50 329 int eval_result = eval_move(gamestate, movestr, &move, mycolor);
universe@33 330 switch (eval_result) {
universe@33 331 case VALID_MOVE_SYNTAX:
universe@51 332 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
universe@33 333 code = net_recieve_code(opponent);
universe@48 334 move.check = code == NETCODE_CHECK ||
universe@48 335 code == NETCODE_CHECKMATE;
universe@33 336 gamestate->checkmate = code == NETCODE_CHECKMATE;
universe@33 337 gamestate->stalemate = code == NETCODE_STALEMATE;
universe@33 338 if (code == NETCODE_DECLINE) {
universe@48 339 uint32_t reason;
universe@48 340 net_recieve_data(opponent, &reason, sizeof(uint32_t));
universe@48 341 reason = ntohl(reason);
universe@48 342 eval_move_failed_msg(reason);
universe@42 343 } else if (code == NETCODE_ACCEPT
universe@42 344 || code == NETCODE_CHECK
universe@42 345 || code == NETCODE_CHECKMATE
universe@42 346 || code == NETCODE_STALEMATE) {
universe@33 347 apply_move(gamestate, &move);
universe@33 348 if (gamestate->checkmate) {
universe@33 349 printw("Checkmate!");
universe@33 350 clrtoeol();
universe@33 351 return 1;
universe@33 352 } else if (gamestate->stalemate) {
universe@33 353 printw("Stalemate!");
universe@33 354 clrtoeol();
universe@33 355 return 1;
universe@33 356 } else {
universe@33 357 return 0;
universe@33 358 }
universe@46 359 } else if (code == NETCODE_CONNLOST) {
universe@46 360 printw("Your opponent left the game.");
universe@46 361 return 1;
universe@42 362 } else {
universe@42 363 printw("Invalid network response.");
universe@33 364 }
universe@33 365 break;
universe@33 366 default:
universe@33 367 eval_move_failed_msg(eval_result);
universe@33 368 }
universe@33 369 clrtoeol();
universe@8 370 }
universe@7 371 }
universe@7 372 }
universe@7 373 }
universe@7 374
universe@33 375 static int recvmove(GameState *gamestate, GameInfo *gameinfo, int opponent) {
universe@7 376
universe@34 377 struct timeval timeout;
universe@7 378 while (1) {
universe@33 379 timecontrol(gamestate, gameinfo);
universe@33 380
universe@18 381 move(inputy, 0);
universe@7 382 printw("Awaiting opponent move...");
universe@7 383 clrtoeol();
universe@7 384 refresh();
universe@7 385
universe@34 386 fd_set readfds;
universe@8 387
universe@34 388 FD_ZERO(&readfds);
universe@34 389 FD_SET(opponent, &readfds);
universe@34 390 timeout.tv_sec = 0;
universe@34 391 timeout.tv_usec = 1e5;
universe@34 392
universe@50 393 // TODO: allow commands
universe@50 394
universe@34 395 int result = select(opponent+1, &readfds, NULL, NULL, &timeout);
universe@34 396 if (result == -1) {
universe@34 397 printw("\rCannot perform asynchronous network IO");
universe@34 398 cbreak(); getch();
universe@34 399 exit(EXIT_FAILURE);
universe@34 400 }
universe@34 401 if (result > 0) {
universe@43 402 uint8_t code = net_recieve_code(opponent);
universe@34 403
universe@34 404 Move move;
universe@34 405 switch (code) {
universe@34 406 case NETCODE_TIMEOVER:
universe@34 407 printw("\rYour opponent's time ran out - you win!");
universe@7 408 clrtoeol();
universe@7 409 return 1;
universe@44 410 case NETCODE_RESIGN:
universe@50 411 gamestate->resign = 1;
universe@44 412 printw("\rYour opponent resigned!");
universe@34 413 clrtoeol();
universe@34 414 return 1;
universe@46 415 case NETCODE_CONNLOST:
universe@46 416 printw("\rYour opponent has left the game.");
universe@46 417 clrtoeol();
universe@46 418 return 1;
universe@34 419 case NETCODE_REMIS:
universe@34 420 if (prompt_yesno(
universe@34 421 "\rYour opponent offers remis - do you accept")) {
universe@50 422 gamestate->remis = 1;
universe@34 423 printw("\rRemis accepted!");
universe@7 424 clrtoeol();
universe@34 425 net_send_code(opponent, NETCODE_ACCEPT);
universe@7 426 return 1;
universe@7 427 } else {
universe@34 428 net_send_code(opponent, NETCODE_DECLINE);
universe@7 429 }
universe@34 430 break;
universe@34 431 case NETCODE_MOVE:
universe@51 432 net_recieve_data(opponent, &move, sizeof(Move));
universe@48 433 code = validate_move(gamestate, &move);
universe@48 434 if (code == VALID_MOVE_SEMANTICS) {
universe@34 435 apply_move(gamestate, &move);
universe@51 436 if (gamestate->checkmate) {
universe@34 437 net_send_code(opponent, NETCODE_CHECKMATE);
universe@34 438 printw("\rCheckmate!");
universe@34 439 clrtoeol();
universe@34 440 return 1;
universe@34 441 } else if (gamestate->stalemate) {
universe@34 442 net_send_code(opponent, NETCODE_STALEMATE);
universe@34 443 printw("\rStalemate!");
universe@34 444 clrtoeol();
universe@34 445 return 1;
universe@51 446 } else if (move.check) {
universe@51 447 net_send_code(opponent, NETCODE_CHECK);
universe@34 448 } else {
universe@34 449 net_send_code(opponent, NETCODE_ACCEPT);
universe@34 450 }
universe@34 451 return 0;
universe@34 452 } else {
universe@48 453 uint32_t reason = htonl(code);
universe@48 454 net_send_data(opponent, NETCODE_DECLINE,
universe@48 455 &reason, sizeof(uint32_t));
universe@34 456 }
universe@45 457 break;
universe@45 458 default:
universe@45 459 printw("\nInvalid network request.");
universe@33 460 }
universe@7 461 }
universe@7 462 }
universe@7 463 }
universe@6 464
universe@50 465 static void post_game(GameState *gamestate, GameInfo *gameinfo) {
universe@50 466 move(0,0);
universe@50 467 draw_board(gamestate, WHITE);
universe@50 468
universe@50 469 // TODO: network connection is still open here - think about it!
universe@50 470
universe@50 471 mvaddstr(getmaxy(stdscr)-1, 0,
universe@50 472 "Press 'q' to quit or 's' to save a PGN file...");
universe@50 473 refresh();
universe@50 474 flushinp();
universe@50 475
universe@50 476 noecho();
universe@50 477 int c;
universe@50 478 do {
universe@50 479 c = getch();
universe@50 480 if (c == 's') {
universe@50 481 addch('\r');
universe@50 482 echo();
universe@50 483 save_pgn(gamestate, gameinfo);
universe@50 484 addstr(" Press 'q' to quit...");
universe@50 485 noecho();
universe@50 486 }
universe@50 487 } while (c != 'q');
universe@50 488 echo();
universe@50 489
universe@50 490 gamestate_cleanup(gamestate);
universe@26 491 }
universe@26 492
universe@26 493 void game_start_singlemachine(Settings *settings) {
universe@33 494 inputy = getmaxy(stdscr) - 6;
universe@33 495
universe@26 496 GameState gamestate;
universe@50 497 gamestate_init(&gamestate);
universe@50 498 uint8_t curcol = WHITE;
universe@50 499
universe@50 500 if (settings->continuepgn) {
universe@50 501 FILE *pgnfile = fopen(settings->continuepgn, "r");
universe@50 502 if (pgnfile) {
universe@50 503 int result = read_pgn(pgnfile, &gamestate, &(settings->gameinfo));
universe@60 504 long position = ftell(pgnfile);
universe@50 505 fclose(pgnfile);
universe@59 506 if (result) {
universe@60 507 printw("Invalid PGN file content at position %ld:\n%s\n",
universe@60 508 position, pgn_error_str(result));
universe@50 509 return;
universe@50 510 }
universe@50 511 if (!is_game_running(&gamestate)) {
universe@50 512 addstr("Game has ended. Use -S to analyze it.\n");
universe@50 513 return;
universe@50 514 }
universe@50 515 curcol = opponent_color(gamestate.lastmove->move.piece&COLOR_MASK);
universe@50 516 } else {
universe@50 517 printw("Can't read PGN file (%s)\n", strerror(errno));
universe@50 518 return;
universe@50 519 }
universe@50 520 }
universe@30 521
universe@26 522 _Bool running;
universe@26 523 do {
universe@26 524 clear();
universe@50 525 draw_board(&gamestate, curcol);
universe@50 526 running = !domove_singlemachine(&gamestate,
universe@50 527 &(settings->gameinfo), curcol);
universe@50 528 curcol = opponent_color(curcol);
universe@26 529 } while (running);
universe@26 530
universe@50 531 post_game(&gamestate, &(settings->gameinfo));
universe@26 532 }
universe@26 533
universe@51 534 void game_continue(Settings *settings, int opponent, GameState *gamestate) {
universe@33 535 inputy = getmaxy(stdscr) - 6;
universe@33 536
universe@51 537 uint8_t mycolor = is_server(settings) ? settings->gameinfo.servercolor :
universe@51 538 opponent_color(settings->gameinfo.servercolor);
universe@8 539
universe@51 540 _Bool myturn = (gamestate->lastmove ?
universe@53 541 (gamestate->lastmove->move.piece & COLOR_MASK) : BLACK) != mycolor;
universe@7 542
universe@23 543 _Bool running;
universe@7 544 do {
universe@7 545 clear();
universe@51 546 draw_board(gamestate, mycolor);
universe@7 547 if (myturn) {
universe@51 548 running = !sendmove(gamestate, &(settings->gameinfo),
universe@50 549 opponent, mycolor);
universe@7 550 } else {
universe@51 551 running = !recvmove(gamestate, &(settings->gameinfo), opponent);
universe@7 552 }
universe@11 553 myturn ^= TRUE;
universe@7 554 } while (running);
universe@7 555
universe@51 556 post_game(gamestate, &(settings->gameinfo));
universe@6 557 }
universe@51 558
universe@51 559 void game_start(Settings *settings, int opponent) {
universe@51 560 GameState gamestate;
universe@51 561 gamestate_init(&gamestate);
universe@51 562
universe@51 563 game_continue(settings, opponent, &gamestate);
universe@51 564 }

mercurial