src/game.c

Mon, 31 Mar 2014 11:16:32 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Mar 2014 11:16:32 +0200
changeset 19
6a26114297a1
parent 18
6008840b859e
child 21
2e5846019b4f
permissions
-rw-r--r--

moved chess rules to separate lib

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@18 38 static void draw_board(Board board, MoveListRoot *movelist, 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@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@8 54 int cy = mycolor == WHITE ? boardy-y : boardy-7+y;
universe@8 55 int cx = 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@8 64 int x = mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
universe@8 65 int y = 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@18 75 MoveList *logelem = movelist->first;
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@18 82 wmove(tchess_window, 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@18 89 char logstr[] = {
universe@18 90 getpiecechr(move.piece),
universe@18 91 filechr(move.fromfile), rowchr(move.fromrow),
universe@18 92 move.capture ? 'x':'\0',
universe@18 93 filechr(move.tofile), rowchr(move.torow),
universe@18 94 move.check ? '+' : (move.checkmate ? '#' :
universe@18 95 (move.promotion ? '=' : '\0')),
universe@18 96 move.promotion ? getpiecechr(move.promotion) : '\0',
universe@18 97 ' '
universe@18 98 };
universe@18 99 for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
universe@18 100 if (logstr[stri]) {
universe@18 101 addch(logstr[stri]);
universe@18 102 }
universe@18 103 }
universe@18 104
universe@18 105 logelem = logelem->next;
universe@18 106 }
universe@18 107 }
universe@7 108 }
universe@7 109
universe@8 110
universe@18 111 static int sendmove(Board board, MoveListRoot *movelist,
universe@18 112 uint8_t mycolor, int opponent) {
universe@18 113
universe@7 114 const size_t buflen = 8;
universe@8 115 char movestr[buflen];
universe@7 116 _Bool remisrejected = FALSE;
universe@11 117 uint8_t code;
universe@7 118
universe@18 119 int inputy = getmaxy(tchess_window) - 6;
universe@7 120 while (1) {
universe@18 121 move(inputy, 0);
universe@7 122 if (remisrejected) {
universe@7 123 printw(
universe@7 124 "Use chess notation to enter your move.\n"
universe@7 125 "Remis offer rejected - type 'surr' to surrender. \n\n"
universe@7 126 "Type your move: ");
universe@7 127 } else {
universe@7 128 printw(
universe@7 129 "Use chess notation to enter your move.\n"
universe@7 130 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
universe@7 131 "Type your move: ");
universe@7 132 }
universe@7 133 clrtoeol();
universe@7 134 refresh();
universe@8 135 getnstr(movestr, buflen);
universe@7 136
universe@8 137 if (strncmp(movestr, "surr", buflen) == 0) {
universe@7 138 printw("You surrendered!");
universe@16 139 clrtoeol();
universe@8 140 refresh();
universe@7 141 net_send_code(opponent, NETCODE_SURRENDER);
universe@7 142 return 1;
universe@8 143 } else if (strncmp(movestr, "remis", buflen) == 0) {
universe@7 144 if (!remisrejected) {
universe@7 145 net_send_code(opponent, NETCODE_REMIS);
universe@7 146 printw("Remis offer sent - waiting for acceptance...");
universe@7 147 refresh();
universe@7 148 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
universe@7 149 printw("\rRemis accepted!");
universe@7 150 clrtoeol();
universe@8 151 refresh();
universe@7 152 return 1;
universe@7 153 } else {
universe@7 154 remisrejected = TRUE;
universe@7 155 }
universe@7 156 }
universe@7 157 } else {
universe@8 158 Move move;
universe@16 159 int eval_result = eval_move(board, mycolor, movestr, &move);
universe@16 160 switch (eval_result) {
universe@18 161 case VALID_MOVE_SYNTAX:
universe@18 162 net_send_code(opponent, NETCODE_MOVE);
universe@18 163 net_send_data(opponent, &move, sizeof(Move));
universe@18 164 code = net_recieve_code(opponent);
universe@18 165 move.check = code == NETCODE_CHECK;
universe@18 166 move.checkmate = code == NETCODE_CHECKMATE;
universe@18 167 addmove(movelist, &move);
universe@18 168 if (code == NETCODE_DECLINE) {
universe@18 169 printw("Invalid move.");
universe@18 170 } else {
universe@18 171 apply_move(board, &move);
universe@18 172 if (move.checkmate) {
universe@18 173 printw("Checkmate!");
universe@18 174 clrtoeol();
universe@18 175 return 1;
universe@14 176 } else {
universe@18 177 return 0;
universe@11 178 }
universe@18 179 }
universe@18 180 break;
universe@18 181 case AMBIGUOUS_MOVE:
universe@18 182 printw("Ambiguous move - please specify the piece to move.");
universe@18 183 break;
universe@18 184 case INVALID_POSITION:
universe@18 185 printw("Cannot find the piece that shall be moved.");
universe@18 186 break;
universe@18 187 case NEED_PROMOTION:
universe@18 188 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
universe@18 189 break;
universe@18 190 default:
universe@18 191 printw("Can't interpret move - please use algebraic notation.");
universe@8 192 }
universe@16 193 clrtoeol();
universe@7 194 }
universe@7 195 }
universe@7 196 }
universe@7 197
universe@18 198 static int recvmove(Board board, MoveListRoot *movelist, int opponent) {
universe@7 199
universe@18 200 int inputy = getmaxy(tchess_window) - 6;
universe@7 201 while (1) {
universe@18 202 move(inputy, 0);
universe@7 203 printw("Awaiting opponent move...");
universe@7 204 clrtoeol();
universe@7 205 refresh();
universe@7 206
universe@7 207 // TODO: nonblocking
universe@7 208 uint32_t code = net_recieve_code(opponent);
universe@8 209
universe@8 210 Move move;
universe@7 211 switch (code) {
universe@7 212 case NETCODE_SURRENDER:
universe@7 213 printw("\rYour opponent surrendered!");
universe@7 214 clrtoeol();
universe@7 215 return 1;
universe@7 216 case NETCODE_REMIS:
universe@7 217 if (prompt_yesno(
universe@7 218 "\rYour opponent offers remis - do you accept")) {
universe@7 219 printw("\rRemis accepted!");
universe@7 220 clrtoeol();
universe@7 221 net_send_code(opponent, NETCODE_ACCEPT);
universe@7 222 return 1;
universe@7 223 } else {
universe@7 224 net_send_code(opponent, NETCODE_DECLINE);
universe@7 225 }
universe@7 226 break;
universe@7 227 case NETCODE_MOVE:
universe@8 228 net_recieve_data(opponent, &move, sizeof(Move));
universe@11 229 if (validate_move(board, &move)) {
universe@8 230 apply_move(board, &move);
universe@18 231 addmove(movelist, &move);
universe@11 232 if (move.check) {
universe@11 233 net_send_code(opponent, NETCODE_CHECK);
universe@11 234 } else if (move.checkmate) {
universe@11 235 net_send_code(opponent, NETCODE_CHECKMATE);
universe@11 236 } else {
universe@11 237 net_send_code(opponent, NETCODE_ACCEPT);
universe@11 238 }
universe@8 239 return 0;
universe@8 240 } else {
universe@8 241 net_send_code(opponent, NETCODE_DECLINE);
universe@8 242 }
universe@7 243 }
universe@7 244 }
universe@7 245 }
universe@6 246
universe@18 247 void freemovelist(MoveListRoot* list) {
universe@18 248 MoveList *elem;
universe@18 249 elem = list->first;
universe@18 250 while (elem) {
universe@18 251 MoveList *cur = elem;
universe@18 252 elem = elem->next;
universe@18 253 free(cur);
universe@18 254 };
universe@18 255 free(list);
universe@18 256 }
universe@18 257
universe@18 258 void addmove(MoveListRoot* list, Move *move) {
universe@18 259 MoveList *elem = malloc(sizeof(MoveList));
universe@18 260 elem->next = NULL;
universe@18 261 elem->move = *move;
universe@18 262
universe@18 263 if (list->last) {
universe@18 264 list->last->next = elem;
universe@18 265 list->last = elem;
universe@18 266 } else {
universe@18 267 list->first = list->last = elem;
universe@18 268 }
universe@18 269 }
universe@18 270
universe@6 271 void game_start(Settings *settings, int opponent) {
universe@7 272 _Bool myturn = is_server(settings) ==
universe@7 273 (settings->gameinfo.servercolor == WHITE);
universe@8 274 uint8_t mycolor = myturn ? WHITE:BLACK;
universe@8 275
universe@7 276 _Bool running;
universe@6 277
universe@18 278 MoveListRoot* movelist = calloc(1, sizeof(MoveListRoot));
universe@18 279
universe@7 280 Board board = {
universe@7 281 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
universe@7 282 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
universe@7 283 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 284 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 285 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 286 {0, 0, 0, 0, 0, 0, 0, 0},
universe@7 287 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
universe@7 288 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
universe@7 289 };
universe@7 290
universe@7 291 do {
universe@7 292 clear();
universe@18 293 draw_board(board, movelist, mycolor);
universe@7 294 if (myturn) {
universe@18 295 running = !sendmove(board, movelist, mycolor, opponent);
universe@7 296 } else {
universe@18 297 running = !recvmove(board, movelist, opponent);
universe@7 298 flushinp(); // flush any input the user hacked in while waiting
universe@7 299 }
universe@11 300 myturn ^= TRUE;
universe@7 301 } while (running);
universe@7 302
universe@18 303 freemovelist(movelist);
universe@18 304
universe@7 305 mvaddstr(getmaxy(tchess_window)-1, 0,
universe@7 306 "Game has ended. Press any key to leave...");
universe@7 307 getch();
universe@6 308 }

mercurial