Sat, 22 Mar 2014 18:56:52 +0100
added network messages for check and checkmate
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@9 | 138 | static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) { |
universe@8 | 139 | memset(move, 0, sizeof(Move)); |
universe@8 | 140 | |
universe@9 | 141 | size_t len = strlen(mstr); |
universe@8 | 142 | |
universe@11 | 143 | /* evaluate check/checkmate flags */ |
universe@9 | 144 | if (mstr[len-1] == '+') { |
universe@9 | 145 | len--; mstr[len] = '\0'; |
universe@8 | 146 | move->check = TRUE; |
universe@11 | 147 | } else if (mstr[len-1] == '#') { |
universe@11 | 148 | len--; mstr[len] = '\0'; |
universe@11 | 149 | move->checkmate = TRUE; |
universe@8 | 150 | } |
universe@8 | 151 | |
universe@8 | 152 | if (len == 2) { |
universe@8 | 153 | /* pawn move (e.g. "e4") */ |
universe@9 | 154 | if (isfile(mstr[0]) && isrow(mstr[1])) { |
universe@10 | 155 | move->piece = PAWN|mycolor; |
universe@10 | 156 | move->tofile = fileidx(mstr[0]); |
universe@9 | 157 | move->torow = rowidx(mstr[1]); |
universe@10 | 158 | if (!pawn_getlocation(board, move)) { |
universe@8 | 159 | move->piece = 0; |
universe@8 | 160 | } |
universe@8 | 161 | } |
universe@8 | 162 | } else if (len == 3) { |
universe@9 | 163 | if (strcmp(mstr, "O-O") == 0) { |
universe@8 | 164 | /* king side castling */ |
universe@10 | 165 | move->piece = KING|mycolor; |
universe@8 | 166 | move->fromfile = fileidx('e'); |
universe@9 | 167 | move->tofile = fileidx('g'); |
universe@8 | 168 | move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; |
universe@8 | 169 | } else { |
universe@8 | 170 | /* unambiguous move (e.g. "Nf3") */ |
universe@8 | 171 | } |
universe@8 | 172 | |
universe@8 | 173 | } else if (len == 4) { |
universe@8 | 174 | /* ambiguous move (e.g. "Ndf3") */ |
universe@8 | 175 | |
universe@8 | 176 | /* unambiguous capture (e.g. "Nxf3", "dxe5") */ |
universe@8 | 177 | |
universe@8 | 178 | } else if (len == 5) { |
universe@9 | 179 | if (strcmp(mstr, "O-O-O") == 0) { |
universe@9 | 180 | /* queen side castling "O-O-O" */ |
universe@10 | 181 | move->piece = KING|mycolor; |
universe@9 | 182 | move->fromfile = fileidx('e'); |
universe@9 | 183 | move->tofile = fileidx('c'); |
universe@9 | 184 | move->fromrow = move->torow = mycolor == WHITE ? 0 : 7; |
universe@9 | 185 | } else { |
universe@9 | 186 | /* ambiguous capture (e.g. "Ndxf3") */ |
universe@9 | 187 | |
universe@9 | 188 | /* long notation move (e.g. "Nc5a4") */ |
universe@9 | 189 | |
universe@9 | 190 | /* long notation capture (e.g. "e5xf6") */ |
universe@9 | 191 | } |
universe@8 | 192 | } else if (len == 6) { |
universe@8 | 193 | /* long notation capture (e.g. "Nc5xf3") */ |
universe@8 | 194 | } |
universe@8 | 195 | |
universe@10 | 196 | return move->piece != 0; |
universe@8 | 197 | } |
universe@8 | 198 | |
universe@8 | 199 | static int sendmove(Board board, uint8_t mycolor, int opponent) { |
universe@7 | 200 | const size_t buflen = 8; |
universe@8 | 201 | char movestr[buflen]; |
universe@7 | 202 | _Bool remisrejected = FALSE; |
universe@11 | 203 | uint8_t code; |
universe@7 | 204 | |
universe@7 | 205 | while (1) { |
universe@7 | 206 | move(boardy+3, 0); |
universe@7 | 207 | if (remisrejected) { |
universe@7 | 208 | printw( |
universe@7 | 209 | "Use chess notation to enter your move.\n" |
universe@7 | 210 | "Remis offer rejected - type 'surr' to surrender. \n\n" |
universe@7 | 211 | "Type your move: "); |
universe@7 | 212 | } else { |
universe@7 | 213 | printw( |
universe@7 | 214 | "Use chess notation to enter your move.\n" |
universe@7 | 215 | "Or type 'surr' to surrender or 'remis' to offer remis.\n\n" |
universe@7 | 216 | "Type your move: "); |
universe@7 | 217 | } |
universe@7 | 218 | clrtoeol(); |
universe@7 | 219 | refresh(); |
universe@8 | 220 | getnstr(movestr, buflen); |
universe@7 | 221 | |
universe@8 | 222 | if (strncmp(movestr, "surr", buflen) == 0) { |
universe@7 | 223 | printw("You surrendered!"); |
universe@8 | 224 | refresh(); |
universe@7 | 225 | net_send_code(opponent, NETCODE_SURRENDER); |
universe@7 | 226 | return 1; |
universe@8 | 227 | } else if (strncmp(movestr, "remis", buflen) == 0) { |
universe@7 | 228 | if (!remisrejected) { |
universe@7 | 229 | net_send_code(opponent, NETCODE_REMIS); |
universe@7 | 230 | printw("Remis offer sent - waiting for acceptance..."); |
universe@7 | 231 | refresh(); |
universe@7 | 232 | if (net_recieve_code(opponent) == NETCODE_ACCEPT) { |
universe@7 | 233 | printw("\rRemis accepted!"); |
universe@7 | 234 | clrtoeol(); |
universe@8 | 235 | refresh(); |
universe@7 | 236 | return 1; |
universe@7 | 237 | } else { |
universe@7 | 238 | remisrejected = TRUE; |
universe@7 | 239 | } |
universe@7 | 240 | } |
universe@7 | 241 | } else { |
universe@8 | 242 | Move move; |
universe@8 | 243 | if (eval_move(board, mycolor, movestr, &move)) { |
universe@8 | 244 | net_send_code(opponent, NETCODE_MOVE); |
universe@8 | 245 | net_send_data(opponent, &move, sizeof(Move)); |
universe@11 | 246 | code = net_recieve_code(opponent); |
universe@11 | 247 | move.check = code == NETCODE_CHECK; |
universe@11 | 248 | move.checkmate = code == NETCODE_CHECKMATE; |
universe@11 | 249 | // TODO: record move |
universe@11 | 250 | if (code == NETCODE_DECLINE) { |
universe@8 | 251 | printw("Invalid move."); |
universe@8 | 252 | clrtoeol(); |
universe@11 | 253 | } else { |
universe@11 | 254 | apply_move(board, &move); |
universe@11 | 255 | if (move.checkmate) { |
universe@11 | 256 | printw("Checkmate!"); |
universe@11 | 257 | return 1; |
universe@11 | 258 | } |
universe@8 | 259 | } |
universe@8 | 260 | } else { |
universe@8 | 261 | printw("Can't interpret move - please use algebraic notation."); |
universe@8 | 262 | } |
universe@7 | 263 | } |
universe@7 | 264 | } |
universe@7 | 265 | } |
universe@7 | 266 | |
universe@11 | 267 | static int recvmove(Board board, int opponent) { |
universe@7 | 268 | |
universe@7 | 269 | while (1) { |
universe@7 | 270 | move(boardy+3, 0); |
universe@7 | 271 | printw("Awaiting opponent move..."); |
universe@7 | 272 | clrtoeol(); |
universe@7 | 273 | refresh(); |
universe@7 | 274 | |
universe@7 | 275 | // TODO: nonblocking |
universe@7 | 276 | uint32_t code = net_recieve_code(opponent); |
universe@8 | 277 | |
universe@8 | 278 | Move move; |
universe@7 | 279 | switch (code) { |
universe@7 | 280 | case NETCODE_SURRENDER: |
universe@7 | 281 | printw("\rYour opponent surrendered!"); |
universe@7 | 282 | clrtoeol(); |
universe@7 | 283 | return 1; |
universe@7 | 284 | case NETCODE_REMIS: |
universe@7 | 285 | if (prompt_yesno( |
universe@7 | 286 | "\rYour opponent offers remis - do you accept")) { |
universe@7 | 287 | printw("\rRemis accepted!"); |
universe@7 | 288 | clrtoeol(); |
universe@7 | 289 | net_send_code(opponent, NETCODE_ACCEPT); |
universe@7 | 290 | return 1; |
universe@7 | 291 | } else { |
universe@7 | 292 | net_send_code(opponent, NETCODE_DECLINE); |
universe@7 | 293 | } |
universe@7 | 294 | break; |
universe@7 | 295 | case NETCODE_MOVE: |
universe@8 | 296 | net_recieve_data(opponent, &move, sizeof(Move)); |
universe@11 | 297 | if (validate_move(board, &move)) { |
universe@8 | 298 | apply_move(board, &move); |
universe@11 | 299 | // TODO: record move |
universe@11 | 300 | if (move.check) { |
universe@11 | 301 | net_send_code(opponent, NETCODE_CHECK); |
universe@11 | 302 | } else if (move.checkmate) { |
universe@11 | 303 | net_send_code(opponent, NETCODE_CHECKMATE); |
universe@11 | 304 | } else { |
universe@11 | 305 | net_send_code(opponent, NETCODE_ACCEPT); |
universe@11 | 306 | } |
universe@8 | 307 | return 0; |
universe@8 | 308 | } else { |
universe@8 | 309 | net_send_code(opponent, NETCODE_DECLINE); |
universe@8 | 310 | } |
universe@7 | 311 | } |
universe@7 | 312 | } |
universe@7 | 313 | } |
universe@6 | 314 | |
universe@6 | 315 | void game_start(Settings *settings, int opponent) { |
universe@7 | 316 | _Bool myturn = is_server(settings) == |
universe@7 | 317 | (settings->gameinfo.servercolor == WHITE); |
universe@8 | 318 | uint8_t mycolor = myturn ? WHITE:BLACK; |
universe@8 | 319 | |
universe@7 | 320 | _Bool running; |
universe@6 | 321 | |
universe@7 | 322 | Board board = { |
universe@7 | 323 | {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK}, |
universe@7 | 324 | {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN}, |
universe@7 | 325 | {0, 0, 0, 0, 0, 0, 0, 0}, |
universe@7 | 326 | {0, 0, 0, 0, 0, 0, 0, 0}, |
universe@7 | 327 | {0, 0, 0, 0, 0, 0, 0, 0}, |
universe@7 | 328 | {0, 0, 0, 0, 0, 0, 0, 0}, |
universe@7 | 329 | {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN}, |
universe@7 | 330 | {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK} |
universe@7 | 331 | }; |
universe@7 | 332 | |
universe@7 | 333 | do { |
universe@7 | 334 | clear(); |
universe@8 | 335 | draw_board(board, mycolor); |
universe@7 | 336 | if (myturn) { |
universe@8 | 337 | running = !sendmove(board, mycolor, opponent); |
universe@7 | 338 | } else { |
universe@11 | 339 | running = !recvmove(board, opponent); |
universe@7 | 340 | flushinp(); // flush any input the user hacked in while waiting |
universe@7 | 341 | } |
universe@11 | 342 | myturn ^= TRUE; |
universe@7 | 343 | } while (running); |
universe@7 | 344 | |
universe@7 | 345 | mvaddstr(getmaxy(tchess_window)-1, 0, |
universe@7 | 346 | "Game has ended. Press any key to leave..."); |
universe@7 | 347 | getch(); |
universe@6 | 348 | } |