Wed, 28 May 2014 15:47:57 +0200
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
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@7 | 38 | |
universe@7 | 39 | static const uint8_t boardx = 10, boardy = 10; |
universe@33 | 40 | static int inputy = 21; /* should be overridden on game startup */ |
universe@7 | 41 | |
universe@33 | 42 | static int timecontrol(GameState *gamestate, GameInfo *gameinfo) { |
universe@30 | 43 | if (gameinfo->timecontrol) { |
universe@33 | 44 | uint16_t white = remaining_movetime(gameinfo, gamestate, WHITE); |
universe@33 | 45 | uint16_t black = remaining_movetime(gameinfo, gamestate, BLACK); |
universe@30 | 46 | mvprintw(boardy+4, boardx-1, |
universe@33 | 47 | "White time: %4" PRIu16 ":%02" PRIu16, |
universe@33 | 48 | white / 60, white % 60); |
universe@30 | 49 | mvprintw(boardy+5, boardx-1, |
universe@33 | 50 | "Black time: %4" PRIu16 ":%02" PRIu16, |
universe@33 | 51 | black / 60, black % 60); |
universe@33 | 52 | |
universe@33 | 53 | if (white == 0) { |
universe@33 | 54 | move(inputy, 0); |
universe@33 | 55 | printw("Time is over - Black wins!"); |
universe@33 | 56 | clrtobot(); |
universe@33 | 57 | refresh(); |
universe@33 | 58 | return 1; |
universe@33 | 59 | } |
universe@33 | 60 | if (black == 0) { |
universe@33 | 61 | move(inputy, 0); |
universe@33 | 62 | printw("Time is over - White wins!"); |
universe@33 | 63 | clrtobot(); |
universe@33 | 64 | refresh(); |
universe@33 | 65 | return 1; |
universe@33 | 66 | } |
universe@30 | 67 | } |
universe@33 | 68 | |
universe@33 | 69 | return 0; |
universe@30 | 70 | } |
universe@30 | 71 | |
universe@23 | 72 | static void draw_board(GameState *gamestate) { |
universe@7 | 73 | for (uint8_t y = 0 ; y < 8 ; y++) { |
universe@7 | 74 | for (uint8_t x = 0 ; x < 8 ; x++) { |
universe@23 | 75 | uint8_t col = gamestate->board[y][x] & COLOR_MASK; |
universe@23 | 76 | uint8_t piece = gamestate->board[y][x] & PIECE_MASK; |
universe@18 | 77 | char piecec; |
universe@18 | 78 | if (piece) { |
universe@18 | 79 | piecec = piece == PAWN ? 'P' : getpiecechr(piece); |
universe@18 | 80 | } else { |
universe@18 | 81 | piecec = ' '; |
universe@7 | 82 | } |
universe@7 | 83 | |
universe@35 | 84 | _Bool boardblack = (y&1)==(x&1); |
universe@35 | 85 | attrset((col==WHITE ? A_BOLD : A_DIM)| |
universe@35 | 86 | COLOR_PAIR(col == WHITE ? |
universe@35 | 87 | (boardblack ? COL_WB : COL_WW) : |
universe@35 | 88 | (boardblack ? COL_BB : COL_BW) |
universe@35 | 89 | ) |
universe@35 | 90 | ); |
universe@7 | 91 | |
universe@23 | 92 | int cy = gamestate->mycolor == WHITE ? boardy-y : boardy-7+y; |
universe@23 | 93 | int cx = gamestate->mycolor == WHITE ? boardx+x*3 : boardx+21-x*3; |
universe@8 | 94 | mvaddch(cy, cx, ' '); |
universe@8 | 95 | mvaddch(cy, cx+1, piecec); |
universe@8 | 96 | mvaddch(cy, cx+2, ' '); |
universe@7 | 97 | } |
universe@7 | 98 | } |
universe@7 | 99 | |
universe@7 | 100 | attrset(A_NORMAL); |
universe@7 | 101 | for (uint8_t i = 0 ; i < 8 ; i++) { |
universe@23 | 102 | int x = gamestate->mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3; |
universe@23 | 103 | int y = gamestate->mycolor == WHITE ? boardy-i : boardy-7+i; |
universe@8 | 104 | mvaddch(boardy+1, x, 'a'+i); |
universe@8 | 105 | mvaddch(y, boardx-2, '1'+i); |
universe@7 | 106 | } |
universe@18 | 107 | |
universe@18 | 108 | /* move log */ |
universe@18 | 109 | // TODO: introduce window to avoid bugs with a long move log |
universe@18 | 110 | uint8_t logy = 0; |
universe@18 | 111 | const uint8_t logx = boardx + 30; |
universe@18 | 112 | int logi = 1; |
universe@23 | 113 | MoveList *logelem = gamestate->movelist; |
universe@18 | 114 | |
universe@18 | 115 | while (logelem) { |
universe@18 | 116 | logi++; |
universe@18 | 117 | if (logi % 2 == 0) { |
universe@18 | 118 | if ((logi - 2) % 4 == 0) { |
universe@18 | 119 | logy++; |
universe@23 | 120 | move(logy, logx); |
universe@18 | 121 | } |
universe@18 | 122 | printw("%d. ", logi / 2); |
universe@18 | 123 | } |
universe@18 | 124 | |
universe@18 | 125 | if (logelem) { |
universe@18 | 126 | Move move = logelem->move; |
universe@25 | 127 | if ((move.piece&PIECE_MASK) == KING && |
universe@25 | 128 | abs(move.tofile-move.fromfile) == 2) { |
universe@25 | 129 | addstr(move.tofile==fileidx('c')?"O-O-O":"O-O"); |
universe@25 | 130 | } else { |
universe@25 | 131 | char logstr[] = { |
universe@25 | 132 | getpiecechr(move.piece), |
universe@25 | 133 | filechr(move.fromfile), rowchr(move.fromrow), |
universe@25 | 134 | move.capture ? 'x':'\0', |
universe@25 | 135 | filechr(move.tofile), rowchr(move.torow), |
universe@27 | 136 | move.check ? '+' : (move.promotion ? '=' : '\0'), |
universe@25 | 137 | move.promotion ? getpiecechr(move.promotion) : '\0' |
universe@25 | 138 | }; |
universe@25 | 139 | for (int stri = 0 ; stri < sizeof(logstr) ; stri++) { |
universe@25 | 140 | if (logstr[stri]) { |
universe@25 | 141 | addch(logstr[stri]); |
universe@25 | 142 | } |
universe@18 | 143 | } |
universe@18 | 144 | } |
universe@27 | 145 | if (!logelem->next) { |
universe@27 | 146 | if (gamestate->checkmate) { |
universe@28 | 147 | addstr("\b#"); |
universe@27 | 148 | } else if (gamestate->stalemate) { |
universe@27 | 149 | addstr(" stalemate"); |
universe@27 | 150 | } |
universe@27 | 151 | } |
universe@25 | 152 | addch(' '); |
universe@18 | 153 | |
universe@18 | 154 | logelem = logelem->next; |
universe@18 | 155 | } |
universe@18 | 156 | } |
universe@7 | 157 | } |
universe@7 | 158 | |
universe@26 | 159 | static void eval_move_failed_msg(int code) { |
universe@26 | 160 | switch (code) { |
universe@26 | 161 | case AMBIGUOUS_MOVE: |
universe@26 | 162 | printw("Ambiguous move - please specify the piece to move."); |
universe@26 | 163 | break; |
universe@26 | 164 | case INVALID_POSITION: |
universe@26 | 165 | printw("Cannot find the piece that shall be moved."); |
universe@26 | 166 | break; |
universe@26 | 167 | case NEED_PROMOTION: |
universe@26 | 168 | printw("You need to promote the pawn (append \"=Q\" e.g.)!"); |
universe@26 | 169 | break; |
universe@47 | 170 | case KING_IN_CHECK: |
universe@47 | 171 | printw("Your king is in check!"); |
universe@47 | 172 | break; |
universe@47 | 173 | case PIECE_PINNED: |
universe@47 | 174 | printw("This piece is pinned!"); |
universe@47 | 175 | break; |
universe@47 | 176 | case INVALID_MOVE_SYNTAX: |
universe@47 | 177 | printw("Can't interpret move - please use algebraic notation."); |
universe@47 | 178 | break; |
universe@26 | 179 | default: |
universe@47 | 180 | printw("Unknown move parser error."); |
universe@26 | 181 | } |
universe@26 | 182 | } |
universe@26 | 183 | |
universe@37 | 184 | #define MOVESTR_BUFLEN 8 |
universe@30 | 185 | static int domove_singlemachine(GameState *gamestate, GameInfo *gameinfo) { |
universe@26 | 186 | |
universe@37 | 187 | |
universe@32 | 188 | size_t bufpos = 0; |
universe@37 | 189 | char movestr[MOVESTR_BUFLEN]; |
universe@26 | 190 | |
universe@33 | 191 | flushinp(); |
universe@26 | 192 | while (1) { |
universe@33 | 193 | if (timecontrol(gamestate, gameinfo)) { |
universe@33 | 194 | return 1; |
universe@33 | 195 | } |
universe@33 | 196 | |
universe@26 | 197 | move(inputy, 0); |
universe@26 | 198 | printw( |
universe@26 | 199 | "Use chess notation to enter your move.\n" |
universe@44 | 200 | "Or type 'resign' to resign or 'remis' to end with remis.\n\n" |
universe@26 | 201 | "Type your move: "); |
universe@26 | 202 | clrtoeol(); |
universe@30 | 203 | |
universe@37 | 204 | if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) { |
universe@44 | 205 | if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) { |
universe@44 | 206 | printw("%s resigned!", |
universe@30 | 207 | gamestate->mycolor==WHITE?"White":"Black"); |
universe@30 | 208 | clrtoeol(); |
universe@30 | 209 | refresh(); |
universe@30 | 210 | return 1; |
universe@37 | 211 | } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) { |
universe@30 | 212 | printw("Game ends remis."); |
universe@30 | 213 | clrtoeol(); |
universe@30 | 214 | refresh(); |
universe@30 | 215 | return 1; |
universe@30 | 216 | } else { |
universe@30 | 217 | Move move; |
universe@30 | 218 | int eval_result = eval_move(gamestate, movestr, &move); |
universe@30 | 219 | switch (eval_result) { |
universe@30 | 220 | case VALID_MOVE_SYNTAX: |
universe@30 | 221 | if (validate_move(gamestate, &move)) { |
universe@30 | 222 | apply_move(gamestate, &move); |
universe@30 | 223 | if (gamestate->checkmate) { |
universe@30 | 224 | printw("Checkmate!"); |
universe@30 | 225 | clrtoeol(); |
universe@30 | 226 | return 1; |
universe@30 | 227 | } else if (gamestate->stalemate) { |
universe@30 | 228 | printw("Stalemate!"); |
universe@30 | 229 | clrtoeol(); |
universe@30 | 230 | return 1; |
universe@30 | 231 | } else { |
universe@30 | 232 | return 0; |
universe@30 | 233 | } |
universe@26 | 234 | } else { |
universe@30 | 235 | printw("Invalid move."); |
universe@26 | 236 | } |
universe@30 | 237 | break; |
universe@30 | 238 | default: |
universe@30 | 239 | eval_move_failed_msg(eval_result); |
universe@26 | 240 | } |
universe@30 | 241 | clrtoeol(); |
universe@26 | 242 | } |
universe@26 | 243 | } |
universe@26 | 244 | } |
universe@26 | 245 | } |
universe@8 | 246 | |
universe@33 | 247 | static int sendmove(GameState *gamestate, GameInfo *gameinfo, int opponent) { |
universe@18 | 248 | |
universe@33 | 249 | size_t bufpos = 0; |
universe@37 | 250 | char movestr[MOVESTR_BUFLEN]; |
universe@7 | 251 | _Bool remisrejected = FALSE; |
universe@11 | 252 | uint8_t code; |
universe@7 | 253 | |
universe@33 | 254 | flushinp(); |
universe@7 | 255 | while (1) { |
universe@33 | 256 | if (timecontrol(gamestate, gameinfo)) { |
universe@33 | 257 | net_send_code(opponent, NETCODE_TIMEOVER); |
universe@33 | 258 | return 1; |
universe@33 | 259 | } |
universe@33 | 260 | |
universe@18 | 261 | move(inputy, 0); |
universe@7 | 262 | if (remisrejected) { |
universe@7 | 263 | printw( |
universe@7 | 264 | "Use chess notation to enter your move.\n" |
universe@44 | 265 | "Remis offer rejected - type 'resign' to resign. \n\n" |
universe@7 | 266 | "Type your move: "); |
universe@7 | 267 | } else { |
universe@7 | 268 | printw( |
universe@7 | 269 | "Use chess notation to enter your move.\n" |
universe@44 | 270 | "Or type 'resign' to resign or 'remis' to offer remis.\n\n" |
universe@7 | 271 | "Type your move: "); |
universe@7 | 272 | } |
universe@7 | 273 | clrtoeol(); |
universe@33 | 274 | |
universe@37 | 275 | if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) { |
universe@44 | 276 | if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) { |
universe@44 | 277 | printw("You resigned!"); |
universe@33 | 278 | clrtoeol(); |
universe@7 | 279 | refresh(); |
universe@44 | 280 | net_send_code(opponent, NETCODE_RESIGN); |
universe@33 | 281 | return 1; |
universe@37 | 282 | } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) { |
universe@33 | 283 | if (!remisrejected) { |
universe@33 | 284 | net_send_code(opponent, NETCODE_REMIS); |
universe@33 | 285 | printw("Remis offer sent - waiting for acceptance..."); |
universe@8 | 286 | refresh(); |
universe@46 | 287 | code = net_recieve_code(opponent); |
universe@46 | 288 | if (code == NETCODE_ACCEPT) { |
universe@33 | 289 | printw("\rRemis accepted!"); |
universe@18 | 290 | clrtoeol(); |
universe@33 | 291 | refresh(); |
universe@26 | 292 | return 1; |
universe@46 | 293 | } else if (code == NETCODE_CONNLOST) { |
universe@46 | 294 | printw("\rYour opponent left the game."); |
universe@46 | 295 | clrtoeol(); |
universe@46 | 296 | refresh(); |
universe@46 | 297 | return 1; |
universe@14 | 298 | } else { |
universe@33 | 299 | remisrejected = TRUE; |
universe@11 | 300 | } |
universe@18 | 301 | } |
universe@33 | 302 | } else { |
universe@33 | 303 | Move move; |
universe@33 | 304 | int eval_result = eval_move(gamestate, movestr, &move); |
universe@33 | 305 | switch (eval_result) { |
universe@33 | 306 | case VALID_MOVE_SYNTAX: |
universe@33 | 307 | net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move)); |
universe@33 | 308 | code = net_recieve_code(opponent); |
universe@33 | 309 | move.check = code == NETCODE_CHECK; |
universe@33 | 310 | gamestate->checkmate = code == NETCODE_CHECKMATE; |
universe@33 | 311 | gamestate->stalemate = code == NETCODE_STALEMATE; |
universe@33 | 312 | if (code == NETCODE_DECLINE) { |
universe@33 | 313 | printw("Invalid move."); |
universe@42 | 314 | } else if (code == NETCODE_ACCEPT |
universe@42 | 315 | || code == NETCODE_CHECK |
universe@42 | 316 | || code == NETCODE_CHECKMATE |
universe@42 | 317 | || code == NETCODE_STALEMATE) { |
universe@33 | 318 | apply_move(gamestate, &move); |
universe@33 | 319 | if (gamestate->checkmate) { |
universe@33 | 320 | printw("Checkmate!"); |
universe@33 | 321 | clrtoeol(); |
universe@33 | 322 | return 1; |
universe@33 | 323 | } else if (gamestate->stalemate) { |
universe@33 | 324 | printw("Stalemate!"); |
universe@33 | 325 | clrtoeol(); |
universe@33 | 326 | return 1; |
universe@33 | 327 | } else { |
universe@33 | 328 | return 0; |
universe@33 | 329 | } |
universe@46 | 330 | } else if (code == NETCODE_CONNLOST) { |
universe@46 | 331 | printw("Your opponent left the game."); |
universe@46 | 332 | return 1; |
universe@42 | 333 | } else { |
universe@42 | 334 | printw("Invalid network response."); |
universe@33 | 335 | } |
universe@33 | 336 | break; |
universe@33 | 337 | default: |
universe@33 | 338 | eval_move_failed_msg(eval_result); |
universe@33 | 339 | } |
universe@33 | 340 | clrtoeol(); |
universe@8 | 341 | } |
universe@7 | 342 | } |
universe@7 | 343 | } |
universe@7 | 344 | } |
universe@7 | 345 | |
universe@33 | 346 | static int recvmove(GameState *gamestate, GameInfo *gameinfo, int opponent) { |
universe@7 | 347 | |
universe@34 | 348 | struct timeval timeout; |
universe@7 | 349 | while (1) { |
universe@33 | 350 | timecontrol(gamestate, gameinfo); |
universe@33 | 351 | |
universe@18 | 352 | move(inputy, 0); |
universe@7 | 353 | printw("Awaiting opponent move..."); |
universe@7 | 354 | clrtoeol(); |
universe@7 | 355 | refresh(); |
universe@7 | 356 | |
universe@34 | 357 | fd_set readfds; |
universe@8 | 358 | |
universe@34 | 359 | FD_ZERO(&readfds); |
universe@34 | 360 | FD_SET(opponent, &readfds); |
universe@34 | 361 | timeout.tv_sec = 0; |
universe@34 | 362 | timeout.tv_usec = 1e5; |
universe@34 | 363 | |
universe@34 | 364 | int result = select(opponent+1, &readfds, NULL, NULL, &timeout); |
universe@34 | 365 | if (result == -1) { |
universe@34 | 366 | printw("\rCannot perform asynchronous network IO"); |
universe@34 | 367 | cbreak(); getch(); |
universe@34 | 368 | exit(EXIT_FAILURE); |
universe@34 | 369 | } |
universe@34 | 370 | if (result > 0) { |
universe@43 | 371 | uint8_t code = net_recieve_code(opponent); |
universe@34 | 372 | |
universe@34 | 373 | Move move; |
universe@34 | 374 | switch (code) { |
universe@34 | 375 | case NETCODE_TIMEOVER: |
universe@34 | 376 | printw("\rYour opponent's time ran out - you win!"); |
universe@7 | 377 | clrtoeol(); |
universe@7 | 378 | return 1; |
universe@44 | 379 | case NETCODE_RESIGN: |
universe@44 | 380 | printw("\rYour opponent resigned!"); |
universe@34 | 381 | clrtoeol(); |
universe@34 | 382 | return 1; |
universe@46 | 383 | case NETCODE_CONNLOST: |
universe@46 | 384 | printw("\rYour opponent has left the game."); |
universe@46 | 385 | clrtoeol(); |
universe@46 | 386 | return 1; |
universe@34 | 387 | case NETCODE_REMIS: |
universe@34 | 388 | if (prompt_yesno( |
universe@34 | 389 | "\rYour opponent offers remis - do you accept")) { |
universe@34 | 390 | printw("\rRemis accepted!"); |
universe@7 | 391 | clrtoeol(); |
universe@34 | 392 | net_send_code(opponent, NETCODE_ACCEPT); |
universe@7 | 393 | return 1; |
universe@7 | 394 | } else { |
universe@34 | 395 | net_send_code(opponent, NETCODE_DECLINE); |
universe@7 | 396 | } |
universe@34 | 397 | break; |
universe@34 | 398 | case NETCODE_MOVE: |
universe@34 | 399 | net_recieve_data(opponent, &move, sizeof(Move)); |
universe@34 | 400 | if (validate_move(gamestate, &move)) { |
universe@34 | 401 | apply_move(gamestate, &move); |
universe@34 | 402 | if (move.check) { |
universe@34 | 403 | net_send_code(opponent, NETCODE_CHECK); |
universe@34 | 404 | } else if (gamestate->checkmate) { |
universe@34 | 405 | net_send_code(opponent, NETCODE_CHECKMATE); |
universe@34 | 406 | printw("\rCheckmate!"); |
universe@34 | 407 | clrtoeol(); |
universe@34 | 408 | return 1; |
universe@34 | 409 | } else if (gamestate->stalemate) { |
universe@34 | 410 | net_send_code(opponent, NETCODE_STALEMATE); |
universe@34 | 411 | printw("\rStalemate!"); |
universe@34 | 412 | clrtoeol(); |
universe@34 | 413 | return 1; |
universe@34 | 414 | } else { |
universe@34 | 415 | net_send_code(opponent, NETCODE_ACCEPT); |
universe@34 | 416 | } |
universe@34 | 417 | return 0; |
universe@34 | 418 | } else { |
universe@34 | 419 | net_send_code(opponent, NETCODE_DECLINE); |
universe@34 | 420 | } |
universe@45 | 421 | break; |
universe@45 | 422 | default: |
universe@45 | 423 | printw("\nInvalid network request."); |
universe@33 | 424 | } |
universe@7 | 425 | } |
universe@7 | 426 | } |
universe@7 | 427 | } |
universe@6 | 428 | |
universe@26 | 429 | static void init_board(GameState *gamestate) { |
universe@26 | 430 | Board initboard = { |
universe@26 | 431 | {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK}, |
universe@26 | 432 | {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN}, |
universe@26 | 433 | {0, 0, 0, 0, 0, 0, 0, 0}, |
universe@26 | 434 | {0, 0, 0, 0, 0, 0, 0, 0}, |
universe@26 | 435 | {0, 0, 0, 0, 0, 0, 0, 0}, |
universe@26 | 436 | {0, 0, 0, 0, 0, 0, 0, 0}, |
universe@26 | 437 | {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN}, |
universe@26 | 438 | {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK} |
universe@26 | 439 | }; |
universe@26 | 440 | memcpy(gamestate->board, initboard, sizeof(Board)); |
universe@26 | 441 | } |
universe@26 | 442 | |
universe@26 | 443 | void game_start_singlemachine(Settings *settings) { |
universe@33 | 444 | inputy = getmaxy(stdscr) - 6; |
universe@33 | 445 | |
universe@26 | 446 | GameState gamestate; |
universe@28 | 447 | memset(&gamestate, 0, sizeof(GameState)); |
universe@26 | 448 | init_board(&gamestate); |
universe@26 | 449 | gamestate.mycolor = WHITE; |
universe@30 | 450 | |
universe@26 | 451 | _Bool running; |
universe@26 | 452 | do { |
universe@26 | 453 | clear(); |
universe@26 | 454 | draw_board(&gamestate); |
universe@30 | 455 | running = !domove_singlemachine(&gamestate, &(settings->gameinfo)); |
universe@26 | 456 | gamestate.mycolor = opponent_color(gamestate.mycolor); |
universe@26 | 457 | } while (running); |
universe@28 | 458 | move(0,0); |
universe@28 | 459 | draw_board(&gamestate); |
universe@26 | 460 | |
universe@26 | 461 | gamestate_cleanup(&gamestate); |
universe@26 | 462 | } |
universe@26 | 463 | |
universe@6 | 464 | void game_start(Settings *settings, int opponent) { |
universe@33 | 465 | inputy = getmaxy(stdscr) - 6; |
universe@33 | 466 | |
universe@7 | 467 | _Bool myturn = is_server(settings) == |
universe@7 | 468 | (settings->gameinfo.servercolor == WHITE); |
universe@8 | 469 | |
universe@23 | 470 | GameState gamestate; |
universe@28 | 471 | memset(&gamestate, 0, sizeof(GameState)); |
universe@26 | 472 | init_board(&gamestate); |
universe@23 | 473 | gamestate.mycolor = myturn ? WHITE:BLACK; |
universe@7 | 474 | |
universe@23 | 475 | _Bool running; |
universe@7 | 476 | do { |
universe@7 | 477 | clear(); |
universe@23 | 478 | draw_board(&gamestate); |
universe@7 | 479 | if (myturn) { |
universe@33 | 480 | running = !sendmove(&gamestate, &(settings->gameinfo), opponent); |
universe@7 | 481 | } else { |
universe@33 | 482 | running = !recvmove(&gamestate, &(settings->gameinfo), opponent); |
universe@7 | 483 | } |
universe@11 | 484 | myturn ^= TRUE; |
universe@7 | 485 | } while (running); |
universe@7 | 486 | |
universe@28 | 487 | move(0,0); |
universe@28 | 488 | draw_board(&gamestate); |
universe@28 | 489 | |
universe@23 | 490 | gamestate_cleanup(&gamestate); |
universe@6 | 491 | } |