diff options
author | boa <boa@pm.me> | 2021-01-31 04:05:45 +0100 |
---|---|---|
committer | boa <boa@pm.me> | 2021-01-31 04:05:45 +0100 |
commit | ec3298ffdd3eab827154bf336a0b5607e8c5f303 (patch) | |
tree | 8fbf12549796ed79d88d1431452dc01fc51755d4 | |
parent | 00d49540a61221a1da5dc58b7c782c22c88e8072 (diff) | |
download | ajedrez-ec3298ffdd3eab827154bf336a0b5607e8c5f303.tar.gz |
bug fixes, more validations
-rwxr-xr-x | chess | bin | 21968 -> 22000 bytes | |||
-rw-r--r-- | chess.c | 29 |
2 files changed, 23 insertions, 6 deletions
Binary files differ @@ -13,6 +13,13 @@ typedef enum color { c_w = 31, c_b = 34 } color; +int abs(int a) { + if (a >= 0) + return a; + else + return -1 * a; +} + typedef enum piece_type { p_no, p_pawn = 'P', @@ -92,11 +99,13 @@ int 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; + int v_x, v_y, l; switch (p.p) { case p_pawn: // TODO: refactor - if ((dx != 0 && (dx != 1 || dy != 1 || table[to_x][to_y].p == p_no)) || + 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)) || dy < -2 || dy > 2 || (dy == 1 && p.c != c_w) || (dy == 2 && (p.c != c_w || from_y != 1 || table[from_x][from_y + 1].p != p_no)) || @@ -108,9 +117,9 @@ int is_valid(int from_x, int from_y, int to_x, int to_y) { case p_rook: if (dx * dy != 0) return 0; - v_x = dx ? ((dx > 0) ? 1 : -1) : 0; - v_y = dy ? ((dy > 0) ? 1 : -1) : 0; - for (int n = 1; n < abs(dx); n++) + v_x = (dx != 0) ? ((dx > 0) ? 1 : -1) : 0; + v_y = (dy != 0) ? ((dy > 0) ? 1 : -1) : 0; + 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 0; break; @@ -128,7 +137,8 @@ int is_valid(int from_x, int from_y, int to_x, int to_y) { return 0; v_x = dx ? ((dx > 0) ? 1 : -1) : 0; v_y = dy ? ((dy > 0) ? 1 : -1) : 0; - for (int n = 1; n < abs(dx); n++) + l = (dx * dy == 0) ? abs(dx + dy) : abs(dx); + for (int n = 1; n < l; n++) if (table[from_x + n * v_x][from_y + n * v_y].p != p_no) return 0; break; @@ -169,6 +179,13 @@ void play_game() { dprintf(*c_fd, "hiba: %d %d %d %d\n", from_x, from_y, to_x, to_y); continue; } else { + if (table[to_x][to_y].p == p_king) { + if (c_c == c_w) + PRINT_ALL("a fehér nyert!"); + else + PRINT_ALL("a fekete nyert!"); + break; + } table[to_x][to_y] = table[from_x][from_y]; table[from_x][from_y] = (piece){p_no, c_w}; } |