summaryrefslogtreecommitdiff
path: root/chess.c
blob: 099c78c256aae6f88b6e0c6c9da32d791abfcba7 (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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vector.h"
#include "console.h"
#include "chesshelpers.h"

vectorp_t get_pawn_moves(int** board, vector_t position, int color) {
	vector_t* moves = malloc(6 * sizeof(vector_t));
	int moveCount = 0;
	int forwardDir = (color == 0 ? -1 : 1);
	vector_t move1 = init_vector(position.mx, position.my + forwardDir);
	if(in_board(move1) && is_empty(board, move1)) {
		moves[moveCount] = move1;
		moveCount++;
		if(position.my == (color == 0 ? 6 : 1)) {
			vector_t move2 = init_vector(position.mx, position.my + (forwardDir * 2));
			if(is_empty(board, move2)) {
				moves[moveCount] = move2;
				moveCount++;
			}
		}
	}
	vector_t captureL = init_vector(position.mx - 1, position.my + forwardDir);
	if(in_board(captureL) && is_color(board, captureL, !color)) {
		moves[moveCount] = captureL;
		moveCount++;
	}
	vector_t captureR = init_vector(position.mx + 1, position.my + forwardDir);
	if(in_board(captureR) && is_color(board, captureR, !color)) {
		moves[moveCount] = captureR;
		moveCount++;
	}
	return init_vectorp(moves, moveCount);
}
vectorp_t get_knight_moves(int** board, vector_t position, int color) {
	vector_t* moves = malloc(8 * sizeof(vector_t));
	int moveCount = 0;
	for(int i = 0; i < 8; i++) {
		int h = ((i == 0 || i == 1) ? 2 : (i == 2 || i == 3) ? -2 : (i == 4 || i == 5) ? 1 : -1);
		int v = ((i == 0 || i == 2) ? -1 : (i == 1 || i == 3) ? 1 : (i == 4 || i == 6) ? 2 : -2);
		vector_t move = init_vector(position.mx + h, position.my + v);
		if(in_board(move) && (is_empty(board, move) || is_color(board, move, !color))) {
			moves[moveCount] = move;
			moveCount++;
		}
	}
	return init_vectorp(moves, moveCount);
}
vectorp_t get_bishop_moves(int** board, vector_t position, int color) {
	vector_t* moves = malloc(14 * sizeof(vector_t));
	int moveCount = 0;
	for(int i = 0; i < 4; i++) {
		int h = i < 2 ? 1 : -1;
		int v = (i == 0 || i == 2) ? 1 : -1;
		vector_t checkPos = init_vector(position.mx, position.my);
		for(int d = 0; d < 7; d++) {
			checkPos.mx += h;
			checkPos.my += v;
			if(in_board(checkPos)) {
				if(is_empty(board, checkPos)) {
					moves[moveCount] = init_vector(checkPos.mx, checkPos.my);
					moveCount++;
				}
				else if(is_color(board, checkPos, !color)) {
					moves[moveCount] = init_vector(checkPos.mx, checkPos.my);
					moveCount++;
					break;
				}
				else {
					break;
				}
			}
			else {
				break;
			}
		}
	}
	return init_vectorp(moves, moveCount);
}
vectorp_t get_rook_moves(int** board, vector_t position, int color) {
	vector_t* moves = malloc(14 * sizeof(vector_t));
	int moveCount = 0;
	for(int i = 0; i < 4; i++) {
		int h = (i == 0 || i == 2) ? 0 : i == 1 ? 1 : -1;
		int v = (i == 1 || i == 3) ? 0 : i == 2 ? 1 : -1;
		vector_t checkPos = init_vector(position.mx, position.my);
		for(int d = 0; d < 7; d++) {
			checkPos.mx += h;
			checkPos.my += v;
			if(in_board(checkPos)) {
				if(is_empty(board, checkPos)) {
					moves[moveCount] = init_vector(checkPos.mx, checkPos.my);
					moveCount++;
				}
				else if(is_color(board, checkPos, !color)) {
					moves[moveCount] = init_vector(checkPos.mx, checkPos.my);
					moveCount++;
					break;
				}
				else {
					break;
				}
			}
			else {
				break;
			}
		}
	}
	return init_vectorp(moves, moveCount);
}
vectorp_t get_queen_moves(int** board, vector_t position, int color) {
	vector_t* moves = malloc(28 * sizeof(vector_t));
	int moveCount = 0;
	for(int i = 0; i < 8; i++) {
		int h;
		int v;
		//diagonal moves
		if(i < 4) {
			h = i < 2 ? 1 : -1;
			v = (i == 0 || i == 2) ? 1 : -1;
		}
		//straight moves
		else {
			h = (i == 4 || i == 6) ? 0 : i == 5 ? 1 : -1;
			v = (i == 5 || i == 7) ? 0 : i == 6 ? 1 : -1;
		}
		vector_t checkPos = init_vector(position.mx, position.my);
		for(int d = 0; d < 7; d++) {
			checkPos.mx += h;
			checkPos.my += v;
			if(in_board(checkPos)) {
				if(is_empty(board, checkPos)) {
					moves[moveCount] = init_vector(checkPos.mx, checkPos.my);
					moveCount++;
				}
				else if(is_color(board, checkPos, !color)) {
					moves[moveCount] = init_vector(checkPos.mx, checkPos.my);
					moveCount++;
					break;
				}
				else {
					break;
				}
			}
			else {
				break;
			}
		}
	}
	return init_vectorp(moves, moveCount);
}
//this function returns moves that will place the player in check!
vectorp_t get_moves(int** board, vector_t position, piece_t piece) {
	int color = 0;
	if(piece > 6) {
		color = 1;
		piece -= 6;
	}
	if(piece == pawn) {
		return get_pawn_moves(board, position, color);
	}
	else if(piece == knight) {
		return get_knight_moves(board, position, color);
	}
	else if(piece == bishop) {
		return get_bishop_moves(board, position, color);
	}
	else if(piece == rook) {
		return get_rook_moves(board, position, color);
	}
	else if(piece == queen) {
		return get_queen_moves(board, position, color);
	}
	else {
		return init_vectorp(0, 0);
	}
}
//array of arrays representing all the moves each piece of color can make
vectorpp_t get_all_moves(int** board, int color) {
	vectorp_t* allMoves = malloc(16 * sizeof(vectorp_t));
	int pieceCount = 0;
	for(int r = 0; r < 8; r++) {
		for(int c = 0; c < 8; c++) {
			vector_t v = init_vector(c, r);
			if(is_color(board, v, color)) {
				allMoves[pieceCount] = get_moves(board, v, board[c][r]);
				pieceCount++;
			}
		}
	}
	allMoves = realloc(allMoves, pieceCount * sizeof(vectorp_t));
	return init_vectorpp(allMoves, pieceCount);
}
//returns the position of color's king
vector_t get_king_position(int** board, int color) {
	for(int r = 0; r < 8; r++) {
		for(int c = 0; c < 8; c++) {
			if(board[c][r] == (king + color * 6)) {
				return init_vector(c, r);
			}
		}
	}
}
//returns true if the player of color is currently in check
int is_in_check(int** board, int color) {
	int returnV = 0;
	vector_t kingPos = get_king_position(board, color);
	//get all the moves for the opponent
	vectorpp_t allMoves = get_all_moves(board, !color);
	for(int i = 0; i < allMoves.length; i++) {
		vectorp_t moves = allMoves.vectorps[i];
		for(int j = 0; j < moves.length; j++) {
			if(moves.vectors[j].mx == kingPos.mx && moves.vectors[j].my == kingPos.my) {
				returnV = 1;
			}
		}
		free(moves.vectors);
	}
	free(allMoves.vectorps);
	return returnV;
}
//this function removes moves that place you in check
vectorp_t get_moves_final(int** board, vector_t position, piece_t piece) {
	int color = 0;
	if(piece > 6) {
		color = 1;
	}
	vectorp_t moves = get_moves(board, position, piece);
	//try making that move, see if it puts the player in check
	int* invalidMoves = malloc(moves.length * sizeof(int));
	int invalidMovesCount = 0;
	for(int i = 0; i < moves.length; i++) {
		int** tempBoard = clone_board(board);
		tempBoard[moves.vectors[i].mx][moves.vectors[i].my] = piece;
		//remove the piece from where it was
		tempBoard[position.mx][position.my] = 0;
		if(is_in_check(tempBoard, color)) {
			invalidMoves[invalidMovesCount];
			invalidMovesCount++;
		}
		free(tempBoard);
	}
	vectorp_t finalMoves = remove_vectors(moves, invalidMoves, invalidMovesCount);
	free(invalidMoves);
	free(moves.vectors);
	return finalMoves;
}
int main() {
	//set up the board
	int boardA[8][8] = { 
		{ rook + 6, pawn + 6, 0, 0, 0, 0, pawn, rook }, 
		{ knight + 6, pawn + 6, 0, 0, 0, 0, pawn, knight },
		{ bishop + 6, pawn + 6, 0, 0, 0, 0, pawn, bishop },
		{ queen + 6, pawn + 6, 0, 0, 0, 0, pawn, queen },
		{ king + 6, pawn + 6, 0, 0, 0, 0, pawn, king },
		{ bishop + 6, pawn + 6, 0, 0, 0, 0, pawn, bishop },
		{ knight + 6, pawn + 6, 0, 0, 0, 0, pawn, knight },
		{ rook + 6, pawn + 6, 0, 0, 0, 0, pawn, rook }
	}; 
	//convert the array to pointers
	int** board = malloc(8 * sizeof(int*));
	for(int r = 0; r < 8; r++) {
		board[r] = malloc(8 * sizeof(int));
		for(int c = 0; c < 8; c++) {
			board[r][c] = boardA[r][c];
		}
	}
	int turn = 0;
	while(1) {
		//draw the board, write some info
		clear_screen();
		draw_board(board);
		printf(turn == 0 ? "\nWhite's turn\n" : "\nBlack's turn\n");
		if(is_in_check(board, turn)) {
			printf("You're in check!\n");
		}
		printf("Enter the coordinates of the piece you want to move\n");
		//get the selection for the piece to be moved
		vector_t selection = user_input_pos();
		int selectedPiece = board[selection.mx][selection.my];
		//check if the piece is of the right colour, and that there is a piece there
		if(selectedPiece == 0 || (selectedPiece > 6 && turn == 0) || (selectedPiece <= 6 && turn == 1)) {
			warn_user("Invalid selection!");
			continue;
		}
		printf("Selected piece: ");
		printf("%d", selectedPiece);
		printf("\n");
		vectorp_t moves = get_moves_final(board, selection, selectedPiece);
		if(moves.length == 0) {
			warn_user("That piece has no valid moves!");
			continue;
		}
		printf("Valid moves: ");
		//write all the valid moves
		for(int i = 0; i < moves.length; i++) {
			char* readable_coords = get_move_name(board, selection, moves.vectors[i]);
			printf(readable_coords);
			free(readable_coords);
			printf("; ");
		}
		printf("\nEnter the coordinates you would like to move that piece to, or enter < to go back\n");
		//get the selection for the location to move the piece to
		vector_t target = user_input_pos();
		if(target.mx == -1) {
			continue;
		}
		//check if target is in list of valid moves
		int match = 0;
		for(int i = 0; i < moves.length; i++) {
			if(moves.vectors[i].mx == target.mx && moves.vectors[i].my == target.my) {
				match = 1;
				break;
			}
		}
		if(!match) {
			warn_user("Invalid move");
			continue;
		}
		board[selection.mx][selection.my] = 0;
		board[target.mx][target.my] = selectedPiece;
		printf("\n");
		turn = !turn;
	}	
	return 0;
}