src/game.c

Fri, 04 Apr 2014 17:36:42 +0200

author
Mike Becker <universe@uap-core.de>
date
Fri, 04 Apr 2014 17:36:42 +0200
changeset 28
0c1371488d87
parent 27
efeb98bc69c9
child 30
a285ee393860
permissions
-rw-r--r--

NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between

6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
1 /*
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
3 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
4 * Copyright 2014 Mike Becker. All rights reserved.
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
5 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
8 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
11 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
15 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
27 *
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
28 */
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
29
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
30 #include "game.h"
19
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents: 18
diff changeset
31 #include "network.h"
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
32 #include "input.h"
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
33 #include <ncurses.h>
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
34 #include <string.h>
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
35
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
36 static const uint8_t boardx = 10, boardy = 10;
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
37
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
38 static void draw_board(GameState *gamestate) {
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
39
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
40 for (uint8_t y = 0 ; y < 8 ; y++) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
41 for (uint8_t x = 0 ; x < 8 ; x++) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
42 uint8_t col = gamestate->board[y][x] & COLOR_MASK;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
43 uint8_t piece = gamestate->board[y][x] & PIECE_MASK;
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
44 char piecec;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
45 if (piece) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
46 piecec = piece == PAWN ? 'P' : getpiecechr(piece);
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
47 } else {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
48 piecec = ' ';
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
49 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
50
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
51 attrset((col == WHITE ? A_BOLD : A_DIM) |
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
52 COLOR_PAIR((y&1)==(x&1) ? COL_WB : COL_BW));
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
53
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
54 int cy = gamestate->mycolor == WHITE ? boardy-y : boardy-7+y;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
55 int cx = gamestate->mycolor == WHITE ? boardx+x*3 : boardx+21-x*3;
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
56 mvaddch(cy, cx, ' ');
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
57 mvaddch(cy, cx+1, piecec);
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
58 mvaddch(cy, cx+2, ' ');
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
59 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
60 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
61
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
62 attrset(A_NORMAL);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
63 for (uint8_t i = 0 ; i < 8 ; i++) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
64 int x = gamestate->mycolor == WHITE ? boardx+i*3+1 : boardx+22-i*3;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
65 int y = gamestate->mycolor == WHITE ? boardy-i : boardy-7+i;
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
66 mvaddch(boardy+1, x, 'a'+i);
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
67 mvaddch(y, boardx-2, '1'+i);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
68 }
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
69
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
70 /* move log */
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
71 // TODO: introduce window to avoid bugs with a long move log
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
72 uint8_t logy = 0;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
73 const uint8_t logx = boardx + 30;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
74 int logi = 1;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
75 MoveList *logelem = gamestate->movelist;
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
76
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
77 while (logelem) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
78 logi++;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
79 if (logi % 2 == 0) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
80 if ((logi - 2) % 4 == 0) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
81 logy++;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
82 move(logy, logx);
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
83 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
84 printw("%d. ", logi / 2);
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
85 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
86
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
87 if (logelem) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
88 Move move = logelem->move;
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
89 if ((move.piece&PIECE_MASK) == KING &&
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
90 abs(move.tofile-move.fromfile) == 2) {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
91 addstr(move.tofile==fileidx('c')?"O-O-O":"O-O");
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
92 } else {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
93 char logstr[] = {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
94 getpiecechr(move.piece),
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
95 filechr(move.fromfile), rowchr(move.fromrow),
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
96 move.capture ? 'x':'\0',
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
97 filechr(move.tofile), rowchr(move.torow),
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
98 move.check ? '+' : (move.promotion ? '=' : '\0'),
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
99 move.promotion ? getpiecechr(move.promotion) : '\0'
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
100 };
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
101 for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
102 if (logstr[stri]) {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
103 addch(logstr[stri]);
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
104 }
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
105 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
106 }
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
107 if (!logelem->next) {
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
108 if (gamestate->checkmate) {
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
109 addstr("\b#");
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
110 } else if (gamestate->stalemate) {
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
111 addstr(" stalemate");
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
112 }
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
113 }
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
114 addch(' ');
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
115
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
116 logelem = logelem->next;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
117 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
118 }
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
119 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
120
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
121 static void eval_move_failed_msg(int code) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
122 switch (code) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
123 case AMBIGUOUS_MOVE:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
124 printw("Ambiguous move - please specify the piece to move.");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
125 break;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
126 case INVALID_POSITION:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
127 printw("Cannot find the piece that shall be moved.");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
128 break;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
129 case NEED_PROMOTION:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
130 printw("You need to promote the pawn (append \"=Q\" e.g.)!");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
131 break;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
132 default:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
133 printw("Can't interpret move - please use algebraic notation.");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
134 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
135 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
136
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
137 static int domove_singlemachine(GameState *gamestate) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
138
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
139 const size_t buflen = 8;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
140 char movestr[buflen];
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
141
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
142 int inputy = getmaxy(stdscr) - 6;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
143 while (1) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
144 move(inputy, 0);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
145 printw(
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
146 "Use chess notation to enter your move.\n"
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
147 "Or type 'surr' to surrender or 'remis' to end with remis.\n\n"
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
148 "Type your move: ");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
149 clrtoeol();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
150 refresh();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
151 getnstr(movestr, buflen);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
152
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
153 if (strncmp(movestr, "surr", buflen) == 0) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
154 printw("%s surrendered!",
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
155 gamestate->mycolor==WHITE?"White":"Black");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
156 clrtoeol();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
157 refresh();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
158 return 1;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
159 } else if (strncmp(movestr, "remis", buflen) == 0) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
160 printw("Game ends remis.");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
161 clrtoeol();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
162 refresh();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
163 return 1;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
164 } else {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
165 Move move;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
166 int eval_result = eval_move(gamestate, movestr, &move);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
167 switch (eval_result) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
168 case VALID_MOVE_SYNTAX:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
169 if (validate_move(gamestate, &move)) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
170 apply_move(gamestate, &move);
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
171 if (gamestate->checkmate) {
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
172 printw("Checkmate!");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
173 clrtoeol();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
174 return 1;
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
175 } else if (gamestate->stalemate) {
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
176 printw("Stalemate!");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
177 clrtoeol();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
178 return 1;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
179 } else {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
180 return 0;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
181 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
182 } else {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
183 printw("Invalid move.");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
184 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
185 break;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
186 default:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
187 eval_move_failed_msg(eval_result);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
188 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
189 clrtoeol();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
190 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
191 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
192 }
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
193
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
194 static int sendmove(GameState *gamestate, int opponent) {
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
195
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
196 const size_t buflen = 8;
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
197 char movestr[buflen];
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
198 _Bool remisrejected = FALSE;
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
199 uint8_t code;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
200
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
201 int inputy = getmaxy(stdscr) - 6;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
202 while (1) {
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
203 move(inputy, 0);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
204 if (remisrejected) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
205 printw(
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
206 "Use chess notation to enter your move.\n"
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
207 "Remis offer rejected - type 'surr' to surrender. \n\n"
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
208 "Type your move: ");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
209 } else {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
210 printw(
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
211 "Use chess notation to enter your move.\n"
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
212 "Or type 'surr' to surrender or 'remis' to offer remis.\n\n"
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
213 "Type your move: ");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
214 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
215 clrtoeol();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
216 refresh();
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
217 getnstr(movestr, buflen);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
218
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
219 if (strncmp(movestr, "surr", buflen) == 0) {
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
220 printw("You surrendered!");
16
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 15
diff changeset
221 clrtoeol();
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
222 refresh();
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
223 net_send_code(opponent, NETCODE_SURRENDER);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
224 return 1;
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
225 } else if (strncmp(movestr, "remis", buflen) == 0) {
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
226 if (!remisrejected) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
227 net_send_code(opponent, NETCODE_REMIS);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
228 printw("Remis offer sent - waiting for acceptance...");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
229 refresh();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
230 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
231 printw("\rRemis accepted!");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
232 clrtoeol();
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
233 refresh();
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
234 return 1;
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
235 } else {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
236 remisrejected = TRUE;
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
237 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
238 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
239 } else {
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
240 Move move;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
241 int eval_result = eval_move(gamestate, movestr, &move);
16
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 15
diff changeset
242 switch (eval_result) {
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
243 case VALID_MOVE_SYNTAX:
22
41bbfd4d17a3 improved network performance
Mike Becker <universe@uap-core.de>
parents: 21
diff changeset
244 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
245 code = net_recieve_code(opponent);
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
246 move.check = code == NETCODE_CHECK;
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
247 gamestate->checkmate = code == NETCODE_CHECKMATE;
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
248 gamestate->stalemate = code == NETCODE_STALEMATE;
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
249 if (code == NETCODE_DECLINE) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
250 printw("Invalid move.");
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
251 } else {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
252 apply_move(gamestate, &move);
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
253 if (gamestate->checkmate) {
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
254 printw("Checkmate!");
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
255 clrtoeol();
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
256 return 1;
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
257 } else if (gamestate->stalemate) {
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
258 printw("Stalemate!");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
259 clrtoeol();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
260 return 1;
14
970748b9a73b fixed crucial bug where both players could move at the same time + added pawn rules (TODO: en passant)
Mike Becker <universe@uap-core.de>
parents: 13
diff changeset
261 } else {
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
262 return 0;
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
263 }
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
264 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
265 break;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
266 default:
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
267 eval_move_failed_msg(eval_result);
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
268 }
16
a298c6637c30 introduced status codes for get_location to produce detailed error messages + added knight rules
Mike Becker <universe@uap-core.de>
parents: 15
diff changeset
269 clrtoeol();
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
270 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
271 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
272 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
273
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
274 static int recvmove(GameState *gamestate, int opponent) {
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
275
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
276 int inputy = getmaxy(stdscr) - 6;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
277 while (1) {
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
278 move(inputy, 0);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
279 printw("Awaiting opponent move...");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
280 clrtoeol();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
281 refresh();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
282
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
283 // TODO: nonblocking
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
284 uint32_t code = net_recieve_code(opponent);
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
285
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
286 Move move;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
287 switch (code) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
288 case NETCODE_SURRENDER:
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
289 printw("\rYour opponent surrendered!");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
290 clrtoeol();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
291 return 1;
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
292 case NETCODE_REMIS:
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
293 if (prompt_yesno(
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
294 "\rYour opponent offers remis - do you accept")) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
295 printw("\rRemis accepted!");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
296 clrtoeol();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
297 net_send_code(opponent, NETCODE_ACCEPT);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
298 return 1;
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
299 } else {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
300 net_send_code(opponent, NETCODE_DECLINE);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
301 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
302 break;
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
303 case NETCODE_MOVE:
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
304 net_recieve_data(opponent, &move, sizeof(Move));
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
305 if (validate_move(gamestate, &move)) {
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
306 apply_move(gamestate, &move);
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
307 if (move.check) {
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
308 net_send_code(opponent, NETCODE_CHECK);
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
309 } else if (gamestate->checkmate) {
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
310 net_send_code(opponent, NETCODE_CHECKMATE);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
311 printw("\rCheckmate!");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
312 clrtoeol();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
313 return 1;
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
314 } else if (gamestate->stalemate) {
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
315 net_send_code(opponent, NETCODE_STALEMATE);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
316 printw("\rStalemate!");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
317 clrtoeol();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
318 return 1;
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
319 } else {
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
320 net_send_code(opponent, NETCODE_ACCEPT);
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
321 }
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
322 return 0;
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
323 } else {
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
324 net_send_code(opponent, NETCODE_DECLINE);
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
325 }
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
326 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
327 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
328 }
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
329
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
330 static void init_board(GameState *gamestate) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
331 Board initboard = {
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
332 {WROOK, WKNIGHT, WBISHOP, WQUEEN, WKING, WBISHOP, WKNIGHT, WROOK},
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
333 {WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN, WPAWN},
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
334 {0, 0, 0, 0, 0, 0, 0, 0},
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
335 {0, 0, 0, 0, 0, 0, 0, 0},
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
336 {0, 0, 0, 0, 0, 0, 0, 0},
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
337 {0, 0, 0, 0, 0, 0, 0, 0},
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
338 {BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN, BPAWN},
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
339 {BROOK, BKNIGHT, BBISHOP, BQUEEN, BKING, BBISHOP, BKNIGHT, BROOK}
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
340 };
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
341 memcpy(gamestate->board, initboard, sizeof(Board));
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
342 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
343
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
344 void game_start_singlemachine(Settings *settings) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
345 GameState gamestate;
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
346 memset(&gamestate, 0, sizeof(GameState));
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
347 init_board(&gamestate);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
348 gamestate.mycolor = WHITE;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
349 // TODO: time limit
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
350 _Bool running;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
351 do {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
352 clear();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
353 draw_board(&gamestate);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
354 running = !domove_singlemachine(&gamestate);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
355 gamestate.mycolor = opponent_color(gamestate.mycolor);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
356 } while (running);
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
357 move(0,0);
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
358 draw_board(&gamestate);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
359
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
360 gamestate_cleanup(&gamestate);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
361
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
362 mvaddstr(getmaxy(stdscr)-1, 0,
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
363 "Game has ended. Press any key to leave...");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
364 refresh();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
365 getch();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
366 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
367
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
368 void game_start(Settings *settings, int opponent) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
369 _Bool myturn = is_server(settings) ==
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
370 (settings->gameinfo.servercolor == WHITE);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
371
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
372 // TODO: time limit
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
373 GameState gamestate;
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
374 memset(&gamestate, 0, sizeof(GameState));
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
375 init_board(&gamestate);
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
376 gamestate.mycolor = myturn ? WHITE:BLACK;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
377
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
378 _Bool running;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
379 do {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
380 clear();
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
381 draw_board(&gamestate);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
382 if (myturn) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
383 running = !sendmove(&gamestate, opponent);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
384 } else {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
385 running = !recvmove(&gamestate, opponent);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
386 flushinp(); // flush any input the user hacked in while waiting
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
387 }
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
388 myturn ^= TRUE;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
389 } while (running);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
390
28
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
391 move(0,0);
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
392 draw_board(&gamestate);
0c1371488d87 NEED TESTING: implemented check and checkmate - TODO: avoid checkmate by moving another piece in between
Mike Becker <universe@uap-core.de>
parents: 27
diff changeset
393
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
394 gamestate_cleanup(&gamestate);
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
395
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
396 mvaddstr(getmaxy(stdscr)-1, 0,
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
397 "Game has ended. Press any key to leave...");
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
398 refresh();
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
399 getch();
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
400 }

mercurial