src/game.c

Wed, 16 Apr 2014 21:46:34 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 16 Apr 2014 21:46:34 +0200
changeset 41
a8346dcf7bbf
parent 37
bcf624518909
child 42
21cb830efe91
permissions
-rw-r--r--

faced struct alignment problems

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"
35
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
33 #include "colors.h"
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
34 #include <ncurses.h>
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
35 #include <string.h>
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
36 #include <inttypes.h>
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
37 #include <sys/select.h>
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
38
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
39 static const uint8_t boardx = 10, boardy = 10;
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
40 static int inputy = 21; /* should be overridden on game startup */
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
41
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
42 static int timecontrol(GameState *gamestate, GameInfo *gameinfo) {
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
43 if (gameinfo->timecontrol) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
44 uint16_t white = remaining_movetime(gameinfo, gamestate, WHITE);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
45 uint16_t black = remaining_movetime(gameinfo, gamestate, BLACK);
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
46 mvprintw(boardy+4, boardx-1,
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
47 "White time: %4" PRIu16 ":%02" PRIu16,
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
48 white / 60, white % 60);
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
49 mvprintw(boardy+5, boardx-1,
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
50 "Black time: %4" PRIu16 ":%02" PRIu16,
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
51 black / 60, black % 60);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
52
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
53 if (white == 0) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
54 move(inputy, 0);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
55 printw("Time is over - Black wins!");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
56 clrtobot();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
57 refresh();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
58 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
59 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
60 if (black == 0) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
61 move(inputy, 0);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
62 printw("Time is over - White wins!");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
63 clrtobot();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
64 refresh();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
65 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
66 }
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
67 }
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
68
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
69 return 0;
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
70 }
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
71
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
72 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
73 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
74 for (uint8_t x = 0 ; x < 8 ; x++) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
75 uint8_t col = gamestate->board[y][x] & COLOR_MASK;
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
76 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
77 char piecec;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
78 if (piece) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
79 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
80 } else {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
81 piecec = ' ';
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
82 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
83
35
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
84 _Bool boardblack = (y&1)==(x&1);
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
85 attrset((col==WHITE ? A_BOLD : A_DIM)|
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
86 COLOR_PAIR(col == WHITE ?
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
87 (boardblack ? COL_WB : COL_WW) :
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
88 (boardblack ? COL_BB : COL_BW)
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
89 )
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
90 );
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
91
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
92 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
93 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
94 mvaddch(cy, cx, ' ');
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
95 mvaddch(cy, cx+1, piecec);
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
96 mvaddch(cy, cx+2, ' ');
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
97 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
98 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
99
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
100 attrset(A_NORMAL);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
101 for (uint8_t i = 0 ; i < 8 ; i++) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
102 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
103 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
104 mvaddch(boardy+1, x, 'a'+i);
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
105 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
106 }
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
107
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
108 /* move log */
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
109 // 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
110 uint8_t logy = 0;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
111 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
112 int logi = 1;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
113 MoveList *logelem = gamestate->movelist;
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
114
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
115 while (logelem) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
116 logi++;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
117 if (logi % 2 == 0) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
118 if ((logi - 2) % 4 == 0) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
119 logy++;
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
120 move(logy, logx);
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
121 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
122 printw("%d. ", logi / 2);
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
123 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
124
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
125 if (logelem) {
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
126 Move move = logelem->move;
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
127 if ((move.piece&PIECE_MASK) == KING &&
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
128 abs(move.tofile-move.fromfile) == 2) {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
129 addstr(move.tofile==fileidx('c')?"O-O-O":"O-O");
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
130 } else {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
131 char logstr[] = {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
132 getpiecechr(move.piece),
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
133 filechr(move.fromfile), rowchr(move.fromrow),
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
134 move.capture ? 'x':'\0',
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
135 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
136 move.check ? '+' : (move.promotion ? '=' : '\0'),
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
137 move.promotion ? getpiecechr(move.promotion) : '\0'
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
138 };
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
139 for (int stri = 0 ; stri < sizeof(logstr) ; stri++) {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
140 if (logstr[stri]) {
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
141 addch(logstr[stri]);
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
142 }
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
143 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
144 }
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
145 if (!logelem->next) {
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
146 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
147 addstr("\b#");
27
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
148 } else if (gamestate->stalemate) {
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
149 addstr(" stalemate");
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
150 }
efeb98bc69c9 moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents: 26
diff changeset
151 }
25
3ab0c2e1a4e2 implemented king
Mike Becker <universe@uap-core.de>
parents: 23
diff changeset
152 addch(' ');
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
153
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
154 logelem = logelem->next;
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
155 }
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
156 }
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
157 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
158
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
159 static void eval_move_failed_msg(int code) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
160 switch (code) {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
161 case AMBIGUOUS_MOVE:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
162 printw("Ambiguous move - please specify the piece to move.");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
163 break;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
164 case INVALID_POSITION:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
165 printw("Cannot find the piece that shall be moved.");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
166 break;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
167 case NEED_PROMOTION:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
168 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
169 break;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
170 default:
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
171 printw("Can't interpret move - please use algebraic notation.");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
172 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
173 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
174
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
175 #define MOVESTR_BUFLEN 8
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
176 static int domove_singlemachine(GameState *gamestate, GameInfo *gameinfo) {
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
177
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
178
32
8a0b85303ee8 improved async input + improved build system + added time values to move struct
Mike Becker <universe@uap-core.de>
parents: 31
diff changeset
179 size_t bufpos = 0;
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
180 char movestr[MOVESTR_BUFLEN];
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
181
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
182 flushinp();
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
183 while (1) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
184 if (timecontrol(gamestate, gameinfo)) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
185 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
186 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
187
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
188 move(inputy, 0);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
189 printw(
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
190 "Use chess notation to enter your move.\n"
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
191 "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
192 "Type your move: ");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
193 clrtoeol();
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
194
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
195 if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) {
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
196 if (strncmp(movestr, "surr", MOVESTR_BUFLEN) == 0) {
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
197 printw("%s surrendered!",
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
198 gamestate->mycolor==WHITE?"White":"Black");
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
199 clrtoeol();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
200 refresh();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
201 return 1;
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
202 } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) {
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
203 printw("Game ends remis.");
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
204 clrtoeol();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
205 refresh();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
206 return 1;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
207 } else {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
208 Move move;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
209 int eval_result = eval_move(gamestate, movestr, &move);
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
210 switch (eval_result) {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
211 case VALID_MOVE_SYNTAX:
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
212 if (validate_move(gamestate, &move)) {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
213 apply_move(gamestate, &move);
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
214 if (gamestate->checkmate) {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
215 printw("Checkmate!");
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
216 clrtoeol();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
217 return 1;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
218 } else if (gamestate->stalemate) {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
219 printw("Stalemate!");
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
220 clrtoeol();
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
221 return 1;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
222 } else {
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
223 return 0;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
224 }
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
225 } else {
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
226 printw("Invalid move.");
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
227 }
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
228 break;
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
229 default:
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
230 eval_move_failed_msg(eval_result);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
231 }
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
232 clrtoeol();
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
233 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
234 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
235 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
236 }
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
237
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
238 static int sendmove(GameState *gamestate, GameInfo *gameinfo, int opponent) {
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
239
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
240 size_t bufpos = 0;
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
241 char movestr[MOVESTR_BUFLEN];
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
242 _Bool remisrejected = FALSE;
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
243 uint8_t code;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
244
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
245 flushinp();
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
246 while (1) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
247 if (timecontrol(gamestate, gameinfo)) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
248 net_send_code(opponent, NETCODE_TIMEOVER);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
249 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
250 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
251
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
252 move(inputy, 0);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
253 if (remisrejected) {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
254 printw(
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
255 "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
256 "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
257 "Type your move: ");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
258 } else {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
259 printw(
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
260 "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
261 "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
262 "Type your move: ");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
263 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
264 clrtoeol();
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
265
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
266 if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) {
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
267 if (strncmp(movestr, "surr", MOVESTR_BUFLEN) == 0) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
268 printw("You surrendered!");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
269 clrtoeol();
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
270 refresh();
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
271 net_send_code(opponent, NETCODE_SURRENDER);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
272 return 1;
37
bcf624518909 minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents: 35
diff changeset
273 } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
274 if (!remisrejected) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
275 net_send_code(opponent, NETCODE_REMIS);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
276 printw("Remis offer sent - waiting for acceptance...");
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
277 refresh();
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
278 if (net_recieve_code(opponent) == NETCODE_ACCEPT) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
279 printw("\rRemis accepted!");
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
280 clrtoeol();
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
281 refresh();
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
282 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
283 } else {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
284 remisrejected = TRUE;
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
285 }
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
286 }
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
287 } else {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
288 Move move;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
289 int eval_result = eval_move(gamestate, movestr, &move);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
290 switch (eval_result) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
291 case VALID_MOVE_SYNTAX:
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
292 net_send_data(opponent, NETCODE_MOVE, &move, sizeof(Move));
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
293 code = net_recieve_code(opponent);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
294 move.check = code == NETCODE_CHECK;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
295 gamestate->checkmate = code == NETCODE_CHECKMATE;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
296 gamestate->stalemate = code == NETCODE_STALEMATE;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
297 if (code == NETCODE_DECLINE) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
298 printw("Invalid move.");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
299 } else {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
300 apply_move(gamestate, &move);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
301 if (gamestate->checkmate) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
302 printw("Checkmate!");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
303 clrtoeol();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
304 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
305 } else if (gamestate->stalemate) {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
306 printw("Stalemate!");
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
307 clrtoeol();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
308 return 1;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
309 } else {
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
310 return 0;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
311 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
312 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
313 break;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
314 default:
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
315 eval_move_failed_msg(eval_result);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
316 }
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
317 clrtoeol();
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
318 }
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
319 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
320 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
321 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
322
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
323 static int recvmove(GameState *gamestate, GameInfo *gameinfo, int opponent) {
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
324
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
325 if (net_setnonblocking(opponent, 1)) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
326 printw("Cannot setup nonblocking IO on network socket");
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
327 cbreak(); getch();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
328 exit(EXIT_FAILURE);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
329 }
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
330
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
331 struct timeval timeout;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
332 while (1) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
333 timecontrol(gamestate, gameinfo);
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
334
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
335 move(inputy, 0);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
336 printw("Awaiting opponent move...");
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
337 clrtoeol();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
338 refresh();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
339
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
340 fd_set readfds;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
341
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
342 FD_ZERO(&readfds);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
343 FD_SET(opponent, &readfds);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
344 timeout.tv_sec = 0;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
345 timeout.tv_usec = 1e5;
8
52d742aee695 implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents: 7
diff changeset
346
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
347 int result = select(opponent+1, &readfds, NULL, NULL, &timeout);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
348 if (result == -1) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
349 printw("\rCannot perform asynchronous network IO");
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
350 cbreak(); getch();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
351 exit(EXIT_FAILURE);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
352 }
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
353 if (result > 0) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
354 uint32_t code = net_recieve_code(opponent);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
355
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
356 Move move;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
357 switch (code) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
358 case NETCODE_TIMEOVER:
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
359 printw("\rYour opponent's time ran out - you win!");
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
360 clrtoeol();
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
361 return 1;
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
362 case NETCODE_SURRENDER:
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
363 printw("\rYour opponent surrendered!");
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
364 clrtoeol();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
365 return 1;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
366 case NETCODE_REMIS:
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
367 if (prompt_yesno(
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
368 "\rYour opponent offers remis - do you accept")) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
369 printw("\rRemis accepted!");
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
370 clrtoeol();
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
371 net_send_code(opponent, NETCODE_ACCEPT);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
372 return 1;
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
373 } else {
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
374 net_send_code(opponent, NETCODE_DECLINE);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
375 }
34
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
376 break;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
377 case NETCODE_MOVE:
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
378 net_recieve_data(opponent, &move, sizeof(Move));
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
379 if (validate_move(gamestate, &move)) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
380 apply_move(gamestate, &move);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
381 if (move.check) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
382 net_send_code(opponent, NETCODE_CHECK);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
383 } else if (gamestate->checkmate) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
384 net_send_code(opponent, NETCODE_CHECKMATE);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
385 printw("\rCheckmate!");
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
386 clrtoeol();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
387 return 1;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
388 } else if (gamestate->stalemate) {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
389 net_send_code(opponent, NETCODE_STALEMATE);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
390 printw("\rStalemate!");
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
391 clrtoeol();
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
392 return 1;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
393 } else {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
394 net_send_code(opponent, NETCODE_ACCEPT);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
395 }
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
396 return 0;
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
397 } else {
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
398 net_send_code(opponent, NETCODE_DECLINE);
c4d4b8a8f902 added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents: 33
diff changeset
399 }
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
400 }
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
401 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
402 }
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
403 }
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
404
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
405 static void init_board(GameState *gamestate) {
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
406 Board initboard = {
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
407 {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
408 {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
409 {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
410 {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
411 {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
412 {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
413 {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
414 {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
415 };
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
416 memcpy(gamestate->board, initboard, sizeof(Board));
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
417 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
418
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
419 void game_start_singlemachine(Settings *settings) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
420 inputy = getmaxy(stdscr) - 6;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
421
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
422 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
423 memset(&gamestate, 0, sizeof(GameState));
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
424 init_board(&gamestate);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
425 gamestate.mycolor = WHITE;
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
426
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
427 _Bool running;
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
428 do {
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
429 clear();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
430 draw_board(&gamestate);
30
a285ee393860 experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents: 28
diff changeset
431 running = !domove_singlemachine(&gamestate, &(settings->gameinfo));
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
432 gamestate.mycolor = opponent_color(gamestate.mycolor);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
433 } 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
434 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
435 draw_board(&gamestate);
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
436
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
437 gamestate_cleanup(&gamestate);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
438
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
439 mvaddstr(getmaxy(stdscr)-1, 0,
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
440 "Game has ended. Press any key to leave...");
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
441 refresh();
35
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
442 cbreak();
6c64b7a073af tried to improve colors
Mike Becker <universe@uap-core.de>
parents: 34
diff changeset
443 flushinp();
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
444 getch();
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
445 }
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
446
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
447 void game_start(Settings *settings, int opponent) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
448 inputy = getmaxy(stdscr) - 6;
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
449
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
450 _Bool myturn = is_server(settings) ==
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
451 (settings->gameinfo.servercolor == WHITE);
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
452
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
453 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
454 memset(&gamestate, 0, sizeof(GameState));
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
455 init_board(&gamestate);
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
456 gamestate.mycolor = myturn ? WHITE:BLACK;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
457
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
458 _Bool running;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
459 do {
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
460 clear();
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
461 draw_board(&gamestate);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
462 if (myturn) {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
463 running = !sendmove(&gamestate, &(settings->gameinfo), opponent);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
464 } else {
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
465 running = !recvmove(&gamestate, &(settings->gameinfo), opponent);
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
466 }
11
08d7a6e3ec31 added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents: 10
diff changeset
467 myturn ^= TRUE;
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
468 } while (running);
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
469
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
470 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
471 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
472
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
473 gamestate_cleanup(&gamestate);
18
6008840b859e fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents: 17
diff changeset
474
23
824c9522ce66 introduced game state structure
Mike Becker <universe@uap-core.de>
parents: 22
diff changeset
475 mvaddstr(getmaxy(stdscr)-1, 0,
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
476 "Game has ended. Press any key to leave...");
26
e0a76ee1bb2b introduced single machine mode
Mike Becker <universe@uap-core.de>
parents: 25
diff changeset
477 refresh();
33
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
478 cbreak();
866025982aa9 implemented time control
Mike Becker <universe@uap-core.de>
parents: 32
diff changeset
479 flushinp();
7
41468077b5bb implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents: 6
diff changeset
480 getch();
6
daaf6e5b3501 structured code
Mike Becker <universe@uap-core.de>
parents:
diff changeset
481 }

mercurial