aboutsummaryrefslogtreecommitdiff
path: root/gnu/games/chess/Xchess/board.c
blob: 4c5b9347d8e6af9cc44af65a1b68b065987fea3f (plain) (blame)
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

/* 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.4 $ on $Date: 86/11/23 17:17:15 $
 *           $Source: /users/faustus/xchess/RCS/board.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.
 *
 * Stuff to deal with the board.
 */

#include "xchess.h"

board *chessboard;

void
board_setup()
{
	chessboard = alloc(board);
	board_init(chessboard);
	return;
}

void
board_init(b)
	board *b;
{
	int i, j;

	for (i = 0; i < 2; i++)
		for (j = 0; j < SIZE; j++)
			b->square[i][j].color = BLACK;
	for (i = 2; i < 6; i++)
		for (j = 0; j < SIZE; j++)
			b->square[i][j].color = NONE;
	for (i = 6; i < 8; i++)
		for (j = 0; j < SIZE; j++)
			b->square[i][j].color = WHITE;
	for (i = 0; i < SIZE; i++)
		b->square[1][i].type = b->square[6][i].type =
				PAWN;
	b->square[0][0].type = b->square[7][0].type = ROOK;
	b->square[0][1].type = b->square[7][1].type = KNIGHT;
	b->square[0][2].type = b->square[7][2].type = BISHOP;
	b->square[0][3].type = b->square[7][3].type = QUEEN;
	b->square[0][4].type = b->square[7][4].type = KING;
	b->square[0][5].type = b->square[7][5].type = BISHOP;
	b->square[0][6].type = b->square[7][6].type = KNIGHT;
	b->square[0][7].type = b->square[7][7].type = ROOK;
	b->black_cant_castle_k = false;
	b->black_cant_castle_q = false;
	b->white_cant_castle_k = false;
	b->white_cant_castle_q = false;

	return;
}

void
board_drawall()
{
	int i, j;

	for (i = 0; i < SIZE; i++)
		for (j = 0; j < SIZE; j++)
			if (chessboard->square[i][j].color != NONE) {
				win_drawpiece(&chessboard->square[i][j], i,
						j, WHITE);
				if (!oneboard)
					win_drawpiece(&chessboard->square[i][j],
							i, j, BLACK);
			}
	return;
}

void
board_move(b, m)
	board *b;
	move *m;
{
	switch (m->type) {

	    case MOVE:
	    case CAPTURE:
		b->square[m->fromy][m->fromx].color = NONE;
		b->square[m->toy][m->tox].color = m->piece.color;
		b->square[m->toy][m->tox].type = m->piece.type;
		if ((m->piece.type == PAWN) && (((m->piece.color == BLACK) &&
				(m->toy == 7)) || ((m->piece.color == WHITE) &&
				(m->toy == 0))))
			b->square[m->toy][m->tox].type = QUEEN;
		if (m->enpassant)
			b->square[m->toy + ((m->piece.color == WHITE) ? 1 :
					-1)][m->tox].color = NONE;
		break;

	    case KCASTLE:
		if (m->piece.color == WHITE) {
			b->square[7][5].color = m->piece.color;
			b->square[7][5].type = ROOK;
			b->square[7][6].color = m->piece.color;
			b->square[7][6].type = KING;
			b->square[7][4].color = NONE;
			b->square[7][7].color = NONE;
		} else {
			b->square[0][5].color = m->piece.color;
			b->square[0][5].type = ROOK;
			b->square[0][6].color = m->piece.color;
			b->square[0][6].type = KING;
			b->square[0][4].color = NONE;
			b->square[0][7].color = NONE;
		}
		break;

	    case QCASTLE:
		if (m->piece.color == WHITE) {
			b->square[7][3].color = m->piece.color;
			b->square[7][3].type = ROOK;
			b->square[7][2].color = m->piece.color;
			b->square[7][2].type = KING;
			b->square[7][4].color = NONE;
			b->square[7][0].color = NONE;
		} else {
			b->square[0][3].color = m->piece.color;
			b->square[0][3].type = ROOK;
			b->square[0][2].color = m->piece.color;
			b->square[0][2].type = KING;
			b->square[0][4].color = NONE;
			b->square[0][0].color = NONE;
		}
		break;

	    default:
		fprintf(stderr, "Bad move type %d\n", m->type);
	}

	if (m->piece.type == KING) {
		if (m->piece.color == WHITE)
			b->white_cant_castle_q =
					b->white_cant_castle_k= true;
		else
			b->black_cant_castle_q =
					b->black_cant_castle_k= true;
	} else if (m->piece.type == ROOK) {
		if (m->piece.color == WHITE) {
			if (m->fromx == 0)
				b->white_cant_castle_q = true;
			else if (m->fromx == 7) 
				b->white_cant_castle_k = true;
		} else {
			if (m->fromx == 0)
				b->black_cant_castle_q = true;
			else if (m->fromx == 7) 
				b->black_cant_castle_k = true;
		}
	}

	return;
}