src/game.c

Tue, 01 Apr 2014 14:04:00 +0200

author
Mike Becker <universe@uap-core.de>
date
Tue, 01 Apr 2014 14:04:00 +0200
changeset 26
e0a76ee1bb2b
parent 25
3ab0c2e1a4e2
child 27
efeb98bc69c9
permissions
-rw-r--r--

introduced single machine mode

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

mercurial