summaryrefslogtreecommitdiffstats
path: root/gnu/games/chess/Xchess/valid.c
blob: 06d1f29b067d4504d25840b9398ef8cdca3ccb3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264

/* This file contains code for X-CHESS.
   Copyright (C) 1986 Free Software Foundation, Inc.

This file is part of X-CHESS.

X-CHESS is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.  No author or distributor
accepts responsibility to anyone for the consequences of using it
or for whether it serves any particular purpose or works at all,
unless he says so in writing.  Refer to the X-CHESS General Public
License for full details.

Everyone is granted permission to copy, modify and redistribute
X-CHESS, but only under the conditions described in the
X-CHESS General Public License.   A copy of this license is
supposed to have been given to you along with X-CHESS so you
can know your rights and responsibilities.  It should be in a
file named COPYING.  Among other things, the copyright notice
and this notice must be preserved on all copies.  */


/* RCS Info: $Revision: 1.1.1.1 $ on $Date: 1993/06/12 14:41:08 $
 *           $Source: /home/ncvs/src/gnu/games/chess/Xchess/valid.c,v $
 * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
 *	Permission is granted to do anything with this code except sell it
 *	or remove this message.
 *
 * Validate a move.
 */

#include "xchess.h"

extern bool ischeck(), couldmove();

bool
valid_move(m, b)
	move *m;
	board *b;
{
	board tb;

	/* First check that the piece can make the move at all... */
	if (!couldmove(m, b))
		return (false);

	/* Now see if the king is in check now. */
	bcopy((char *) b, (char *) &tb, sizeof (board));
	board_move(&tb, m);
	if (ischeck(&tb, m->piece.color))
		return (false);

	if (ischeck(&tb, ((m->piece.color == WHITE) ? BLACK : WHITE)))
		m->check = true;

	return (true);
}

