-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
259 lines (236 loc) · 8.14 KB
/
main.c
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
#include <stdio.h>
#include "ChessPiece.h"
#include "ChessBoard.h"
#include "ChessGame.h"
int main(int argc, char **argv) {
ChessPiece chessPieces[32];
int chessBoard[8][8];
create_chessPieces(chessPieces);
print_out_chessPieces(chessPieces);
create_chessBoard(chessBoard, chessPieces);
// ----------------------------------------------
int moves = 0;
bool isWhitePlayerTurn = true;
char playerInput[MAX_INPUT];
bool success;
for (;;) {
print_out_chessBoard(chessBoard, chessPieces);
printf("Player %s turn. Moves made %d\n", (isWhitePlayerTurn ? "1 (White)" : "2 (Black)"), moves);
printf("Your move: ");
fgets(playerInput, sizeof(stdin), stdin);
success = handle_player_input(playerInput, chessBoard, chessPieces);
if (success) {
moves++;
isWhitePlayerTurn = !isWhitePlayerTurn;
}
else
printf("Error in player input!\n");
}
return 0;
}
bool handle_player_input(char playerInput[MAX_INPUT], int chessBoard[8][8], ChessPiece chessPieces[32]) {
/*
Yeah, nah... pretty dank logic
y - - - - - - - >
x
| -x -y -x +y
| \ /
| P
| / \
| +x -y +x +y
|
v
*/
int x1 = playerInput[0] - '0';
int y1 = playerInput[1] - '0';
char sp = playerInput[2];
int x2 = playerInput[3] - '0';
int y2 = playerInput[4] - '0';
if (sp != ' ') return false;
if (x1 < 0 || y1 < 0 || x2 < 0 || y2 < 0) return false;
if (x1 > 7 || y1 > 7 || x2 > 7 || y2 > 7) return false;
if (x1 == x2 && y1 == y2) return false;
int move_piece = chessBoard[x1][y1];
int place_piece = chessBoard[x2][y2];
if (move_piece == -1) return false;
if (place_piece != -1 && chessPieces[move_piece].isWhite == chessPieces[place_piece].isWhite) return false;
if (chessPieces[move_piece].type == PAWN) {
if (chessPieces[move_piece].isWhite) {
if (x1 > x2) {
printf ("Pawn cannot move backwards\n");
return false;
}
if (((x2 - x1) > 1 && (x2 - x1) != 2) ||
((x2 - x1) == 2 && x1 != 1)) {
printf("Pawn cannot move more than one place\n");
return false;
}
if ((y1 != y2 && (x2 - x1) != 1) ||
(y1 < y2 && (y2 - y1) > 1) ||
(y2 < y1 && (y1 - y2) > 1)) {
printf("Pawn can only move one diagonaly\n");
return false;
}
if ((place_piece != -1 && y1 == y2) ||
((x2 - x1) == 2 && chessBoard[2][y1] != -1)) {
printf("Pawn cannot take/overtake a piece in front of it\n");
return false;
}
if (place_piece == -1 && y1 != y2) {
printf("Pawn can only take another piece on the diagonal\n");
return false;
}
if (x2 == 7) {
printf("Pawn just became a Queen!\n");
chessPieces[move_piece].type = QUEEN;
}
}
else { // is Black
if (x2 > x1) {
printf("Pawn cannot move backwards\n");
return false;
}
if (((x1 - x2) > 1 && (x1 - x2) != 2) ||
((x1 - x2) == 2 && x1 != 6)) {
printf("Pawn cannot move more than one place\n");
return false;
}
if ((y1 != y2 && (x1 - x2) != 1) ||
(y1 < y2 && (y2 - y1) > 1) ||
(y2 < y1 && (y1 - y2) > 1)) {
printf("Pawn can only move one diagonaly\n");
return false;
}
if ((place_piece != -1 && y1 == y2) ||
((x1 - x2) == 2 && chessBoard[5][y1] != -1)) {
printf("Pawn cannot take/overtake a piece in front of it\n");
return false;
}
if (place_piece == -1 && y1 != y2) {
printf("Pawn can only take another piece on the diagonal\n");
return false;
}
if (x2 == 0) {
printf("Pawn just became a Queen!\n");
chessPieces[move_piece].type = QUEEN;
}
}
}
else if (chessPieces[move_piece].type == ROOK) {
if (x1 != x2 && y1 != y2) {
printf("Rook can only move in a straight line\n");
return false;
}
if (x1 != x2) {
if (x1 < x2) {
for (int i = (x1 + 1); i < x2; i++) {
if (chessBoard[i][y1] != -1) {
printf("Rook cannot overtake another piece\n");
return false;
}
}
}
else { // x2 > x1
for (int i = (x1 - 1); i > x2; i--) {
if (chessBoard[i][y1] != -1) {
printf("Rook cannot overtake another piece\n");
return false;
}
}
}
}
else { // y1 != y2
if (y1 < y2) {
for (int i = (y1 + 1); i < y2; i++) {
if (chessBoard[x1][i] != -1) {
printf("Rook cannot overtake another piece\n");
return false;
}
}
}
else { // y2 < y1
for (int i = (y1 - 1); i > y2; i--) {
if (chessBoard[x1][i] != -1) {
printf("Rook cannot overtake another piece\n");
return false;
}
}
}
}
}
else if (chessPieces[move_piece].type == KNIGHT) {
printf("Knight is not implemented yet, will not move\n");
return false;
}
else if (chessPieces[move_piece].type == BISHOP) {
int x_diff = 0;
int y_diff = 0;
bool x_positive;
bool y_positive;
if (x1 < x2) {
x_diff = x2 - x1;
x_positive = true;
}
else { // x1 > x2
x_diff = x1 - x2;
x_positive = false;
}
if (y1 < y2) {
y_diff = y2 - y1;
y_positive = true;
}
else { // y1 > y2
y_diff = y1 - y2;
y_positive = false;
}
if (x_diff != y_diff) {
printf("Bishop can only move diagonaly\n");
return false;
}
if (x_positive && !y_positive) {
for (int xc = (x1 + 1), yc = (y1 - 1); xc < x2; xc++, yc--) {
if (chessBoard[xc][yc] != -1) {
printf("Bishop cannot overtake another piece\n");
return false;
}
}
}
else if (x_positive && y_positive) {
for (int xc = (x1 + 1), yc = (y1 + 1); xc < x2; xc++, yc++) {
if (chessBoard[xc][yc] != -1) {
printf("Bishop cannot overtake another piece\n");
return false;
}
}
}
else if (!x_positive && y_positive) {
for (int xc = (x1 - 1), yc = (y1 + 1); xc > x2; xc--, yc++) {
if (chessBoard[xc][yc] != -1) {
printf("Bishop cannot overtake another piece\n");
return false;
}
}
}
else { // !x_positive && !y_positive
for (int xc = (x1 - 1), yc = (y1 - 1); xc > x2; xc--, yc--) {
if (chessBoard[xc][yc] != -1) {
printf("Bishop cannot overtake another piece\n");
return false;
}
}
}
}
else if (chessPieces[move_piece].type == QUEEN) {
printf("Queen is not implemented yet, will not move\n");
return false;
}
else { // is King
printf("King is not implemented yet, will not move\n");
return false;
}
chessBoard[x1][y1] = -1;
chessBoard[x2][y2] = move_piece;
if (place_piece != -1) chessPieces[place_piece].isAlive = false;
//print_out_chessPieces(chessPieces);
return true;
}