src/game.c

Wed, 26 Mar 2014 14:12:59 +0100

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

completed eval_move

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

mercurial