diff options
author | boa <boa@pm.me> | 2021-01-31 14:26:34 +0100 |
---|---|---|
committer | boa <boa@pm.me> | 2021-01-31 14:26:34 +0100 |
commit | dc3f15b4d3a48233c639c657464d3307c8d4b58a (patch) | |
tree | 215fa94c3afbb127f4cbb3308adeb1db24083644 | |
parent | ec3298ffdd3eab827154bf336a0b5607e8c5f303 (diff) | |
download | ajedrez-dc3f15b4d3a48233c639c657464d3307c8d4b58a.tar.gz |
colors
-rwxr-xr-x | chess | bin | 22000 -> 22000 bytes | |||
-rw-r--r-- | chess.c | 71 |
2 files changed, 49 insertions, 22 deletions
Binary files differ @@ -11,7 +11,7 @@ dprintf(b_fd, (f_), ##__VA_ARGS__); \ } while (0) -typedef enum color { c_w = 31, c_b = 34 } color; +typedef enum color { c_w = 39, c_b = 30 } color; int abs(int a) { if (a >= 0) @@ -38,35 +38,58 @@ typedef struct piece { piece table[8][8]; int w_fd = -1, b_fd = -1; int *c_fd = &w_fd; // current fd +bool w_moved_king = false, b_moved_king = false; void print_table(); void setup_table(); void play_game(); void print_table() { - PRINT_ALL(" \x1B[31mWHITE\x1B[0m\n |"); - for (int x = 0; x < 8; x++) - PRINT_ALL("%c", 'a' + x); - PRINT_ALL("|\n-+"); - for (int x = 0; x < 8; x++) - PRINT_ALL("-"); - PRINT_ALL("+-\n"); - for (int y = 0; y < 8; y++) { - PRINT_ALL("%d|", y + 1); + 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) { + dprintf(w_fd, "\x1B[0m " + "\e[43m|---+---+---+---+---+---+---+---|\x1B[0m\n"); + } + dprintf(w_fd, " %d \e[43m|", y + 1); for (int x = 0; x < 8; x++) if (table[x][y].p) - PRINT_ALL("\x1B[%dm%c\x1B[0m", table[x][y].c, table[x][y].p); + dprintf(w_fd, "\e[43m\x1B[%dm \033[1m%c \x1B[0m\e[43m|", table[x][y].c, + table[x][y].p); else - PRINT_ALL("."); - PRINT_ALL("|%d\n", y + 1); + dprintf(w_fd, " |"); + dprintf(w_fd, "\x1B[0m %d\x1B[0m\n", y + 1); } - PRINT_ALL("-+"); - for (int x = 0; x < 8; x++) - PRINT_ALL("-"); - PRINT_ALL("+-\n |"); - for (int x = 0; x < 8; x++) - PRINT_ALL("%c", 'a' + x); - PRINT_ALL("|\n \x1B[34mBLACK\n\x1B[0m"); + for (int y = 0; y < 8; y++) { + if (y != 0) { + dprintf(b_fd, "\x1B[0m " + "\e[43m|---+---+---+---+---+---+---+---|\x1B[0m\n"); + } + dprintf(b_fd, " %d \e[43m|", y + 1); + for (int x = 7; x >= 0; x--) + if (table[x][y].p) + dprintf(b_fd, "\e[43m\x1B[%dm \033[1m%c \x1B[0m\e[43m|", table[x][y].c, + table[x][y].p); + else + 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); + } + + dprintf(w_fd, + "\n\n \e[43m\x1B[39m\033[1m WHITE \x1B[0m\n\n\x1B[0m"); + dprintf(b_fd, + "\n\n \e[43m\x1B[30m\033[1m BLACK \x1B[0m\n\n\x1B[0m"); } void setup_table() { @@ -111,7 +134,8 @@ int is_valid(int from_x, int from_y, int to_x, int to_y) { (p.c != c_w || from_y != 1 || table[from_x][from_y + 1].p != p_no)) || (dy == -1 && p.c != c_b) || (dy == -2 && - (p.c != c_b || from_y != 6 || table[from_x][from_y - 1].p != p_no))) + (p.c != c_b || from_y != 6 || table[from_x][from_y - 1].p != p_no)) || + (dx == 0 && table[to_x][to_y].p != p_no)) return 0; break; case p_rook: @@ -143,6 +167,7 @@ int is_valid(int from_x, int from_y, int to_x, int to_y) { return 0; break; case p_king: + printf("%d %d\n", dx, dy); if (abs(dx) > 1 || abs(dy) > 1) return 0; break; @@ -174,7 +199,8 @@ void play_game() { int to_y = buf[4] - '1'; 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 || table[to_x][to_y].c == c_c || + table[from_x][from_y].c != c_c || + (table[to_x][to_y].c == c_c && table[to_x][to_y].p != p_no) || !is_valid(from_x, from_y, to_x, to_y)) { dprintf(*c_fd, "hiba: %d %d %d %d\n", from_x, from_y, to_x, to_y); continue; @@ -201,6 +227,7 @@ void play_game() { int main() { setup_table(); + print_table(); // stolen from: https://git.sr.ht/~martijnbraam/among-sus fd_set rfds, afds; uint16_t port = 1234; |