Mon, 16 Jun 2014 13:45:31 +0200
added pgn parser and writer (without comment support yet) + minor refactorings
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@35 | 33 | #include "colors.h" |
universe@7 | 34 | #include <ncurses.h> |
universe@7 | 35 | #include <string.h> |
universe@30 | 36 | #include <inttypes.h> |
universe@34 | 37 | #include <sys/select.h> |
universe@50 | 38 | #include <stdio.h> |
universe@50 | 39 | #include <errno.h> |
universe@7 | 40 | |
universe@7 | 41 | static const uint8_t boardx = 10, boardy = 10; |
universe@33 | 42 | static int inputy = 21; /* should be overridden on game startup */ |
universe@7 | 43 | |
universe@33 | 44 | static int timecontrol(GameState *gamestate, GameInfo *gameinfo) { |
universe@30 | 45 | if (gameinfo->timecontrol) { |
universe@33 | 46 | uint16_t white = remaining_movetime(gameinfo, gamestate, WHITE); |
universe@33 | 47 | uint16_t black = remaining_movetime(gameinfo, gamestate, BLACK); |
universe@30 | 48 | mvprintw(boardy+4, boardx-1, |
universe@33 | 49 | "White time: %4" PRIu16 ":%02" PRIu16, |
universe@33 | 50 | white / 60, white % 60); |
universe@30 | 51 | mvprintw(boardy+5, boardx-1, |
universe@33 | 52 | "Black time: %4" PRIu16 ":%02" PRIu16, |
universe@33 | 53 | black / 60, black % 60); |
universe@33 | 54 | |
universe@33 | 55 | if (white == 0) { |
universe@33 | 56 | move(inputy, 0); |
universe@33 | 57 | printw("Time is over - Black wins!"); |
universe@33 | 58 | clrtobot(); |
universe@33 | 59 | refresh(); |
universe@33 | 60 | return 1; |
universe@33 | 61 | } |
universe@33 | 62 | if (black == 0) { |
universe@33 | 63 | move(inputy, 0); |
universe@33 | 64 | printw("Time is over - White wins!"); |
universe@33 | 65 | clrtobot(); |
universe@33 | 66 | refresh(); |
universe@33 | 67 | return 1; |
universe@33 | 68 | } |
universe@30 | 69 | } |
universe@33 | 70 | |
universe@33 | 71 | return 0; |
universe@30 | 72 | } |
universe@30 | 73 | |
universe@50 | 74 | static void draw_board(GameState *gamestate, uint8_t perspective) { |
universe@7 | 75 | for (uint8_t y = 0 ; y < 8 ; y++) { |
universe@7 | 76 | for (uint8_t x = 0 ; x < 8 ; x++) { |
universe@23 | 77 | uint8_t col = gamestate->board[y][x] & COLOR_MASK; |
universe@23 | 78 | uint8_t piece = gamestate->board[y][x] & PIECE_MASK; |
universe@18 | 79 | char piecec; |
universe@18 | 80 | if (piece) { |
universe@18 | 81 | piecec = piece == PAWN ? 'P' : getpiecechr(piece); |
universe@18 | 82 | } else { |
universe@18 | 83 | piecec = ' '; |
universe@7 | 84 | } |
universe@7 | 85 | |
universe@35 | 86 | _Bool boardblack = (y&1)==(x&1); |
universe@35 | 87 | attrset((col==WHITE ? A_BOLD : A_DIM)| |
universe@35 | 88 | COLOR_PAIR(col == WHITE ? |
universe@35 | 89 | (boardblack ? COL_WB : COL_WW) : |
universe@35 | 90 | (boardblack ? COL_BB : COL_BW) |
universe@35 | 91 | ) |
universe@35 | 92 | ); |
universe@7 | 93 | |
universe@50 | 94 | int cy = perspective == WHITE ? boardy-y : boardy-7+y; |
universe@50 | 95 | int cx = perspective == WHITE ? boardx+x*3 : boardx+21-x*3; |
universe@8 | 96 | mvaddch(cy, cx, ' '); |
universe@8 | 97 | mvaddch(cy, cx+1, piecec); |
universe@8 | 98 | mvaddch(cy, cx+2, ' '); |
universe@7 | 99 | } |
universe@7 | 100 | } |
universe@7 | 101 | |
universe@7 | 102 | attrset(A_NORMAL); |
universe@7 | 103 | for (uint8_t i = 0 ; i < 8 ; i++) { |
universe@50 | 104 | int x = perspective == WHITE ? boardx+i*3+1 : boardx+22-i*3; |
universe@50 | 105 | int y = perspective == WHITE ? boardy-i : boardy-7+i; |
universe@8 | 106 | mvaddch(boardy+1, x, 'a'+i); |
universe@8 | 107 | mvaddch(y, boardx-2, '1'+i); |
universe@7 | 108 | } |
universe@18 | 109 | |
universe@18 | 110 | /* move log */ |
universe@18 | 111 | // TODO: introduce window to avoid bugs with a long move log |
universe@18 | 112 | uint8_t logy = 0; |
universe@18 | 113 | const uint8_t logx = boardx + 30; |
universe@18 | 114 | int logi = 1; |
universe@23 | 115 | MoveList *logelem = gamestate->movelist; |
universe@18 | 116 | |
universe@18 | 117 | while (logelem) { |
universe@18 | 118 | logi++; |
universe@18 | 119 | if (logi % 2 == 0) { |
universe@18 | 120 | if ((logi - 2) % 4 == 0) { |
universe@18 | 121 | logy++; |
universe@23 | 122 | move(logy, logx); |
universe@18 | 123 | } |
universe@18 | 124 | printw("%d. ", logi / 2); |
universe@18 | 125 | } |
universe@18 | 126 | |
universe@50 | 127 | addstr(logelem->move.string); |
universe@50 | 128 | if (!logelem->next) { |
universe@50 | 129 | if (gamestate->stalemate) { |
universe@50 | 130 | addstr(" stalemate"); |
universe@27 | 131 | } |
universe@18 | 132 | } |
universe@50 | 133 | addch(' '); |
universe@50 | 134 | |
universe@50 | 135 | logelem = logelem->next; |
universe@18 | 136 | } |
universe@7 | 137 | } |
universe@7 | 138 | |
universe@26 | 139 | static void eval_move_failed_msg(int code) { |
universe@26 | 140 | switch (code) { |
universe@26 | 141 | case AMBIGUOUS_MOVE: |
universe@26 | 142 | printw("Ambiguous move - please specify the piece to move."); |
universe@26 | 143 | break; |
universe@26 | 144 | case INVALID_POSITION: |
universe@48 | 145 | printw("No piece can be moved this way."); |
universe@26 | 146 | break; |
universe@26 | 147 | case NEED_PROMOTION: |
universe@26 | 148 | printw("You need to promote the pawn (append \"=Q\" e.g.)!"); |
universe@26 | 149 | break; |
universe@47 | 150 | case KING_IN_CHECK: |
universe@47 | 151 | printw("Your king is in check!"); |
universe@47 | 152 | break; |
universe@47 | 153 | case PIECE_PINNED: |
universe@47 | 154 | printw("This piece is pinned!"); |
universe@47 | 155 | break; |
universe@47 | 156 | case INVALID_MOVE_SYNTAX: |
universe@47 | 157 | printw("Can't interpret move - please use algebraic notation."); |
universe@47 | 158 | break; |
universe@48 | 159 | case RULES_VIOLATED: |
universe@48 | 160 | printw("Move does not comply chess rules."); |
universe@48 | 161 | break; |
universe@48 | 162 | case KING_MOVES_INTO_CHECK: |
universe@48 | 163 | printw("Can't move the king into a check position."); |
universe@48 | 164 | break; |
universe@26 | 165 | default: |
universe@47 | 166 | printw("Unknown move parser error."); |
universe@26 | 167 | } |
universe@26 | 168 | } |
universe@26 | 169 | |
universe@50 | 170 | static void save_pgn(GameState *gamestate, GameInfo *gameinfo) { |
universe@50 | 171 | printw("Filename: "); |
universe@50 | 172 | clrtoeol(); |
universe@50 | 173 | refresh(); |
universe@50 | 174 | |
universe@50 | 175 | char filename[64]; |
universe@50 | 176 | int y = getcury(stdscr); |
universe@50 | 177 | if (getnstr(filename, 64) == OK) { |
universe@50 | 178 | move(y, 0); |
universe@50 | 179 | FILE *file = fopen(filename, "w"); |
universe@50 | 180 | if (file) { |
universe@50 | 181 | write_pgn(file, gamestate, gameinfo); |
universe@50 | 182 | fclose(file); |
universe@50 | 183 | printw("File saved."); |
universe@50 | 184 | } else { |
universe@50 | 185 | printw("Can't write to file (%s).", strerror(errno)); |
universe@50 | 186 | } |
universe@50 | 187 | clrtoeol(); |
universe@50 | 188 | } |
universe@50 | 189 | } |
universe@50 | 190 | |
universe@50 | 191 | #define MOVESTR_BUFLEN 10 |
universe@50 | 192 | static int domove_singlemachine(GameState *gamestate, |
universe@50 | 193 | GameInfo *gameinfo, uint8_t curcolor) { |
universe@26 | 194 | |
universe@37 | 195 | |
universe@32 | 196 | size_t bufpos = 0; |
universe@37 | 197 | char movestr[MOVESTR_BUFLEN]; |
universe@26 | 198 | |
universe@33 | 199 | flushinp(); |
universe@26 | 200 | while (1) { |
universe@33 | 201 | if (timecontrol(gamestate, gameinfo)) { |
universe@33 | 202 | return 1; |
universe@33 | 203 | } |
universe@33 | 204 | |
universe@26 | 205 | move(inputy, 0); |
universe@26 | 206 | printw( |
universe@26 | 207 | "Use chess notation to enter your move.\n" |
universe@50 | 208 | "Or use a command: remis, resign, savepgn\n\n" |
universe@26 | 209 | "Type your move: "); |
universe@26 | 210 | clrtoeol(); |
universe@30 | 211 | |
universe@37 | 212 | if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) { |
universe@44 | 213 | if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) { |
universe@50 | 214 | gamestate->resign = 1; |
universe@44 | 215 | printw("%s resigned!", |
universe@50 | 216 | curcolor==WHITE?"White":"Black"); |
universe@50 | 217 | clrtobot(); |
universe@30 | 218 | refresh(); |
universe@30 | 219 | return 1; |
universe@37 | 220 | } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) { |
universe@50 | 221 | gamestate->remis = 1; |
universe@30 | 222 | printw("Game ends remis."); |
universe@50 | 223 | clrtobot(); |
universe@30 | 224 | refresh(); |
universe@30 | 225 | return 1; |
universe@50 | 226 | } else if (strncmp(movestr, "savepgn", MOVESTR_BUFLEN) == 0) { |
universe@50 | 227 | save_pgn(gamestate, gameinfo); |
universe@30 | 228 | } else { |
universe@30 | 229 | Move move; |
universe@50 | 230 | int result = eval_move(gamestate, movestr, &move, curcolor); |
universe@50 | 231 | switch (result) { |
universe@30 | 232 | case VALID_MOVE_SYNTAX: |
universe@50 | 233 | result = validate_move(gamestate, &move); |
universe@50 | 234 | if (result == VALID_MOVE_SEMANTICS) { |
universe@30 | 235 | apply_move(gamestate, &move); |
universe@30 | 236 | if (gamestate->checkmate) { |
universe@30 | 237 | printw("Checkmate!"); |
universe@30 | 238 | clrtoeol(); |
universe@30 | 239 | return 1; |
universe@30 | 240 | } else if (gamestate->stalemate) { |
universe@30 | 241 | printw("Stalemate!"); |
universe@30 | 242 | clrtoeol(); |
universe@30 | 243 | return 1; |
universe@30 | 244 | } else { |
universe@30 | 245 | return 0; |
universe@30 | 246 | } |
universe@26 | 247 | } else { |
universe@50 | 248 | eval_move_failed_msg(result); |
universe@26 | 249 | } |
universe@30 | 250 | break; |
universe@30 | 251 | default: |
universe@50 | 252 | eval_move_failed_msg(result); |
universe@26 | 253 | } |
universe@30 | 254 | clrtoeol(); |
universe@26 | 255 | } |
universe@26 | 256 | } |
universe@26 | 257 | } |
universe@26 | 258 | } |
universe@8 | 259 | |
universe@50 | 260 | static int sendmove(GameState *gamestate, GameInfo *gameinfo, |
universe@50 | 261 | int opponent, uint8_t mycolor) { |
universe@18 | 262 | |
universe@33 | 263 | size_t bufpos = 0; |
universe@37 | 264 | char movestr[MOVESTR_BUFLEN]; |
universe@7 | 265 | _Bool remisrejected = FALSE; |
universe@11 | 266 | uint8_t code; |
universe@7 | 267 | |
universe@33 | 268 | flushinp(); |
universe@7 | 269 | while (1) { |
universe@33 | 270 | if (timecontrol(gamestate, gameinfo)) { |
universe@33 | 271 | net_send_code(opponent, NETCODE_TIMEOVER); |
universe@33 | 272 | return 1; |
universe@33 | 273 | } |
universe@33 | 274 | |
universe@18 | 275 | move(inputy, 0); |
universe@7 | 276 | if (remisrejected) { |
universe@7 | 277 | printw( |
universe@7 | 278 | "Use chess notation to enter your move.\n" |
universe@50 | 279 | "Remis offer rejected \n\n" |
universe@7 | 280 | "Type your move: "); |
universe@7 | 281 | } else { |
universe@7 | 282 | printw( |
universe@7 | 283 | "Use chess notation to enter your move.\n" |
universe@50 | 284 | "Or use a command: remis, resign, savepgn\n\n" |
universe@7 | 285 | "Type your move: "); |
universe@7 | 286 | } |
universe@7 | 287 | clrtoeol(); |
universe@33 | 288 | |
universe@37 | 289 | if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) { |
universe@44 | 290 | if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) { |
universe@50 | 291 | gamestate->resign = 1; |
universe@44 | 292 | printw("You resigned!"); |
universe@33 | 293 | clrtoeol(); |
universe@7 | 294 | refresh(); |
universe@44 | 295 | net_send_code(opponent, NETCODE_RESIGN); |
universe@33 | 296 | return 1; |
universe@50 | 297 | } else if (strncmp(movestr, "savepgn", MOVESTR_BUFLEN) == 0) { |
universe@50 | 298 | save_pgn(gamestate, gameinfo); |
universe@37 | 299 | } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) { |
universe@33 | 300 | if (!remisrejected) { |
universe@33 | 301 | net_send_code(opponent, NETCODE_REMIS); |
universe@33 | 302 | printw("Remis offer sent - waiting for acceptance..."); |
universe@8 | 303 | refresh(); |
universe@46 | 304 | code = net_recieve_code(opponent); |
universe@46 | 305 | if (code == NETCODE_ACCEPT) { |
universe@50 | 306 | gamestate->remis = 1; |
universe@33 | 307 | printw("\rRemis accepted!"); |
universe@18 | 308 | clrtoeol(); |
universe@33 | 309 | refresh(); |
universe@26 | 310 | return 1; |
universe@46 | 311 | } else if (code == NETCODE_CONNLOST) { |
universe@46 | 312 | printw("\rYour opponent left the game."); |
universe@46 | 313 | clrtoeol(); |
universe@46 | 314 | refresh(); |
universe@46 | 315 | return 1; |
universe@14 | 316 | } else { |
universe@33 | 317 | remisrejected = TRUE; |
universe@11 | 318 | } |
universe@18 | 319 | } |
universe@33 | 320 | } else { |
universe@33 | 321 | Move move; |
universe@50 | 322 | int eval_result = eval_move(gamestate, movestr, &move, mycolor); |
universe@33 | 323 | switch (eval_result) { |
universe@33 | 324 | case VALID_MOVE_SYNTAX: |
universe@49 | 325 | net_send_data(opponent, NETCODE_MOVE, &move, |
universe@49 | 326 | sizeof(Move)-8); |
universe@33 | 327 | code = net_recieve_code(opponent); |
universe@48 | 328 | move.check = code == NETCODE_CHECK || |
universe@48 | 329 | code == NETCODE_CHECKMATE; |
universe@33 | 330 | gamestate->checkmate = code == NETCODE_CHECKMATE; |
universe@33 | 331 | gamestate->stalemate = code == NETCODE_STALEMATE; |
universe@33 | 332 | if (code == NETCODE_DECLINE) { |
universe@48 | 333 | uint32_t reason; |
universe@48 | 334 | net_recieve_data(opponent, &reason, sizeof(uint32_t)); |
universe@48 | 335 | reason = ntohl(reason); |
universe@48 | 336 | eval_move_failed_msg(reason); |
universe@42 | 337 | } else if (code == NETCODE_ACCEPT |
universe@42 | 338 | || code == NETCODE_CHECK |
universe@42 | 339 | || code == NETCODE_CHECKMATE |
universe@42 | 340 | || code == NETCODE_STALEMATE) { |
universe@33 | 341 | apply_move(gamestate, &move); |
universe@33 | 342 | if (gamestate->checkmate) { |
universe@33 | 343 | printw("Checkmate!"); |
universe@33 | 344 | clrtoeol(); |
universe@33 | 345 | return 1; |
universe@33 | 346 | } else if (gamestate->stalemate) { |
universe@33 | 347 | printw("Stalemate!"); |
universe@33 | 348 | clrtoeol(); |
universe@33 | 349 | return 1; |
universe@33 | 350 | } else { |
universe@33 | 351 | return 0; |
universe@33 | 352 | } |
universe@46 | 353 | } else if (code == NETCODE_CONNLOST) { |
universe@46 | 354 | printw("Your opponent left the game."); |
universe@46 | 355 | return 1; |
universe@42 | 356 | } else { |
universe@42 | 357 | printw("Invalid network response."); |
universe@33 | 358 | } |
universe@33 | 359 | break; |
universe@33 | 360 | default: |
universe@33 | 361 | eval_move_failed_msg(eval_result); |
universe@33 | 362 | } |
universe@33 | 363 | clrtoeol(); |
universe@8 | 364 | } |
universe@7 | 365 | } |
universe@7 | 366 | } |
universe@7 | 367 | } |
universe@7 | 368 | |
universe@33 | 369 | static int recvmove(GameState *gamestate, GameInfo *gameinfo, int opponent) { |
universe@7 | 370 | |
universe@34 | 371 | struct timeval timeout; |
universe@7 | 372 | while (1) { |
universe@33 | 373 | timecontrol(gamestate, gameinfo); |
universe@33 | 374 | |
universe@18 | 375 | move(inputy, 0); |
universe@7 | 376 | printw("Awaiting opponent move..."); |
universe@7 | 377 | clrtoeol(); |
universe@7 | 378 | refresh(); |
universe@7 | 379 | |
universe@34 | 380 | fd_set readfds; |
universe@8 | 381 | |
universe@34 | 382 | FD_ZERO(&readfds); |
universe@34 | 383 | FD_SET(opponent, &readfds); |
universe@34 | 384 | timeout.tv_sec = 0; |
universe@34 | 385 | timeout.tv_usec = 1e5; |
universe@34 | 386 | |
universe@50 | 387 | // TODO: allow commands |
universe@50 | 388 | |
universe@34 | 389 | int result = select(opponent+1, &readfds, NULL, NULL, &timeout); |
universe@34 | 390 | if (result == -1) { |
universe@34 | 391 | printw("\rCannot perform asynchronous network IO"); |
universe@34 | 392 | cbreak(); getch(); |
universe@34 | 393 | exit(EXIT_FAILURE); |
universe@34 | 394 | } |
universe@34 | 395 | if (result > 0) { |
universe@43 | 396 | uint8_t code = net_recieve_code(opponent); |
universe@34 | 397 | |
universe@34 | 398 | Move move; |
universe@34 | 399 | switch (code) { |
universe@34 | 400 | case NETCODE_TIMEOVER: |
universe@34 | 401 | printw("\rYour opponent's time ran out - you win!"); |
universe@7 | 402 | clrtoeol(); |
universe@7 | 403 | return 1; |
universe@44 | 404 | case NETCODE_RESIGN: |
universe@50 | 405 | gamestate->resign = 1; |
universe@44 | 406 | printw("\rYour opponent resigned!"); |
universe@34 | 407 | clrtoeol(); |
universe@34 | 408 | return 1; |
universe@46 | 409 | case NETCODE_CONNLOST: |
universe@46 | 410 | printw("\rYour opponent has left the game."); |
universe@46 | 411 | clrtoeol(); |
universe@46 | 412 | return 1; |
universe@34 | 413 | case NETCODE_REMIS: |
universe@34 | 414 | if (prompt_yesno( |
universe@34 | 415 | "\rYour opponent offers remis - do you accept")) { |
universe@50 | 416 | gamestate->remis = 1; |
universe@34 | 417 | printw("\rRemis accepted!"); |
universe@7 | 418 | clrtoeol(); |
universe@34 | 419 | net_send_code(opponent, NETCODE_ACCEPT); |
universe@7 | 420 | return 1; |
universe@7 | 421 | } else { |
universe@34 | 422 | net_send_code(opponent, NETCODE_DECLINE); |
universe@7 | 423 | } |
universe@34 | 424 | break; |
universe@34 | 425 | case NETCODE_MOVE: |
universe@49 | 426 | net_recieve_data(opponent, &move, sizeof(Move)-8); |
universe@48 | 427 | code = validate_move(gamestate, &move); |
universe@48 | 428 | if (code == VALID_MOVE_SEMANTICS) { |
universe@34 | 429 | apply_move(gamestate, &move); |
universe@34 | 430 | if (move.check) { |
universe@34 | 431 | net_send_code(opponent, NETCODE_CHECK); |
universe@34 | 432 | } else if (gamestate->checkmate) { |
universe@34 | 433 | net_send_code(opponent, NETCODE_CHECKMATE); |
universe@34 | 434 | printw("\rCheckmate!"); |
universe@34 | 435 | clrtoeol(); |
universe@34 | 436 | return 1; |
universe@34 | 437 | } else if (gamestate->stalemate) { |
universe@34 | 438 | net_send_code(opponent, NETCODE_STALEMATE); |
universe@34 | 439 | printw("\rStalemate!"); |
universe@34 | 440 | clrtoeol(); |
universe@34 | 441 | return 1; |
universe@34 | 442 | } else { |
universe@34 | 443 | net_send_code(opponent, NETCODE_ACCEPT); |
universe@34 | 444 | } |
universe@34 | 445 | return 0; |
universe@34 | 446 | } else { |
universe@48 | 447 | uint32_t reason = htonl(code); |
universe@48 | 448 | net_send_data(opponent, NETCODE_DECLINE, |
universe@48 | 449 | &reason, sizeof(uint32_t)); |
universe@34 | 450 | } |
universe@45 | 451 | break; |
universe@45 | 452 | default: |
universe@45 | 453 | printw("\nInvalid network request."); |
universe@33 | 454 | } |
universe@7 | 455 | } |
universe@7 | 456 | } |
universe@7 | 457 | } |
universe@6 | 458 | |
universe@50 | 459 | static void post_game(GameState *gamestate, GameInfo *gameinfo) { |
universe@50 | 460 | move(0,0); |
universe@50 | 461 | draw_board(gamestate, WHITE); |
universe@50 | 462 | |
universe@50 | 463 | // TODO: network connection is still open here - think about it! |
universe@50 | 464 | |
universe@50 | 465 | mvaddstr(getmaxy(stdscr)-1, 0, |
universe@50 | 466 | "Press 'q' to quit or 's' to save a PGN file..."); |
universe@50 | 467 | refresh(); |
universe@50 | 468 | flushinp(); |
universe@50 | 469 | |
universe@50 | 470 | noecho(); |
universe@50 | 471 | int c; |
universe@50 | 472 | do { |
universe@50 | 473 | c = getch(); |
universe@50 | 474 | if (c == 's') { |
universe@50 | 475 | addch('\r'); |
universe@50 | 476 | echo(); |
universe@50 | 477 | save_pgn(gamestate, gameinfo); |
universe@50 | 478 | addstr(" Press 'q' to quit..."); |
universe@50 | 479 | noecho(); |
universe@50 | 480 | } |
universe@50 | 481 | } while (c != 'q'); |
universe@50 | 482 | echo(); |
universe@50 | 483 | |
universe@50 | 484 | gamestate_cleanup(gamestate); |
universe@26 | 485 | } |
universe@26 | 486 | |
universe@26 | 487 | void game_start_singlemachine(Settings *settings) { |
universe@33 | 488 | inputy = getmaxy(stdscr) - 6; |
universe@33 | 489 | |
universe@26 | 490 | GameState gamestate; |
universe@50 | 491 | gamestate_init(&gamestate); |
universe@50 | 492 | uint8_t curcol = WHITE; |
universe@50 | 493 | |
universe@50 | 494 | if (settings->continuepgn) { |
universe@50 | 495 | FILE *pgnfile = fopen(settings->continuepgn, "r"); |
universe@50 | 496 | if (pgnfile) { |
universe@50 | 497 | int result = read_pgn(pgnfile, &gamestate, &(settings->gameinfo)); |
universe@50 | 498 | fclose(pgnfile); |
universe@50 | 499 | if (result != EXIT_SUCCESS) { |
universe@50 | 500 | addstr("Invalid PGN file content.\n"); |
universe@50 | 501 | return; |
universe@50 | 502 | } |
universe@50 | 503 | if (!is_game_running(&gamestate)) { |
universe@50 | 504 | addstr("Game has ended. Use -S to analyze it.\n"); |
universe@50 | 505 | return; |
universe@50 | 506 | } |
universe@50 | 507 | curcol = opponent_color(gamestate.lastmove->move.piece&COLOR_MASK); |
universe@50 | 508 | } else { |
universe@50 | 509 | printw("Can't read PGN file (%s)\n", strerror(errno)); |
universe@50 | 510 | return; |
universe@50 | 511 | } |
universe@50 | 512 | } |
universe@30 | 513 | |
universe@26 | 514 | _Bool running; |
universe@26 | 515 | do { |
universe@26 | 516 | clear(); |
universe@50 | 517 | draw_board(&gamestate, curcol); |
universe@50 | 518 | running = !domove_singlemachine(&gamestate, |
universe@50 | 519 | &(settings->gameinfo), curcol); |
universe@50 | 520 | curcol = opponent_color(curcol); |
universe@26 | 521 | } while (running); |
universe@26 | 522 | |
universe@50 | 523 | post_game(&gamestate, &(settings->gameinfo)); |
universe@26 | 524 | } |
universe@26 | 525 | |
universe@6 | 526 | void game_start(Settings *settings, int opponent) { |
universe@33 | 527 | inputy = getmaxy(stdscr) - 6; |
universe@33 | 528 | |
universe@7 | 529 | _Bool myturn = is_server(settings) == |
universe@7 | 530 | (settings->gameinfo.servercolor == WHITE); |
universe@50 | 531 | uint8_t mycolor = myturn ? WHITE : BLACK; |
universe@8 | 532 | |
universe@23 | 533 | GameState gamestate; |
universe@50 | 534 | gamestate_init(&gamestate); |
universe@7 | 535 | |
universe@23 | 536 | _Bool running; |
universe@7 | 537 | do { |
universe@7 | 538 | clear(); |
universe@50 | 539 | draw_board(&gamestate, mycolor); |
universe@7 | 540 | if (myturn) { |
universe@50 | 541 | running = !sendmove(&gamestate, &(settings->gameinfo), |
universe@50 | 542 | opponent, mycolor); |
universe@7 | 543 | } else { |
universe@33 | 544 | running = !recvmove(&gamestate, &(settings->gameinfo), opponent); |
universe@7 | 545 | } |
universe@11 | 546 | myturn ^= TRUE; |
universe@7 | 547 | } while (running); |
universe@7 | 548 | |
universe@50 | 549 | post_game(&gamestate, &(settings->gameinfo)); |
universe@6 | 550 | } |