xref: /386bsd/usr/src/games/chess/Xchess/board.c (revision a2142627)
1 
2 /* This file contains code for X-CHESS.
3    Copyright (C) 1986 Free Software Foundation, Inc.
4 
5 This file is part of X-CHESS.
6 
7 X-CHESS is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY.  No author or distributor
9 accepts responsibility to anyone for the consequences of using it
10 or for whether it serves any particular purpose or works at all,
11 unless he says so in writing.  Refer to the X-CHESS General Public
12 License for full details.
13 
14 Everyone is granted permission to copy, modify and redistribute
15 X-CHESS, but only under the conditions described in the
16 X-CHESS General Public License.   A copy of this license is
17 supposed to have been given to you along with X-CHESS so you
18 can know your rights and responsibilities.  It should be in a
19 file named COPYING.  Among other things, the copyright notice
20 and this notice must be preserved on all copies.  */
21 
22 
23 /* RCS Info: $Revision: 1.4 $ on $Date: 86/11/23 17:17:15 $
24  *           $Source: /users/faustus/xchess/RCS/board.c,v $
25  * Copyright (c) 1986 Wayne A. Christopher, U. C. Berkeley CAD Group
26  *	Permission is granted to do anything with this code except sell it
27  *	or remove this message.
28  *
29  * Stuff to deal with the board.
30  */
31 
32 #include "xchess.h"
33 
34 board *chessboard;
35 
36 void
board_setup()37 board_setup()
38 {
39 	chessboard = alloc(board);
40 	board_init(chessboard);
41 	return;
42 }
43 
44 void
board_init(b)45 board_init(b)
46 	board *b;
47 {
48 	int i, j;
49 
50 	for (i = 0; i < 2; i++)
51 		for (j = 0; j < SIZE; j++)
52 			b->square[i][j].color = BLACK;
53 	for (i = 2; i < 6; i++)
54 		for (j = 0; j < SIZE; j++)
55 			b->square[i][j].color = NONE;
56 	for (i = 6; i < 8; i++)
57 		for (j = 0; j < SIZE; j++)
58 			b->square[i][j].color = WHITE;
59 	for (i = 0; i < SIZE; i++)
60 		b->square[1][i].type = b->square[6][i].type =
61 				PAWN;
62 	b->square[0][0].type = b->square[7][0].type = ROOK;
63 	b->square[0][1].type = b->square[7][1].type = KNIGHT;
64 	b->square[0][2].type = b->square[7][2].type = BISHOP;
65 	b->square[0][3].type = b->square[7][3].type = QUEEN;
66 	b->square[0][4].type = b->square[7][4].type = KING;
67 	b->square[0][5].type = b->square[7][5].type = BISHOP;
68 	b->square[0][6].type = b->square[7][6].type = KNIGHT;
69 	b->square[0][7].type = b->square[7][7].type = ROOK;
70 	b->black_cant_castle_k = false;
71 	b->black_cant_castle_q = false;
72 	b->white_cant_castle_k = false;
73 	b->white_cant_castle_q = false;
74 
75 	return;
76 }
77 
78 void
board_drawall()79 board_drawall()
80 {
81 	int i, j;
82 
83 	for (i = 0; i < SIZE; i++)
84 		for (j = 0; j < SIZE; j++)
85 			if (chessboard->square[i][j].color != NONE) {
86 				win_drawpiece(&chessboard->square[i][j], i,
87 						j, WHITE);
88 				if (!oneboard)
89 					win_drawpiece(&chessboard->square[i][j],
90 							i, j, BLACK);
91 			}
92 	return;
93 }
94 
95 void
board_move(b,m)96 board_move(b, m)
97 	board *b;
98 	move *m;
99 {
100 	switch (m->type) {
101 
102 	    case MOVE:
103 	    case CAPTURE:
104 		b->square[m->fromy][m->fromx].color = NONE;
105 		b->square[m->toy][m->tox].color = m->piece.color;
106 		b->square[m->toy][m->tox].type = m->piece.type;
107 		if ((m->piece.type == PAWN) && (((m->piece.color == BLACK) &&
108 				(m->toy == 7)) || ((m->piece.color == WHITE) &&
109 				(m->toy == 0))))
110 			b->square[m->toy][m->tox].type = QUEEN;
111 		if (m->enpassant)
112 			b->square[m->toy + ((m->piece.color == WHITE) ? 1 :
113 					-1)][m->tox].color = NONE;
114 		break;
115 
116 	    case KCASTLE:
117 		if (m->piece.color == WHITE) {
118 			b->square[7][5].color = m->piece.color;
119 			b->square[7][5].type = ROOK;
120 			b->square[7][6].color = m->piece.color;
121 			b->square[7][6].type = KING;
122 			b->square[7][4].color = NONE;
123 			b->square[7][7].color = NONE;
124 		} else {
125 			b->square[0][5].color = m->piece.color;
126 			b->square[0][5].type = ROOK;
127 			b->square[0][6].color = m->piece.color;
128 			b->square[0][6].type = KING;
129 			b->square[0][4].color = NONE;
130 			b->square[0][7].color = NONE;
131 		}
132 		break;
133 
134 	    case QCASTLE:
135 		if (m->piece.color == WHITE) {
136 			b->square[7][3].color = m->piece.color;
137 			b->square[7][3].type = ROOK;
138 			b->square[7][2].color = m->piece.color;
139 			b->square[7][2].type = KING;
140 			b->square[7][4].color = NONE;
141 			b->square[7][0].color = NONE;
142 		} else {
143 			b->square[0][3].color = m->piece.color;
144 			b->square[0][3].type = ROOK;
145 			b->square[0][2].color = m->piece.color;
146 			b->square[0][2].type = KING;
147 			b->square[0][4].color = NONE;
148 			b->square[0][0].color = NONE;
149 		}
150 		break;
151 
152 	    default:
153 		fprintf(stderr, "Bad move type %d\n", m->type);
154 	}
155 
156 	if (m->piece.type == KING) {
157 		if (m->piece.color == WHITE)
158 			b->white_cant_castle_q =
159 					b->white_cant_castle_k= true;
160 		else
161 			b->black_cant_castle_q =
162 					b->black_cant_castle_k= true;
163 	} else if (m->piece.type == ROOK) {
164 		if (m->piece.color == WHITE) {
165 			if (m->fromx == 0)
166 				b->white_cant_castle_q = true;
167 			else if (m->fromx == 7)
168 				b->white_cant_castle_k = true;
169 		} else {
170 			if (m->fromx == 0)
171 				b->black_cant_castle_q = true;
172 			else if (m->fromx == 7)
173 				b->black_cant_castle_k = true;
174 		}
175 	}
176 
177 	return;
178 }
179 
180