Wed, 11 Jun 2014 15:38:01 +0200
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
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 | |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
36 | static GameState gamestate_copy_sim(GameState *gamestate) { |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
37 | GameState simulation = *gamestate; |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
38 | if (simulation.lastmove) { |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
39 | MoveList *lastmovecopy = malloc(sizeof(MoveList)); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
40 | *lastmovecopy = *(simulation.lastmove); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
41 | simulation.movelist = simulation.lastmove = lastmovecopy; |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
42 | } |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
43 | |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
44 | return simulation; |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
45 | } |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
46 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
47 | void gamestate_cleanup(GameState *gamestate) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
48 | MoveList *elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
49 | elem = gamestate->movelist; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
50 | while (elem) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
51 | MoveList *cur = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
52 | elem = elem->next; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
53 | free(cur); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
54 | }; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
55 | } |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
56 | |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
57 | static void addmove(GameState* gamestate, Move *move) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
58 | MoveList *elem = malloc(sizeof(MoveList)); |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
59 | elem->next = NULL; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
60 | elem->move = *move; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
61 | |
40
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
62 | struct timeval curtimestamp; |
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
63 | gettimeofday(&curtimestamp, NULL); |
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
64 | elem->move.timestamp.tv_sec = curtimestamp.tv_sec; |
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
65 | elem->move.timestamp.tv_usec = curtimestamp.tv_usec; |
33 | 66 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
67 | if (gamestate->lastmove) { |
40
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
68 | struct movetimeval *lasttstamp = &(gamestate->lastmove->move.timestamp); |
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
69 | uint64_t sec = curtimestamp.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
|
70 | suseconds_t micros; |
40
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
71 | if (curtimestamp.tv_usec < lasttstamp->tv_usec) { |
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
72 | micros = 1e6L-(lasttstamp->tv_usec - curtimestamp.tv_usec); |
33 | 73 | sec--; |
74 | } else { | |
40
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
75 | micros = curtimestamp.tv_usec - lasttstamp->tv_usec; |
33 | 76 | } |
77 | ||
78 | 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
|
79 | elem->move.movetime.tv_usec = micros; |
33 | 80 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
81 | gamestate->lastmove->next = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
82 | gamestate->lastmove = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
83 | } 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
|
84 | elem->move.movetime.tv_usec = 0; |
33 | 85 | elem->move.movetime.tv_sec = 0; |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
86 | gamestate->movelist = gamestate->lastmove = elem; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
87 | } |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
88 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
89 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
90 | char getpiecechr(uint8_t piece) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
91 | switch (piece & PIECE_MASK) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
92 | case ROOK: return 'R'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
93 | case KNIGHT: return 'N'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
94 | case BISHOP: return 'B'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
95 | case QUEEN: return 'Q'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
96 | case KING: return 'K'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
97 | default: return '\0'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
98 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
99 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
100 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
101 | uint8_t getpiece(char c) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
102 | switch (c) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
103 | case 'R': return ROOK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
104 | case 'N': return KNIGHT; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
105 | case 'B': return BISHOP; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
106 | case 'Q': return QUEEN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
107 | case 'K': return KING; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
108 | default: return 0; |
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 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
111 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
112 | void apply_move(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
113 | uint8_t piece = move->piece & PIECE_MASK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
114 | uint8_t color = move->piece & COLOR_MASK; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
115 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
116 | /* en passant capture */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
117 | if (move->capture && piece == PAWN && |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
118 | mdst(gamestate->board, move) == 0) { |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
119 | gamestate->board[move->fromrow][move->tofile] = 0; |
19
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
122 | /* remove old en passant threats */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
123 | for (uint8_t file = 0 ; file < 8 ; file++) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
124 | gamestate->board[3][file] &= ~ENPASSANT_THREAT; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
125 | gamestate->board[4][file] &= ~ENPASSANT_THREAT; |
19
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
128 | /* add new en passant threat */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
129 | if (piece == PAWN && ( |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
130 | (move->fromrow == 1 && move->torow == 3) || |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
131 | (move->fromrow == 6 && move->torow == 4))) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
132 | move->piece |= ENPASSANT_THREAT; |
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
135 | /* move (and maybe capture or promote) */ |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
136 | msrc(gamestate->board, move) = 0; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
137 | if (move->promotion) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
138 | mdst(gamestate->board, move) = move->promotion; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
139 | } else { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
140 | mdst(gamestate->board, move) = move->piece; |
19
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 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
143 | /* castling */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
144 | if (piece == KING && |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
145 | move->fromfile == fileidx('e')) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
146 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
147 | if (move->tofile == fileidx('g')) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
148 | gamestate->board[move->torow][fileidx('h')] = 0; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
149 | 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
|
150 | } else if (move->tofile == fileidx('c')) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
151 | gamestate->board[move->torow][fileidx('a')] = 0; |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
152 | 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
|
153 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
154 | } |
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
|
155 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
156 | addmove(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
157 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
158 | |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
159 | static int validate_move_rules(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
160 | /* validate indices (don't trust opponent) */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
161 | if (!chkidx(move)) { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
162 | return INVALID_POSITION; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
163 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
164 | |
21
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
165 | /* must move */ |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
166 | if (move->fromfile == move->tofile && move->fromrow == move->torow) { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
167 | return INVALID_MOVE_SYNTAX; |
21
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
168 | } |
2e5846019b4f
implemented rook + some fixes
Mike Becker <universe@uap-core.de>
parents:
19
diff
changeset
|
169 | |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
170 | /* does piece exist */ |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
171 | if ((msrc(gamestate->board, move)&(PIECE_MASK|COLOR_MASK)) |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
172 | != (move->piece&(PIECE_MASK|COLOR_MASK))) { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
173 | return INVALID_POSITION; |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
174 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
175 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
176 | /* 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
|
177 | if ((mdst(gamestate->board, move) & COLOR_MASK) |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
178 | == (move->piece & COLOR_MASK)) { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
179 | return RULES_VIOLATED; |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
180 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
181 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
182 | /* must capture, if and only if destination is occupied */ |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
183 | if ((mdst(gamestate->board, move) == 0 && move->capture) || |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
184 | (mdst(gamestate->board, move) != 0 && !move->capture)) { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
185 | return INVALID_MOVE_SYNTAX; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
186 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
187 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
188 | /* validate individual rules */ |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
189 | _Bool chkrules; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
190 | switch (move->piece & PIECE_MASK) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
191 | case PAWN: |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
192 | chkrules = pawn_chkrules(gamestate, move) && |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
193 | !pawn_isblocked(gamestate, move); |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
194 | break; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
195 | case ROOK: |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
196 | chkrules = rook_chkrules(move) && |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
197 | !rook_isblocked(gamestate, move); |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
198 | break; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
199 | case KNIGHT: |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
200 | chkrules = knight_chkrules(move); /* knight is never blocked */ |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
201 | break; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
202 | case BISHOP: |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
203 | chkrules = bishop_chkrules(move) && |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
204 | !bishop_isblocked(gamestate, move); |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
205 | break; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
206 | case QUEEN: |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
207 | chkrules = queen_chkrules(move) && |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
208 | !queen_isblocked(gamestate, move); |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
209 | break; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
210 | case KING: |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
211 | chkrules = king_chkrules(gamestate, move) && |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
212 | !king_isblocked(gamestate, move); |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
213 | break; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
214 | default: |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
215 | return INVALID_MOVE_SYNTAX; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
216 | } |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
217 | |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
218 | return chkrules ? VALID_MOVE_SEMANTICS : RULES_VIOLATED; |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
219 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
220 | |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
221 | int validate_move(GameState *gamestate, Move *move) { |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
222 | |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
223 | int result = validate_move_rules(gamestate, move); |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
224 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
225 | /* cancel processing to save resources */ |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
226 | if (result != VALID_MOVE_SEMANTICS) { |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
227 | return result; |
25 | 228 | } |
229 | ||
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
|
230 | /* 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
|
231 | 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
|
232 | |
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
|
233 | 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
|
234 | 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
|
235 | 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
|
236 | 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
|
237 | (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
|
238 | 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
|
239 | 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
|
240 | } 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
|
241 | (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
|
242 | 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
|
243 | 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
|
244 | } |
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
|
245 | } |
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
|
246 | } |
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 | |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
248 | /* simulate move for check validation */ |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
249 | GameState simulation = gamestate_copy_sim(gamestate); |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
250 | Move simmove = *move; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
251 | apply_move(&simulation, &simmove); |
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
|
252 | |
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 | /* 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
|
254 | 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
|
255 | opponent_color(piececolor))) { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
256 | |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
257 | gamestate_cleanup(&simulation); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
258 | if ((move->piece & PIECE_MASK) == KING) { |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
259 | return KING_MOVES_INTO_CHECK; |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
260 | } else { |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
261 | /* last move is always not null in this case */ |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
262 | return gamestate->lastmove->move.check ? |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
263 | KING_IN_CHECK : PIECE_PINNED; |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
264 | } |
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
|
265 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
266 | |
25 | 267 | /* 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
|
268 | Move threats[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
269 | uint8_t threatcount; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
270 | 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
|
271 | piececolor, threats, &threatcount); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
272 | |
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
|
273 | 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
|
274 | /* 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
|
275 | _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
|
276 | 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
|
277 | 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
|
278 | 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
|
279 | 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
|
280 | |
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
|
281 | /* 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
|
282 | 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
|
283 | & 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
|
284 | 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
|
285 | 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
|
286 | } |
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
|
287 | } |
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
|
288 | } |
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
|
289 | } |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
290 | /* 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
|
291 | 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
|
292 | 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
|
293 | 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
|
294 | } |
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 | /* 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
|
297 | 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
|
298 | 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
|
299 | 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
|
300 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
301 | /* 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
|
302 | 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
|
303 | || threatpiece == QUEEN) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
304 | 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
|
305 | /* 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
|
306 | 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
|
307 | 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
|
308 | 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
|
309 | file += d; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
310 | 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
|
311 | 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
|
312 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
313 | } 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
|
314 | /* 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
|
315 | 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
|
316 | 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
|
317 | 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
|
318 | row += d; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
319 | 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
|
320 | 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
|
321 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
322 | } else { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
323 | /* bishop aspect */ |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
324 | 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
|
325 | 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
|
326 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
327 | 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
|
328 | 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
|
329 | 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
|
330 | && 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
|
331 | row += dr; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
332 | file += df; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
333 | 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
|
334 | opponent_color(piececolor)); |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
335 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
336 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
337 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
338 | } |
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
|
339 | |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
340 | if (!canescape) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
341 | 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
|
342 | } |
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
|
343 | } |
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
|
344 | |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
345 | gamestate_cleanup(&simulation); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
346 | |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
347 | return VALID_MOVE_SEMANTICS; |
19
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 | |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
350 | _Bool get_threats(GameState *gamestate, uint8_t row, uint8_t file, |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
351 | uint8_t color, Move *threats, uint8_t *threatcount) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
352 | Move candidates[32]; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
353 | int candidatecount = 0; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
354 | for (uint8_t r = 0 ; r < 8 ; r++) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
355 | for (uint8_t f = 0 ; f < 8 ; f++) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
356 | if ((gamestate->board[r][f] & COLOR_MASK) == color) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
357 | // non-capturing move |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
358 | memset(&(candidates[candidatecount]), 0, sizeof(Move)); |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
359 | candidates[candidatecount].piece = gamestate->board[r][f]; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
360 | candidates[candidatecount].fromrow = r; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
361 | candidates[candidatecount].fromfile = f; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
362 | candidates[candidatecount].torow = row; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
363 | candidates[candidatecount].tofile = file; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
364 | candidatecount++; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
365 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
366 | // capturing move |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
367 | memcpy(&(candidates[candidatecount]), |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
368 | &(candidates[candidatecount-1]), sizeof(Move)); |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
369 | candidates[candidatecount].capture = 1; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
370 | candidatecount++; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
371 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
372 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
373 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
374 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
375 | if (threatcount) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
376 | *threatcount = 0; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
377 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
378 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
379 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
380 | _Bool result = 0; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
381 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
382 | for (int i = 0 ; i < candidatecount ; i++) { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
383 | if (validate_move_rules(gamestate, &(candidates[i])) |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
384 | == VALID_MOVE_SEMANTICS) { |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
385 | result = 1; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
386 | if (threats && threatcount) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
387 | threats[(*threatcount)++] = candidates[i]; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
388 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
389 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
390 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
391 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
392 | return result; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
393 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
394 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
395 | _Bool is_pinned(GameState *gamestate, Move *move) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
396 | uint8_t color = move->piece & COLOR_MASK; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
397 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
398 | uint8_t kingfile = 0, kingrow = 0; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
399 | for (uint8_t row = 0 ; row < 8 ; row++) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
400 | for (uint8_t file = 0 ; file < 8 ; file++) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
401 | if (gamestate->board[row][file] == (color|KING)) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
402 | kingfile = file; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
403 | kingrow = row; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
404 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
405 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
406 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
407 | |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
408 | GameState simulation = gamestate_copy_sim(gamestate); |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
409 | Move simmove = *move; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
410 | apply_move(&simulation, &simmove); |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
411 | _Bool covered = is_covered(&simulation, |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
412 | kingrow, kingfile, opponent_color(color)); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
413 | gamestate_cleanup(&simulation); |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
414 | |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
415 | return covered; |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
416 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
417 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
418 | _Bool get_real_threats(GameState *gamestate, uint8_t row, uint8_t file, |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
419 | uint8_t color, Move *threats, uint8_t *threatcount) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
420 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
421 | if (threatcount) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
422 | *threatcount = 0; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
423 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
424 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
425 | Move candidates[16]; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
426 | uint8_t candidatecount; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
427 | if (get_threats(gamestate, row, file, color, candidates, &candidatecount)) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
428 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
429 | _Bool result = 0; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
430 | uint8_t kingfile = 0, kingrow = 0; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
431 | for (uint8_t row = 0 ; row < 8 ; row++) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
432 | for (uint8_t file = 0 ; file < 8 ; file++) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
433 | if (gamestate->board[row][file] == (color|KING)) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
434 | kingfile = file; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
435 | kingrow = row; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
436 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
437 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
438 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
439 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
440 | for (uint8_t i = 0 ; i < candidatecount ; i++) { |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
441 | GameState simulation = gamestate_copy_sim(gamestate); |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
442 | Move simmove = candidates[i]; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
443 | apply_move(&simulation, &simmove); |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
444 | if (!is_covered(&simulation, kingrow, kingfile, |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
445 | opponent_color(color))) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
446 | result = 1; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
447 | if (threats && threatcount) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
448 | threats[(*threatcount)++] = candidates[i]; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
449 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
450 | } |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
451 | gamestate_cleanup(&simulation); |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
452 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
453 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
454 | return result; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
455 | } else { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
456 | return 0; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
457 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
458 | } |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
459 | |
47
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
460 | static int getlocation(GameState *gamestate, Move *move) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
461 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
462 | uint8_t color = move->piece & COLOR_MASK; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
463 | _Bool incheck = gamestate->lastmove?gamestate->lastmove->move.check:0; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
464 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
465 | Move threats[16], *threat = NULL; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
466 | uint8_t threatcount; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
467 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
468 | if (get_threats(gamestate, move->torow, move->tofile, color, |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
469 | threats, &threatcount)) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
470 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
471 | // find threats for the specified position |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
472 | for (uint8_t i = 0 ; i < threatcount ; i++) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
473 | if ((threats[i].piece & (PIECE_MASK | COLOR_MASK)) |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
474 | == move->piece && |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
475 | (move->fromrow == POS_UNSPECIFIED || |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
476 | move->fromrow == threats[i].fromrow) && |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
477 | (move->fromfile == POS_UNSPECIFIED || |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
478 | move->fromfile == threats[i].fromfile)) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
479 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
480 | if (threat) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
481 | return AMBIGUOUS_MOVE; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
482 | } else { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
483 | threat = &(threats[i]); |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
484 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
485 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
486 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
487 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
488 | // can't threaten specified position |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
489 | if (!threat) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
490 | return INVALID_POSITION; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
491 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
492 | |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
493 | // found threat is no real threat |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
494 | if (is_pinned(gamestate, threat)) { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
495 | return incheck?KING_IN_CHECK:PIECE_PINNED; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
496 | } else { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
497 | memcpy(move, threat, sizeof(Move)); |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
498 | return VALID_MOVE_SYNTAX; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
499 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
500 | } else { |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
501 | return INVALID_POSITION; |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
502 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
503 | } |
d726e4b46c33
refactoring of getlocation mechanism for better short algebraic notation support (does now respect pinned pieces) + fixed a bug where a pawn could advance through a piece (e.g. e2e4 could jump over a piece on e3)
Mike Becker <universe@uap-core.de>
parents:
40
diff
changeset
|
504 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
505 | 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
|
506 | memset(move, 0, sizeof(Move)); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
507 | move->fromfile = POS_UNSPECIFIED; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
508 | move->fromrow = POS_UNSPECIFIED; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
509 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
510 | size_t len = strlen(mstr); |
48
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
511 | if (len < 1 || len > 6) { |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
512 | return INVALID_MOVE_SYNTAX; |
0cedda2544da
added return code to move validation (for more informative messages) + fixed a bug where simulations added movelist items to the original gamestate
Mike Becker <universe@uap-core.de>
parents:
47
diff
changeset
|
513 | } |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
514 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
515 | /* evaluate check/checkmate flags */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
516 | if (mstr[len-1] == '+') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
517 | len--; mstr[len] = '\0'; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
518 | move->check = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
519 | } else if (mstr[len-1] == '#') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
520 | len--; mstr[len] = '\0'; |
27
efeb98bc69c9
moved checkmate and stalemate flags to gamestate
Mike Becker <universe@uap-core.de>
parents:
25
diff
changeset
|
521 | /* ignore - validation should set game state */ |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
522 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
523 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
524 | /* evaluate promotion */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
525 | if (len > 3 && mstr[len-2] == '=') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
526 | move->promotion = getpiece(mstr[len-1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
527 | if (!move->promotion) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
528 | return INVALID_MOVE_SYNTAX; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
529 | } else { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
530 | move->promotion |= gamestate->mycolor; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
531 | len -= 2; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
532 | mstr[len] = 0; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
533 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
534 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
535 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
536 | if (len == 2) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
537 | /* pawn move (e.g. "e4") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
538 | move->piece = PAWN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
539 | move->tofile = fileidx(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
540 | move->torow = rowidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
541 | } else if (len == 3) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
542 | if (strcmp(mstr, "O-O") == 0) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
543 | /* king side castling */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
544 | move->piece = KING; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
545 | move->fromfile = fileidx('e'); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
546 | move->tofile = fileidx('g'); |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
547 | 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
|
548 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
549 | /* move (e.g. "Nf3") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
550 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
551 | move->tofile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
552 | move->torow = rowidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
553 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
554 | } else if (len == 4) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
555 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
556 | if (!move->piece) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
557 | move->piece = PAWN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
558 | move->fromfile = fileidx(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
559 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
560 | if (mstr[1] == 'x') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
561 | /* capture (e.g. "Nxf3", "dxe5") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
562 | move->capture = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
563 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
564 | /* move (e.g. "Ndf3", "N2c3", "e2e4") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
565 | if (isfile(mstr[1])) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
566 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
567 | if (move->piece == PAWN) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
568 | move->piece = 0; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
569 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
570 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
571 | move->fromrow = rowidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
572 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
573 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
574 | move->tofile = fileidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
575 | move->torow = rowidx(mstr[3]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
576 | } else if (len == 5) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
577 | if (strcmp(mstr, "O-O-O") == 0) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
578 | /* queen side castling "O-O-O" */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
579 | move->piece = KING; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
580 | move->fromfile = fileidx('e'); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
581 | move->tofile = fileidx('c'); |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
582 | 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
|
583 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
584 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
585 | if (mstr[2] == 'x') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
586 | move->capture = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
587 | if (move->piece) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
588 | /* capture (e.g. "Ndxf3") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
589 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
590 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
591 | /* long notation capture (e.g. "e5xf6") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
592 | move->piece = PAWN; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
593 | move->fromfile = fileidx(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
594 | move->fromrow = rowidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
595 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
596 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
597 | /* long notation move (e.g. "Nc5a4") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
598 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
599 | move->fromrow = rowidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
600 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
601 | move->tofile = fileidx(mstr[3]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
602 | move->torow = rowidx(mstr[4]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
603 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
604 | } else if (len == 6) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
605 | /* long notation capture (e.g. "Nc5xf3") */ |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
606 | if (mstr[3] == 'x') { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
607 | move->capture = 1; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
608 | move->piece = getpiece(mstr[0]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
609 | move->fromfile = fileidx(mstr[1]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
610 | move->fromrow = rowidx(mstr[2]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
611 | move->tofile = fileidx(mstr[4]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
612 | move->torow = rowidx(mstr[5]); |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
613 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
614 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
615 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
616 | |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
617 | if (move->piece) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
618 | if (move->piece == PAWN |
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
619 | && move->torow == (gamestate->mycolor==WHITE?7:0) |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
620 | && !move->promotion) { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
621 | return NEED_PROMOTION; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
622 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
623 | |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
624 | move->piece |= gamestate->mycolor; |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
625 | if (move->fromfile == POS_UNSPECIFIED |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
626 | || move->fromrow == POS_UNSPECIFIED) { |
23
824c9522ce66
introduced game state structure
Mike Becker <universe@uap-core.de>
parents:
21
diff
changeset
|
627 | return getlocation(gamestate, move); |
19
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
628 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
629 | return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
630 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
631 | } else { |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
632 | return INVALID_MOVE_SYNTAX; |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
633 | } |
6a26114297a1
moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff
changeset
|
634 | } |
29
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
635 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
636 | _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
|
637 | uint8_t color) { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
638 | |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
639 | Move threats[16]; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
640 | uint8_t threatcount; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
641 | 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
|
642 | 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
|
643 | 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
|
644 | return 1; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
645 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
646 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
647 | return 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
648 | } else { |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
649 | return 0; |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
650 | } |
c6a1ad6cf749
fixed checkmate and completed implementation (more testing is still advised)
Mike Becker <universe@uap-core.de>
parents:
28
diff
changeset
|
651 | } |
33 | 652 | |
653 | uint16_t remaining_movetime(GameInfo *gameinfo, GameState *gamestate, | |
654 | uint8_t color) { | |
655 | if (!gameinfo->timecontrol) { | |
656 | return 0; | |
657 | } | |
658 | ||
659 | if (gamestate->movelist) { | |
660 | 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
|
661 | suseconds_t micros = 0; |
33 | 662 | |
663 | MoveList *movelist = color == WHITE ? | |
664 | gamestate->movelist : gamestate->movelist->next; | |
665 | ||
666 | while (movelist) { | |
667 | time += gameinfo->addtime; | |
668 | ||
40
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
669 | struct movetimeval *movetime = &(movelist->move.movetime); |
33 | 670 | if (movetime->tv_sec >= time) { |
671 | return 0; | |
672 | } | |
673 | ||
674 | 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
|
675 | micros += movetime->tv_usec; |
33 | 676 | |
677 | movelist = movelist->next ? movelist->next->next : NULL; | |
678 | } | |
679 | ||
680 | time_t sec; | |
681 | movelist = gamestate->lastmove; | |
682 | if ((movelist->move.piece & COLOR_MASK) != color) { | |
40
47162a7621da
fixed Move struct size inconsistancy across plattforms
Mike Becker <universe@uap-core.de>
parents:
36
diff
changeset
|
683 | struct movetimeval *lastmovetstamp = &(movelist->move.timestamp); |
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
684 | 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
|
685 | 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
|
686 | micros += currenttstamp.tv_usec - lastmovetstamp->tv_usec; |
33 | 687 | sec = currenttstamp.tv_sec - lastmovetstamp->tv_sec; |
688 | if (sec >= time) { | |
689 | return 0; | |
690 | } | |
691 | ||
692 | time -= sec; | |
693 | } | |
694 | ||
36
ebe0c961e9a6
reduced awesome great nanosecond precision so we can compile on OS X
Mike Becker <universe@uap-core.de>
parents:
33
diff
changeset
|
695 | sec = micros / 1e6L; |
33 | 696 | |
697 | if (sec >= time) { | |
698 | return 0; | |
699 | } | |
700 | ||
701 | time -= sec; | |
702 | ||
703 | return time; | |
704 | } else { | |
705 | return gameinfo->time; | |
706 | } | |
707 | } |