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