diff options
| -rw-r--r-- | .gitignore | 1 | ||||
| -rw-r--r-- | snake.c | 22 |
2 files changed, 13 insertions, 10 deletions
@@ -2,3 +2,4 @@ a.out a.exe *.swp snake3 +*.core @@ -5,6 +5,7 @@ #include <signal.h> /* handling ctrl-c */ #include <sys/ioctl.h> /* terminal size */ #include <string.h> /* argument parsing */ +#include <math.h> /* time calculations */ #include "vector.h" #include "snake.h" @@ -15,7 +16,7 @@ /*** GLOBAL VARIABLES ***/ volatile sig_atomic_t intCtrlC = 0; /* flag for ctrl-c interrupt */ -int speed = 1; +int speed = 3; int score = 0; int boardSize = 15; int updateInterval = 300000000L; @@ -35,7 +36,7 @@ int main(int argc, char **argv) { /* parsing arguments */ char *last_flag; int was_flag = 0; - for (int i = 1; i < argc; i ++) { + for(int i = 1; i < argc; i++) { if(strcmp("--version", argv[i]) == 0) { printf("snake3, version 1.0\n"); printf("Copyright 2023 Kai Stevenson\n"); @@ -47,24 +48,25 @@ int main(int argc, char **argv) { printf("usage: snake3 [--version] [--help] [--size board_size] [--speed speed]\n"); return 0; } - else if (strcmp("--size", argv[i]) == 0 || strcmp("--speed", argv[i]) == 0) { + else if(strcmp("--size", argv[i]) == 0 || strcmp("--speed", argv[i]) == 0) { last_flag = argv[i]; was_flag = 1; } - else if (was_flag) { - if (strcmp("--size", last_flag) == 0) { + else if(was_flag) { + if(strcmp("--size", last_flag) == 0) { boardSize = atoi(argv[i]); - if (boardSize < 3) { + if(boardSize < 3) { fprintf(stderr, "%sboard size too small or argument invalid, please try again.%s\n", RED, COLOR_RESET); exit(1); } } - else if (strcmp("speed", last_flag) == 0) { + else if(strcmp("--speed", last_flag) == 0) { speed = atoi(argv[i]); - if (speed <= 0) { + if(speed <= 0) { fprintf(stderr, "%sspeed level must be greater than zero and integer. Please try again.%s\n", RED, COLOR_RESET); exit(1); - } if (speed > 10) { + } + if(speed > 10) { fprintf(stderr, "%sspeed must not be greater than ten. Please try again.%s\n", RED, COLOR_RESET); exit(1); } @@ -131,7 +133,7 @@ int main(int argc, char **argv) { * wait for the specified interval */ struct timespec time; time.tv_sec = 0; - time.tv_nsec = updateInterval; + time.tv_nsec = round((float)updateInterval / ((float)speed / 3)); nanosleep(&time, NULL); /* read the most recent value emitted by the listener */ vector_t dir = parse_dir(*(char*)lastInput); |
