diff options
author | boa <boa@pm.me> | 2021-02-02 01:08:15 +0100 |
---|---|---|
committer | boa <boa@pm.me> | 2021-02-02 01:08:15 +0100 |
commit | f05687d372bea4b26ea710e0143f5dcaee1b2632 (patch) | |
tree | 371ee247dbbfcd797c553221fa37f82be1a826b2 | |
parent | 6a1977cdc695996fd4c2e1af3baa0757136fdf98 (diff) | |
download | ajedrez-f05687d372bea4b26ea710e0143f5dcaee1b2632.tar.gz |
timer
-rwxr-xr-x | chess | bin | 37432 -> 38064 bytes | |||
-rw-r--r-- | q | 1 | ||||
-rwxr-xr-x | sakkom | bin | 0 -> 27128 bytes | |||
-rw-r--r-- | sakkom.c | 51 | ||||
-rw-r--r-- | td.h>q | 53 |
5 files changed, 97 insertions, 8 deletions
Binary files differ @@ -0,0 +1 @@ +td.h Binary files differ@@ -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] == ':') { @@ -0,0 +1,53 @@ +SLEEP(3) Linux Programmer's Manual SLEEP(3) + +NNAAMMEE + sleep - sleep for a specified number of seconds + +SSYYNNOOPPSSIISS + ##iinncclluuddee <<uunniissttdd..hh>> + + uunnssiiggnneedd iinntt sslleeeepp((uunnssiiggnneedd iinntt _s_e_c_o_n_d_s));; + +DDEESSCCRRIIPPTTIIOONN + sslleeeepp() causes the calling thread to sleep either until the number of + real-time seconds specified in _s_e_c_o_n_d_s have elapsed or until a signal ar‐ + rives which is not ignored. + +RREETTUURRNN VVAALLUUEE + Zero if the requested time has elapsed, or the number of seconds left to + sleep, if the call was interrupted by a signal handler. + +AATTTTRRIIBBUUTTEESS + For an explanation of the terms used in this section, see aattttrriibbuutteess(7). + + ┌──────────┬───────────────┬─────────────────────────────┐ + │IInntteerrffaaccee │ AAttttrriibbuuttee │ VVaalluuee │ + ├──────────┼───────────────┼─────────────────────────────┤ + │sslleeeepp() │ Thread safety │ MT-Unsafe sig:SIGCHLD/linux │ + └──────────┴───────────────┴─────────────────────────────┘ + +CCOONNFFOORRMMIINNGG TTOO + POSIX.1-2001, POSIX.1-2008. + +NNOOTTEESS + On Linux, sslleeeepp() is implemented via nnaannoosslleeeepp(2). See the nnaannoosslleeeepp(2) + man page for a discussion of the clock used. + + PPoorrttaabbiilliittyy nnootteess + On some systems, sslleeeepp() may be implemented using aallaarrmm(2) and SSIIGGAALLRRMM + (POSIX.1 permits this); mixing calls to aallaarrmm(2) and sslleeeepp() is a bad + idea. + + Using lloonnggjjmmpp(3) from a signal handler or modifying the handling of + SSIIGGAALLRRMM while sleeping will cause undefined results. + +SSEEEE AALLSSOO + sslleeeepp(1), aallaarrmm(2), nnaannoosslleeeepp(2), ssiiggnnaall(2), ssiiggnnaall(7) + +CCOOLLOOPPHHOONN + This page is part of release 5.10 of the Linux _m_a_n_-_p_a_g_e_s project. A de‐ + scription of the project, information about reporting bugs, and the lat‐ + est version of this page, can be found at + https://www.kernel.org/doc/man-pages/. + +GNU 2017-09-15 SLEEP(3) |