src/game.c

Wed, 09 Apr 2014 11:12:04 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 09 Apr 2014 11:12:04 +0200
changeset 33
866025982aa9
parent 32
8a0b85303ee8
child 34
c4d4b8a8f902
permissions
-rw-r--r--

implemented time control

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@7 36
universe@7 37 static const uint8_t boardx = 10, boardy = 10;
universe@33 38 static int inputy = 21; /* should be overridden on game startup */
universe@7 39
universe@33 40 static int timecontrol(GameState *gamestate, GameInfo *gameinfo) {
universe@30 41 if (gameinfo->timecontrol) {
universe@33 42 uint16_t white = remaining_movetime(gameinfo, gamestate, WHITE);
universe@33 43 uint16_t black = remaining_movetime(gameinfo, gamestate, BLACK);
universe@30 44 mvprintw(boardy+4, boardx-1,
universe@33 45 "White time: %4" PRIu16 ":%02" PRIu16,
universe@33 46 white / 60, white % 60);
universe@30 47 mvprintw(boardy+5, boardx-1,
universe@33 48 "Black time: %4" PRIu16 ":%02" PRIu16,
universe@33 49 black / 60, black % 60);
universe@33 50
universe@33 51 if (white == 0) {
universe@33 52 move(inputy, 0);
universe@33 53 printw("Time is over - Black wins!");
universe@33 54 clrtobot();
universe@33 55 refresh();
universe@33 56 return 1;
universe@33 57 }
universe@33 58 if (black == 0) {
universe@33 59 move(inputy, 0);
universe@33 60 printw("Time is over - White wins!");
universe@33 61 clrtobot();
universe@33 62 refresh();
universe@33 63 return 1;
universe@33 64 }
universe@30 65 }
universe@33 66
universe@33 67 return 0;
universe@30 68 }
universe@30 69
universe@23 70 static void draw_board(GameState *gamestate) {
universe@7 71 for (uint8_t y = 0 ; y < 8 ; y++) {
universe@7 72 for (uint8_t x = 0 ; x < 8 ; x++) {
universe@23 73 uint8_t col = gamestate->board[y][x] & COLOR_MASK;
universe@23 74 uint8_t piece = gamestate->board[y][x] & PIECE_MASK;
universe@18 75 char piecec;
universe@18 76 if (piece) {
universe@18 77 piecec = piece == PAWN ? 'P' : getpiecechr(piece);
universe@18 78 } else {
universe@18 79 piecec = ' ';
universe@7 80 }
universe@7 81
universe@7 82 attrset((col == WHITE ? A_BOLD : A_DIM) |
universe@7 83 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
universe@7 84
universe@23 85 int cy = gamestate->mycolor == WHITE ? boardy-y : boardy-7+y;
universe@23 86 int cx = gamestate->mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
universe@8 87 mvaddch(cy, cx, ' ');
universe@8 88 mvaddch(cy, cx+1, piecec);
universe@8 89 mvaddch(cy, cx+2, ' ');
universe@7 90 }
universe@7 91 }
universe@7 92
universe@7 93 attrset(A_NORMAL);
universe@7 94 for (uint8_t i = 0 ; i < 8 ; i++) {
universe@23 95 int x = gamestate->mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
universe@23 96 int y = gamestate->mycolor == WHITE ? boardy-i : boardy-7+i;
universe@8 97 mvaddch(boardy+1, x, 'a'+i);
universe@8 98 mvaddch(y, boardx-2, '1'+i);
universe@7 99 }
universe@18 100
universe@18 101 /* move log */
universe@18 102 // TODO: introduce window to avoid bugs with a long move log
universe@18 103 uint8_t logy = 0;
universe@18 104 const uint8_t logx = boardx + 30;
universe@18 105 int logi = 1;
universe@23 106 MoveList *logelem = gamestate->movelist;
universe@18 107
universe@18 108 while (logelem) {
universe@18 109 logi++;
universe@18 110 if (logi % 2 == 0) {
universe@18 111 if ((logi - 2) % 4 == 0) {
universe@18 112 logy++;
universe@23 113 move(logy, logx);
universe@18 114 }
universe@18 115 printw("%d. ", logi / 2);
universe@18 116 }
universe@18 117
universe@18 118 if (logelem) {
universe@18 119 Move move = logelem->move;
universe@25 120 if ((move.piece&PIECE_MASK) == KING &&
universe@25 121 abs(move.tofile-move.fromfile) == 2) {
universe@25 122 addstr(move.tofile==fileidx('c')?"O-O-O":"O-O");
universe@25 123 } else {
universe@25 124 char logstr[] = {
universe@25 125 getpiecechr(move.piece),
universe@25 126 filechr(move.fromfile), rowchr(move.fromrow),
universe@25 127 move.capture ? 'x':'\0',
universe@25 128 filechr(move.tofile), rowchr(move.torow),
universe@27 129 move.check ? '+' : (move.promotion ? '=' : '\0'),
universe@25 130 move.promotion ? getpiecechr(move.promotion) : '\0'
universe@25 131 };
universe@25 132 for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
universe@25 133 if (logstr[stri]) {
universe@25 134 addch(logstr[stri]);
universe@25 135 }
universe@18 136 }
universe@18 137 }
universe@27 138 if (!logelem->next) {
universe@27 139 if (gamestate->checkmate) {
universe@28 140 addstr("\b#");
universe@27 141 } else if (gamestate->stalemate) {
universe@27 142 addstr(" stalemate");
universe@27 143 }
universe@27 144 }
universe@25 145 addch(' ');
universe@18 146
universe@18 147 logelem = logelem->next;
universe@18 148 }
universe@18 149 }
universe@7 150 }
universe@7 151
universe@26 152 static void eval_move_failed_msg(int code) {
universe@26 153 switch (code) {
universe@26 154 case AMBIGUOUS_MOVE:
universe@26 155 printw("Ambiguous move - please specify the piece to move.");
universe@26 156 break;
universe@26 157 case INVALID_POSITION:
universe@26 158 printw("Cannot find the piece that shall be moved.");
universe@26 159 break;
universe@26 160 case NEED_PROMOTION:
universe@26 161 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
universe@26 162 break;
universe@26 163 default:
universe@26 164 printw("Can't interpret move - please use algebraic notation.");
universe@26 165 }
universe@26 166 }
universe@26 167
universe@30 168 static int domove_singlemachine(GameState *gamestate, GameInfo *gameinfo) {
universe@26 169
universe@26 170 const size_t buflen = 8;
universe@32 171 size_t bufpos = 0;
universe@26 172 char movestr[buflen];
universe@26 173
universe@33 174 flushinp();
universe@26 175 while (1) {
universe@33 176 if (timecontrol(gamestate, gameinfo)) {
universe@33 177 return 1;
universe@33 178 }
universe@33 179
universe@26 180 move(inputy, 0);
universe@26 181 printw(
universe@26 182 "Use chess notation to enter your move.\n"
universe@26 183 "Or type 'surr' to surrender or 'remis' to end with remis.\n\n"
universe@26 184 "Type your move: ");
universe@26 185 clrtoeol();
universe@30 186
universe@32 187 if (asyncgetnstr(movestr, &bufpos, buflen)) {
universe@30 188 if (strncmp(movestr, "surr", buflen) == 0) {
universe@30 189 printw("%s surrendered!",
universe@30 190 gamestate->mycolor==WHITE?"White":"Black");
universe@30 191 clrtoeol();
universe@30 192 refresh();
universe@30 193 return 1;
universe@30 194 } else if (strncmp(movestr, "remis", buflen) == 0) {
universe@30 195 printw("Game ends remis.");
universe@30 196 clrtoeol();
universe@30 197 refresh();
universe@30 198 return 1;
universe@30 199 } else {
universe@30 200 Move move;
universe@30 201 int eval_result = eval_move(gamestate, movestr, &move);
universe@30 202 switch (eval_result) {
universe@30 203 case VALID_MOVE_SYNTAX:
universe@30 204 if (validate_move(gamestate, &move)) {
universe@30 205 apply_move(gamestate, &move);
universe@30 206 if (gamestate->checkmate) {
universe@30 207 printw("Checkmate!");
universe@30 208 clrtoeol();
universe@30 209 return 1;
universe@30 210 } else if (gamestate->stalemate) {
universe@30 211 printw("Stalemate!");
universe@30 212 clrtoeol();
universe@30 213 return 1;
universe@30 214 } else {
universe@30 215 return 0;
universe@30 216 }
universe@26 217 } else {
universe@30 218 printw("Invalid move.");
universe@26 219 }
universe@30 220 break;
universe@30 221 default:
universe@30 222 eval_move_failed_msg(eval_result);
universe@26 223 }
universe@30 224 clrtoeol();
universe@26 225 }
universe@26 226 }
universe@26 227 }
universe@26 228 }
universe@8 229
universe@33 230 static int sendmove(GameState *gamestate, GameInfo *gameinfo, int opponent) {
universe@18 231
universe@7 232 const size_t buflen = 8;
universe@33 233 size_t bufpos = 0;
universe@8 234 char movestr[buflen];
universe@7 235 _Bool remisrejected = FALSE;
universe@11 236 uint8_t code;
universe@7 237
universe@33 238 flushinp();
universe@7 239 while (1) {
universe@33 240 if (timecontrol(gamestate, gameinfo)) {
universe@33 241 net_send_code(opponent, NETCODE_TIMEOVER);
universe@33 242 return 1;
universe@33 243 }
universe@33 244
universe@18 245 move(inputy, 0);
universe@7 246 if (remisrejected) {
universe@7 247 printw(
universe@7 248 "Use chess notation to enter your move.\n"
universe@7 249 "Remis offer rejected - type 'surr' to surrender. \n\n"
universe@7 250 "Type your move: ");
universe@7 251 } else {
universe@7 252 printw(
universe@7 253 "Use chess notation to enter your move.\n"
universe@7 254 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
universe@7 255 "Type your move: ");
universe@7 256 }
universe@7 257 clrtoeol();
universe@33 258
universe@33 259 if (asyncgetnstr(movestr, &bufpos, buflen)) {
universe@33 260 if (strncmp(movestr, "surr", buflen) == 0) {
universe@33 261 printw("You surrendered!");
universe@33 262 clrtoeol();
universe@7 263 refresh();
universe@33 264 net_send_code(opponent, NETCODE_SURRENDER);
universe@33 265 return 1;
universe@33 266 } else if (strncmp(movestr, "remis", buflen) == 0) {
universe@33 267 if (!remisrejected) {
universe@33 268 net_send_code(opponent, NETCODE_REMIS);
universe@33 269 printw("Remis offer sent - waiting for acceptance...");
universe@8 270 refresh();
universe@33 271 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
universe@33 272 printw("\rRemis accepted!");
universe@18 273 clrtoeol();
universe@33 274 refresh();
universe@26 275 return 1;
universe@14 276 } else {
universe@33 277 remisrejected = TRUE;
universe@11 278 }
universe@18 279 }
universe@33 280 } else {
universe@33 281 Move move;
universe@33 282 int eval_result = eval_move(gamestate, movestr, &move);
universe@33 283 switch (eval_result) {
universe@33 284 case VALID_MOVE_SYNTAX:
universe@33 285 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
universe@33 286 code = net_recieve_code(opponent);
universe@33 287 move.check = code == NETCODE_CHECK;
universe@33 288 gamestate->checkmate = code == NETCODE_CHECKMATE;
universe@33 289 gamestate->stalemate = code == NETCODE_STALEMATE;
universe@33 290 if (code == NETCODE_DECLINE) {
universe@33 291 printw("Invalid move.");
universe@33 292 } else {
universe@33 293 apply_move(gamestate, &move);
universe@33 294 if (gamestate->checkmate) {
universe@33 295 printw("Checkmate!");
universe@33 296 clrtoeol();
universe@33 297 return 1;
universe@33 298 } else if (gamestate->stalemate) {
universe@33 299 printw("Stalemate!");
universe@33 300 clrtoeol();
universe@33 301 return 1;
universe@33 302 } else {
universe@33 303 return 0;
universe@33 304 }
universe@33 305 }
universe@33 306 break;
universe@33 307 default:
universe@33 308 eval_move_failed_msg(eval_result);
universe@33 309 }
universe@33 310 clrtoeol();
universe@8 311 }
universe@7 312 }
universe@7 313 }
universe@7 314 }
universe@7 315
universe@33 316 static int recvmove(GameState *gamestate, GameInfo *gameinfo, int opponent) {
universe@7 317
universe@7 318 while (1) {
universe@33 319 timecontrol(gamestate, gameinfo);
universe@33 320
universe@18 321 move(inputy, 0);
universe@7 322 printw("Awaiting opponent move...");
universe@7 323 clrtoeol();
universe@7 324 refresh();
universe@7 325
universe@7 326 // TODO: nonblocking
universe@7 327 uint32_t code = net_recieve_code(opponent);
universe@8 328
universe@8 329 Move move;
universe@7 330 switch (code) {
universe@33 331 case NETCODE_TIMEOVER:
universe@33 332 printw("\rYour opponent's time ran out - you win!");
universe@33 333 clrtoeol();
universe@33 334 return 1;
universe@33 335 case NETCODE_SURRENDER:
universe@33 336 printw("\rYour opponent surrendered!");
universe@33 337 clrtoeol();
universe@33 338 return 1;
universe@33 339 case NETCODE_REMIS:
universe@33 340 if (prompt_yesno(
universe@33 341 "\rYour opponent offers remis - do you accept")) {
universe@33 342 printw("\rRemis accepted!");
universe@7 343 clrtoeol();
universe@33 344 net_send_code(opponent, NETCODE_ACCEPT);
universe@7 345 return 1;
universe@33 346 } else {
universe@33 347 net_send_code(opponent, NETCODE_DECLINE);
universe@33 348 }
universe@33 349 break;
universe@33 350 case NETCODE_MOVE:
universe@33 351 net_recieve_data(opponent, &move, sizeof(Move));
universe@33 352 if (validate_move(gamestate, &move)) {
universe@33 353 apply_move(gamestate, &move);
universe@33 354 if (move.check) {
universe@33 355 net_send_code(opponent, NETCODE_CHECK);
universe@33 356 } else if (gamestate->checkmate) {
universe@33 357 net_send_code(opponent, NETCODE_CHECKMATE);
universe@33 358 printw("\rCheckmate!");
universe@7 359 clrtoeol();
universe@33 360 return 1;
universe@33 361 } else if (gamestate->stalemate) {
universe@33 362 net_send_code(opponent, NETCODE_STALEMATE);
universe@33 363 printw("\rStalemate!");
universe@33 364 clrtoeol();
universe@7 365 return 1;
universe@7 366 } else {
universe@33 367 net_send_code(opponent, NETCODE_ACCEPT);
universe@7 368 }
universe@33 369 return 0;
universe@33 370 } else {
universe@33 371 net_send_code(opponent, NETCODE_DECLINE);
universe@33 372 }
universe@7 373 }
universe@7 374 }
universe@7 375 }
universe@6 376
universe@26 377 static void init_board(GameState *gamestate) {
universe@26 378 Board initboard = {
universe@26 379 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
universe@26 380 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
universe@26 381 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 382 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 383 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 384 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 385 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
universe@26 386 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
universe@26 387 };
universe@26 388 memcpy(gamestate->board, initboard, sizeof(Board));
universe@26 389 }
universe@26 390
universe@26 391 void game_start_singlemachine(Settings *settings) {
universe@33 392 inputy = getmaxy(stdscr) - 6;
universe@33 393
universe@26 394 GameState gamestate;
universe@28 395 memset(&gamestate, 0, sizeof(GameState));
universe@26 396 init_board(&gamestate);
universe@26 397 gamestate.mycolor = WHITE;
universe@30 398
universe@26 399 _Bool running;
universe@26 400 do {
universe@26 401 clear();
universe@26 402 draw_board(&gamestate);
universe@30 403 running = !domove_singlemachine(&gamestate, &(settings->gameinfo));
universe@26 404 gamestate.mycolor = opponent_color(gamestate.mycolor);
universe@26 405 } while (running);
universe@28 406 move(0,0);
universe@28 407 draw_board(&gamestate);
universe@26 408
universe@26 409 gamestate_cleanup(&gamestate);
universe@26 410
universe@26 411 mvaddstr(getmaxy(stdscr)-1, 0,
universe@26 412 "Game has ended. Press any key to leave...");
universe@26 413 refresh();
universe@26 414 getch();
universe@26 415 }
universe@26 416
universe@6 417 void game_start(Settings *settings, int opponent) {
universe@33 418 inputy = getmaxy(stdscr) - 6;
universe@33 419
universe@7 420 _Bool myturn = is_server(settings) ==
universe@7 421 (settings->gameinfo.servercolor == WHITE);
universe@8 422
universe@23 423 GameState gamestate;
universe@28 424 memset(&gamestate, 0, sizeof(GameState));
universe@26 425 init_board(&gamestate);
universe@23 426 gamestate.mycolor = myturn ? WHITE:BLACK;
universe@7 427
universe@23 428 _Bool running;
universe@7 429 do {
universe@7 430 clear();
universe@23 431 draw_board(&gamestate);
universe@7 432 if (myturn) {
universe@33 433 running = !sendmove(&gamestate, &(settings->gameinfo), opponent);
universe@7 434 } else {
universe@33 435 running = !recvmove(&gamestate, &(settings->gameinfo), opponent);
universe@7 436 }
universe@11 437 myturn ^= TRUE;
universe@7 438 } while (running);
universe@7 439
universe@28 440 move(0,0);
universe@28 441 draw_board(&gamestate);
universe@28 442
universe@23 443 gamestate_cleanup(&gamestate);
universe@18 444
universe@23 445 mvaddstr(getmaxy(stdscr)-1, 0,
universe@7 446 "Game has ended. Press any key to leave...");
universe@26 447 refresh();
universe@33 448 cbreak();
universe@33 449 flushinp();
universe@7 450 getch();
universe@6 451 }

mercurial