diff options
| author | Preston Pan <preston@nullring.xyz> | 2023-02-06 12:47:50 -0800 |
|---|---|---|
| committer | Preston Pan <preston@nullring.xyz> | 2023-02-06 12:47:50 -0800 |
| commit | 4dc3200598e0af85db301b14a5b938d44cba8a82 (patch) | |
| tree | 37155906ca01aa3a5d54a303edf651eaa85f23cc | |
| parent | d5cb95bf86ad066bf6b65a7cae6445df4d4c5751 (diff) | |
branch now compiles with c89
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | snake.c | 57 | ||||
| -rw-r--r-- | vector.c | 9 |
3 files changed, 42 insertions, 26 deletions
@@ -1,4 +1,4 @@ -CFLAGS=-I. -pthread -Wall -Wextra -Wpedantic -std=c99 -march=native -O3 +CFLAGS=-I. -pthread -Wall -Wextra -Wpedantic -std=c89 -march=native -O3 CFLAGS+= -Wshadow -Wstrict-prototypes -Wundef -Wpointer-arith -Wcast-align -Wstrict-overflow=5 -Wswitch-default CFLAGS+= -Wunreachable-code -Wswitch-enum -Wformat=2 PREFIX ?= /usr @@ -33,8 +33,18 @@ void interrupt_handler(int sig) { } int main(int argc, char **argv) { + int i; + struct winsize win; + int width; + int height; + int smallestAxis; + volatile int *lastInput; + int *cancellationToken; + threadArg_t threadArg; + pthread_t listenerID; + /* parsing arguments */ - for (int i = 1; i < argc; i ++) { + for (i = 1; i < argc; i ++) { if (strcmp("--version", argv[i]) == 0) { printf("snake3, version 1.0\n"); printf("Copyright 2023 Kai Stevenson\n"); @@ -51,12 +61,11 @@ 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); + width = (int)(win.ws_col); + 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; + smallestAxis = width / 2 < height ? width / 2 : height; boardSize = smallestAxis < boardSize ? smallestAxis : boardSize; /* hide cursor */ printf("\033[?25l"); @@ -75,17 +84,20 @@ int main(int argc, char **argv) { /* clear the screen so as to prevent artifacting */ printf("\033[2J"); /* prepare the pointers which will be used to communicate with the input getting thread */ - volatile int* lastInput = calloc(1, sizeof(int)); - int* cancellationToken = calloc(1, sizeof(int)); + lastInput = calloc(1, sizeof(int)); + cancellationToken = calloc(1, sizeof(int)); /* and the wrapper struct */ - threadArg_t threadArg; threadArg.returnV = (void*)lastInput; threadArg.cancellationToken = cancellationToken; /* create listener thread */ - pthread_t listenerID; pthread_create(&listenerID, NULL, get_dir_loop, &threadArg); /* main game logic loop */ while(1) { + struct timespec time; + int ateFood = 0; + int died = 0; + vector_t dir; + vectorp_t newSnakePositions; /* handle ctrl-c interrupt */ if (INT_CTRL_C) { /* unhide cursor */ @@ -103,12 +115,11 @@ int main(int argc, char **argv) { draw_board(); /* handle player input; * wait for the specified interval */ - struct timespec time; time.tv_sec = 0; time.tv_nsec = updateInterval; nanosleep(&time, NULL); /* read the most recent value emitted by the listener */ - vector_t dir = parse_dir(*(char*)lastInput); + dir = parse_dir(*(char*)lastInput); /* if the player inputted a valid direction, use it. otherwise, use his last valid input */ if((dir.mx != 0 || dir.my != 0) && !(dir.mx == 1 && lastMoveDir.mx == -1) && !(dir.mx == -1 && lastMoveDir.mx == 1) && !(dir.my == 1 && lastMoveDir.my == -1) && !(dir.my == -1 && lastMoveDir.my == 1)) { lastMoveDir = dir; @@ -116,7 +127,6 @@ int main(int argc, char **argv) { else { } /* check if snake landed on the food */ - 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; @@ -125,10 +135,10 @@ int main(int argc, char **argv) { } /* move the food */ while(1) { + int overlap = 0; foodPos = init_vector(1 + (rand() % (boardSize - 2)), 1 + (rand() % (boardSize - 2))); /* if food overlaps snake, try again */ - int overlap = 0; - for(int i = 0; i < snakePositions.length; i++) { + for(i = 0; i < snakePositions.length; i++) { if((snakePositions.vectors[i].mx == foodPos.mx) && (snakePositions.vectors[i].my == foodPos.my)) { overlap = 1; } @@ -139,12 +149,11 @@ int main(int argc, char **argv) { } } /* move the snake */ - vectorp_t newSnakePositions = move_snake(lastMoveDir, ateFood); + newSnakePositions = move_snake(lastMoveDir, ateFood); free(snakePositions.vectors); snakePositions = newSnakePositions; /* check if snake landed on self */ - int died = 0; - for(int i = 1; i < snakePositions.length; i++) { + for(i = 1; i < snakePositions.length; i++) { if(snakePositions.vectors[0].mx == snakePositions.vectors[i].mx && snakePositions.vectors[0].my == snakePositions.vectors[i].my) { /* game over */ died = 1; @@ -188,28 +197,32 @@ vector_t parse_dir(char cDir) { } } vectorp_t move_snake(vector_t dir, int lengthen) { + int i; int newLength = snakePositions.length + (lengthen ? 1 : 0); vector_t* newSnakePositions = malloc((newLength * sizeof(vector_t))); /* wrap around edges */ int nhx = snakePositions.vectors[0].mx + dir.mx; - nhx = nhx < 0 ? boardSize - 1 : nhx >= boardSize ? 0 : nhx; int nhy = snakePositions.vectors[0].my + dir.my; + nhx = nhx < 0 ? boardSize - 1 : nhx >= boardSize ? 0 : nhx; nhy = nhy < 0 ? boardSize - 1 : nhy >= boardSize ? 0 : nhy; newSnakePositions[0] = init_vector(nhx, nhy); - for(int i = 1; i < newLength; i++) { + for(i = 1; i < newLength; i++) { newSnakePositions[i] = snakePositions.vectors[i - 1]; } return init_vectorp(newSnakePositions, newLength); } void draw_board() { + int r; + int c; + int i; /* move the cursor to the top left corner of the screen */ printf("\033[0;0H"); /* iterate through and write the board */ - for(int r = 0; r < boardSize; r++) { - for(int c = 0; c < boardSize; c++) { + for(r = 0; r < boardSize; r++) { + for(c = 0; c < boardSize; c++) { /* determine the contents of the board position */ int type = 0; - for(int i = 0; i < snakePositions.length; i++) { + for(i = 0; i < snakePositions.length; i++) { if(snakePositions.vectors[i].mx == c && snakePositions.vectors[i].my == r) { type = i ? 2 : 1; break; @@ -14,7 +14,8 @@ vectorp_t init_vectorp(vector_t* vectors, int length) { } vectorp_t remove_vector(vectorp_t vectorp, int index) { vector_t* vectors = malloc((vectorp.length - 1) * sizeof(vector_t)); - for(int i = 0; i < vectorp.length - 1; i++) { + int i; + for(i = 0; i < vectorp.length - 1; i++) { int correctedIndex = i > index ? i - 1 : i; if(i != index) { vectors[correctedIndex] = vectorp.vectors[i]; @@ -25,9 +26,11 @@ vectorp_t remove_vector(vectorp_t vectorp, int index) { vectorp_t remove_vectors(vectorp_t vectorp, int* indexes, int indexCount) { vector_t* vectors = malloc((vectorp.length - indexCount) * sizeof(vector_t)); int correctedIndex = 0; - for(int i = 0; i < vectorp.length - indexCount; i++) { + int i; + int j; + for(i = 0; i < vectorp.length - indexCount; i++) { int toRemove = 0; - for(int j = 0; j < indexCount; j++) { + for(j = 0; j < indexCount; j++) { if(i == indexes[j]) { toRemove = 1; break; |
