From 3e86fe9d5916e7574e630a35639bc2bae68d6f27 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Mon, 6 Feb 2023 00:50:24 -0800 Subject: fixed some formatting --- snake.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/snake.c b/snake.c index f7097d9..2afd70d 100644 --- a/snake.c +++ b/snake.c @@ -9,23 +9,22 @@ volatile sig_atomic_t INT_CTRL_C = 0; -// unsafe functions like printf cannot be called in async interrupt handler +//unsafe functions like printf cannot be called in async interrupt handler void interrupt_handler(int sig) { INT_CTRL_C = 1; } -const int boardSize = 12; +int boardSize = 12; const int updateInterval = 300000000L; vectorp_t snakePositions; vector_t foodPos; vector_t lastMoveDir; int main(int argc, char **argv) { - printf("\e[?25l"); /* hide cursor */ - + //hide cursor + printf("\e[?25l"); //make a new thread handling interrupts signal(SIGINT, interrupt_handler); - //init values snakePositions = init_vectorp(malloc(5 * sizeof(vector_t)), 5); snakePositions.vectors[0] = init_vector(5, 5); @@ -50,7 +49,7 @@ int main(int argc, char **argv) { pthread_create(&listenerID, NULL, get_dir_loop, &threadArg); //main game logic loop while(1) { - // handle ctrl-c interrupt + //handle ctrl-c interrupt if (INT_CTRL_C) { //unhide cursor printf("\e[?25h"); -- cgit v1.3.1 From bc9372c5ceffc7229afa5963e0227a1d3c0a15d9 Mon Sep 17 00:00:00 2001 From: Kai Stevenson Date: Mon, 6 Feb 2023 01:07:52 -0800 Subject: board automatically shrinks if terminal is too small --- snake.c | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/snake.c b/snake.c index 2afd70d..242ea27 100644 --- a/snake.c +++ b/snake.c @@ -3,6 +3,7 @@ #include #include #include //handling ctrl-c +#include //terminal size #include "vector.h" #include "snake.h" #include "conio.h" @@ -14,13 +15,21 @@ void interrupt_handler(int sig) { INT_CTRL_C = 1; } -int boardSize = 12; +int boardSize = 15; const int updateInterval = 300000000L; vectorp_t snakePositions; vector_t foodPos; vector_t lastMoveDir; int main(int argc, char **argv) { + //get terminal size + struct winsize win; + ioctl(fileno(stdout), TIOCGWINSZ, &win); + int width = (int)(win.ws_col); + int height = (int)(win.ws_row); + //multiplied by 2 to account for the fact that the board is twice as wide as high + int smallestAxis = width / 2 < height ? width / 2 : height; + boardSize = smallestAxis < boardSize ? smallestAxis : boardSize; //hide cursor printf("\e[?25l"); //make a new thread handling interrupts -- cgit v1.3.1