aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorboa <boa@pm.me>2021-02-03 12:36:09 +0100
committerboa <boa@pm.me>2021-02-03 12:36:09 +0100
commitd52937530d951a9c9a7c5fe00057837c7d9ce131 (patch)
tree7e4537f435e97c9f68bcfc95309c1a6aef0a9553
parent556c74808482748cb393e4f34bdfbc989dc2f8d9 (diff)
downloadajedrez-d52937530d951a9c9a7c5fe00057837c7d9ce131.tar.gz
fix castle
-rwxr-xr-xsakkombin27128 -> 27112 bytes
-rw-r--r--sakkom.c72
2 files changed, 56 insertions, 16 deletions
diff --git a/sakkom b/sakkom
index 9fba27f..2c34001 100755
--- a/sakkom
+++ b/sakkom
Binary files differ
diff --git a/sakkom.c b/sakkom.c
index 47387af..e470efc 100644
--- a/sakkom.c
+++ b/sakkom.c
@@ -23,6 +23,8 @@
typedef enum color { c_w = 0, c_b = 1 } color;
+typedef enum castle { castle_no, castle_long, castle_short } castle;
+
int colors[] = {39, 30};
int abs(int a) {
@@ -98,10 +100,12 @@ bool is_move_inside(move m) {
void print_table() {
dprintf(w_fd, "\n \e[43m\x1B[30m\033[1m BLACK \x1B[0m\n\n ");
dprintf(b_fd, "\n \e[43m\x1B[39m\033[1m WHITE \x1B[0m\n\n ");
+
for (int x = 0; x < 8; x++) {
dprintf(w_fd, " %c ", 'A' + x);
dprintf(b_fd, " %c ", 'A' + 7 - x);
}
+
PRINT_ALL("\n \e[43m+-------------------------------+\x1B[0m\n");
for (int y = 7; y >= 0; y--) {
if (y != 7) {
@@ -117,6 +121,7 @@ void print_table() {
dprintf(w_fd, " |");
dprintf(w_fd, "\x1B[0m %d\x1B[0m\n", y + 1);
}
+
for (int y = 0; y < 8; y++) {
if (y != 0) {
dprintf(b_fd, "\x1B[0m "
@@ -131,7 +136,9 @@ void print_table() {
dprintf(b_fd, " |");
dprintf(b_fd, "\x1B[0m %d\n", y + 1);
}
+
PRINT_ALL(" \e[43m+-------------------------------+\x1B[0m\n ");
+
for (int x = 0; x < 8; x++) {
dprintf(b_fd, " %c ", 'A' + 7 - x);
dprintf(w_fd, " %c ", 'A' + x);
@@ -156,7 +163,6 @@ bool in_check(color c, int cx, int cy) {
m.f.y = y;
m.p = table[x][y];
if (table[x][y].c != c && is_move_legal(m.p.c, m)) {
- printf("true check\n");
return true;
}
}
@@ -294,6 +300,29 @@ move parse_move(char buf[50]) {
return m;
}
+castle is_castle(color c, move m) {
+ int dx = m.t.x - m.f.x, dy = m.t.y - m.f.y;
+ if (m.p.p == p_king) {
+ if (!moved_king[c] && dy == 0) {
+ if (!moved_rook_l[c] && dx == -2 && table[m.f.x - 1][m.f.y].p == p_no &&
+ table[m.f.x - 3][m.f.y].p == p_rook &&
+ table[m.f.x - 3][m.f.y].c == c && !in_check(c, m.f.x - 1, m.f.y) &&
+ !in_check(c, m.f.x, m.f.y)) {
+ return castle_short;
+ } else if (!moved_rook_r[c] && dx == 3 &&
+ table[m.f.x + 1][m.f.y].p == p_no &&
+ table[m.f.x + 2][m.f.y].p == p_no &&
+ table[m.f.x + 4][m.f.y].p == p_rook &&
+ table[m.f.x + 4][m.f.y].c == c &&
+ !in_check(c, m.f.x + 1, m.f.y) &&
+ !in_check(c, m.f.x + 2, m.f.y) && !in_check(c, m.f.x, m.f.y)) {
+ return castle_long;
+ }
+ }
+ }
+ return castle_no;
+}
+
bool is_move_legal(color c, move m) {
if (!is_move_inside(m) || is_empty(m.p) || is_enemy_of(c, m.p) ||
is_friend_of(c, m.e))
@@ -301,8 +330,6 @@ bool is_move_legal(color c, move m) {
int dx = m.t.x - m.f.x, dy = m.t.y - m.f.y;
int v_x, v_y, l;
- if (in_check_sim(m))
- return false;
switch (m.p.p) {
case p_pawn:
if ((dx != 0 && ((dx != 1 && dx != -1) || (dy != 1 && c == c_w) ||
@@ -325,10 +352,6 @@ bool is_move_legal(color c, move m) {
for (int n = 1; n < abs(dx + dy); n++)
if (!is_empty(table[m.f.x + n * v_x][m.f.y + n * v_y]))
return false;
- if ((m.f.x == 7 && m.f.y == 0) || (m.f.x == 7 && m.f.y == 7))
- moved_rook_r[c] = true;
- if ((m.f.x == 0 && m.f.y == 0) || (m.f.x == 0 && m.f.y == 7))
- moved_rook_l[c] = true;
break;
case p_bishop:
if (abs(dx) != abs(dy))
@@ -350,19 +373,18 @@ bool is_move_legal(color c, move m) {
return false;
break;
case p_king:
+ printf("dx: %d dy: %d\n", dx, dy);
if (abs(dx) > 3 || abs(dy) > 1)
- return 0;
+ return false;
if (in_check(c, m.t.x, m.t.y))
- return 0;
+ return false;
if (abs(dx) > 1) {
if (!moved_king[c] && dy == 0) {
if (!moved_rook_l[c] && dx == -2 && table[m.f.x - 1][m.f.y].p == p_no &&
table[m.f.x - 3][m.f.y].p == p_rook &&
table[m.f.x - 3][m.f.y].c == c && !in_check(c, m.f.x - 1, m.f.y) &&
!in_check(c, m.f.x, m.f.y)) {
- table[m.f.x - 1][m.f.y] = table[m.f.x - 3][m.f.y];
- table[m.f.x - 3][m.f.y].p = p_no;
- break;
+ return true;
} else if (!moved_rook_r[c] && dx == 3 &&
table[m.f.x + 1][m.f.y].p == p_no &&
table[m.f.x + 2][m.f.y].p == p_no &&
@@ -371,14 +393,11 @@ bool is_move_legal(color c, move m) {
!in_check(c, m.f.x + 1, m.f.y) &&
!in_check(c, m.f.x + 2, m.f.y) &&
!in_check(c, m.f.x, m.f.y)) {
- table[m.f.x + 2][m.f.y] = table[m.f.x + 4][m.f.y];
- table[m.f.x + 4][m.f.y].p = p_no;
- break;
+ return true;
}
}
return false;
}
- moved_king[c] = true;
break;
case p_knight:
if (!((abs(dx) == 2 && abs(dy) == 1) || (abs(dx) == 1 && abs(dy) == 2)))
@@ -388,6 +407,8 @@ bool is_move_legal(color c, move m) {
return false;
break;
}
+ if (in_check_sim(m))
+ return false;
return true;
}
@@ -488,6 +509,24 @@ void play_game() {
print_move(m);
do_move(m);
+ castle ct = is_castle(c_c, m);
+ if (ct == castle_short) {
+ table[m.f.x - 1][m.f.y] = table[m.f.x - 3][m.f.y];
+ table[m.f.x - 3][m.f.y].p = p_no;
+ } else if (ct == castle_long) {
+ table[m.f.x + 2][m.f.y] = table[m.f.x + 4][m.f.y];
+ table[m.f.x + 4][m.f.y].p = p_no;
+ }
+
+ if (m.p.p == p_rook &&
+ ((m.f.x == 7 && m.f.y == 0) || (m.f.x == 7 && m.f.y == 7)))
+ moved_rook_r[c_c] = true;
+ else if (m.p.p == p_rook &&
+ ((m.f.x == 0 && m.f.y == 0) || (m.f.x == 0 && m.f.y == 7)))
+ moved_rook_l[c_c] = true;
+ else if (m.p.p == p_king)
+ moved_king[c_c] = true;
+
// en passant
if (enp.valid && is_same_pos(enp.p, m.t) && m.p.p == p_pawn)
enp.to_remove->p = p_no;
@@ -507,6 +546,7 @@ void play_game() {
dprintf(*c_fd, "hiba\n");
goto no_print;
}
+
skip:
if (c_fd == &w_fd) {
c_fd = &b_fd;
Un proyecto texto-plano.xyz