diff options
author | boa <boa@pm.me> | 2021-01-31 22:09:04 +0100 |
---|---|---|
committer | boa <boa@pm.me> | 2021-01-31 22:09:04 +0100 |
commit | 9654b87ee25af52ae957a339e5053dabd45e4e9f (patch) | |
tree | cd2db2e20412bf18342d0d41ccceb11fd62d5478 | |
parent | 983978ecd478ee13be2b9c423d7a3fd2f9457f24 (diff) | |
download | ajedrez-9654b87ee25af52ae957a339e5053dabd45e4e9f.tar.gz |
this should work
-rwxr-xr-x | chess | bin | 22064 -> 26312 bytes | |||
-rw-r--r-- | chess.c | 87 |
2 files changed, 83 insertions, 4 deletions
Binary files differ @@ -1,3 +1,5 @@ +// do what the fuck you want + #include <arpa/inet.h> #include <stdbool.h> #include <stdio.h> @@ -40,6 +42,8 @@ piece table[8][8]; int w_fd = -1, b_fd = -1; int *c_fd = &w_fd; // current fd bool moved_king[2] = {false, false}; +bool moved_rook_l[2] = {false, false}; +bool moved_rook_r[2] = {false, false}; bool is_valid(); void print_table(); @@ -105,6 +109,25 @@ bool in_check(color c, int cx, int cy) { return false; } +bool in_check_sim(int from_x, int from_y, int to_x, int to_y) { + piece tmp = table[to_x][to_y]; + table[to_x][to_y] = table[from_x][from_y]; + table[from_x][from_y].p = p_no; + bool res = false; + for (int y = 0; y < 8; y++) { + for (int x = 0; x < 8; x++) { + if (table[x][y].p == p_king && table[x][y].c == table[to_x][to_y].c) { + res = in_check(table[x][y].c, x, y); + goto finish; + } + } + } +finish: + table[from_x][from_y] = table[to_x][to_y]; + table[to_x][to_y] = tmp; + return res; +} + void setup_table() { table[0][0] = (piece){p_rook, c_w}; table[1][0] = (piece){p_knight, c_w}; @@ -131,14 +154,57 @@ void setup_table() { int is_inside(int i) { return i >= 0 && i < 8; } +bool is_checkmate(color c) { + int kx = -1, ky = -1; + for (int y = 0; y < 8; y++) { + for (int x = 0; x < 8; x++) { + if (table[x][y].p == p_king && table[x][y].c == c) { + kx = x; + ky = y; + } + } + } + if (!in_check(c, kx, ky)) { + return false; + } + for (int y = -1; y <= 1; y++) { + for (int x = -1; x <= 1; x++) { + if (x == 0 && y == 0) + continue; + if (is_valid(kx, ky, kx + x, ky + y) && !in_check(c, kx + x, ky + y)) { + return false; + } + } + } + for (int y = 0; y < 8; y++) { + for (int x = 0; x < 8; x++) { + if (table[x][y].c != c && is_valid(x, y, kx, ky)) { + int dx = kx - x, dy = ky - y; + if (table[x][y].p == p_rook || table[x][y].p == p_queen || + table[x][y].p == p_bishop) { + int v_x = dx ? ((dx > 0) ? 1 : -1) : 0; + int v_y = dy ? ((dy > 0) ? 1 : -1) : 0; + for (int n = 1; n < abs(dx); n++) + if (table[x + n * v_x][y + n * v_y].p == p_no) + if (in_check(!c, x + n * v_x, y + n * v_y)) + return false; + } else if (table[x][y].p == p_rook) { + } + } + } + } + return true; +} + bool is_valid(int from_x, int from_y, int to_x, int to_y) { - // TODO: sáncolás piece p = table[from_x][from_y]; int dx = to_x - from_x, dy = to_y - from_y; int v_x, v_y, l; + if (in_check_sim(from_x, from_y, to_x, to_y)) + return 0; switch (p.p) { case p_pawn: - // TODO: refactor + // TODO: refactor, en passant if ((dx != 0 && ((dx != 1 && dx != -1) || (dy != 1 && p.c == c_w) || (dy != -1 && p.c == c_b) || table[to_x][to_y].p == p_no)) || @@ -159,6 +225,10 @@ bool is_valid(int from_x, int from_y, int to_x, int to_y) { for (int n = 1; n < abs(dx + dy); n++) if (table[from_x + n * v_x][from_y + n * v_y].p != p_no) return false; + if ((from_x == 7 && from_y == 0) || (from_x == 7 && from_y == 7)) + moved_rook_r[p.c] = true; + if ((from_x == 0 && from_y == 0) || (from_x == 0 && from_y == 7)) + moved_rook_l[p.c] = true; break; case p_bishop: if (abs(dx) != abs(dy)) @@ -184,7 +254,8 @@ bool is_valid(int from_x, int from_y, int to_x, int to_y) { return 0; if (abs(dx) > 1 || abs(dy) > 1) { if (!moved_king[p.c] && dy == 0) { - if (dx == -2 && table[from_x - 1][from_y].p == p_no && + if (!moved_rook_l[p.c] && dx == -2 && + table[from_x - 1][from_y].p == p_no && table[from_x - 3][from_y].p == p_rook && table[from_x - 3][from_y].c == p.c && !in_check(p.c, from_x - 1, from_y) && @@ -192,7 +263,8 @@ bool is_valid(int from_x, int from_y, int to_x, int to_y) { table[from_x - 1][from_y] = table[from_x - 3][from_y]; table[from_x - 3][from_y].p = p_no; break; - } else if (dx == 3 && table[from_x + 1][from_y].p == p_no && + } else if (!moved_rook_r[p.c] && dx == 3 && + table[from_x + 1][from_y].p == p_no && table[from_x + 2][from_y].p == p_no && table[from_x + 4][from_y].p == p_rook && table[from_x + 4][from_y].c == p.c && @@ -242,6 +314,13 @@ void play_game() { int from_y = buf[1] - '1'; int to_x = buf[3] - 'a'; int to_y = buf[4] - '1'; + if (is_checkmate(c_c)) { + if (c_c == c_w) + PRINT_ALL("a fekete nyert!"); + else + PRINT_ALL("a fehér nyert!"); + break; + } if (!is_inside(from_x) || !is_inside(from_y) || !is_inside(to_x) || !is_inside(to_y) || table[from_x][from_y].p == p_no || table[from_x][from_y].c != c_c || |