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