static bool
couldmove(m, b)
	move *m;
	board *b;
{
	int x, y;

	switch (m->type) {
	    case KCASTLE:
		if ((m->piece.color == WHITE) && (b->white_cant_castle_k) ||
				(m->piece.color == BLACK) &&
				(b->black_cant_castle_k))
			return (false);
		if ((b->square[m->fromy][5].color != NONE) ||
				(b->square[m->fromy][6].color != NONE))
			return (false);
		if (ischeck(b, m->piece.color))
			return (false);
		break;

	    case QCASTLE:
		if ((m->piece.color == WHITE) && (b->white_cant_castle_q) ||
				(m->piece.color == BLACK) &&
				(b->black_cant_castle_q))
			return (false);
		if ((b->square[m->fromy][1].color != NONE) ||
				(b->square[m->fromy][2].color != NONE) ||
				(b->square[m->fromy][3].color != NONE))
			return (false);
		if (ischeck(b, m->piece.color))
			return (false);
		break;

	    case MOVE:
	    case CAPTURE:
		/* There is one special case here, that of taking a pawn
		 * en passant.  In this case we change the move field to
		 * CAPTURE if it's ok.
		 */
		switch (m->piece.type) {
		    case PAWN:
			if ((m->type == MOVE) && (m->fromx == m->tox)) {
				/* A normal move. */
				if ((m->piece.color == WHITE) && (m->fromy ==
						m->toy + 1))
					break;
				if ((m->piece.color == WHITE) && (m->fromy ==
						6) && (m->toy == 4) &&
						(b->square[5][m->fromx].color
						== NONE))
					break;
				if ((m->piece.color == BLACK) && (m->fromy ==
						m->toy - 1))
					break;
				if ((m->piece.color == BLACK) && (m->fromy ==
						1) && (m->toy == 3) &&
						(b->square[2][m->fromx].color
						== NONE))
					break;
				return (false);
			} else if (m->type == CAPTURE) {
				if ((((m->piece.color == WHITE) && (m->fromy ==
					    m->toy + 1)) || ((m->piece.color ==
					    BLACK) && (m->fromy == m->toy -
					    1))) && ((m->fromx == m->tox + 1) ||
					    (m->fromx == m->tox - 1)))
					break;
				/* Now maybe it's enpassant...  We've already
				 * checked for some of these things in the
				 * calling routine.
				 */
				if (m->enpassant) {
					if (b->square[(m->piece.color == WHITE)
						    ? 3 : 4][m->tox].color ==
						    ((m->piece.color == WHITE) ?
						    BLACK : WHITE))
						break;
				}
				return (false);
			}
			return (false);

		    case ROOK:
			if (m->fromx == m->tox) {
				for (y = m->fromy + ((m->fromy > m->toy) ? -1 :
						1); y != m->toy; y += ((m->fromy
						> m->toy) ? -1 : 1))
					if (b->square[y][m->tox].color != NONE)
						return (false);
				break;
			}
			if (m->fromy == m->toy) {
				for (x = m->fromx + ((m->fromx > m->tox) ? -1 :
						1); x != m->tox; x += ((m->fromx
						> m->tox) ? -1 : 1))
					if (b->square[m->toy][x].color != NONE)
						return (false);
				break;
			}
			return (false);

		    case KNIGHT:
			x = m->fromx - m->tox;
			y = m->fromy - m->toy;
			if ((((x == 2) || (x == -2)) &&
					((y == 1) || (y == -1))) ||
					(((x == 1) || (x == -1)) &&
					((y == 2) || (y == -2))))
				break;
			return (false);

		    case BISHOP:
			x = m->fromx - m->tox;
			y = m->fromy - m->toy;
			if ((x != y) && (x != - y))
				return (false);
			for (x = m->fromx + ((m->fromx > m->tox) ? -1 : 1), y =
					m->fromy + ((m->fromy > m->toy) ? -1 :
					1); x != m->tox;
					x += ((m->fromx > m->tox) ? -1 : 1),
					y += ((m->fromy > m->toy) ? -1 : 1))
				if (b->square[y][x].color != NONE)
					return (false);
			break;

		    case QUEEN:
			if (m->fromx == m->tox) {
				for (y = m->fromy + ((m->fromy > m->toy) ? -1 :
						1); y != m->toy; y += ((m->fromy
						> m->toy) ? -1 : 1))
					if (b->square[y][m->tox].color != NONE)
						return (false);
				break;
			}
			if (m->fromy == m->toy) {
				for (x = m->fromx + ((m->fromx > m->tox) ? -1 :
						1); x != m->tox; x += ((m->fromx
						> m->tox) ? -1 : 1))
					if (b->square[m->toy][x].color != NONE)
						return (false);
				break;
			}
			x = m->fromx - m->tox;
			y = m->fromy - m->toy;
			if ((x != y) && (x != - y))
				return (false);
			for (x = m->fromx + ((m->fromx > m->tox) ? -1 : 1), y =
					m->fromy + ((m->fromy > m->toy) ? -1 :
					1); x != m->tox;
					x += ((m->fromx > m->tox) ? -1 : 1),
					y += ((m->fromy > m->toy) ? -1 : 1))
				if (b->square[y][x].color != NONE)
					return (false);
			break;

		    case KING:
			x = m->fromx - m->tox;
			y = m->fromy - m->toy;
			if ((x >= -1) && (x <= 1) && (y >= -1) && (y <= 1))
				break;
			return (false);
		}
		break;
	}
	return (true);
}

/* Say whether either king is in check...  If move is non-NULL, say whether he
 * in in check after the move takes place.  We do this in a rather stupid way.
 */

static bool
ischeck(b, col)
	board *b;
	color col;
{
	int x, y, kx, ky;
	move ch;

	for (x = 0; x < SIZE; x++)
		for (y = 0; y < SIZE; y++)
			if ((b->square[y][x].color == col) &&
				    (b->square[y][x].type == KING)) {
				kx = x;
				ky = y;
			}

	for (x = 0; x < SIZE; x++)
		for (y = 0; y < SIZE; y++)
			if (b->square[y][x].color == ((col == WHITE) ?
					BLACK : WHITE)) {
				ch.type = CAPTURE;
				ch.piece.color = b->square[y][x].color;
				ch.piece.type = b->square[y][x].type;
				ch.fromx = x;
				ch.fromy = y;
				ch.tox = kx;
				ch.toy = ky;
				ch.enpassant = false;
				if (couldmove(&ch, b))
					return (true);
			}

	return (false);
}

OpenPOWER on IntegriCloud