Tue, 01 Apr 2014 14:04:00 +0200
introduced single machine mode
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 "network.h"
32 #include "input.h"
33 #include <ncurses.h>
34 #include <string.h>
36 static const uint8_t boardx = 10, boardy = 10;
38 static void draw_board(GameState *gamestate) {
40 for (uint8_t y = 0 ; y < 8 ; y++) {
41 for (uint8_t x = 0 ; x < 8 ; x++) {
42 uint8_t col = gamestate->board[y][x] & COLOR_MASK;
43 uint8_t piece = gamestate->board[y][x] & PIECE_MASK;
44 char piecec;
45 if (piece) {
46 piecec = piece == PAWN ? 'P' : getpiecechr(piece);
47 } else {
48 piecec = ' ';
49 }
51 attrset((col == WHITE ? A_BOLD : A_DIM) |
52 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
54 int cy = gamestate->mycolor == WHITE ? boardy-y : boardy-7+y;
55 int cx = gamestate->mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
56 mvaddch(cy, cx, ' ');
57 mvaddch(cy, cx+1, piecec);
58 mvaddch(cy, cx+2, ' ');
59 }
60 }
62 attrset(A_NORMAL);
63 for (uint8_t i = 0 ; i < 8 ; i++) {
64 int x = gamestate->mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
65 int y = gamestate->mycolor == WHITE ? boardy-i : boardy-7+i;
66 mvaddch(boardy+1, x, 'a'+i);
67 mvaddch(y, boardx-2, '1'+i);
68 }
70 /* move log */
71 // TODO: introduce window to avoid bugs with a long move log
72 uint8_t logy = 0;
73 const uint8_t logx = boardx + 30;
74 int logi = 1;
75 MoveList *logelem = gamestate->movelist;
77 while (logelem) {
78 logi++;
79 if (logi % 2 == 0) {
80 if ((logi - 2) % 4 == 0) {
81 logy++;
82 move(logy, logx);
83 }
84 printw("%d. ", logi / 2);
85 }
87 if (logelem) {
88 Move move = logelem->move;
89 if ((move.piece&PIECE_MASK) == KING &&
90 abs(move.tofile-move.fromfile) == 2) {
91 addstr(move.tofile==fileidx('c')?"O-O-O":"O-O");
92 } else {
93 char logstr[] = {
94 getpiecechr(move.piece),
95 filechr(move.fromfile), rowchr(move.fromrow),
96 move.capture ? 'x':'\0',
97 filechr(move.tofile), rowchr(move.torow),
98 move.check ? '+' : (move.checkmate ? '#' :
99 (move.promotion ? '=' : '\0')),
100 move.promotion ? getpiecechr(move.promotion) : '\0'
101 };
102 for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
103 if (logstr[stri]) {
104 addch(logstr[stri]);
105 }
106 }
107 }
108 addch(' ');
110 logelem = logelem->next;
111 }
112 }
113 }
115 static void eval_move_failed_msg(int code) {
116 switch (code) {
117 case AMBIGUOUS_MOVE:
118 printw("Ambiguous move - please specify the piece to move.");
119 break;
120 case INVALID_POSITION:
121 printw("Cannot find the piece that shall be moved.");
122 break;
123 case NEED_PROMOTION:
124 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
125 break;
126 default:
127 printw("Can't interpret move - please use algebraic notation.");
128 }
129 }
131 static int domove_singlemachine(GameState *gamestate) {
133 const size_t buflen = 8;
134 char movestr[buflen];
136 int inputy = getmaxy(stdscr) - 6;
137 while (1) {
138 move(inputy, 0);
139 printw(
140 "Use chess notation to enter your move.\n"
141 "Or type 'surr' to surrender or 'remis' to end with remis.\n\n"
142 "Type your move: ");
143 clrtoeol();
144 refresh();
145 getnstr(movestr, buflen);
147 if (strncmp(movestr, "surr", buflen) == 0) {
148 printw("%s surrendered!",
149 gamestate->mycolor==WHITE?"White":"Black");
150 clrtoeol();
151 refresh();
152 return 1;
153 } else if (strncmp(movestr, "remis", buflen) == 0) {
154 printw("Game ends remis.");
155 clrtoeol();
156 refresh();
157 return 1;
158 } else {
159 Move move;
160 int eval_result = eval_move(gamestate, movestr, &move);
161 switch (eval_result) {
162 case VALID_MOVE_SYNTAX:
163 if (validate_move(gamestate, &move)) {
164 apply_move(gamestate, &move);
165 if (move.checkmate) {
166 printw("Checkmate!");
167 clrtoeol();
168 return 1;
169 } else if (move.stalemate) {
170 printw("Stalemate!");
171 clrtoeol();
172 return 1;
173 } else {
174 return 0;
175 }
176 } else {
177 printw("Invalid move.");
178 }
179 break;
180 default:
181 eval_move_failed_msg(eval_result);
182 }
183 clrtoeol();
184 }
185 }
186 }
188 static int sendmove(GameState *gamestate, int opponent) {
190 const size_t buflen = 8;
191 char movestr[buflen];
192 _Bool remisrejected = FALSE;
193 uint8_t code;
195 int inputy = getmaxy(stdscr) - 6;
196 while (1) {
197 move(inputy, 0);
198 if (remisrejected) {
199 printw(
200 "Use chess notation to enter your move.\n"
201 "Remis offer rejected - type 'surr' to surrender. \n\n"
202 "Type your move: ");
203 } else {
204 printw(
205 "Use chess notation to enter your move.\n"
206 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
207 "Type your move: ");
208 }
209 clrtoeol();
210 refresh();
211 getnstr(movestr, buflen);
213 if (strncmp(movestr, "surr", buflen) == 0) {
214 printw("You surrendered!");
215 clrtoeol();
216 refresh();
217 net_send_code(opponent, NETCODE_SURRENDER);
218 return 1;
219 } else if (strncmp(movestr, "remis", buflen) == 0) {
220 if (!remisrejected) {
221 net_send_code(opponent, NETCODE_REMIS);
222 printw("Remis offer sent - waiting for acceptance...");
223 refresh();
224 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
225 printw("\rRemis accepted!");
226 clrtoeol();
227 refresh();
228 return 1;
229 } else {
230 remisrejected = TRUE;
231 }
232 }
233 } else {
234 Move move;
235 int eval_result = eval_move(gamestate, movestr, &move);
236 switch (eval_result) {
237 case VALID_MOVE_SYNTAX:
238 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
239 code = net_recieve_code(opponent);
240 move.check = code == NETCODE_CHECK;
241 move.checkmate = code == NETCODE_CHECKMATE;
242 move.stalemate = code == NETCODE_STALEMATE;
243 if (code == NETCODE_DECLINE) {
244 printw("Invalid move.");
245 } else {
246 apply_move(gamestate, &move);
247 if (move.checkmate) {
248 printw("Checkmate!");
249 clrtoeol();
250 return 1;
251 } else if (move.stalemate) {
252 printw("Stalemate!");
253 clrtoeol();
254 return 1;
255 } else {
256 return 0;
257 }
258 }
259 break;
260 default:
261 eval_move_failed_msg(eval_result);
262 }
263 clrtoeol();
264 }
265 }
266 }
268 static int recvmove(GameState *gamestate, int opponent) {
270 int inputy = getmaxy(stdscr) - 6;
271 while (1) {
272 move(inputy, 0);
273 printw("Awaiting opponent move...");
274 clrtoeol();
275 refresh();
277 // TODO: nonblocking
278 uint32_t code = net_recieve_code(opponent);
280 Move move;
281 switch (code) {
282 case NETCODE_SURRENDER:
283 printw("\rYour opponent surrendered!");
284 clrtoeol();
285 return 1;
286 case NETCODE_REMIS:
287 if (prompt_yesno(
288 "\rYour opponent offers remis - do you accept")) {
289 printw("\rRemis accepted!");
290 clrtoeol();
291 net_send_code(opponent, NETCODE_ACCEPT);
292 return 1;
293 } else {
294 net_send_code(opponent, NETCODE_DECLINE);
295 }
296 break;
297 case NETCODE_MOVE:
298 net_recieve_data(opponent, &move, sizeof(Move));
299 if (validate_move(gamestate, &move)) {
300 apply_move(gamestate, &move);
301 if (move.check) {
302 net_send_code(opponent, NETCODE_CHECK);
303 } else if (move.checkmate) {
304 net_send_code(opponent, NETCODE_CHECKMATE);
305 printw("\rCheckmate!");
306 clrtoeol();
307 return 1;
308 } else if (move.stalemate) {
309 net_send_code(opponent, NETCODE_STALEMATE);
310 printw("\rStalemate!");
311 clrtoeol();
312 return 1;
313 } else {
314 net_send_code(opponent, NETCODE_ACCEPT);
315 }
316 return 0;
317 } else {
318 net_send_code(opponent, NETCODE_DECLINE);
319 }
320 }
321 }
322 }
324 static void init_board(GameState *gamestate) {
325 Board initboard = {
326 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
327 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
328 {0, 0, 0, 0, 0, 0, 0, 0},
329 {0, 0, 0, 0, 0, 0, 0, 0},
330 {0, 0, 0, 0, 0, 0, 0, 0},
331 {0, 0, 0, 0, 0, 0, 0, 0},
332 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
333 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
334 };
335 memcpy(gamestate->board, initboard, sizeof(Board));
336 }
338 void game_start_singlemachine(Settings *settings) {
339 GameState gamestate;
340 init_board(&gamestate);
341 gamestate.mycolor = WHITE;
342 gamestate.movelist = gamestate.lastmove = NULL;
343 // TODO: time limit
344 _Bool running;
345 do {
346 clear();
347 draw_board(&gamestate);
348 running = !domove_singlemachine(&gamestate);
349 gamestate.mycolor = opponent_color(gamestate.mycolor);
350 } while (running);
352 gamestate_cleanup(&gamestate);
354 mvaddstr(getmaxy(stdscr)-1, 0,
355 "Game has ended. Press any key to leave...");
356 refresh();
357 getch();
358 }
360 void game_start(Settings *settings, int opponent) {
361 _Bool myturn = is_server(settings) ==
362 (settings->gameinfo.servercolor == WHITE);
364 // TODO: time limit
365 GameState gamestate;
366 init_board(&gamestate);
367 gamestate.mycolor = myturn ? WHITE:BLACK;
368 gamestate.movelist = gamestate.lastmove = NULL;
370 _Bool running;
371 do {
372 clear();
373 draw_board(&gamestate);
374 if (myturn) {
375 running = !sendmove(&gamestate, opponent);
376 } else {
377 running = !recvmove(&gamestate, opponent);
378 flushinp(); // flush any input the user hacked in while waiting
379 }
380 myturn ^= TRUE;
381 } while (running);
383 gamestate_cleanup(&gamestate);
385 mvaddstr(getmaxy(stdscr)-1, 0,
386 "Game has ended. Press any key to leave...");
387 refresh();
388 getch();
389 }