src/chess/rules.c

Wed, 09 Apr 2014 09:34:07 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 09 Apr 2014 09:34:07 +0200
changeset 32
8a0b85303ee8
parent 29
c6a1ad6cf749
child 33
866025982aa9
permissions
-rw-r--r--

improved async input + improved build system + added time values to move struct

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

mercurial