src/chess/rules.c

Mon, 31 Mar 2014 11:41:08 +0200

author
Mike Becker <universe@uap-core.de>
date
Mon, 31 Mar 2014 11:41:08 +0200
changeset 20
fd1eb081de40
parent 19
6a26114297a1
child 21
2e5846019b4f
permissions
-rw-r--r--

added optional alternate config for chess lib

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>
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
33
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
34 char getpiecechr(uint8_t piece) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
35 switch (piece & PIECE_MASK) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
36 case ROOK: return 'R';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
37 case KNIGHT: return 'N';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
38 case BISHOP: return 'B';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
39 case QUEEN: return 'Q';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
40 case KING: return 'K';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
41 default: return '\0';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
42 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
43 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
44
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
45 uint8_t getpiece(char c) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
46 switch (c) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
47 case 'R': return ROOK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
48 case 'N': return KNIGHT;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
49 case 'B': return BISHOP;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
50 case 'Q': return QUEEN;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
51 case 'K': return KING;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
52 default: return 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
53 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
54 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
55
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
56 /**
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
57 * Guesses the location of a piece for short algebraic notation.
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
58 *
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
59 * @param board the current state of the board
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
60 * @param move the move date to operate on
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
61 * @return status code (see rules/rules.h for the codes)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
62 */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
63 static int getlocation(Board board, Move *move) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
64 uint8_t piece = move->piece & PIECE_MASK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
65 switch (piece) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
66 case PAWN: return pawn_getlocation(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
67 case ROOK: return rook_getlocation(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
68 case KNIGHT: return knight_getlocation(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
69 case BISHOP: return bishop_getlocation(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
70 case QUEEN: return queen_getlocation(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
71 case KING: return king_getlocation(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
72 default: return INVALID_MOVE_SYNTAX;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
73 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
74 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
75
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
76
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
77 void apply_move(Board board, Move *move) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
78 uint8_t piece = move->piece & PIECE_MASK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
79 uint8_t color = move->piece & COLOR_MASK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
80
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
81 /* en passant capture */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
82 if (move->capture && piece == PAWN &&
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
83 mdst(board, move) == 0) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
84 board[move->fromrow][move->tofile] = 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
85 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
86
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
87 /* remove old en passant threats */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
88 for (uint8_t file = 0 ; file < 8 ; file++) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
89 board[3][file] &= ~ENPASSANT_THREAT;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
90 board[4][file] &= ~ENPASSANT_THREAT;
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
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
93 /* add new en passant threat */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
94 if (piece == PAWN && (
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
95 (move->fromrow == 1 && move->torow == 3) ||
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
96 (move->fromrow == 6 && move->torow == 4))) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
97 move->piece |= ENPASSANT_THREAT;
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 /* move (and maybe capture or promote) */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
101 msrc(board, move) = 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
102 if (move->promotion) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
103 mdst(board, move) = move->promotion;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
104 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
105 mdst(board, move) = move->piece;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
106 }
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 /* castling */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
109 if (piece == KING &&
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
110 move->fromfile == fileidx('e')) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
111
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
112 if (move->tofile == fileidx('g')) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
113 board[move->torow][fileidx('h')] = 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
114 board[move->torow][fileidx('f')] = color|ROOK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
115 } else if (move->tofile == fileidx('c')) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
116 board[move->torow][fileidx('a')] = 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
117 board[move->torow][fileidx('d')] = color|ROOK;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
118 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
119 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
120 }
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 _Bool validate_move(Board board, Move *move) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
123 _Bool result;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
124
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
125 /* validate indices (don't trust opponent) */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
126 if (!chkidx(move)) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
127 return 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
128 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
129
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
130 /* does piece exist */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
131 result = msrc(board, move) == move->piece;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
132
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
133 /* can't capture own pieces */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
134 if ((mdst(board, move) & COLOR_MASK) == (move->piece & COLOR_MASK)) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
135 return 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
136 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
137
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
138 /* validate individual rules */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
139 switch (move->piece & PIECE_MASK) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
140 case PAWN:
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
141 result = result && pawn_chkrules(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
142 result = result && !pawn_isblocked(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
143 break;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
144 case ROOK:
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
145 result = result && rook_chkrules(move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
146 result = result && !rook_isblocked(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
147 break;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
148 case KNIGHT:
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
149 result = result && knight_chkrules(move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
150 result = result && !knight_isblocked(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
151 break;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
152 case BISHOP:
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
153 result = result && bishop_chkrules(move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
154 result = result && !bishop_isblocked(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
155 break;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
156 case QUEEN:
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
157 result = result && queen_chkrules(move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
158 result = result && !queen_isblocked(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
159 break;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
160 case KING:
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
161 result = result && king_chkrules(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
162 result = result && !king_isblocked(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
163 break;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
164 default:
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
165 result = 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
166 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
167
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
168 /* is piece pinned */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
169 // TODO: make it so
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
170
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
171 /* correct check and checkmate flags */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
172 // TODO: make it so
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
173
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
174 return result;
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
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
177 int eval_move(Board board, uint8_t mycolor, char *mstr, Move *move) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
178 memset(move, 0, sizeof(Move));
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
179 move->fromfile = POS_UNSPECIFIED;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
180 move->fromrow = POS_UNSPECIFIED;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
181
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
182 size_t len = strlen(mstr);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
183
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
184 /* evaluate check/checkmate flags */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
185 if (mstr[len-1] == '+') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
186 len--; mstr[len] = '\0';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
187 move->check = 1;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
188 } else if (mstr[len-1] == '#') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
189 len--; mstr[len] = '\0';
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
190 move->checkmate = 1;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
191 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
192
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
193 /* evaluate promotion */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
194 if (len > 3 && mstr[len-2] == '=') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
195 move->promotion = getpiece(mstr[len-1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
196 if (!move->promotion) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
197 return INVALID_MOVE_SYNTAX;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
198 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
199 move->promotion |= mycolor;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
200 len -= 2;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
201 mstr[len] = 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
202 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
203 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
204
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
205 if (len == 2) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
206 /* pawn move (e.g. "e4") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
207 move->piece = PAWN;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
208 move->tofile = fileidx(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
209 move->torow = rowidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
210 } else if (len == 3) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
211 if (strcmp(mstr, "O-O") == 0) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
212 /* king side castling */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
213 move->piece = KING;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
214 move->fromfile = fileidx('e');
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
215 move->tofile = fileidx('g');
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
216 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
217 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
218 /* move (e.g. "Nf3") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
219 move->piece = getpiece(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
220 move->tofile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
221 move->torow = rowidx(mstr[2]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
222 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
223
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
224 } else if (len == 4) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
225 move->piece = getpiece(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
226 if (!move->piece) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
227 move->piece = PAWN;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
228 move->fromfile = fileidx(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
229 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
230 if (mstr[1] == 'x') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
231 /* capture (e.g. "Nxf3", "dxe5") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
232 move->capture = 1;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
233 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
234 /* move (e.g. "Ndf3", "N2c3", "e2e4") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
235 if (isfile(mstr[1])) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
236 move->fromfile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
237 if (move->piece == PAWN) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
238 move->piece = 0;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
239 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
240 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
241 move->fromrow = rowidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
242 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
243 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
244 move->tofile = fileidx(mstr[2]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
245 move->torow = rowidx(mstr[3]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
246 } else if (len == 5) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
247 if (strcmp(mstr, "O-O-O") == 0) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
248 /* queen side castling "O-O-O" */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
249 move->piece = KING;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
250 move->fromfile = fileidx('e');
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
251 move->tofile = fileidx('c');
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
252 move->fromrow = move->torow = mycolor == WHITE ? 0 : 7;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
253 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
254 move->piece = getpiece(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
255 if (mstr[2] == 'x') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
256 move->capture = 1;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
257 if (move->piece) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
258 /* capture (e.g. "Ndxf3") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
259 move->fromfile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
260 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
261 /* long notation capture (e.g. "e5xf6") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
262 move->piece = PAWN;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
263 move->fromfile = fileidx(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
264 move->fromrow = rowidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
265 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
266 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
267 /* long notation move (e.g. "Nc5a4") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
268 move->fromfile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
269 move->fromrow = rowidx(mstr[2]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
270 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
271 move->tofile = fileidx(mstr[3]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
272 move->torow = rowidx(mstr[4]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
273 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
274 } else if (len == 6) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
275 /* long notation capture (e.g. "Nc5xf3") */
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
276 if (mstr[3] == 'x') {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
277 move->capture = 1;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
278 move->piece = getpiece(mstr[0]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
279 move->fromfile = fileidx(mstr[1]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
280 move->fromrow = rowidx(mstr[2]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
281 move->tofile = fileidx(mstr[4]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
282 move->torow = rowidx(mstr[5]);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
283 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
284 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
285
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
286
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
287 if (move->piece) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
288 if (move->piece == PAWN && move->torow == (mycolor==WHITE?7:0)
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
289 && !move->promotion) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
290 return NEED_PROMOTION;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
291 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
292
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
293 move->piece |= mycolor;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
294 if (move->fromfile == POS_UNSPECIFIED
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
295 || move->fromrow == POS_UNSPECIFIED) {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
296 return getlocation(board, move);
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
297 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
298 return chkidx(move) ? VALID_MOVE_SYNTAX : INVALID_POSITION;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
299 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
300 } else {
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
301 return INVALID_MOVE_SYNTAX;
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
302 }
6a26114297a1 moved chess rules to separate lib
Mike Becker <universe@uap-core.de>
parents:
diff changeset
303 }

mercurial