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