diff options
Diffstat (limited to 'sakkom.c')
-rw-r--r-- | sakkom.c | 51 |
1 files changed, 43 insertions, 8 deletions
@@ -5,10 +5,13 @@ // boa #include <arpa/inet.h> +#include <pthread.h> +#include <signal.h> #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <string.h> +#include <sys/types.h> #include <time.h> #include <unistd.h> @@ -18,7 +21,9 @@ dprintf(b_fd, (f_), ##__VA_ARGS__); \ } while (0) -typedef enum color { c_w = 39, c_b = 30 } color; +typedef enum color { c_w = 0, c_b = 1 } color; + +int colors[] = {39, 30}; int abs(int a) { if (a >= 0) @@ -68,6 +73,7 @@ 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}; +int remaining_time[2] = {30 * 60, 30 * 60}; void do_move(move m); bool is_move_legal(color c, move m); @@ -105,8 +111,8 @@ void print_table() { dprintf(w_fd, " %d \e[43m|", y + 1); for (int x = 0; x < 8; x++) if (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); + dprintf(w_fd, "\e[43m\x1B[%dm \033[1m%c \x1B[0m\e[43m|", + colors[table[x][y].c], table[x][y].p); else dprintf(w_fd, " |"); dprintf(w_fd, "\x1B[0m %d\x1B[0m\n", y + 1); @@ -119,8 +125,8 @@ void print_table() { 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); + dprintf(b_fd, "\e[43m\x1B[%dm \033[1m%c \x1B[0m\e[43m|", + colors[table[x][y].c], table[x][y].p); else dprintf(b_fd, " |"); dprintf(b_fd, "\x1B[0m %d\n", y + 1); @@ -413,16 +419,42 @@ piece_type choose_type(int fd) { } } +color c_c; +bool is_printing = false; +void *run_timer(void *vargp) { + while (true) { + if (!is_printing) { + dprintf(*c_fd, "\033[s\r%02d:%02d > \033[u", remaining_time[c_c] / 60, + remaining_time[c_c] % 60); + remaining_time[c_c]--; + if (remaining_time[c_c] <= 0) { + if (c_c == c_w) + PRINT_ALL("fehérnek lejárt az ideje, fekete nyert!\n"); + else + PRINT_ALL("feketének lejárt az ideje, fehér nyert!\n"); + raise(SIGTERM); + } + } + sleep(1); + } + return NULL; +} + void play_game() { - color c_c = c_w; // current color + c_c = c_w; // current color + is_printing = true; + pthread_t thread_id; + pthread_create(&thread_id, NULL, run_timer, NULL); + for (;;) { + is_printing = true; print_table(); if (c_c == c_w) dprintf(b_fd, "fehér gondolkodik...\n"); else dprintf(w_fd, "fekete gondolkodik...\n"); no_print: - dprintf(*c_fd, "> "); + // dprintf(*c_fd, "> "); if (is_checkmate(c_c)) { if (c_c == c_w) @@ -434,9 +466,12 @@ void play_game() { PRINT_ALL("\ndöntetlen\n"); break; } - + dprintf(*c_fd, "\n%02d:%02d > ", remaining_time[c_c] / 60, + remaining_time[c_c] % 60); + is_printing = false; char buf[50] = {0}; read(*c_fd, buf, 50); + is_printing = true; if (buf[0] == 's') goto skip; if (buf[0] == ':') { |