summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--snake.c26
1 files changed, 13 insertions, 13 deletions
diff --git a/snake.c b/snake.c
index 33e9487..aa6cb81 100644
--- a/snake.c
+++ b/snake.c
@@ -14,9 +14,8 @@
#define UNUSED(x) (void)(x)
/*** GLOBAL VARIABLES ***/
-volatile sig_atomic_t INT_CTRL_C = 0; /* flag for ctrl-c interrupt */
-int SCORE = 0;
-
+volatile sig_atomic_t intCtrlC = 0; /* flag for ctrl-c interrupt */
+int score = 0;
int boardSize = 15;
int updateInterval = 300000000L;
@@ -29,27 +28,28 @@ void interrupt_handler(int sig); /* passed into signal(3) to handle ctrl-c inter
/* unsafe functions like printf cannot be called in async interrupt handler */
void interrupt_handler(int sig) {
UNUSED(sig);
- INT_CTRL_C = 1;
+ intCtrlC = 1;
}
int main(int argc, char **argv) {
/* parsing arguments */
- for (int i = 1; i < argc; i ++) {
- if (strcmp("--version", argv[i]) == 0) {
+ if(argc > 1) {
+ if(strcmp("--version", argv[1]) == 0) {
printf("snake3, version 1.0\n");
printf("Copyright 2023 Kai Stevenson\n");
printf("This software was licensed under the MIT license;\n");
printf("for more information, see the license file.\n");
return 0;
- } else if (strcmp("--help", argv[i]) == 0) {
+ }
+ else if (strcmp("--help", argv[1]) == 0) {
printf("usage: snake3 [--version] [--help]\n");
return 0;
- } else {
+ }
+ else {
printf("usage: snake3 [--version] [--help]\n");
return 1;
}
}
-
/* get terminal size */
struct winsize win;
ioctl(fileno(stdout), TIOCGWINSZ, &win);
@@ -87,12 +87,12 @@ int main(int argc, char **argv) {
/* main game logic loop */
while(1) {
/* handle ctrl-c interrupt */
- if (INT_CTRL_C) {
+ if(intCtrlC) {
/* unhide cursor */
printf("\033[?25h");
/* kill the listener thread */
*cancellationToken = 1;
- printf("Your score was %d. Press any key to exit\n", SCORE);
+ printf("Your score was %d. Press any key to exit\n", score);
pthread_join(listenerID, NULL);
free(cancellationToken);
free((int*)lastInput);
@@ -119,7 +119,7 @@ int main(int argc, char **argv) {
int ateFood = 0;
if((snakePositions.vectors[0].mx + lastMoveDir.mx == foodPos.mx) && (snakePositions.vectors[0].my + lastMoveDir.my == foodPos.my)) {
ateFood = 1;
- SCORE = SCORE + 1;
+ score += 1;
if (updateInterval > 50000000) {
updateInterval -= 20000000;
}
@@ -156,7 +156,7 @@ int main(int argc, char **argv) {
}
/* clear the last getch call from the listener */
*cancellationToken = 1;
- printf("\n%sYou died. Score: %d. Press any key to exit%s\n", RED, SCORE, COLOR_RESET);
+ printf("\n%sYou died. Score: %d. Press any key to exit%s\n", RED, score, COLOR_RESET);
printf("\033[?25h");
pthread_join(listenerID, NULL);
free(cancellationToken);