Sat, 12 Apr 2014 13:42:57 +0200
fixed memory underflow bug in mvwasyncgetnstr
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> |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
34 | #include <sys/time.h> |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
35 | |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
36 | void gamestate_cleanup(GameState *gamestate) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
37 | MoveList *elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
38 | elem = gamestate->movelist; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
39 | while (elem) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
40 | MoveList *cur = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
41 | elem = elem->next; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
42 | free(cur); |
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 | |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
46 | static void addmove(GameState* gamestate, Move *move) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
47 | MoveList *elem = malloc(sizeof(MoveList)); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
48 | elem->next = NULL; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
49 | elem->move = *move; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
50 | |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
51 | gettimeofday(&(elem->move.timestamp), NULL); |
33 | 52 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
53 | if (gamestate->lastmove) { |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
54 | struct timeval *lasttstamp = &(gamestate->lastmove->move.timestamp); |
33 | 55 | time_t sec = elem->move.timestamp.tv_sec - lasttstamp->tv_sec; |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
56 | suseconds_t micros; |
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
57 | if (elem->move.timestamp.tv_usec < lasttstamp->tv_usec) { |
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
58 | micros = 1e6L-(lasttstamp->tv_usec - elem->move.timestamp.tv_usec); |
33 | 59 | sec--; |
60 | } else { | |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
61 | micros = elem->move.timestamp.tv_usec - lasttstamp->tv_usec; |
33 | 62 | } |
63 | ||
64 | elem->move.movetime.tv_sec = sec; | |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
65 | elem->move.movetime.tv_usec = micros; |
33 | 66 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
67 | gamestate->lastmove->next = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
68 | gamestate->lastmove = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
69 | } else { |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
70 | elem->move.movetime.tv_usec = 0; |
33 | 71 | elem->move.movetime.tv_sec = 0; |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
72 | gamestate->movelist = gamestate->lastmove = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
73 | } |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
74 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
75 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
76 | char getpiecechr(uint8_t piece) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
77 | switch (piece & PIECE_MASK) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
78 | case ROOK: return 'R'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
79 | case KNIGHT: return 'N'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
80 | case BISHOP: return 'B'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
81 | case QUEEN: return 'Q'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
82 | case KING: return 'K'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
83 | default: return '\0'; |
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
87 | uint8_t getpiece(char c) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
88 | switch (c) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | case 'R': return ROOK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | case 'N': return KNIGHT; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | case 'B': return BISHOP; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | case 'Q': return QUEEN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | case 'K': return KING; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | default: return 0; |
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 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
98 | static int getlocation(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | uint8_t piece = move->piece & PIECE_MASK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | switch (piece) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
101 | case PAWN: return pawn_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
102 | case ROOK: return rook_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
103 | case KNIGHT: return knight_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
104 | case BISHOP: return bishop_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
105 | case QUEEN: return queen_getlocation(gamestate, move); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
106 | case KING: return king_getlocation(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | default: return INVALID_MOVE_SYNTAX; |
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 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
110 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
111 | void apply_move(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
112 | uint8_t piece = move->piece & PIECE_MASK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | uint8_t color = move->piece & COLOR_MASK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | /* en passant capture */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | if (move->capture && piece == PAWN && |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
117 | mdst(gamestate->board, move) == 0) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
118 | gamestate->board[move->fromrow][move->tofile] = 0; |
19
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
121 | /* remove old en passant threats */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | for (uint8_t file = 0 ; file < 8 ; file++) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
123 | gamestate->board[3][file] &= ~ENPASSANT_THREAT; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
124 | gamestate->board[4][file] &= ~ENPASSANT_THREAT; |
19
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
127 | /* add new en passant threat */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | if (piece == PAWN && ( |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | (move->fromrow == 1 && move->torow == 3) || |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
130 | (move->fromrow == 6 && move->torow == 4))) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | move->piece |= ENPASSANT_THREAT; |
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
134 | /* move (and maybe capture or promote) */ |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
135 | msrc(gamestate->board, move) = 0; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
136 | if (move->promotion) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
137 | mdst(gamestate->board, move) = move->promotion; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
138 | } else { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
139 | mdst(gamestate->board, move) = move->piece; |
19
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
142 | /* castling */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
143 | if (piece == KING && |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
144 | move->fromfile == fileidx('e')) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
145 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
146 | if (move->tofile == fileidx('g')) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
147 | gamestate->board[move->torow][fileidx('h')] = 0; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
148 | 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
|
149 | } else if (move->tofile == fileidx('c')) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
150 | gamestate->board[move->torow][fileidx('a')] = 0; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
151 | 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
|
152 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
153 | } |
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
|
154 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
155 | addmove(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
156 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
157 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
158 | 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
|
159 | /* validate indices (don't trust opponent) */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
160 | if (!chkidx(move)) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
161 | return 0; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
162 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
163 | |
21
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
164 | /* must move */ |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
165 | if (move->fromfile == move->tofile && move->fromrow == move->torow) { |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
166 | return 0; |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
167 | } |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
168 | |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
169 | /* 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
|
170 | 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
|
171 | return 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
172 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
173 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
174 | /* 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
|
175 | 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
|
176 | == (move->piece & COLOR_MASK)) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
177 | return 0; |
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
180 | /* validate individual rules */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
181 | switch (move->piece & PIECE_MASK) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
182 | case PAWN: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
183 | 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
|
184 | !pawn_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
185 | case ROOK: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
186 | 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
|
187 | !rook_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | case KNIGHT: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
189 | 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
|
190 | case BISHOP: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
191 | 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
|
192 | !bishop_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
193 | case QUEEN: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
194 | 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
|
195 | !queen_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
196 | case KING: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
197 | 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
|
198 | !king_isblocked(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
199 | default: |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
200 | return 0; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
201 | } |
29
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 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
204 | _Bool validate_move(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
205 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
206 | _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
|
207 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
208 | /* cancel processing to save resources */ |
25 | 209 | if (!result) { |
210 | return 0; | |
211 | } | |
212 | ||
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
|
213 | /* 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
|
214 | 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
|
215 | |
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
|
216 | 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
|
217 | 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
|
218 | 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
|
219 | 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
|
220 | (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
|
221 | 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
|
222 | 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
|
223 | } 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
|
224 | (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
|
225 | 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
|
226 | 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
|
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 | |
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 | /* 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
|
232 | 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
|
233 | 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
|
234 | 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
|
235 | |
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 | /* 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
|
237 | 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
|
238 | 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
|
239 | 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
|
240 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
241 | |
25 | 242 | /* 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
|
243 | Move threats[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
244 | uint8_t threatcount; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
245 | 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
|
246 | piececolor, threats, &threatcount); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
247 | |
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
|
248 | 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
|
249 | /* 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
|
250 | _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
|
251 | 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
|
252 | 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
|
253 | 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
|
254 | 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
|
255 | |
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 | /* 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
|
257 | 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
|
258 | & 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
|
259 | 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
|
260 | 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
|
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 | } |
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
|
264 | } |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
265 | /* 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
|
266 | 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
|
267 | 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
|
268 | 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
|
269 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
270 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
271 | /* 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
|
272 | 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
|
273 | 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
|
274 | 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
|
275 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
276 | /* 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
|
277 | 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
|
278 | || threatpiece == QUEEN) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
279 | 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
|
280 | /* 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
|
281 | 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
|
282 | 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
|
283 | 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
|
284 | file += d; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
285 | 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
|
286 | 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
|
287 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
288 | } 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
|
289 | /* 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
|
290 | 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
|
291 | 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
|
292 | 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
|
293 | row += d; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
294 | 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
|
295 | 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
|
296 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
297 | } else { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
298 | /* bishop aspect */ |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
299 | 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
|
300 | 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
|
301 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
302 | 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
|
303 | 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
|
304 | 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
|
305 | && 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
|
306 | row += dr; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
307 | file += df; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
308 | 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
|
309 | opponent_color(piececolor)); |
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 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
313 | } |
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
|
314 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
315 | if (!canescape) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
316 | 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
|
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 | |
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
|
320 | return 1; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
321 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
322 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
323 | 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
|
324 | memset(move, 0, sizeof(Move)); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
325 | move->fromfile = POS_UNSPECIFIED; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
326 | move->fromrow = POS_UNSPECIFIED; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
327 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
328 | size_t len = strlen(mstr); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
329 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
330 | /* evaluate check/checkmate flags */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
331 | if (mstr[len-1] == '+') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
332 | len--; mstr[len] = '\0'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
333 | move->check = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
334 | } else if (mstr[len-1] == '#') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
335 | len--; mstr[len] = '\0'; |
27
efeb98bc69c9
moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
336 | /* ignore - validation should set game state */ |
19
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
339 | /* evaluate promotion */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
340 | if (len > 3 && mstr[len-2] == '=') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
341 | move->promotion = getpiece(mstr[len-1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
342 | if (!move->promotion) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
343 | return INVALID_MOVE_SYNTAX; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
344 | } else { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
345 | move->promotion |= gamestate->mycolor; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
346 | len -= 2; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
347 | mstr[len] = 0; |
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
351 | if (len == 2) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
352 | /* pawn move (e.g. "e4") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
353 | move->piece = PAWN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
354 | move->tofile = fileidx(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
355 | move->torow = rowidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
356 | } else if (len == 3) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
357 | if (strcmp(mstr, "O-O") == 0) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
358 | /* king side castling */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
359 | move->piece = KING; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
360 | move->fromfile = fileidx('e'); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
361 | move->tofile = fileidx('g'); |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
362 | 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
|
363 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
364 | /* move (e.g. "Nf3") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
365 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
366 | move->tofile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
367 | move->torow = rowidx(mstr[2]); |
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
370 | } else if (len == 4) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
371 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
372 | if (!move->piece) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
373 | move->piece = PAWN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
374 | move->fromfile = fileidx(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
375 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
376 | if (mstr[1] == 'x') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
377 | /* capture (e.g. "Nxf3", "dxe5") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
378 | move->capture = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
379 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
380 | /* move (e.g. "Ndf3", "N2c3", "e2e4") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
381 | if (isfile(mstr[1])) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
382 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
383 | if (move->piece == PAWN) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
384 | move->piece = 0; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
385 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
386 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
387 | move->fromrow = rowidx(mstr[1]); |
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 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
390 | move->tofile = fileidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
391 | move->torow = rowidx(mstr[3]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
392 | } else if (len == 5) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
393 | if (strcmp(mstr, "O-O-O") == 0) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
394 | /* queen side castling "O-O-O" */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
395 | move->piece = KING; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
396 | move->fromfile = fileidx('e'); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
397 | move->tofile = fileidx('c'); |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
398 | 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
|
399 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
400 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
401 | if (mstr[2] == 'x') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
402 | move->capture = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
403 | if (move->piece) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
404 | /* capture (e.g. "Ndxf3") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
405 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
406 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
407 | /* long notation capture (e.g. "e5xf6") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
408 | move->piece = PAWN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
409 | move->fromfile = fileidx(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
410 | move->fromrow = rowidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
411 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
412 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
413 | /* long notation move (e.g. "Nc5a4") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
414 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
415 | move->fromrow = rowidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
416 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
417 | move->tofile = fileidx(mstr[3]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
418 | move->torow = rowidx(mstr[4]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
419 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
420 | } else if (len == 6) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
421 | /* long notation capture (e.g. "Nc5xf3") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
422 | if (mstr[3] == 'x') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
423 | move->capture = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
424 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
425 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
426 | move->fromrow = rowidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
427 | move->tofile = fileidx(mstr[4]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
428 | move->torow = rowidx(mstr[5]); |
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
433 | if (move->piece) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
434 | if (move->piece == PAWN |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
435 | && move->torow == (gamestate->mycolor==WHITE?7:0) |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
436 | && !move->promotion) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
437 | return NEED_PROMOTION; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
438 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
439 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
440 | move->piece |= gamestate->mycolor; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
441 | if (move->fromfile == POS_UNSPECIFIED |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
442 | || move->fromrow == POS_UNSPECIFIED) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
443 | return getlocation(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
444 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
445 | return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
446 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
447 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
448 | return INVALID_MOVE_SYNTAX; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
449 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
450 | } |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
451 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
452 | _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
|
453 | 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
|
454 | Move candidates[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
455 | int candidatecount = 0; |
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 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
|
457 | 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
|
458 | 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
|
459 | 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
|
460 | 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
|
461 | 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
|
462 | 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
|
463 | 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
|
464 | 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
|
465 | candidatecount++; |
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 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
470 | if (threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
471 | *threatcount = 0; |
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 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
475 | _Bool result = 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
476 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
477 | 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
|
478 | 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
|
479 | result = 1; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
480 | if (threats && threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
481 | 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
|
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 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
486 | return result; |
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 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
489 | _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
|
490 | 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
|
491 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
492 | Move candidates[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
493 | uint8_t candidatecount; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
494 | 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
|
495 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
496 | if (threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
497 | *threatcount = 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
498 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
499 | _Bool result = 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
500 | 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
|
501 | 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
|
502 | 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
|
503 | 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
|
504 | kingfile = file; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
505 | kingrow = row; |
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 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
510 | 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
|
511 | GameState simulation; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
512 | 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
|
513 | 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
|
514 | 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
|
515 | opponent_color(color))) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
516 | result = 1; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
517 | if (threats && threatcount) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
518 | 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
|
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 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
523 | return result; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
524 | } else { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
525 | return 0; |
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 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
529 | _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
|
530 | uint8_t color) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
531 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
532 | Move threats[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
533 | uint8_t threatcount; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
534 | 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
|
535 | 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
|
536 | 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
|
537 | return 1; |
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 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
540 | return 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
541 | } else { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
542 | return 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
543 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
544 | } |
33 | 545 | |
546 | uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate, | |
547 | uint8_t color) { | |
548 | if (!gameinfo->timecontrol) { | |
549 | return 0; | |
550 | } | |
551 | ||
552 | if (gamestate->movelist) { | |
553 | uint16_t time = gameinfo->time; | |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
554 | suseconds_t micros = 0; |
33 | 555 | |
556 | MoveList *movelist = color == WHITE ? | |
557 | gamestate->movelist : gamestate->movelist->next; | |
558 | ||
559 | while (movelist) { | |
560 | time += gameinfo->addtime; | |
561 | ||
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
562 | struct timeval *movetime = &(movelist->move.movetime); |
33 | 563 | if (movetime->tv_sec >= time) { |
564 | return 0; | |
565 | } | |
566 | ||
567 | time -= movetime->tv_sec; | |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
568 | micros += movetime->tv_usec; |
33 | 569 | |
570 | movelist = movelist->next ? movelist->next->next : NULL; | |
571 | } | |
572 | ||
573 | time_t sec; | |
574 | movelist = gamestate->lastmove; | |
575 | if ((movelist->move.piece & COLOR_MASK) != color) { | |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
576 | struct timeval *lastmovetstamp = &(movelist->move.timestamp); |
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
577 | struct timeval currenttstamp; |
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
578 | gettimeofday(¤ttstamp, NULL); |
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
579 | micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec; |
33 | 580 | sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; |
581 | if (sec >= time) { | |
582 | return 0; | |
583 | } | |
584 | ||
585 | time -= sec; | |
586 | } | |
587 | ||
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
588 | sec = micros / 1e6L; |
33 | 589 | |
590 | if (sec >= time) { | |
591 | return 0; | |
592 | } | |
593 | ||
594 | time -= sec; | |
595 | ||
596 | return time; | |
597 | } else { | |
598 | return gameinfo->time; | |
599 | } | |
600 | } |