src/game.c

Mon, 07 Apr 2014 17:39:46 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 07 Apr 2014 17:39:46 +0200
changeset 30
a285ee393860
parent 28
0c1371488d87
child 31
ed440bcd9740
permissions
-rw-r--r--

experimental async input for single machine mode

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

mercurial