src/game.c

Fri, 04 Apr 2014 17:36:42 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 04 Apr 2014 17:36:42 +0200
changeset 28
0c1371488d87
parent 27
efeb98bc69c9
child 30
a285ee393860
permissions
-rw-r--r--

NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between

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@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@23 38 static void draw_board(GameState *gamestate) {
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@23 42 uint8_t col = gamestate->board[y][x] & COLOR_MASK;
universe@23 43 uint8_t piece = gamestate->board[y][x] & PIECE_MASK;
universe@18 44 char piecec;
universe@18 45 if (piece) {
universe@18 46 piecec = piece == PAWN ? 'P' : getpiecechr(piece);
universe@18 47 } else {
universe@18 48 piecec = ' ';
universe@7 49 }
universe@7 50
universe@7 51 attrset((col == WHITE ? A_BOLD : A_DIM) |
universe@7 52 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
universe@7 53
universe@23 54 int cy = gamestate->mycolor == WHITE ? boardy-y : boardy-7+y;
universe@23 55 int cx = gamestate->mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
universe@8 56 mvaddch(cy, cx, ' ');
universe@8 57 mvaddch(cy, cx+1, piecec);
universe@8 58 mvaddch(cy, cx+2, ' ');
universe@7 59 }
universe@7 60 }
universe@7 61
universe@7 62 attrset(A_NORMAL);
universe@7 63 for (uint8_t i = 0 ; i < 8 ; i++) {
universe@23 64 int x = gamestate->mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
universe@23 65 int y = gamestate->mycolor == WHITE ? boardy-i : boardy-7+i;
universe@8 66 mvaddch(boardy+1, x, 'a'+i);
universe@8 67 mvaddch(y, boardx-2, '1'+i);
universe@7 68 }
universe@18 69
universe@18 70 /* move log */
universe@18 71 // TODO: introduce window to avoid bugs with a long move log
universe@18 72 uint8_t logy = 0;
universe@18 73 const uint8_t logx = boardx + 30;
universe@18 74 int logi = 1;
universe@23 75 MoveList *logelem = gamestate->movelist;
universe@18 76
universe@18 77 while (logelem) {
universe@18 78 logi++;
universe@18 79 if (logi % 2 == 0) {
universe@18 80 if ((logi - 2) % 4 == 0) {
universe@18 81 logy++;
universe@23 82 move(logy, logx);
universe@18 83 }
universe@18 84 printw("%d. ", logi / 2);
universe@18 85 }
universe@18 86
universe@18 87 if (logelem) {
universe@18 88 Move move = logelem->move;
universe@25 89 if ((move.piece&PIECE_MASK) == KING &&
universe@25 90 abs(move.tofile-move.fromfile) == 2) {
universe@25 91 addstr(move.tofile==fileidx('c')?"O-O-O":"O-O");
universe@25 92 } else {
universe@25 93 char logstr[] = {
universe@25 94 getpiecechr(move.piece),
universe@25 95 filechr(move.fromfile), rowchr(move.fromrow),
universe@25 96 move.capture ? 'x':'\0',
universe@25 97 filechr(move.tofile), rowchr(move.torow),
universe@27 98 move.check ? '+' : (move.promotion ? '=' : '\0'),
universe@25 99 move.promotion ? getpiecechr(move.promotion) : '\0'
universe@25 100 };
universe@25 101 for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
universe@25 102 if (logstr[stri]) {
universe@25 103 addch(logstr[stri]);
universe@25 104 }
universe@18 105 }
universe@18 106 }
universe@27 107 if (!logelem->next) {
universe@27 108 if (gamestate->checkmate) {
universe@28 109 addstr("\b#");
universe@27 110 } else if (gamestate->stalemate) {
universe@27 111 addstr(" stalemate");
universe@27 112 }
universe@27 113 }
universe@25 114 addch(' ');
universe@18 115
universe@18 116 logelem = logelem->next;
universe@18 117 }
universe@18 118 }
universe@7 119 }
universe@7 120
universe@26 121 static void eval_move_failed_msg(int code) {
universe@26 122 switch (code) {
universe@26 123 case AMBIGUOUS_MOVE:
universe@26 124 printw("Ambiguous move - please specify the piece to move.");
universe@26 125 break;
universe@26 126 case INVALID_POSITION:
universe@26 127 printw("Cannot find the piece that shall be moved.");
universe@26 128 break;
universe@26 129 case NEED_PROMOTION:
universe@26 130 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
universe@26 131 break;
universe@26 132 default:
universe@26 133 printw("Can't interpret move - please use algebraic notation.");
universe@26 134 }
universe@26 135 }
universe@26 136
universe@26 137 static int domove_singlemachine(GameState *gamestate) {
universe@26 138
universe@26 139 const size_t buflen = 8;
universe@26 140 char movestr[buflen];
universe@26 141
universe@26 142 int inputy = getmaxy(stdscr) - 6;
universe@26 143 while (1) {
universe@26 144 move(inputy, 0);
universe@26 145 printw(
universe@26 146 "Use chess notation to enter your move.\n"
universe@26 147 "Or type 'surr' to surrender or 'remis' to end with remis.\n\n"
universe@26 148 "Type your move: ");
universe@26 149 clrtoeol();
universe@26 150 refresh();
universe@26 151 getnstr(movestr, buflen);
universe@26 152
universe@26 153 if (strncmp(movestr, "surr", buflen) == 0) {
universe@26 154 printw("%s surrendered!",
universe@26 155 gamestate->mycolor==WHITE?"White":"Black");
universe@26 156 clrtoeol();
universe@26 157 refresh();
universe@26 158 return 1;
universe@26 159 } else if (strncmp(movestr, "remis", buflen) == 0) {
universe@26 160 printw("Game ends remis.");
universe@26 161 clrtoeol();
universe@26 162 refresh();
universe@26 163 return 1;
universe@26 164 } else {
universe@26 165 Move move;
universe@26 166 int eval_result = eval_move(gamestate, movestr, &move);
universe@26 167 switch (eval_result) {
universe@26 168 case VALID_MOVE_SYNTAX:
universe@26 169 if (validate_move(gamestate, &move)) {
universe@26 170 apply_move(gamestate, &move);
universe@27 171 if (gamestate->checkmate) {
universe@26 172 printw("Checkmate!");
universe@26 173 clrtoeol();
universe@26 174 return 1;
universe@27 175 } else if (gamestate->stalemate) {
universe@26 176 printw("Stalemate!");
universe@26 177 clrtoeol();
universe@26 178 return 1;
universe@26 179 } else {
universe@26 180 return 0;
universe@26 181 }
universe@26 182 } else {
universe@26 183 printw("Invalid move.");
universe@26 184 }
universe@26 185 break;
universe@26 186 default:
universe@26 187 eval_move_failed_msg(eval_result);
universe@26 188 }
universe@26 189 clrtoeol();
universe@26 190 }
universe@26 191 }
universe@26 192 }
universe@8 193
universe@23 194 static int sendmove(GameState *gamestate, int opponent) {
universe@18 195
universe@7 196 const size_t buflen = 8;
universe@8 197 char movestr[buflen];
universe@7 198 _Bool remisrejected = FALSE;
universe@11 199 uint8_t code;
universe@7 200
universe@23 201 int inputy = getmaxy(stdscr) - 6;
universe@7 202 while (1) {
universe@18 203 move(inputy, 0);
universe@7 204 if (remisrejected) {
universe@7 205 printw(
universe@7 206 "Use chess notation to enter your move.\n"
universe@7 207 "Remis offer rejected - type 'surr' to surrender. \n\n"
universe@7 208 "Type your move: ");
universe@7 209 } else {
universe@7 210 printw(
universe@7 211 "Use chess notation to enter your move.\n"
universe@7 212 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
universe@7 213 "Type your move: ");
universe@7 214 }
universe@7 215 clrtoeol();
universe@7 216 refresh();
universe@8 217 getnstr(movestr, buflen);
universe@7 218
universe@8 219 if (strncmp(movestr, "surr", buflen) == 0) {
universe@7 220 printw("You surrendered!");
universe@16 221 clrtoeol();
universe@8 222 refresh();
universe@7 223 net_send_code(opponent, NETCODE_SURRENDER);
universe@7 224 return 1;
universe@8 225 } else if (strncmp(movestr, "remis", buflen) == 0) {
universe@7 226 if (!remisrejected) {
universe@7 227 net_send_code(opponent, NETCODE_REMIS);
universe@7 228 printw("Remis offer sent - waiting for acceptance...");
universe@7 229 refresh();
universe@7 230 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
universe@7 231 printw("\rRemis accepted!");
universe@7 232 clrtoeol();
universe@8 233 refresh();
universe@7 234 return 1;
universe@7 235 } else {
universe@7 236 remisrejected = TRUE;
universe@7 237 }
universe@7 238 }
universe@7 239 } else {
universe@8 240 Move move;
universe@23 241 int eval_result = eval_move(gamestate, movestr, &move);
universe@16 242 switch (eval_result) {
universe@18 243 case VALID_MOVE_SYNTAX:
universe@22 244 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
universe@18 245 code = net_recieve_code(opponent);
universe@18 246 move.check = code == NETCODE_CHECK;
universe@27 247 gamestate->checkmate = code == NETCODE_CHECKMATE;
universe@27 248 gamestate->stalemate = code == NETCODE_STALEMATE;
universe@18 249 if (code == NETCODE_DECLINE) {
universe@18 250 printw("Invalid move.");
universe@18 251 } else {
universe@23 252 apply_move(gamestate, &move);
universe@27 253 if (gamestate->checkmate) {
universe@18 254 printw("Checkmate!");
universe@18 255 clrtoeol();
universe@18 256 return 1;
universe@27 257 } else if (gamestate->stalemate) {
universe@26 258 printw("Stalemate!");
universe@26 259 clrtoeol();
universe@26 260 return 1;
universe@14 261 } else {
universe@18 262 return 0;
universe@11 263 }
universe@18 264 }
universe@18 265 break;
universe@18 266 default:
universe@26 267 eval_move_failed_msg(eval_result);
universe@8 268 }
universe@16 269 clrtoeol();
universe@7 270 }
universe@7 271 }
universe@7 272 }
universe@7 273
universe@23 274 static int recvmove(GameState *gamestate, int opponent) {
universe@7 275
universe@23 276 int inputy = getmaxy(stdscr) - 6;
universe@7 277 while (1) {
universe@18 278 move(inputy, 0);
universe@7 279 printw("Awaiting opponent move...");
universe@7 280 clrtoeol();
universe@7 281 refresh();
universe@7 282
universe@7 283 // TODO: nonblocking
universe@7 284 uint32_t code = net_recieve_code(opponent);
universe@8 285
universe@8 286 Move move;
universe@7 287 switch (code) {
universe@7 288 case NETCODE_SURRENDER:
universe@7 289 printw("\rYour opponent surrendered!");
universe@7 290 clrtoeol();
universe@7 291 return 1;
universe@7 292 case NETCODE_REMIS:
universe@7 293 if (prompt_yesno(
universe@7 294 "\rYour opponent offers remis - do you accept")) {
universe@7 295 printw("\rRemis accepted!");
universe@7 296 clrtoeol();
universe@7 297 net_send_code(opponent, NETCODE_ACCEPT);
universe@7 298 return 1;
universe@7 299 } else {
universe@7 300 net_send_code(opponent, NETCODE_DECLINE);
universe@7 301 }
universe@7 302 break;
universe@7 303 case NETCODE_MOVE:
universe@8 304 net_recieve_data(opponent, &move, sizeof(Move));
universe@23 305 if (validate_move(gamestate, &move)) {
universe@23 306 apply_move(gamestate, &move);
universe@11 307 if (move.check) {
universe@11 308 net_send_code(opponent, NETCODE_CHECK);
universe@27 309 } else if (gamestate->checkmate) {
universe@11 310 net_send_code(opponent, NETCODE_CHECKMATE);
universe@26 311 printw("\rCheckmate!");
universe@26 312 clrtoeol();
universe@26 313 return 1;
universe@27 314 } else if (gamestate->stalemate) {
universe@26 315 net_send_code(opponent, NETCODE_STALEMATE);
universe@26 316 printw("\rStalemate!");
universe@26 317 clrtoeol();
universe@26 318 return 1;
universe@11 319 } else {
universe@11 320 net_send_code(opponent, NETCODE_ACCEPT);
universe@11 321 }
universe@8 322 return 0;
universe@8 323 } else {
universe@8 324 net_send_code(opponent, NETCODE_DECLINE);
universe@8 325 }
universe@7 326 }
universe@7 327 }
universe@7 328 }
universe@6 329
universe@26 330 static void init_board(GameState *gamestate) {
universe@26 331 Board initboard = {
universe@26 332 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
universe@26 333 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
universe@26 334 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 335 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 336 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 337 {0, 0, 0, 0, 0, 0, 0, 0},
universe@26 338 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
universe@26 339 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
universe@26 340 };
universe@26 341 memcpy(gamestate->board, initboard, sizeof(Board));
universe@26 342 }
universe@26 343
universe@26 344 void game_start_singlemachine(Settings *settings) {
universe@26 345 GameState gamestate;
universe@28 346 memset(&gamestate, 0, sizeof(GameState));
universe@26 347 init_board(&gamestate);
universe@26 348 gamestate.mycolor = WHITE;
universe@26 349 // TODO: time limit
universe@26 350 _Bool running;
universe@26 351 do {
universe@26 352 clear();
universe@26 353 draw_board(&gamestate);
universe@26 354 running = !domove_singlemachine(&gamestate);
universe@26 355 gamestate.mycolor = opponent_color(gamestate.mycolor);
universe@26 356 } while (running);
universe@28 357 move(0,0);
universe@28 358 draw_board(&gamestate);
universe@26 359
universe@26 360 gamestate_cleanup(&gamestate);
universe@26 361
universe@26 362 mvaddstr(getmaxy(stdscr)-1, 0,
universe@26 363 "Game has ended. Press any key to leave...");
universe@26 364 refresh();
universe@26 365 getch();
universe@26 366 }
universe@26 367
universe@6 368 void game_start(Settings *settings, int opponent) {
universe@7 369 _Bool myturn = is_server(settings) ==
universe@7 370 (settings->gameinfo.servercolor == WHITE);
universe@8 371
universe@26 372 // TODO: time limit
universe@23 373 GameState gamestate;
universe@28 374 memset(&gamestate, 0, sizeof(GameState));
universe@26 375 init_board(&gamestate);
universe@23 376 gamestate.mycolor = myturn ? WHITE:BLACK;
universe@7 377
universe@23 378 _Bool running;
universe@7 379 do {
universe@7 380 clear();
universe@23 381 draw_board(&gamestate);
universe@7 382 if (myturn) {
universe@23 383 running = !sendmove(&gamestate, opponent);
universe@7 384 } else {
universe@23 385 running = !recvmove(&gamestate, opponent);
universe@7 386 flushinp(); // flush any input the user hacked in while waiting
universe@7 387 }
universe@11 388 myturn ^= TRUE;
universe@7 389 } while (running);
universe@7 390
universe@28 391 move(0,0);
universe@28 392 draw_board(&gamestate);
universe@28 393
universe@23 394 gamestate_cleanup(&gamestate);
universe@18 395
universe@23 396 mvaddstr(getmaxy(stdscr)-1, 0,
universe@7 397 "Game has ended. Press any key to leave...");
universe@26 398 refresh();
universe@7 399 getch();
universe@6 400 }

mercurial