Wed, 11 Jun 2014 16:54:20 +0200
logging string representation of moves in short algebraic notation
6 | 1 | /* |
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. | |
3 | * | |
4 | * Copyright 2014 Mike Becker. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions are met: | |
8 | * | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * | |
12 | * 2. Redistributions in binary form must reproduce the above copyright | |
13 | * notice, this list of conditions and the following disclaimer in the | |
14 | * documentation and/or other materials provided with the distribution. | |
15 | * | |
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | |
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
26 | * POSSIBILITY OF SUCH DAMAGE. | |
27 | * | |
28 | */ | |
29 | ||
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 | 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 | 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 | 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 | 44 | uint16_t white = remaining_movetime(gameinfo, gamestate, WHITE); |
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 | 47 | "White time: %4" PRIu16 ":%02" PRIu16, |
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 | 50 | "Black time: %4" PRIu16 ":%02" PRIu16, |
51 | black / 60, black % 60); | |
52 | ||
53 | if (white == 0) { | |
54 | move(inputy, 0); | |
55 | printw("Time is over - Black wins!"); | |
56 | clrtobot(); | |
57 | refresh(); | |
58 | return 1; | |
59 | } | |
60 | if (black == 0) { | |
61 | move(inputy, 0); | |
62 | printw("Time is over - White wins!"); | |
63 | clrtobot(); | |
64 | refresh(); | |
65 | return 1; | |
66 | } | |
30
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
67 | } |
33 | 68 | |
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 | 84 | _Bool boardblack = (y&1)==(x&1); |
85 | attrset((col==WHITE ? A_BOLD : A_DIM)| | |
86 | COLOR_PAIR(col == WHITE ? | |
87 | (boardblack ? COL_WB : COL_WW) : | |
88 | (boardblack ? COL_BB : COL_BW) | |
89 | ) | |
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) { |
49
02c509a44e98
logging string representation of moves in short algebraic notation
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
126 | addstr(logelem->move.string); |
27
efeb98bc69c9
moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
127 | if (!logelem->next) { |
49
02c509a44e98
logging string representation of moves in short algebraic notation
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
128 | if (gamestate->stalemate) { |
27
efeb98bc69c9
moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
129 | addstr(" stalemate"); |
efeb98bc69c9
moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
130 | } |
efeb98bc69c9
moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents:
26
diff
changeset
|
131 | } |
25 | 132 | addch(' '); |
18
6008840b859e
fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
133 | |
6008840b859e
fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
134 | logelem = logelem->next; |
6008840b859e
fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
135 | } |
6008840b859e
fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
136 | } |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
137 | } |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
138 | |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
139 | static void eval_move_failed_msg(int code) { |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
140 | switch (code) { |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
141 | case AMBIGUOUS_MOVE: |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
142 | printw("Ambiguous move - please specify the piece to move."); |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
143 | break; |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
144 | case INVALID_POSITION: |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
145 | printw("No piece can be moved this way."); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
146 | break; |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
147 | case NEED_PROMOTION: |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
148 | 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
|
149 | break; |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
150 | case KING_IN_CHECK: |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
151 | printw("Your king is in check!"); |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
152 | break; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
153 | case PIECE_PINNED: |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
154 | printw("This piece is pinned!"); |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
155 | break; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
156 | case INVALID_MOVE_SYNTAX: |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
157 | printw("Can't interpret move - please use algebraic notation."); |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
158 | break; |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
159 | case RULES_VIOLATED: |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
160 | printw("Move does not comply chess rules."); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
161 | break; |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
162 | case KING_MOVES_INTO_CHECK: |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
163 | printw("Can't move the king into a check position."); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
164 | break; |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
165 | default: |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
46
diff
changeset
|
166 | printw("Unknown move parser error."); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
167 | } |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
168 | } |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
169 | |
37
bcf624518909
minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
170 | #define MOVESTR_BUFLEN 8 |
30
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
171 | static int domove_singlemachine(GameState *gamestate, GameInfo *gameinfo) { |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
172 | |
37
bcf624518909
minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
173 | |
32
8a0b85303ee8
improved async input + improved build system + added time values to move struct
Mike Becker <universe@uap-core.de>
parents:
31
diff
changeset
|
174 | size_t bufpos = 0; |
37
bcf624518909
minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
175 | char movestr[MOVESTR_BUFLEN]; |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
176 | |
33 | 177 | flushinp(); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
178 | while (1) { |
33 | 179 | if (timecontrol(gamestate, gameinfo)) { |
180 | return 1; | |
181 | } | |
182 | ||
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
183 | move(inputy, 0); |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
184 | printw( |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
185 | "Use chess notation to enter your move.\n" |
44 | 186 | "Or type 'resign' to resign or 'remis' to end with remis.\n\n" |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
187 | "Type your move: "); |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
188 | clrtoeol(); |
30
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
189 | |
37
bcf624518909
minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
190 | if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) { |
44 | 191 | if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) { |
192 | printw("%s resigned!", | |
30
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
193 | gamestate->mycolor==WHITE?"White":"Black"); |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
194 | clrtoeol(); |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
195 | refresh(); |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
196 | return 1; |
37
bcf624518909
minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
197 | } 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
|
198 | printw("Game ends remis."); |
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; |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
202 | } else { |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
203 | Move move; |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
204 | 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
|
205 | switch (eval_result) { |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
206 | case VALID_MOVE_SYNTAX: |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
207 | eval_result = validate_move(gamestate, &move); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
208 | if (eval_result == VALID_MOVE_SEMANTICS) { |
30
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
209 | apply_move(gamestate, &move); |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
210 | if (gamestate->checkmate) { |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
211 | printw("Checkmate!"); |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
212 | clrtoeol(); |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
213 | return 1; |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
214 | } else if (gamestate->stalemate) { |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
215 | printw("Stalemate!"); |
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 { |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
219 | return 0; |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
220 | } |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
221 | } else { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
222 | eval_move_failed_msg(eval_result); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
223 | } |
30
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
224 | break; |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
225 | default: |
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
226 | eval_move_failed_msg(eval_result); |
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 | clrtoeol(); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
229 | } |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
230 | } |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
231 | } |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
232 | } |
8
52d742aee695
implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
233 | |
33 | 234 | 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
|
235 | |
33 | 236 | size_t bufpos = 0; |
37
bcf624518909
minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
237 | char movestr[MOVESTR_BUFLEN]; |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
238 | _Bool remisrejected = FALSE; |
11
08d7a6e3ec31
added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
239 | uint8_t code; |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
240 | |
33 | 241 | flushinp(); |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
242 | while (1) { |
33 | 243 | if (timecontrol(gamestate, gameinfo)) { |
244 | net_send_code(opponent, NETCODE_TIMEOVER); | |
245 | return 1; | |
246 | } | |
247 | ||
18
6008840b859e
fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
248 | move(inputy, 0); |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
249 | if (remisrejected) { |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
250 | printw( |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
251 | "Use chess notation to enter your move.\n" |
44 | 252 | "Remis offer rejected - type 'resign' to resign. \n\n" |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
253 | "Type your move: "); |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
254 | } else { |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
255 | printw( |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
256 | "Use chess notation to enter your move.\n" |
44 | 257 | "Or type 'resign' to resign or 'remis' to offer remis.\n\n" |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
258 | "Type your move: "); |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
259 | } |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
260 | clrtoeol(); |
33 | 261 | |
37
bcf624518909
minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
262 | if (asyncgetnstr(movestr, &bufpos, MOVESTR_BUFLEN)) { |
44 | 263 | if (strncmp(movestr, "resign", MOVESTR_BUFLEN) == 0) { |
264 | printw("You resigned!"); | |
33 | 265 | clrtoeol(); |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
266 | refresh(); |
44 | 267 | net_send_code(opponent, NETCODE_RESIGN); |
33 | 268 | return 1; |
37
bcf624518909
minor improvements by using macros
Mike Becker <universe@uap-core.de>
parents:
35
diff
changeset
|
269 | } else if (strncmp(movestr, "remis", MOVESTR_BUFLEN) == 0) { |
33 | 270 | if (!remisrejected) { |
271 | net_send_code(opponent, NETCODE_REMIS); | |
272 | printw("Remis offer sent - waiting for acceptance..."); | |
8
52d742aee695
implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
273 | refresh(); |
46
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
274 | code = net_recieve_code(opponent); |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
275 | if (code == NETCODE_ACCEPT) { |
33 | 276 | printw("\rRemis accepted!"); |
18
6008840b859e
fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
277 | clrtoeol(); |
33 | 278 | refresh(); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
279 | return 1; |
46
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
280 | } else if (code == NETCODE_CONNLOST) { |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
281 | printw("\rYour opponent left the game."); |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
282 | clrtoeol(); |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
283 | refresh(); |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
284 | 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
|
285 | } else { |
33 | 286 | remisrejected = TRUE; |
11
08d7a6e3ec31
added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
287 | } |
18
6008840b859e
fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
288 | } |
33 | 289 | } else { |
290 | Move move; | |
291 | int eval_result = eval_move(gamestate, movestr, &move); | |
292 | switch (eval_result) { | |
293 | case VALID_MOVE_SYNTAX: | |
49
02c509a44e98
logging string representation of moves in short algebraic notation
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
294 | net_send_data(opponent, NETCODE_MOVE, &move, |
02c509a44e98
logging string representation of moves in short algebraic notation
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
295 | sizeof(Move)-8); |
33 | 296 | code = net_recieve_code(opponent); |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
297 | move.check = code == NETCODE_CHECK || |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
298 | code == NETCODE_CHECKMATE; |
33 | 299 | gamestate->checkmate = code == NETCODE_CHECKMATE; |
300 | gamestate->stalemate = code == NETCODE_STALEMATE; | |
301 | if (code == NETCODE_DECLINE) { | |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
302 | uint32_t reason; |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
303 | net_recieve_data(opponent, &reason, sizeof(uint32_t)); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
304 | reason = ntohl(reason); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
305 | eval_move_failed_msg(reason); |
42
21cb830efe91
fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
306 | } else if (code == NETCODE_ACCEPT |
21cb830efe91
fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
307 | || code == NETCODE_CHECK |
21cb830efe91
fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
308 | || code == NETCODE_CHECKMATE |
21cb830efe91
fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
309 | || code == NETCODE_STALEMATE) { |
33 | 310 | apply_move(gamestate, &move); |
311 | if (gamestate->checkmate) { | |
312 | printw("Checkmate!"); | |
313 | clrtoeol(); | |
314 | return 1; | |
315 | } else if (gamestate->stalemate) { | |
316 | printw("Stalemate!"); | |
317 | clrtoeol(); | |
318 | return 1; | |
319 | } else { | |
320 | return 0; | |
321 | } | |
46
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
322 | } else if (code == NETCODE_CONNLOST) { |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
323 | printw("Your opponent left the game."); |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
324 | return 1; |
42
21cb830efe91
fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
325 | } else { |
21cb830efe91
fixed bug where an invalid network response lead to an accepted move
Mike Becker <universe@uap-core.de>
parents:
37
diff
changeset
|
326 | printw("Invalid network response."); |
33 | 327 | } |
328 | break; | |
329 | default: | |
330 | eval_move_failed_msg(eval_result); | |
331 | } | |
332 | clrtoeol(); | |
8
52d742aee695
implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
333 | } |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
334 | } |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
335 | } |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
336 | } |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
337 | |
33 | 338 | 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
|
339 | |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
340 | struct timeval timeout; |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
341 | while (1) { |
33 | 342 | timecontrol(gamestate, gameinfo); |
343 | ||
18
6008840b859e
fixed bishop + added pawn promotion + added move log
Mike Becker <universe@uap-core.de>
parents:
17
diff
changeset
|
344 | move(inputy, 0); |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
345 | printw("Awaiting opponent move..."); |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
346 | clrtoeol(); |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
347 | refresh(); |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
348 | |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
349 | fd_set readfds; |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
350 | |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
351 | FD_ZERO(&readfds); |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
352 | 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
|
353 | 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
|
354 | timeout.tv_usec = 1e5; |
8
52d742aee695
implemented simple pawn movement
Mike Becker <universe@uap-core.de>
parents:
7
diff
changeset
|
355 | |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
356 | 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
|
357 | if (result == -1) { |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
358 | 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
|
359 | cbreak(); getch(); |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
360 | exit(EXIT_FAILURE); |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
361 | } |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
362 | if (result > 0) { |
43 | 363 | uint8_t code = net_recieve_code(opponent); |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
364 | |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
365 | Move move; |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
366 | switch (code) { |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
367 | case NETCODE_TIMEOVER: |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
368 | 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
|
369 | clrtoeol(); |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
370 | return 1; |
44 | 371 | case NETCODE_RESIGN: |
372 | printw("\rYour opponent resigned!"); | |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
373 | clrtoeol(); |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
374 | return 1; |
46
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
375 | case NETCODE_CONNLOST: |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
376 | printw("\rYour opponent has left the game."); |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
377 | clrtoeol(); |
4dcfb4c58b6d
netcode is now aware of connection losses
Mike Becker <universe@uap-core.de>
parents:
45
diff
changeset
|
378 | return 1; |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
379 | case NETCODE_REMIS: |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
380 | if (prompt_yesno( |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
381 | "\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
|
382 | printw("\rRemis accepted!"); |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
383 | clrtoeol(); |
34
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_ACCEPT); |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
385 | return 1; |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
386 | } else { |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
387 | 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
|
388 | } |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
389 | break; |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
390 | case NETCODE_MOVE: |
49
02c509a44e98
logging string representation of moves in short algebraic notation
Mike Becker <universe@uap-core.de>
parents:
48
diff
changeset
|
391 | net_recieve_data(opponent, &move, sizeof(Move)-8); |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
392 | code = validate_move(gamestate, &move); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
393 | if (code == VALID_MOVE_SEMANTICS) { |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
394 | 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
|
395 | if (move.check) { |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
396 | 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
|
397 | } 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
|
398 | 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
|
399 | printw("\rCheckmate!"); |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
400 | clrtoeol(); |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
401 | return 1; |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
402 | } 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
|
403 | 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
|
404 | printw("\rStalemate!"); |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
405 | clrtoeol(); |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
406 | return 1; |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
407 | } else { |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
408 | 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
|
409 | } |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
410 | return 0; |
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
411 | } else { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
412 | uint32_t reason = htonl(code); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
413 | net_send_data(opponent, NETCODE_DECLINE, |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
414 | &reason, sizeof(uint32_t)); |
34
c4d4b8a8f902
added nonblocking read for network games + minor build system fixes
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
415 | } |
45
e14a1d9aa91d
removed nonblocking sockets for causing problems and not being necessary
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
416 | break; |
e14a1d9aa91d
removed nonblocking sockets for causing problems and not being necessary
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
417 | default: |
e14a1d9aa91d
removed nonblocking sockets for causing problems and not being necessary
Mike Becker <universe@uap-core.de>
parents:
44
diff
changeset
|
418 | printw("\nInvalid network request."); |
33 | 419 | } |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
420 | } |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
421 | } |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
422 | } |
6 | 423 | |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
424 | static void init_board(GameState *gamestate) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
425 | Board initboard = { |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
426 | {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
|
427 | {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
|
428 | {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
|
429 | {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
|
430 | {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
|
431 | {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
|
432 | {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
|
433 | {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
|
434 | }; |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
435 | memcpy(gamestate->board, initboard, sizeof(Board)); |
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 | |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
438 | void game_start_singlemachine(Settings *settings) { |
33 | 439 | inputy = getmaxy(stdscr) - 6; |
440 | ||
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
441 | 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
|
442 | memset(&gamestate, 0, sizeof(GameState)); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
443 | init_board(&gamestate); |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
444 | gamestate.mycolor = WHITE; |
30
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
445 | |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
446 | _Bool running; |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
447 | do { |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
448 | clear(); |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
449 | draw_board(&gamestate); |
30
a285ee393860
experimental async input for single machine mode
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
450 | running = !domove_singlemachine(&gamestate, &(settings->gameinfo)); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
451 | gamestate.mycolor = opponent_color(gamestate.mycolor); |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
452 | } 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
|
453 | 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
|
454 | draw_board(&gamestate); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
455 | |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
456 | gamestate_cleanup(&gamestate); |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
457 | } |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
458 | |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
459 | void game_start(Settings *settings, int opponent) { |
33 | 460 | inputy = getmaxy(stdscr) - 6; |
461 | ||
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
462 | _Bool myturn = is_server(settings) == |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
463 | (settings->gameinfo.servercolor == WHITE); |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
464 | |
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
465 | 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
|
466 | memset(&gamestate, 0, sizeof(GameState)); |
26
e0a76ee1bb2b
introduced single machine mode
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
467 | init_board(&gamestate); |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
468 | gamestate.mycolor = myturn ? WHITE:BLACK; |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
469 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
470 | _Bool running; |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
471 | do { |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
472 | clear(); |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
473 | draw_board(&gamestate); |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
474 | if (myturn) { |
33 | 475 | 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
|
476 | } else { |
33 | 477 | 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
|
478 | } |
11
08d7a6e3ec31
added network messages for check and checkmate
Mike Becker <universe@uap-core.de>
parents:
10
diff
changeset
|
479 | myturn ^= TRUE; |
7
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
480 | } while (running); |
41468077b5bb
implemented chess board and remis/surrender messages
Mike Becker <universe@uap-core.de>
parents:
6
diff
changeset
|
481 | |
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
|
482 | 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
|
483 | 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
|
484 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
22
diff
changeset
|
485 | gamestate_cleanup(&gamestate); |
6 | 486 | } |