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
|
#include <stdio.h>
#include <stdlib.h>
typedef enum {
pawn = 1,
knight = 2,
bishop = 3,
rook = 4,
queen = 5,
king = 6,
} piece_t;
typedef struct Vector {
int mx;
int my;
} vector_t;
vector_t init_vector(int mx, int my) {
vector_t vector;
vector.mx = mx;
vector.my = my;
return vector;
}
typedef struct VectorP {
vector_t* vectors;
int length;
} vectorp_t;
vectorp_t init_vectorp(vector_t* vectors, int length) {
vectorp_t vectorP;
vectorP.vectors = vectors;
vectorP.length = length;
return vectorP;
}
void draw_board(int board[8][8]) {
printf(" ABCDEFGH\n");
for(int r = 0; r < 8; r++) {
char* line = calloc(10, sizeof(char));
line[0] = '8' - r;
line[1] = ' ';
for(int c = 0; c < 8; c++) {
if(board[c][r] <= 6) {
switch(board[c][r]) {
case 0:
line[c + 2] = ' ';
break;
case pawn:
line[c + 2] = 'P';
break;
case knight:
line[c + 2] = 'N';
break;
case bishop:
line[c + 2] = 'B';
break;
case rook:
line[c + 2] = 'R';
break;
case queen:
line[c + 2] = 'Q';
break;
case king:
line[c + 2] = 'K';
break;
}
}
else {
switch(board[c][r] - 6) {
case 0:
line[c + 2] = ' ';
break;
case pawn:
line[c + 2] = 'p';
break;
case knight:
line[c + 2] = 'n';
break;
case bishop:
line[c + 2] = 'b';
break;
case rook:
line[c + 2] = 'r';
break;
case queen:
line[c + 2] = 'q';
break;
case king:
line[c + 2] = 'k';
break;
}
}
}
printf(line);
printf("\n");
free(line);
}
}
int in_board(vector_t position) {
return position.mx < 8 && position.mx >= 0 && position.my < 8 && position.my >= 0;
}
int is_empty(int board[8][8], vector_t position) {
return board[position.mx][position.my] == 0;
}
int get_owner(int board[8][8], vector_t position) {
}
int
vectorp_t get_moves(int board[8][8], vector_t position, piece_t piece) {
vector_t* moves = malloc(27 * sizeof(vector_t));
int moveCount = 0;
if(piece == pawn) {
vector_t move1 = init_vector(position.mx, position.my - 1);
if(in_board(move1) && is_empty(board, move1)) {
moves[0] = move1;
moveCount++;
if(position.my == 6) {
vector_t move2 = init_vector(position.mx, position.my - 2);
if(is_empty(board, move2)) {
moves[1] = move2;
moveCount++;
}
}
}
vector_t captureL = init_vector(position.mx - 1, position.my - 1);
if(in_board(captureL) &&
return init_vectorp(moves, moveCount);
}
else if(piece == pawn + 6) {
vector_t move1 = init_vector(position.mx, position.my + 1);
if(in_board(move1) && is_empty(board, move1)) {
moves[0] = move1;
moveCount++;
if(position.my == 1) {
vector_t move2 = init_vector(position.mx, position.my + 2);
if(is_empty(board, move2)) {
moves[1] = move2;
moveCount++;
}
}
}
return init_vectorp(moves, moveCount);
}
}
vector_t user_input_pos() {
char* inputString = malloc(3 * sizeof(char));
vector_t pos;
printf("Enter coordinates: ");
scanf("%2s", inputString);
for(int i = 0; i < 2; i++) {
if(inputString[i] <= '9' && inputString[i] >= '0') {
pos.my = 8 - (inputString[i] - '0');
}
else if(inputString[i] <= 'Z' && inputString[i] >= 'A') {
pos.mx = inputString[i] - 'A';
}
else if(inputString[i] <= 'z' && inputString[i] >= 'a') {
pos.mx = inputString[i] - 'a';
}
else {
return init_vector(-1, -1);
}
}
free(inputString);
return pos;
}
//converts board coordinates into algebraic notation
char* get_readable_coords(vector_t position) {
char* output = malloc(3 * sizeof(char));
output[0] = 'A' + position.mx;
output[1] = '8' - position.my;
output[2] = '\0';
return output;
}
void clear_screen() {
system("@cls||clear");
}
void warn_user(char* message) {
clear_screen();
printf("\n");
printf(message);
printf("\nPress any key to continue...\n");
while(getchar() != '\n');
getchar();
}
int main() {
int board[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 }
};
int turn = 0;
while(1) {
//draw the board, write some info
clear_screen(board);
draw_board(board);
printf(turn == 0 ? "\nWhite's turn\n" : "\nBlack's turn\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(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_readable_coords(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;
}
|