summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPreston Pan <preston@nullring.xyz>2023-02-06 14:20:45 -0800
committerPreston Pan <preston@nullring.xyz>2023-02-06 14:20:45 -0800
commitae0f85a2ea9f7900b8869f1e960675f7a6ee5692 (patch)
treeae7f16e4de610bdc7af3baf40456711c43d422e8
parent326dd0bae76036d5c72ab6d4979611cdab0dacc8 (diff)
parent3d6e9778240afdfd7e7adcb6330b9c85544d7ac8 (diff)
Merge branch 'master' of ssh://aberrantflux.xyz:/home/git/repo/public/snake3
-rw-r--r--.gitignore1
-rw-r--r--snake.c22
2 files changed, 13 insertions, 10 deletions
diff --git a/.gitignore b/.gitignore
index bc54d59..d77bb67 100644
--- a/.gitignore
+++ b/.gitignore
@@ -2,3 +2,4 @@ a.out
a.exe
*.swp
snake3
+*.core
diff --git a/snake.c b/snake.c
index d273935..d61949e 100644
--- a/snake.c
+++ b/snake.c
@@ -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);