-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmove_validation.c
187 lines (167 loc) · 6.61 KB
/
move_validation.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
#include <stdbool.h>
#include <stdlib.h>
#include "board.h"
#include "move_validation.h"
#include <stdio.h>
// my_move -> My current move that is being tested
// op_move oponents last move
bool is_valid_move_pawn_white(const Move *my_move, const Move *op_move) {
if (my_move->src.i >= my_move->dst.i) return false;
else if (my_move->dst.i - my_move->src.i == 1) {
if (my_move->src.j == my_move->dst.j) {
if (pieceat(&my_move->dst) != EMPTY) return false;
}
else if (abs(my_move->dst.j - my_move->src.j) == 1) {
if (my_move->src.i == 4 && pieceat(&op_move->dst) == PAWN_B && op_move->src.i == 6 && op_move->dst.i == 4 && my_move->dst.j == op_move->dst.j) { // En passant
setpiece(&op_move->dst, EMPTY);
return true;
}
if (pieceat(&my_move->dst) == EMPTY) return false;
}
else return false;
}
else if (my_move->src.i == 1) {
if (my_move->dst.i - my_move->src.i != 2 || my_move->src.j != my_move->dst.j || pieceat(&my_move->dst) != EMPTY || pieceatindex(my_move->dst.i - 1, my_move->dst.j) != EMPTY) return false;
}
else return false;
return true;
}
bool is_valid_move_pawn_black(const Move *my_move, const Move *op_move) {
if (my_move->src.i <= my_move->dst.i) return false;
else if (my_move->src.i - my_move->dst.i == 1) {
if (my_move->src.j == my_move->dst.j) {
if (pieceat(&my_move->dst) != EMPTY) return false;
}
else if (abs(my_move->dst.j - my_move->src.j) == 1) {
if (my_move->src.i == 3 && pieceat(&op_move->dst) == PAWN_W && op_move->src.i == 1 && op_move->dst.i == 3 && my_move->dst.j == op_move->dst.j) { // En passant
setpiece(&op_move->dst, EMPTY);
return true;
}
if (pieceat(&my_move->dst) == EMPTY) return false;
}
else return false;
}
else if (my_move->src.i == 6) {
if (my_move->src.i - my_move->dst.i != 2 || my_move->src.j != my_move->dst.j || pieceat(&my_move->dst) != EMPTY || pieceatindex(my_move->dst.i + 1, my_move->dst.j) != EMPTY) return false;
}
else return false;
return true;
}
bool is_valid_move_rook(const Move *my_move, const Move* op_move) {
int index;
if (my_move->dst.j != my_move->src.j) {
if (my_move->dst.i != my_move->src.i) return false;
if (my_move->dst.j > my_move->src.j) {
for (index = my_move->src.j + 1; index < my_move->dst.j; index++) {
if (pieceatindex(my_move->dst.i, index) != EMPTY) return false;
}
}
else {
for (index = my_move->dst.j + 1; index < my_move->src.j; index++) {
if (pieceatindex(my_move->dst.i, index) != EMPTY) return false;
}
}
}
else {
if (my_move->dst.i > my_move->src.i) {
for (index = my_move->src.i + 1; index < my_move->dst.i; index++) {
if (pieceatindex(index, my_move->dst.j) != EMPTY) return false;
}
}
else {
for (index = my_move->dst.i + 1; index < my_move->src.i; index++) {
if (pieceatindex(index, my_move->dst.j) != EMPTY) return false;
}
}
}
return true;
}
bool is_valid_move_knight(const Move *my_move, const Move *op_move) {
return abs(my_move->src.i - my_move->dst.i) + abs(my_move->src.j - my_move->dst.j) == 3 && my_move->src.i != my_move->dst.i && my_move->src.j != my_move->dst.j;
}
bool is_valid_move_bishop(const Move *my_move, const Move *op_move) {
int index;
if (abs(my_move->dst.i - my_move->src.i) != abs(my_move->dst.j - my_move->src.j)) return false;
else {
if (my_move->dst.i > my_move->src.i) {
if (my_move->dst.j > my_move->src.j) {
for (index = 1; index < my_move->dst.i - my_move->src.i; index++) {
if (pieceatindex(my_move->src.i + index, my_move->src.j + index) != EMPTY) return false;
}
}
else if (my_move->dst.j < my_move->src.j) {
for (index = 1; index < my_move->dst.i - my_move->src.i; index++) {
if (pieceatindex(my_move->src.i + index, my_move->src.j - index) != EMPTY) return false;
}
}
else return false;
}
else if (my_move->dst.i < my_move->src.i) {
if (my_move->dst.j > my_move->src.j) {
for (index = 1; index < my_move->src.i - my_move->dst.i; index++) {
if (pieceatindex(my_move->src.i - index, my_move->src.j + index) != EMPTY) return false;
}
}
else if (my_move->dst.j < my_move->src.j) {
for (index = 1; index < my_move->src.i - my_move->dst.i; index++) {
if (pieceatindex(my_move->src.i - index, my_move->src.j - index) != EMPTY) return false;
}
}
else return false;
}
else return false;
}
return true;
}
bool is_valid_move_queen(const Move *my_move, const Move *op_move) {
return is_valid_move_rook(my_move, op_move) || is_valid_move_bishop(my_move, op_move);
}
bool is_valid_move_king_white(const Move *my_move, const Move *op_move) {
if (my_move->src.i == 0 && my_move->src.j == 4 && my_move->dst.i == 0) {
if (my_move->dst.j == 2) {
if (has_moved(my_move->src)) return false;
if (pieceatindex(0, 1) != EMPTY || pieceatindex(0, 2) != EMPTY || pieceatindex(0, 3) != EMPTY) return false;
if (threatened_index(0, 2, BLACK, op_move) || threatened_index(0, 3, BLACK, op_move) || threatened_index(0, 4, BLACK, op_move)) return false;
setpiece_index(0, 0, EMPTY);
setpiece_index(0, 3, ROOK_W);
return true;
}
else if (my_move->dst.j == 6) {
if (has_moved(my_move->src)) return false;
if (pieceatindex(0, 6) != EMPTY || pieceatindex(0, 5) != EMPTY) return false;
if (threatened_index(0, 6, BLACK, op_move) || threatened_index(0, 4, BLACK, op_move)) return false;
setpiece_index(0, 7, EMPTY);
setpiece_index(0, 5, ROOK_W);
return true;
}
else return false;
}
else if (abs(my_move->dst.i - my_move->src.i) > 1 || abs(my_move->dst.j - my_move->src.j) > 1) return false;
return true;
}
bool is_valid_move_king_black(const Move *my_move, const Move *op_move) {
if (my_move->src.i == 7 && my_move->src.j == 4 && my_move->dst.i == 7) {
if (my_move->dst.j == 2) {
if (has_moved(my_move->src)) return false;
if (pieceatindex(7, 1) != EMPTY || pieceatindex(7, 2) != EMPTY || pieceatindex(7, 3) != EMPTY) return false;
if (threatened_index(7, 2, WHITE, op_move) || threatened_index(7, 3, WHITE, op_move) || threatened_index(7, 4, WHITE, op_move)) return false;
setpiece_index(7, 0, EMPTY);
setpiece_index(7, 3, ROOK_B);
return true;
}
else if (my_move->dst.j == 6) {
if (has_moved(my_move->src)) return false;
if (pieceatindex(7, 6) != EMPTY || pieceatindex(7, 5) != EMPTY) return false;
if (threatened_index(7, 6, WHITE, op_move) || threatened_index(7, 4, WHITE, op_move)) return false;
setpiece_index(7, 7, EMPTY);
setpiece_index(7, 5, ROOK_B);
return true;
}
else return false;
}
else if (abs(my_move->dst.i - my_move->src.i) > 1 || abs(my_move->dst.j - my_move->src.j) > 1) return false;
return true;
}
bool is_valid_move_empty(const Move * my_move, const Move *op_move) {
return false;
}