src/game.c

Wed, 26 Mar 2014 14:53:15 +0100

author
Mike Becker <universe@uap-core.de>
date
Wed, 26 Mar 2014 14:53:15 +0100
changeset 14
970748b9a73b
parent 13
faec61c4901f
child 15
7ffd66591afe
permissions
-rw-r--r--

fixed crucial bug where both players could move at the same time + added pawn rules (TODO: en passant)

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@7 31 #include "input.h"
universe@10 32 #include "rules/rules.h"
universe@7 33 #include <ncurses.h>
universe@7 34 #include <string.h>
universe@7 35
universe@7 36 static const uint8_t boardx = 10, boardy = 10;
universe@7 37
universe@8 38 static void draw_board(Board board, uint8_t mycolor) {
universe@7 39
universe@7 40 for (uint8_t y = 0 ; y < 8 ; y++) {
universe@7 41 for (uint8_t x = 0 ; x < 8 ; x++) {
universe@7 42 uint8_t col = board[y][x] & COLOR_MASK;
universe@7 43 uint8_t piece = board[y][x] & PIECE_MASK;
universe@7 44 char piecec = ' ';
universe@7 45 switch (piece) {
universe@7 46 case PAWN: piecec = 'P'; break;
universe@7 47 case ROOK: piecec = 'R'; break;
universe@7 48 case KNIGHT: piecec = 'N'; break;
universe@7 49 case BISHOP: piecec = 'B'; break;
universe@7 50 case QUEEN: piecec = 'Q'; break;
universe@7 51 case KING: piecec = 'K'; break;
universe@7 52 }
universe@7 53
universe@7 54 attrset((col == WHITE ? A_BOLD : A_DIM) |
universe@7 55 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
universe@7 56
universe@8 57 int cy = mycolor == WHITE ? boardy-y : boardy-7+y;
universe@8 58 int cx = mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
universe@8 59 mvaddch(cy, cx, ' ');
universe@8 60 mvaddch(cy, cx+1, piecec);
universe@8 61 mvaddch(cy, cx+2, ' ');
universe@7 62 }
universe@7 63 }
universe@7 64
universe@7 65 attrset(A_NORMAL);
universe@7 66 for (uint8_t i = 0 ; i < 8 ; i++) {
universe@8 67 int x = mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
universe@8 68 int y = mycolor == WHITE ? boardy-i : boardy-7+i;
universe@8 69 mvaddch(boardy+1, x, 'a'+i);
universe@8 70 mvaddch(y, boardx-2, '1'+i);
universe@7 71 }
universe@7 72 }
universe@7 73
universe@13 74 /**
universe@13 75 * Applies a move and deletes captured pieces.
universe@13 76 *
universe@13 77 * @param board the current board state
universe@13 78 * @param move the move to apply
universe@13 79 */
universe@8 80 static void apply_move(Board board, Move *move) {
universe@14 81 msrc(board, move) = 0;
universe@8 82 // TODO: care for en passant capture
universe@14 83 mdst(board, move) = move->piece;
universe@9 84
universe@9 85 /* castling */
universe@9 86 if ((move->piece & PIECE_MASK) == KING &&
universe@9 87 move->fromfile == fileidx('e')) {
universe@9 88 uint8_t color = move->piece & COLOR_MASK;
universe@9 89
universe@9 90 if (move->tofile == fileidx('g')) {
universe@9 91 board[move->torow][fileidx('h')] = 0;
universe@9 92 board[move->torow][fileidx('f')] = color|ROOK;
universe@9 93 } else if (move->tofile == fileidx('c')) {
universe@9 94 board[move->torow][fileidx('a')] = 0;
universe@9 95 board[move->torow][fileidx('d')] = color|ROOK;
universe@9 96 }
universe@9 97 }
universe@8 98 }
universe@8 99
universe@13 100 /**
universe@13 101 * Validates move by applying chess rules.
universe@13 102 * @param board the current board state
universe@13 103 * @param move the move to validate
universe@13 104 * @return TRUE, if the move complies to chess rules, FALSE otherwise
universe@13 105 */
universe@11 106 static _Bool validate_move(Board board, Move *move) {
universe@10 107 _Bool result;
universe@8 108
universe@13 109 /* validate indices (don't trust opponent) */
universe@13 110 if (!chkidx(move)) {
universe@13 111 return FALSE;
universe@13 112 }
universe@13 113
universe@8 114 /* does piece exist */
universe@14 115 result = msrc(board, move) == move->piece;
universe@8 116
universe@14 117 /* can't capture own pieces */
universe@14 118 if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) {
universe@14 119 return FALSE;
universe@14 120 }
universe@14 121
universe@14 122 /* validate individual rules */
universe@10 123 switch (move->piece & PIECE_MASK) {
universe@10 124 case PAWN:
universe@10 125 result = result && pawn_chkrules(board, move);
universe@10 126 result = result && !pawn_isblocked(board, move);
universe@10 127 break;
universe@10 128 case ROOK:
universe@10 129 result = result && rook_chkrules(board, move);
universe@10 130 result = result && !rook_isblocked(board, move);
universe@10 131 break;
universe@10 132 case KNIGHT:
universe@10 133 result = result && knight_chkrules(board, move);
universe@10 134 result = result && !knight_isblocked(board, move);
universe@10 135 break;
universe@10 136 case BISHOP:
universe@10 137 result = result && bishop_chkrules(board, move);
universe@10 138 result = result && !bishop_isblocked(board, move);
universe@10 139 break;
universe@10 140 case QUEEN:
universe@10 141 result = result && queen_chkrules(board, move);
universe@10 142 result = result && !queen_isblocked(board, move);
universe@10 143 break;
universe@10 144 case KING:
universe@10 145 result = result && king_chkrules(board, move);
universe@10 146 result = result && !king_isblocked(board, move);
universe@10 147 break;
universe@10 148 default:
universe@10 149 result = FALSE;
universe@10 150 }
universe@8 151
universe@8 152 /* is piece pinned */
universe@8 153 // TODO: make it so
universe@8 154
universe@11 155 /* correct check and checkmate flags */
universe@11 156 // TODO: make it so
universe@11 157
universe@8 158 return result;
universe@8 159 }
universe@8 160
universe@12 161 /**
universe@12 162 * Maps a character to a piece.
universe@12 163 *
universe@12 164 * Does not work for pawns, since they don't have a character.
universe@12 165 *
universe@12 166 * @param c one of R,N,B,Q,K
universe@12 167 * @return numeric value for the specified piece
universe@12 168 */
universe@12 169 static uint8_t getpiece(char c) {
universe@12 170 switch (c) {
universe@12 171 case 'R': return ROOK;
universe@12 172 case 'N': return KNIGHT;
universe@12 173 case 'B': return BISHOP;
universe@12 174 case 'Q': return QUEEN;
universe@12 175 case 'K': return KING;
universe@12 176 default: return 0;
universe@12 177 }
universe@12 178 }
universe@12 179
universe@12 180 /**
universe@12 181 * Guesses the location of a piece for short algebraic notation.
universe@12 182 *
universe@12 183 * @param board the current state of the board
universe@12 184 * @param move the move date to operate on
universe@12 185 * @return TRUE if the location could be retrieved, FALSE if the location is
universe@12 186 * ambiguous
universe@12 187 */
universe@13 188 static _Bool getlocation(Board board, Move *move) {
universe@12 189 uint8_t piece = move->piece & PIECE_MASK;
universe@12 190 switch (piece) {
universe@12 191 case PAWN: return pawn_getlocation(board, move);
universe@12 192 case ROOK: return rook_getlocation(board, move);
universe@12 193 case KNIGHT: return knight_getlocation(board, move);
universe@12 194 case BISHOP: return bishop_getlocation(board, move);
universe@12 195 case QUEEN: return queen_getlocation(board, move);
universe@12 196 case KING: return king_getlocation(board, move);
universe@12 197 default: return FALSE;
universe@12 198 }
universe@12 199 }
universe@12 200
universe@12 201 /**
universe@12 202 * Evaluates a move syntactically and stores the move data in the specified
universe@12 203 * object.
universe@12 204 *
universe@12 205 * @param board the current state of the board
universe@12 206 * @param mycolor the color of the current player
universe@12 207 * @param mstr the input string to parse
universe@12 208 * @param move a pointer to object where the move data shall be stored
universe@12 209 * @return TRUE, if the move is syntactically valid, FALSE otherwise
universe@12 210 */
universe@9 211 static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
universe@8 212 memset(move, 0, sizeof(Move));
universe@12 213 move->fromfile = POS_UNSPECIFIED;
universe@12 214 move->fromrow = POS_UNSPECIFIED;
universe@8 215
universe@9 216 size_t len = strlen(mstr);
universe@8 217
universe@11 218 /* evaluate check/checkmate flags */
universe@9 219 if (mstr[len-1] == '+') {
universe@9 220 len--; mstr[len] = '\0';
universe@8 221 move->check = TRUE;
universe@11 222 } else if (mstr[len-1] == '#') {
universe@11 223 len--; mstr[len] = '\0';
universe@11 224 move->checkmate = TRUE;
universe@8 225 }
universe@8 226
universe@8 227 if (len == 2) {
universe@8 228 /* pawn move (e.g. "e4") */
universe@13 229 move->piece = PAWN;
universe@13 230 move->tofile = fileidx(mstr[0]);
universe@13 231 move->torow = rowidx(mstr[1]);
universe@8 232 } else if (len == 3) {
universe@9 233 if (strcmp(mstr, "O-O") == 0) {
universe@8 234 /* king side castling */
universe@12 235 move->piece = KING;
universe@8 236 move->fromfile = fileidx('e');
universe@9 237 move->tofile = fileidx('g');
universe@8 238 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
universe@8 239 } else {
universe@13 240 /* move (e.g. "Nf3") */
universe@12 241 move->piece = getpiece(mstr[0]);
universe@13 242 move->tofile = fileidx(mstr[1]);
universe@13 243 move->torow = rowidx(mstr[2]);
universe@8 244 }
universe@8 245
universe@8 246 } else if (len == 4) {
universe@13 247 move->piece = getpiece(mstr[0]);
universe@13 248 if (mstr[1] == 'x') {
universe@13 249 /* capture (e.g. "Nxf3", "dxe5") */
universe@13 250 move->capture = TRUE;
universe@13 251 if (!move->piece) {
universe@13 252 move->piece = PAWN;
universe@13 253 move->fromfile = fileidx(mstr[0]);
universe@13 254 }
universe@13 255 } else {
universe@14 256 /* move (e.g. "Ndf3", "N2c3") */
universe@14 257 move->fromfile = isfile(mstr[1]) ?
universe@14 258 fileidx(mstr[1]) : rowidx(mstr[1]);
universe@13 259 }
universe@13 260 move->tofile = fileidx(mstr[2]);
universe@13 261 move->torow = rowidx(mstr[3]);
universe@8 262 } else if (len == 5) {
universe@9 263 if (strcmp(mstr, "O-O-O") == 0) {
universe@9 264 /* queen side castling "O-O-O" */
universe@12 265 move->piece = KING;
universe@9 266 move->fromfile = fileidx('e');
universe@9 267 move->tofile = fileidx('c');
universe@9 268 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
universe@9 269 } else {
universe@13 270 move->piece = getpiece(mstr[0]);
universe@13 271 if (mstr[2] == 'x') {
universe@13 272 move->capture = TRUE;
universe@13 273 if (move->piece) {
universe@13 274 /* capture (e.g. "Ndxf3") */
universe@13 275 move->fromfile = fileidx(mstr[1]);
universe@13 276 } else {
universe@13 277 /* long notation capture (e.g. "e5xf6") */
universe@13 278 move->piece = PAWN;
universe@13 279 move->fromfile = fileidx(mstr[0]);
universe@13 280 move->fromrow = rowidx(mstr[1]);
universe@13 281 }
universe@13 282 } else {
universe@13 283 /* long notation move (e.g. "Nc5a4") */
universe@13 284 move->fromfile = fileidx(mstr[1]);
universe@13 285 move->fromrow = rowidx(mstr[2]);
universe@13 286 }
universe@13 287 move->tofile = fileidx(mstr[3]);
universe@13 288 move->torow = rowidx(mstr[4]);
universe@9 289 }
universe@8 290 } else if (len == 6) {
universe@8 291 /* long notation capture (e.g. "Nc5xf3") */
universe@13 292 if (mstr[3] == 'x') {
universe@13 293 move->capture = TRUE;
universe@13 294 move->piece = getpiece(mstr[0]);
universe@13 295 move->fromfile = fileidx(mstr[1]);
universe@13 296 move->fromrow = rowidx(mstr[2]);
universe@13 297 move->tofile = fileidx(mstr[4]);
universe@13 298 move->torow = rowidx(mstr[5]);
universe@13 299 }
universe@8 300 }
universe@12 301
universe@13 302
universe@12 303 if (move->piece) {
universe@12 304 move->piece |= mycolor;
universe@13 305 if (move->fromfile == POS_UNSPECIFIED
universe@13 306 || move->fromrow == POS_UNSPECIFIED) {
universe@13 307 return getlocation(board, move) && chkidx(move);
universe@13 308 } else {
universe@13 309 return chkidx(move);
universe@13 310 }
universe@13 311 } else {
universe@13 312 return FALSE;
universe@12 313 }
universe@13 314 // TODO: return status code to indicate the error type
universe@8 315 }
universe@8 316
universe@8 317 static int sendmove(Board board, uint8_t mycolor, int opponent) {
universe@7 318 const size_t buflen = 8;
universe@8 319 char movestr[buflen];
universe@7 320 _Bool remisrejected = FALSE;
universe@11 321 uint8_t code;
universe@7 322
universe@7 323 while (1) {
universe@7 324 move(boardy+3, 0);
universe@7 325 if (remisrejected) {
universe@7 326 printw(
universe@7 327 "Use chess notation to enter your move.\n"
universe@7 328 "Remis offer rejected - type 'surr' to surrender. \n\n"
universe@7 329 "Type your move: ");
universe@7 330 } else {
universe@7 331 printw(
universe@7 332 "Use chess notation to enter your move.\n"
universe@7 333 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
universe@7 334 "Type your move: ");
universe@7 335 }
universe@7 336 clrtoeol();
universe@7 337 refresh();
universe@8 338 getnstr(movestr, buflen);
universe@7 339
universe@8 340 if (strncmp(movestr, "surr", buflen) == 0) {
universe@7 341 printw("You surrendered!");
universe@8 342 refresh();
universe@7 343 net_send_code(opponent, NETCODE_SURRENDER);
universe@7 344 return 1;
universe@8 345 } else if (strncmp(movestr, "remis", buflen) == 0) {
universe@7 346 if (!remisrejected) {
universe@7 347 net_send_code(opponent, NETCODE_REMIS);
universe@7 348 printw("Remis offer sent - waiting for acceptance...");
universe@7 349 refresh();
universe@7 350 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
universe@7 351 printw("\rRemis accepted!");
universe@7 352 clrtoeol();
universe@8 353 refresh();
universe@7 354 return 1;
universe@7 355 } else {
universe@7 356 remisrejected = TRUE;
universe@7 357 }
universe@7 358 }
universe@7 359 } else {
universe@8 360 Move move;
universe@8 361 if (eval_move(board, mycolor, movestr, &move)) {
universe@8 362 net_send_code(opponent, NETCODE_MOVE);
universe@8 363 net_send_data(opponent, &move, sizeof(Move));
universe@11 364 code = net_recieve_code(opponent);
universe@11 365 move.check = code == NETCODE_CHECK;
universe@11 366 move.checkmate = code == NETCODE_CHECKMATE;
universe@11 367 // TODO: record move
universe@11 368 if (code == NETCODE_DECLINE) {
universe@8 369 printw("Invalid move.");
universe@8 370 clrtoeol();
universe@11 371 } else {
universe@11 372 apply_move(board, &move);
universe@11 373 if (move.checkmate) {
universe@11 374 printw("Checkmate!");
universe@11 375 return 1;
universe@14 376 } else {
universe@14 377 return 0;
universe@11 378 }
universe@8 379 }
universe@8 380 } else {
universe@8 381 printw("Can't interpret move - please use algebraic notation.");
universe@8 382 }
universe@7 383 }
universe@7 384 }
universe@7 385 }
universe@7 386
universe@11 387 static int recvmove(Board board, int opponent) {
universe@7 388
universe@7 389 while (1) {
universe@7 390 move(boardy+3, 0);
universe@7 391 printw("Awaiting opponent move...");
universe@7 392 clrtoeol();
universe@7 393 refresh();
universe@7 394
universe@7 395 // TODO: nonblocking
universe@7 396 uint32_t code = net_recieve_code(opponent);
universe@8 397
universe@8 398 Move move;
universe@7 399 switch (code) {
universe@7 400 case NETCODE_SURRENDER:
universe@7 401 printw("\rYour opponent surrendered!");
universe@7 402 clrtoeol();
universe@7 403 return 1;
universe@7 404 case NETCODE_REMIS:
universe@7 405 if (prompt_yesno(
universe@7 406 "\rYour opponent offers remis - do you accept")) {
universe@7 407 printw("\rRemis accepted!");
universe@7 408 clrtoeol();
universe@7 409 net_send_code(opponent, NETCODE_ACCEPT);
universe@7 410 return 1;
universe@7 411 } else {
universe@7 412 net_send_code(opponent, NETCODE_DECLINE);
universe@7 413 }
universe@7 414 break;
universe@7 415 case NETCODE_MOVE:
universe@8 416 net_recieve_data(opponent, &move, sizeof(Move));
universe@11 417 if (validate_move(board, &move)) {
universe@8 418 apply_move(board, &move);
universe@11 419 // TODO: record move
universe@11 420 if (move.check) {
universe@11 421 net_send_code(opponent, NETCODE_CHECK);
universe@11 422 } else if (move.checkmate) {
universe@11 423 net_send_code(opponent, NETCODE_CHECKMATE);
universe@11 424 } else {
universe@11 425 net_send_code(opponent, NETCODE_ACCEPT);
universe@11 426 }
universe@8 427 return 0;
universe@8 428 } else {
universe@8 429 net_send_code(opponent, NETCODE_DECLINE);
universe@8 430 }
universe@7 431 }
universe@7 432 }
universe@7 433 }
universe@6 434
universe@6 435 void game_start(Settings *settings, int opponent) {
universe@7 436 _Bool myturn = is_server(settings) ==
universe@7 437 (settings->gameinfo.servercolor == WHITE);
universe@8 438 uint8_t mycolor = myturn ? WHITE:BLACK;
universe@8 439
universe@7 440 _Bool running;
universe@6 441
universe@7 442 Board board = {
universe@7 443 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
universe@7 444 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
universe@7 445 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 446 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 447 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 448 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 449 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
universe@7 450 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
universe@7 451 };
universe@7 452
universe@7 453 do {
universe@7 454 clear();
universe@8 455 draw_board(board, mycolor);
universe@7 456 if (myturn) {
universe@8 457 running = !sendmove(board, mycolor, opponent);
universe@7 458 } else {
universe@11 459 running = !recvmove(board, opponent);
universe@7 460 flushinp(); // flush any input the user hacked in while waiting
universe@7 461 }
universe@11 462 myturn ^= TRUE;
universe@7 463 } while (running);
universe@7 464
universe@7 465 mvaddstr(getmaxy(tchess_window)-1, 0,
universe@7 466 "Game has ended. Press any key to leave...");
universe@7 467 getch();
universe@6 468 }

mercurial