Wed, 26 Mar 2014 13:16:49 +0100
interface for retrieving locations when using short algebraic notation
inverted return values of not yet implemented functions to prevent bugs
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2014 Mike Becker. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 *
28 */
30 #include "game.h"
31 #include "input.h"
32 #include "rules/rules.h"
33 #include <ncurses.h>
34 #include <string.h>
36 static const uint8_t boardx = 10, boardy = 10;
38 static void draw_board(Board board, uint8_t mycolor) {
40 for (uint8_t y = 0 ; y < 8 ; y++) {
41 for (uint8_t x = 0 ; x < 8 ; x++) {
42 uint8_t col = board[y][x] & COLOR_MASK;
43 uint8_t piece = board[y][x] & PIECE_MASK;
44 char piecec = ' ';
45 switch (piece) {
46 case PAWN: piecec = 'P'; break;
47 case ROOK: piecec = 'R'; break;
48 case KNIGHT: piecec = 'N'; break;
49 case BISHOP: piecec = 'B'; break;
50 case QUEEN: piecec = 'Q'; break;
51 case KING: piecec = 'K'; break;
52 }
54 attrset((col == WHITE ? A_BOLD : A_DIM) |
55 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
57 int cy = mycolor == WHITE ? boardy-y : boardy-7+y;
58 int cx = mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
59 mvaddch(cy, cx, ' ');
60 mvaddch(cy, cx+1, piecec);
61 mvaddch(cy, cx+2, ' ');
62 }
63 }
65 attrset(A_NORMAL);
66 for (uint8_t i = 0 ; i < 8 ; i++) {
67 int x = mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
68 int y = mycolor == WHITE ? boardy-i : boardy-7+i;
69 mvaddch(boardy+1, x, 'a'+i);
70 mvaddch(y, boardx-2, '1'+i);
71 }
72 }
74 static void apply_move(Board board, Move *move) {
75 board[move->fromrow][move->fromfile] = 0;
76 // TODO: care for en passant capture
77 board[move->torow][move->tofile] = move->piece;
79 /* castling */
80 if ((move->piece & PIECE_MASK) == KING &&
81 move->fromfile == fileidx('e')) {
82 uint8_t color = move->piece & COLOR_MASK;
84 if (move->tofile == fileidx('g')) {
85 board[move->torow][fileidx('h')] = 0;
86 board[move->torow][fileidx('f')] = color|ROOK;
87 } else if (move->tofile == fileidx('c')) {
88 board[move->torow][fileidx('a')] = 0;
89 board[move->torow][fileidx('d')] = color|ROOK;
90 }
91 }
92 }
94 static _Bool validate_move(Board board, Move *move) {
95 _Bool result;
97 /* does piece exist */
98 result = board[move->fromrow][move->fromfile] == move->piece;
100 switch (move->piece & PIECE_MASK) {
101 case PAWN:
102 result = result && pawn_chkrules(board, move);
103 result = result && !pawn_isblocked(board, move);
104 break;
105 case ROOK:
106 result = result && rook_chkrules(board, move);
107 result = result && !rook_isblocked(board, move);
108 break;
109 case KNIGHT:
110 result = result && knight_chkrules(board, move);
111 result = result && !knight_isblocked(board, move);
112 break;
113 case BISHOP:
114 result = result && bishop_chkrules(board, move);
115 result = result && !bishop_isblocked(board, move);
116 break;
117 case QUEEN:
118 result = result && queen_chkrules(board, move);
119 result = result && !queen_isblocked(board, move);
120 break;
121 case KING:
122 result = result && king_chkrules(board, move);
123 result = result && !king_isblocked(board, move);
124 break;
125 default:
126 result = FALSE;
127 }
129 /* is piece pinned */
130 // TODO: make it so
132 /* correct check and checkmate flags */
133 // TODO: make it so
135 return result;
136 }
138 /**
139 * Maps a character to a piece.
140 *
141 * Does not work for pawns, since they don't have a character.
142 *
143 * @param c one of R,N,B,Q,K
144 * @return numeric value for the specified piece
145 */
146 static uint8_t getpiece(char c) {
147 switch (c) {
148 case 'R': return ROOK;
149 case 'N': return KNIGHT;
150 case 'B': return BISHOP;
151 case 'Q': return QUEEN;
152 case 'K': return KING;
153 default: return 0;
154 }
155 }
157 /**
158 * Guesses the location of a piece for short algebraic notation.
159 *
160 * @param board the current state of the board
161 * @param move the move date to operate on
162 * @return TRUE if the location could be retrieved, FALSE if the location is
163 * ambiguous
164 */
165 static _Bool getlocation(Board board, Move *move) {
166 uint8_t piece = move->piece & PIECE_MASK;
167 switch (piece) {
168 case PAWN: return pawn_getlocation(board, move);
169 case ROOK: return rook_getlocation(board, move);
170 case KNIGHT: return knight_getlocation(board, move);
171 case BISHOP: return bishop_getlocation(board, move);
172 case QUEEN: return queen_getlocation(board, move);
173 case KING: return king_getlocation(board, move);
174 default: return FALSE;
175 }
176 }
178 /**
179 * Evaluates a move syntactically and stores the move data in the specified
180 * object.
181 *
182 * @param board the current state of the board
183 * @param mycolor the color of the current player
184 * @param mstr the input string to parse
185 * @param move a pointer to object where the move data shall be stored
186 * @return TRUE, if the move is syntactically valid, FALSE otherwise
187 */
188 static _Bool eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
189 memset(move, 0, sizeof(Move));
190 move->fromfile = POS_UNSPECIFIED;
191 move->fromrow = POS_UNSPECIFIED;
193 size_t len = strlen(mstr);
195 /* evaluate check/checkmate flags */
196 if (mstr[len-1] == '+') {
197 len--; mstr[len] = '\0';
198 move->check = TRUE;
199 } else if (mstr[len-1] == '#') {
200 len--; mstr[len] = '\0';
201 move->checkmate = TRUE;
202 }
204 if (len == 2) {
205 /* pawn move (e.g. "e4") */
206 if (isfile(mstr[0]) && isrow(mstr[1])) {
207 move->piece = PAWN;
208 move->tofile = fileidx(mstr[0]);
209 move->torow = rowidx(mstr[1]);
210 }
211 } else if (len == 3) {
212 if (strcmp(mstr, "O-O") == 0) {
213 /* king side castling */
214 move->piece = KING;
215 move->fromfile = fileidx('e');
216 move->tofile = fileidx('g');
217 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
218 } else {
219 /* unambiguous move (e.g. "Nf3") */
220 move->piece = getpiece(mstr[0]);
221 move->tofile = isfile(mstr[1]) ? fileidx(mstr[1]) : POS_UNSPECIFIED;
222 move->torow = isrow(mstr[2]) ? fileidx(mstr[2]) : POS_UNSPECIFIED;
223 }
225 } else if (len == 4) {
226 /* ambiguous move (e.g. "Ndf3") */
228 /* unambiguous capture (e.g. "Nxf3", "dxe5") */
230 } else if (len == 5) {
231 if (strcmp(mstr, "O-O-O") == 0) {
232 /* queen side castling "O-O-O" */
233 move->piece = KING;
234 move->fromfile = fileidx('e');
235 move->tofile = fileidx('c');
236 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
237 } else {
238 /* ambiguous capture (e.g. "Ndxf3") */
240 /* long notation move (e.g. "Nc5a4") */
242 /* long notation capture (e.g. "e5xf6") */
243 }
244 } else if (len == 6) {
245 /* long notation capture (e.g. "Nc5xf3") */
246 }
248 if (move->piece) {
249 move->piece |= mycolor;
250 }
252 if (!getlocation(board, move)) {
253 // TODO: return status code to indicate the error type
254 move->piece = 0;
255 }
257 return move->piece != 0;
258 }
260 static int sendmove(Board board, uint8_t mycolor, int opponent) {
261 const size_t buflen = 8;
262 char movestr[buflen];
263 _Bool remisrejected = FALSE;
264 uint8_t code;
266 while (1) {
267 move(boardy+3, 0);
268 if (remisrejected) {
269 printw(
270 "Use chess notation to enter your move.\n"
271 "Remis offer rejected - type 'surr' to surrender. \n\n"
272 "Type your move: ");
273 } else {
274 printw(
275 "Use chess notation to enter your move.\n"
276 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
277 "Type your move: ");
278 }
279 clrtoeol();
280 refresh();
281 getnstr(movestr, buflen);
283 if (strncmp(movestr, "surr", buflen) == 0) {
284 printw("You surrendered!");
285 refresh();
286 net_send_code(opponent, NETCODE_SURRENDER);
287 return 1;
288 } else if (strncmp(movestr, "remis", buflen) == 0) {
289 if (!remisrejected) {
290 net_send_code(opponent, NETCODE_REMIS);
291 printw("Remis offer sent - waiting for acceptance...");
292 refresh();
293 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
294 printw("\rRemis accepted!");
295 clrtoeol();
296 refresh();
297 return 1;
298 } else {
299 remisrejected = TRUE;
300 }
301 }
302 } else {
303 Move move;
304 if (eval_move(board, mycolor, movestr, &move)) {
305 net_send_code(opponent, NETCODE_MOVE);
306 net_send_data(opponent, &move, sizeof(Move));
307 code = net_recieve_code(opponent);
308 move.check = code == NETCODE_CHECK;
309 move.checkmate = code == NETCODE_CHECKMATE;
310 // TODO: record move
311 if (code == NETCODE_DECLINE) {
312 printw("Invalid move.");
313 clrtoeol();
314 } else {
315 apply_move(board, &move);
316 if (move.checkmate) {
317 printw("Checkmate!");
318 return 1;
319 }
320 }
321 } else {
322 printw("Can't interpret move - please use algebraic notation.");
323 }
324 }
325 }
326 }
328 static int recvmove(Board board, int opponent) {
330 while (1) {
331 move(boardy+3, 0);
332 printw("Awaiting opponent move...");
333 clrtoeol();
334 refresh();
336 // TODO: nonblocking
337 uint32_t code = net_recieve_code(opponent);
339 Move move;
340 switch (code) {
341 case NETCODE_SURRENDER:
342 printw("\rYour opponent surrendered!");
343 clrtoeol();
344 return 1;
345 case NETCODE_REMIS:
346 if (prompt_yesno(
347 "\rYour opponent offers remis - do you accept")) {
348 printw("\rRemis accepted!");
349 clrtoeol();
350 net_send_code(opponent, NETCODE_ACCEPT);
351 return 1;
352 } else {
353 net_send_code(opponent, NETCODE_DECLINE);
354 }
355 break;
356 case NETCODE_MOVE:
357 net_recieve_data(opponent, &move, sizeof(Move));
358 if (validate_move(board, &move)) {
359 apply_move(board, &move);
360 // TODO: record move
361 if (move.check) {
362 net_send_code(opponent, NETCODE_CHECK);
363 } else if (move.checkmate) {
364 net_send_code(opponent, NETCODE_CHECKMATE);
365 } else {
366 net_send_code(opponent, NETCODE_ACCEPT);
367 }
368 return 0;
369 } else {
370 net_send_code(opponent, NETCODE_DECLINE);
371 }
372 }
373 }
374 }
376 void game_start(Settings *settings, int opponent) {
377 _Bool myturn = is_server(settings) ==
378 (settings->gameinfo.servercolor == WHITE);
379 uint8_t mycolor = myturn ? WHITE:BLACK;
381 _Bool running;
383 Board board = {
384 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
385 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
386 {0, 0, 0, 0, 0, 0, 0, 0},
387 {0, 0, 0, 0, 0, 0, 0, 0},
388 {0, 0, 0, 0, 0, 0, 0, 0},
389 {0, 0, 0, 0, 0, 0, 0, 0},
390 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
391 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
392 };
394 do {
395 clear();
396 draw_board(board, mycolor);
397 if (myturn) {
398 running = !sendmove(board, mycolor, opponent);
399 } else {
400 running = !recvmove(board, opponent);
401 flushinp(); // flush any input the user hacked in while waiting
402 }
403 myturn ^= TRUE;
404 } while (running);
406 mvaddstr(getmaxy(tchess_window)-1, 0,
407 "Game has ended. Press any key to leave...");
408 getch();
409 }