Wed, 09 Apr 2014 18:11:51 +0200
tried to improve colors
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
1 | /* |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
3 | * |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
4 | * Copyright 2014 Mike Becker. All rights reserved. |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
5 | * |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
8 | * |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
11 | * |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
15 | * |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
6a26114297a1
moved chess rules to separate lib
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 |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
27 | * |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
28 | */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
29 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
30 | #include "rules.h" |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
31 | #include "chess.h" |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
32 | #include <string.h> |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
33 | #include <stdlib.h> |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
34 | |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
35 | void gamestate_cleanup(GameState *gamestate) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
36 | MoveList *elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
37 | elem = gamestate->movelist; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
38 | while (elem) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
39 | MoveList *cur = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
40 | elem = elem->next; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
41 | free(cur); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
42 | }; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
43 | } |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
44 | |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
45 | static void addmove(GameState* gamestate, Move *move) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
46 | MoveList *elem = malloc(sizeof(MoveList)); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
47 | elem->next = NULL; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
48 | elem->move = *move; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
49 | |
33 | 50 | clock_gettime(CLOCK_REALTIME, &(elem->move.timestamp)); |
51 | ||
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
52 | if (gamestate->lastmove) { |
33 | 53 | struct timespec *lasttstamp = &(gamestate->lastmove->move.timestamp); |
54 | time_t sec = elem->move.timestamp.tv_sec - lasttstamp->tv_sec; | |
55 | long int nanos; | |
56 | if (elem->move.timestamp.tv_nsec < lasttstamp->tv_nsec) { | |
57 | nanos = 1e9L-(lasttstamp->tv_nsec - elem->move.timestamp.tv_nsec); | |
58 | sec--; | |
59 | } else { | |
60 | nanos = elem->move.timestamp.tv_nsec - lasttstamp->tv_nsec; | |
61 | } | |
62 | ||
63 | elem->move.movetime.tv_sec = sec; | |
64 | elem->move.movetime.tv_nsec = nanos; | |
65 | ||
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
66 | gamestate->lastmove->next = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
67 | gamestate->lastmove = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
68 | } else { |
33 | 69 | elem->move.movetime.tv_nsec = 0; |
70 | elem->move.movetime.tv_sec = 0; | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
71 | gamestate->movelist = gamestate->lastmove = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
72 | } |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
73 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
74 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | char getpiecechr(uint8_t piece) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | switch (piece & PIECE_MASK) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | case ROOK: return 'R'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | case KNIGHT: return 'N'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | case BISHOP: return 'B'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | case QUEEN: return 'Q'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | case KING: return 'K'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | default: return '\0'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
84 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
85 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
86 | uint8_t getpiece(char c) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | switch (c) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | case 'R': return ROOK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | case 'N': return KNIGHT; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | case 'B': return BISHOP; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | case 'Q': return QUEEN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | case 'K': return KING; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | default: return 0; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
97 | static int getlocation(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | uint8_t piece = move->piece & PIECE_MASK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | switch (piece) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
100 | case PAWN: return pawn_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
101 | case ROOK: return rook_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
102 | case KNIGHT: return knight_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
103 | case BISHOP: return bishop_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
104 | case QUEEN: return queen_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
105 | case KING: return king_getlocation(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | default: return INVALID_MOVE_SYNTAX; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
109 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
110 | void apply_move(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | uint8_t piece = move->piece & PIECE_MASK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | uint8_t color = move->piece & COLOR_MASK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | /* en passant capture */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | if (move->capture && piece == PAWN && |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
116 | mdst(gamestate->board, move) == 0) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
117 | gamestate->board[move->fromrow][move->tofile] = 0; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
118 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
119 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
120 | /* remove old en passant threats */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | for (uint8_t file = 0 ; file < 8 ; file++) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
122 | gamestate->board[3][file] &= ~ENPASSANT_THREAT; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
123 | gamestate->board[4][file] &= ~ENPASSANT_THREAT; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
124 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
125 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
126 | /* add new en passant threat */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
127 | if (piece == PAWN && ( |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | (move->fromrow == 1 && move->torow == 3) || |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | (move->fromrow == 6 && move->torow == 4))) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
130 | move->piece |= ENPASSANT_THREAT; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
132 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
133 | /* move (and maybe capture or promote) */ |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
134 | msrc(gamestate->board, move) = 0; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
135 | if (move->promotion) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
136 | mdst(gamestate->board, move) = move->promotion; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
137 | } else { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
138 | mdst(gamestate->board, move) = move->piece; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
139 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
140 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
141 | /* castling */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
142 | if (piece == KING && |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
143 | move->fromfile == fileidx('e')) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
144 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
145 | if (move->tofile == fileidx('g')) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
146 | gamestate->board[move->torow][fileidx('h')] = 0; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
147 | gamestate->board[move->torow][fileidx('f')] = color|ROOK; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
148 | } else if (move->tofile == fileidx('c')) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
149 | gamestate->board[move->torow][fileidx('a')] = 0; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
150 | gamestate->board[move->torow][fileidx('d')] = color|ROOK; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
151 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
152 | } |
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
|
153 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
154 | addmove(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
155 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
156 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
157 | static _Bool validate_move_rules(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
158 | /* validate indices (don't trust opponent) */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
159 | if (!chkidx(move)) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
160 | return 0; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
161 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
162 | |
21
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
163 | /* must move */ |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
164 | if (move->fromfile == move->tofile && move->fromrow == move->torow) { |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
165 | return 0; |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
166 | } |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
167 | |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
168 | /* does piece exist */ |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
169 | if (msrc(gamestate->board, move) != move->piece) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
170 | return 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
171 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
172 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
173 | /* can't capture own pieces */ |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
174 | if ((mdst(gamestate->board, move) & COLOR_MASK) |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
175 | == (move->piece & COLOR_MASK)) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
176 | return 0; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
177 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
178 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
179 | /* validate individual rules */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
180 | switch (move->piece & PIECE_MASK) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
181 | case PAWN: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
182 | return pawn_chkrules(gamestate, move) && |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
183 | !pawn_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
184 | case ROOK: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
185 | return rook_chkrules(move) && |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
186 | !rook_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | case KNIGHT: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
188 | return knight_chkrules(move); /* knight is never blocked */ |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
189 | case BISHOP: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
190 | return bishop_chkrules(move) && |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
191 | !bishop_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
192 | case QUEEN: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
193 | return queen_chkrules(move) && |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
194 | !queen_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
195 | case KING: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
196 | return king_chkrules(gamestate, move) && |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
197 | !king_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
198 | default: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
199 | return 0; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
200 | } |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
201 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
202 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
203 | _Bool validate_move(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
204 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
205 | _Bool result = validate_move_rules(gamestate, move); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
206 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
207 | /* cancel processing to save resources */ |
25 | 208 | if (!result) { |
209 | return 0; | |
210 | } | |
211 | ||
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
|
212 | /* find kings for check validation */ |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
213 | uint8_t piececolor = (move->piece & COLOR_MASK); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
214 | |
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
|
215 | uint8_t mykingfile = 0, mykingrow = 0, opkingfile = 0, opkingrow = 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
|
216 | for (uint8_t row = 0 ; row < 8 ; row++) { |
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
|
217 | for (uint8_t file = 0 ; file < 8 ; file++) { |
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
|
218 | if (gamestate->board[row][file] == |
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
|
219 | (piececolor == WHITE?WKING:BKING)) { |
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
|
220 | mykingfile = file; |
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
|
221 | mykingrow = row; |
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
|
222 | } else if (gamestate->board[row][file] == |
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
|
223 | (piececolor == WHITE?BKING:WKING)) { |
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
|
224 | opkingfile = file; |
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
|
225 | opkingrow = row; |
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
|
226 | } |
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
|
227 | } |
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
|
228 | } |
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
|
229 | |
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
|
230 | /* simulation move for check validation */ |
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
|
231 | GameState simulation; |
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
|
232 | memcpy(&simulation, gamestate, sizeof(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
|
233 | apply_move(&simulation, move); |
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
|
234 | |
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
|
235 | /* don't move into or stay in check position */ |
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
|
236 | if (is_covered(&simulation, mykingrow, mykingfile, |
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
|
237 | opponent_color(piececolor))) { |
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
|
238 | return 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
|
239 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
240 | |
25 | 241 | /* correct check and checkmate flags (move is still valid) */ |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
242 | Move threats[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
243 | uint8_t threatcount; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
244 | move->check = get_threats(&simulation, opkingrow, opkingfile, |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
245 | piececolor, threats, &threatcount); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
246 | |
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
|
247 | if (move->check) { |
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
|
248 | /* determine possible escape fields */ |
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
|
249 | _Bool canescape = 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
|
250 | for (int dr = -1 ; dr <= 1 && !canescape ; dr++) { |
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
|
251 | for (int df = -1 ; df <= 1 && !canescape ; df++) { |
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
|
252 | if (!(dr == 0 && df == 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
|
253 | isidx(opkingrow + dr) && isidx(opkingfile + df)) { |
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
|
254 | |
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
|
255 | /* escape field neither blocked nor covered */ |
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
|
256 | if ((simulation.board[opkingrow + dr][opkingfile + df] |
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
|
257 | & COLOR_MASK) != opponent_color(piececolor)) { |
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
|
258 | canescape |= !is_covered(&simulation, |
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
|
259 | opkingrow + dr, opkingfile + df, piececolor); |
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
|
260 | } |
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
|
261 | } |
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
|
262 | } |
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
|
263 | } |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
264 | /* can't escape, can he capture? */ |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
265 | if (!canescape && threatcount == 1) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
266 | canescape = is_attacked(&simulation, threats[0].fromrow, |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
267 | threats[0].fromfile, opponent_color(piececolor)); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
268 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
269 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
270 | /* can't capture, can he block? */ |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
271 | if (!canescape && threatcount == 1) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
272 | Move *threat = &(threats[0]); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
273 | uint8_t threatpiece = threat->piece & PIECE_MASK; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
274 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
275 | /* knight, pawns and the king cannot be blocked */ |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
276 | if (threatpiece == BISHOP || threatpiece == ROOK |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
277 | || threatpiece == QUEEN) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
278 | if (threat->fromrow == threat->torow) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
279 | /* rook aspect (on row) */ |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
280 | int d = threat->tofile > threat->fromfile ? 1 : -1; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
281 | uint8_t file = threat->fromfile; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
282 | while (!canescape && file != threat->tofile - d) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
283 | file += d; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
284 | canescape |= is_protected(&simulation, |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
285 | threat->torow, file, opponent_color(piececolor)); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
286 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
287 | } else if (threat->fromfile == threat->tofile) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
288 | /* rook aspect (on file) */ |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
289 | int d = threat->torow > threat->fromrow ? 1 : -1; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
290 | uint8_t row = threat->fromrow; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
291 | while (!canescape && row != threat->torow - d) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
292 | row += d; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
293 | canescape |= is_protected(&simulation, |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
294 | row, threat->tofile, opponent_color(piececolor)); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
295 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
296 | } else { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
297 | /* bishop aspect */ |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
298 | int dr = move->torow > move->fromrow ? 1 : -1; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
299 | int df = move->tofile > move->fromfile ? 1 : -1; |
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
|
300 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
301 | uint8_t row = move->fromrow; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
302 | uint8_t file = move->fromfile; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
303 | while (!canescape && file != move->tofile - df |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
304 | && row != move->torow - dr) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
305 | row += dr; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
306 | file += df; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
307 | canescape |= is_protected(&simulation, row, file, |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
308 | opponent_color(piececolor)); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
309 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
310 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
311 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
312 | } |
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
|
313 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
314 | if (!canescape) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
315 | gamestate->checkmate = 1; |
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
|
316 | } |
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
|
317 | } |
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
|
318 | |
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
|
319 | return 1; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
320 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
321 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
322 | int eval_move(GameState *gamestate, char *mstr, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
323 | memset(move, 0, sizeof(Move)); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
324 | move->fromfile = POS_UNSPECIFIED; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
325 | move->fromrow = POS_UNSPECIFIED; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
326 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
327 | size_t len = strlen(mstr); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
328 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
329 | /* evaluate check/checkmate flags */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
330 | if (mstr[len-1] == '+') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
331 | len--; mstr[len] = '\0'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
332 | move->check = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
333 | } else if (mstr[len-1] == '#') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
334 | len--; mstr[len] = '\0'; |
27
efeb98bc69c9
moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
335 | /* ignore - validation should set game state */ |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
336 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
337 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
338 | /* evaluate promotion */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
339 | if (len > 3 && mstr[len-2] == '=') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
340 | move->promotion = getpiece(mstr[len-1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
341 | if (!move->promotion) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
342 | return INVALID_MOVE_SYNTAX; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
343 | } else { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
344 | move->promotion |= gamestate->mycolor; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
345 | len -= 2; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
346 | mstr[len] = 0; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
347 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
348 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
349 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
350 | if (len == 2) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
351 | /* pawn move (e.g. "e4") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
352 | move->piece = PAWN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
353 | move->tofile = fileidx(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
354 | move->torow = rowidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
355 | } else if (len == 3) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
356 | if (strcmp(mstr, "O-O") == 0) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
357 | /* king side castling */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
358 | move->piece = KING; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
359 | move->fromfile = fileidx('e'); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
360 | move->tofile = fileidx('g'); |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
361 | move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
362 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
363 | /* move (e.g. "Nf3") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
364 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
365 | move->tofile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
366 | move->torow = rowidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
367 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
368 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
369 | } else if (len == 4) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
370 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
371 | if (!move->piece) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
372 | move->piece = PAWN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
373 | move->fromfile = fileidx(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
374 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
375 | if (mstr[1] == 'x') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
376 | /* capture (e.g. "Nxf3", "dxe5") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
377 | move->capture = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
378 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
379 | /* move (e.g. "Ndf3", "N2c3", "e2e4") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
380 | if (isfile(mstr[1])) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
381 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
382 | if (move->piece == PAWN) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
383 | move->piece = 0; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
384 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
385 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
386 | move->fromrow = rowidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
387 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
388 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
389 | move->tofile = fileidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
390 | move->torow = rowidx(mstr[3]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
391 | } else if (len == 5) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
392 | if (strcmp(mstr, "O-O-O") == 0) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
393 | /* queen side castling "O-O-O" */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
394 | move->piece = KING; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
395 | move->fromfile = fileidx('e'); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
396 | move->tofile = fileidx('c'); |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
397 | move->fromrow = move->torow = gamestate->mycolor == WHITE ? 0 : 7; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
398 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
399 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
400 | if (mstr[2] == 'x') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
401 | move->capture = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
402 | if (move->piece) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
403 | /* capture (e.g. "Ndxf3") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
404 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
405 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
406 | /* long notation capture (e.g. "e5xf6") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
407 | move->piece = PAWN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
408 | move->fromfile = fileidx(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
409 | move->fromrow = rowidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
410 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
411 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
412 | /* long notation move (e.g. "Nc5a4") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
413 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
414 | move->fromrow = rowidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
415 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
416 | move->tofile = fileidx(mstr[3]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
417 | move->torow = rowidx(mstr[4]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
418 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
419 | } else if (len == 6) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
420 | /* long notation capture (e.g. "Nc5xf3") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
421 | if (mstr[3] == 'x') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
422 | move->capture = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
423 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
424 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
425 | move->fromrow = rowidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
426 | move->tofile = fileidx(mstr[4]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
427 | move->torow = rowidx(mstr[5]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
428 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
429 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
430 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
431 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
432 | if (move->piece) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
433 | if (move->piece == PAWN |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
434 | && move->torow == (gamestate->mycolor==WHITE?7:0) |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
435 | && !move->promotion) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
436 | return NEED_PROMOTION; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
437 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
438 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
439 | move->piece |= gamestate->mycolor; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
440 | if (move->fromfile == POS_UNSPECIFIED |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
441 | || move->fromrow == POS_UNSPECIFIED) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
442 | return getlocation(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
443 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
444 | return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
445 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
446 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
447 | return INVALID_MOVE_SYNTAX; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
448 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
449 | } |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
450 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
451 | _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file, |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
452 | uint8_t color, Move *threats, uint8_t *threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
453 | Move candidates[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
454 | int candidatecount = 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
455 | for (uint8_t r = 0 ; r < 8 ; r++) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
456 | for (uint8_t f = 0 ; f < 8 ; f++) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
457 | if ((gamestate->board[r][f] & COLOR_MASK) == color) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
458 | memset(&(candidates[candidatecount]), 0, sizeof(Move)); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
459 | candidates[candidatecount].piece = gamestate->board[r][f]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
460 | candidates[candidatecount].fromrow = r; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
461 | candidates[candidatecount].fromfile = f; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
462 | candidates[candidatecount].torow = row; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
463 | candidates[candidatecount].tofile = file; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
464 | candidatecount++; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
465 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
466 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
467 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
468 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
469 | if (threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
470 | *threatcount = 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
471 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
472 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
473 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
474 | _Bool result = 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
475 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
476 | for (int i = 0 ; i < candidatecount ; i++) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
477 | if (validate_move_rules(gamestate, &(candidates[i]))) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
478 | result = 1; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
479 | if (threats && threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
480 | threats[(*threatcount)++] = candidates[i]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
481 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
482 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
483 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
484 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
485 | return result; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
486 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
487 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
488 | _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file, |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
489 | uint8_t color, Move *threats, uint8_t *threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
490 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
491 | Move candidates[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
492 | uint8_t candidatecount; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
493 | if (get_threats(gamestate, row, file, color, candidates, &candidatecount)) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
494 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
495 | if (threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
496 | *threatcount = 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
497 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
498 | _Bool result = 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
499 | uint8_t kingfile = 0, kingrow = 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
500 | for (uint8_t row = 0 ; row < 8 ; row++) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
501 | for (uint8_t file = 0 ; file < 8 ; file++) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
502 | if ((gamestate->board[row][file] & COLOR_MASK) == color) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
503 | kingfile = file; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
504 | kingrow = row; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
505 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
506 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
507 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
508 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
509 | for (uint8_t i = 0 ; i < candidatecount ; i++) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
510 | GameState simulation; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
511 | memcpy(&simulation, gamestate, sizeof(GameState)); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
512 | apply_move(&simulation, &(candidates[i])); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
513 | if (!is_covered(&simulation, kingrow, kingfile, |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
514 | opponent_color(color))) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
515 | result = 1; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
516 | if (threats && threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
517 | threats[(*threatcount)++] = candidates[i]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
518 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
519 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
520 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
521 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
522 | return result; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
523 | } else { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
524 | return 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
525 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
526 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
527 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
528 | _Bool is_protected(GameState *gamestate, uint8_t row, uint8_t file, |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
529 | uint8_t color) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
530 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
531 | Move threats[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
532 | uint8_t threatcount; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
533 | if (get_real_threats(gamestate, row, file, color, threats, &threatcount)) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
534 | for (int i = 0 ; i < threatcount ; i++) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
535 | if (threats[i].piece != (color|KING)) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
536 | return 1; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
537 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
538 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
539 | return 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
540 | } else { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
541 | return 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
542 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
543 | } |
33 | 544 | |
545 | uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate, | |
546 | uint8_t color) { | |
547 | if (!gameinfo->timecontrol) { | |
548 | return 0; | |
549 | } | |
550 | ||
551 | if (gamestate->movelist) { | |
552 | uint16_t time = gameinfo->time; | |
553 | long int nanos = 0; | |
554 | ||
555 | MoveList *movelist = color == WHITE ? | |
556 | gamestate->movelist : gamestate->movelist->next; | |
557 | ||
558 | while (movelist) { | |
559 | time += gameinfo->addtime; | |
560 | ||
561 | struct timespec *movetime = &(movelist->move.movetime); | |
562 | if (movetime->tv_sec >= time) { | |
563 | return 0; | |
564 | } | |
565 | ||
566 | time -= movetime->tv_sec; | |
567 | nanos += movetime->tv_nsec; | |
568 | ||
569 | movelist = movelist->next ? movelist->next->next : NULL; | |
570 | } | |
571 | ||
572 | time_t sec; | |
573 | movelist = gamestate->lastmove; | |
574 | if ((movelist->move.piece & COLOR_MASK) != color) { | |
575 | struct timespec *lastmovetstamp = &(movelist->move.timestamp); | |
576 | struct timespec currenttstamp; | |
577 | clock_gettime(CLOCK_REALTIME, ¤ttstamp); | |
578 | nanos += currenttstamp.tv_nsec - lastmovetstamp->tv_nsec; | |
579 | sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; | |
580 | if (sec >= time) { | |
581 | return 0; | |
582 | } | |
583 | ||
584 | time -= sec; | |
585 | } | |
586 | ||
587 | sec = nanos / 1e9L; | |
588 | ||
589 | if (sec >= time) { | |
590 | return 0; | |
591 | } | |
592 | ||
593 | time -= sec; | |
594 | ||
595 | return time; | |
596 | } else { | |
597 | return gameinfo->time; | |
598 | } | |
599 | } |