src/game.c

Sat, 22 Mar 2014 17:23:07 +0100

author
Mike Becker <universe@uap-core.de>
date
Sat, 22 Mar 2014 17:23:07 +0100
changeset 10
1347e4dabac0
parent 9
4e4f156bba58
child 11
08d7a6e3ec31
permissions
-rw-r--r--

prepared code base for implementing rules

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@7 31 #include "input.h"
universe@10 32 #include "rules/rules.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@8 38 static void draw_board(Board board, uint8_t mycolor) {
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@7 42 uint8_t col = board[y][x] & COLOR_MASK;
universe@7 43 uint8_t piece = board[y][x] & PIECE_MASK;
universe@7 44 char piecec = ' ';
universe@7 45 switch (piece) {
universe@7 46 case PAWN: piecec = 'P'; break;
universe@7 47 case ROOK: piecec = 'R'; break;
universe@7 48 case KNIGHT: piecec = 'N'; break;
universe@7 49 case BISHOP: piecec = 'B'; break;
universe@7 50 case QUEEN: piecec = 'Q'; break;
universe@7 51 case KING: piecec = 'K'; break;
universe@7 52 }
universe@7 53
universe@7 54 attrset((col == WHITE ? A_BOLD : A_DIM) |
universe@7 55 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
universe@7 56
universe@8 57 int cy = mycolor == WHITE ? boardy-y : boardy-7+y;
universe@8 58 int cx = mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
universe@8 59 mvaddch(cy, cx, ' ');
universe@8 60 mvaddch(cy, cx+1, piecec);
universe@8 61 mvaddch(cy, cx+2, ' ');
universe@7 62 }
universe@7 63 }
universe@7 64
universe@7 65 attrset(A_NORMAL);
universe@7 66 for (uint8_t i = 0 ; i < 8 ; i++) {
universe@8 67 int x = mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
universe@8 68 int y = mycolor == WHITE ? boardy-i : boardy-7+i;
universe@8 69 mvaddch(boardy+1, x, 'a'+i);
universe@8 70 mvaddch(y, boardx-2, '1'+i);
universe@7 71 }
universe@7 72 }
universe@7 73
universe@8 74 static void apply_move(Board board, Move *move) {
universe@8 75 board[move->fromrow][move->fromfile] = 0;
universe@8 76 // TODO: care for en passant capture
universe@8 77 board[move->torow][move->tofile] = move->piece;
universe@9 78
universe@9 79 /* castling */
universe@9 80 if ((move->piece & PIECE_MASK) == KING &&
universe@9 81 move->fromfile == fileidx('e')) {
universe@9 82 uint8_t color = move->piece & COLOR_MASK;
universe@9 83
universe@9 84 if (move->tofile == fileidx('g')) {
universe@9 85 board[move->torow][fileidx('h')] = 0;
universe@9 86 board[move->torow][fileidx('f')] = color|ROOK;
universe@9 87 } else if (move->tofile == fileidx('c')) {
universe@9 88 board[move->torow][fileidx('a')] = 0;
universe@9 89 board[move->torow][fileidx('d')] = color|ROOK;
universe@9 90 }
universe@9 91 }
universe@8 92 }
universe@8 93
universe@8 94 static _Bool validate_move(Board board, uint8_t mycolor, Move *move) {
universe@10 95 _Bool result;
universe@8 96
universe@8 97 /* does piece exist */
universe@10 98 result = board[move->fromrow][move->fromfile] == move->piece;
universe@8 99
universe@10 100 switch (move->piece & PIECE_MASK) {
universe@10 101 case PAWN:
universe@10 102 result = result && pawn_chkrules(board, move);
universe@10 103 result = result && !pawn_isblocked(board, move);
universe@10 104 break;
universe@10 105 case ROOK:
universe@10 106 result = result && rook_chkrules(board, move);
universe@10 107 result = result && !rook_isblocked(board, move);
universe@10 108 break;
universe@10 109 case KNIGHT:
universe@10 110 result = result && knight_chkrules(board, move);
universe@10 111 result = result && !knight_isblocked(board, move);
universe@10 112 break;
universe@10 113 case BISHOP:
universe@10 114 result = result && bishop_chkrules(board, move);
universe@10 115 result = result && !bishop_isblocked(board, move);
universe@10 116 break;
universe@10 117 case QUEEN:
universe@10 118 result = result && queen_chkrules(board, move);
universe@10 119 result = result && !queen_isblocked(board, move);
universe@10 120 break;
universe@10 121 case KING:
universe@10 122 result = result && king_chkrules(board, move);
universe@10 123 result = result && !king_isblocked(board, move);
universe@10 124 break;
universe@10 125 default:
universe@10 126 result = FALSE;
universe@10 127 }
universe@8 128
universe@8 129 /* is piece pinned */
universe@8 130 // TODO: make it so
universe@8 131
universe@8 132 return result;
universe@8 133 }
universe@8 134
universe@9 135 static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
universe@8 136 memset(move, 0, sizeof(Move));
universe@8 137
universe@9 138 size_t len = strlen(mstr);
universe@8 139
universe@8 140 /* remove check */
universe@9 141 if (mstr[len-1] == '+') {
universe@9 142 len--; mstr[len] = '\0';
universe@8 143 move->check = TRUE;
universe@8 144 }
universe@8 145
universe@8 146 if (len == 2) {
universe@8 147 /* pawn move (e.g. "e4") */
universe@9 148 if (isfile(mstr[0]) && isrow(mstr[1])) {
universe@10 149 move->piece = PAWN|mycolor;
universe@10 150 move->tofile = fileidx(mstr[0]);
universe@9 151 move->torow = rowidx(mstr[1]);
universe@10 152 if (!pawn_getlocation(board, move)) {
universe@8 153 move->piece = 0;
universe@8 154 }
universe@8 155 }
universe@8 156 } else if (len == 3) {
universe@9 157 if (strcmp(mstr, "O-O") == 0) {
universe@8 158 /* king side castling */
universe@10 159 move->piece = KING|mycolor;
universe@8 160 move->fromfile = fileidx('e');
universe@9 161 move->tofile = fileidx('g');
universe@8 162 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
universe@8 163 } else {
universe@8 164 /* unambiguous move (e.g. "Nf3") */
universe@8 165 }
universe@8 166
universe@8 167 } else if (len == 4) {
universe@8 168 /* ambiguous move (e.g. "Ndf3") */
universe@8 169
universe@8 170 /* unambiguous capture (e.g. "Nxf3", "dxe5") */
universe@8 171
universe@8 172 } else if (len == 5) {
universe@9 173 if (strcmp(mstr, "O-O-O") == 0) {
universe@9 174 /* queen side castling "O-O-O" */
universe@10 175 move->piece = KING|mycolor;
universe@9 176 move->fromfile = fileidx('e');
universe@9 177 move->tofile = fileidx('c');
universe@9 178 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
universe@9 179 } else {
universe@9 180 /* ambiguous capture (e.g. "Ndxf3") */
universe@9 181
universe@9 182 /* long notation move (e.g. "Nc5a4") */
universe@9 183
universe@9 184 /* long notation capture (e.g. "e5xf6") */
universe@9 185 }
universe@8 186 } else if (len == 6) {
universe@8 187 /* long notation capture (e.g. "Nc5xf3") */
universe@8 188 }
universe@8 189
universe@10 190 return move->piece != 0;
universe@8 191 }
universe@8 192
universe@8 193 static int sendmove(Board board, uint8_t mycolor, int opponent) {
universe@7 194 const size_t buflen = 8;
universe@8 195 char movestr[buflen];
universe@7 196 _Bool remisrejected = FALSE;
universe@7 197
universe@7 198 while (1) {
universe@7 199 move(boardy+3, 0);
universe@7 200 if (remisrejected) {
universe@7 201 printw(
universe@7 202 "Use chess notation to enter your move.\n"
universe@7 203 "Remis offer rejected - type 'surr' to surrender. \n\n"
universe@7 204 "Type your move: ");
universe@7 205 } else {
universe@7 206 printw(
universe@7 207 "Use chess notation to enter your move.\n"
universe@7 208 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
universe@7 209 "Type your move: ");
universe@7 210 }
universe@7 211 clrtoeol();
universe@7 212 refresh();
universe@8 213 getnstr(movestr, buflen);
universe@7 214
universe@8 215 if (strncmp(movestr, "surr", buflen) == 0) {
universe@7 216 printw("You surrendered!");
universe@8 217 refresh();
universe@7 218 net_send_code(opponent, NETCODE_SURRENDER);
universe@7 219 return 1;
universe@8 220 } else if (strncmp(movestr, "remis", buflen) == 0) {
universe@7 221 if (!remisrejected) {
universe@7 222 net_send_code(opponent, NETCODE_REMIS);
universe@7 223 printw("Remis offer sent - waiting for acceptance...");
universe@7 224 refresh();
universe@7 225 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
universe@7 226 printw("\rRemis accepted!");
universe@7 227 clrtoeol();
universe@8 228 refresh();
universe@7 229 return 1;
universe@7 230 } else {
universe@7 231 remisrejected = TRUE;
universe@7 232 }
universe@7 233 }
universe@7 234 } else {
universe@8 235 Move move;
universe@8 236 if (eval_move(board, mycolor, movestr, &move)) {
universe@8 237 net_send_code(opponent, NETCODE_MOVE);
universe@8 238 net_send_data(opponent, &move, sizeof(Move));
universe@8 239 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
universe@8 240 apply_move(board, &move);
universe@8 241 return 0;
universe@8 242 } else {
universe@8 243 printw("Invalid move.");
universe@8 244 clrtoeol();
universe@8 245 }
universe@8 246 } else {
universe@8 247 printw("Can't interpret move - please use algebraic notation.");
universe@8 248 }
universe@7 249 }
universe@7 250 }
universe@7 251 }
universe@7 252
universe@8 253 static int recvmove(Board board, uint8_t mycolor, int opponent) {
universe@7 254
universe@7 255 while (1) {
universe@7 256 move(boardy+3, 0);
universe@7 257 printw("Awaiting opponent move...");
universe@7 258 clrtoeol();
universe@7 259 refresh();
universe@7 260
universe@7 261 // TODO: nonblocking
universe@7 262 uint32_t code = net_recieve_code(opponent);
universe@8 263
universe@8 264 Move move;
universe@7 265 switch (code) {
universe@7 266 case NETCODE_SURRENDER:
universe@7 267 printw("\rYour opponent surrendered!");
universe@7 268 clrtoeol();
universe@7 269 return 1;
universe@7 270 case NETCODE_REMIS:
universe@7 271 if (prompt_yesno(
universe@7 272 "\rYour opponent offers remis - do you accept")) {
universe@7 273 printw("\rRemis accepted!");
universe@7 274 clrtoeol();
universe@7 275 net_send_code(opponent, NETCODE_ACCEPT);
universe@7 276 return 1;
universe@7 277 } else {
universe@7 278 net_send_code(opponent, NETCODE_DECLINE);
universe@7 279 }
universe@7 280 break;
universe@7 281 case NETCODE_MOVE:
universe@8 282 net_recieve_data(opponent, &move, sizeof(Move));
universe@8 283 if (validate_move(board, mycolor, &move)) {
universe@8 284 apply_move(board, &move);
universe@8 285 net_send_code(opponent, NETCODE_ACCEPT);
universe@8 286 return 0;
universe@8 287 } else {
universe@8 288 net_send_code(opponent, NETCODE_DECLINE);
universe@8 289 }
universe@7 290 }
universe@7 291 }
universe@7 292 }
universe@6 293
universe@6 294 void game_start(Settings *settings, int opponent) {
universe@7 295 _Bool myturn = is_server(settings) ==
universe@7 296 (settings->gameinfo.servercolor == WHITE);
universe@8 297 uint8_t mycolor = myturn ? WHITE:BLACK;
universe@8 298
universe@7 299 _Bool running;
universe@6 300
universe@7 301 Board board = {
universe@7 302 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
universe@7 303 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
universe@7 304 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 305 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 306 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 307 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 308 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
universe@7 309 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
universe@7 310 };
universe@7 311
universe@7 312 do {
universe@7 313 clear();
universe@8 314 draw_board(board, mycolor);
universe@7 315 if (myturn) {
universe@8 316 running = !sendmove(board, mycolor, opponent);
universe@7 317 } else {
universe@8 318 running = !recvmove(board, mycolor, opponent);
universe@7 319 flushinp(); // flush any input the user hacked in while waiting
universe@7 320 }
universe@7 321 myturn ^= 1;
universe@7 322 } while (running);
universe@7 323
universe@7 324 mvaddstr(getmaxy(tchess_window)-1, 0,
universe@7 325 "Game has ended. Press any key to leave...");
universe@7 326 getch();
universe@6 327 }

mercurial