src/game.c

Tue, 08 Apr 2014 21:13:28 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 08 Apr 2014 21:13:28 +0200
changeset 31
ed440bcd9740
parent 30
a285ee393860
child 32
8a0b85303ee8
permissions
-rw-r--r--

fixed some type bugs, uninitialized memory and async input function

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

mercurial