diff options
Diffstat (limited to 'snake.c')
| -rw-r--r-- | snake.c | 83 |
1 files changed, 42 insertions, 41 deletions
@@ -2,9 +2,9 @@ #include <stdlib.h> #include <time.h> #include <pthread.h> -#include <signal.h> //handling ctrl-c -#include <sys/ioctl.h> //terminal size -#include <string.h> // argument parsing +#include <signal.h> /* handling ctrl-c */ +#include <sys/ioctl.h> /* terminal size */ +#include <string.h> /* argument parsing */ #include "vector.h" #include "snake.h" @@ -14,7 +14,7 @@ #define UNUSED(x) (void)(x) /*** GLOBAL VARIABLES ***/ -volatile sig_atomic_t INT_CTRL_C = 0; //flag for ctrl-c interrupt +volatile sig_atomic_t INT_CTRL_C = 0; /* flag for ctrl-c interrupt */ int SCORE = 0; int boardSize = 15; @@ -24,8 +24,9 @@ vectorp_t snakePositions; vector_t foodPos; vector_t lastMoveDir; -void interrupt_handler(int sig); // passed into signal(3) to handle ctrl-c interrupts -//unsafe functions like printf cannot be called in async interrupt handler +void interrupt_handler(int sig); /* passed into signal(3) to handle ctrl-c interrupts */ + +/* unsafe functions like printf cannot be called in async interrupt handler */ void interrupt_handler(int sig) { UNUSED(sig); INT_CTRL_C = 1; @@ -49,19 +50,19 @@ int main(int argc, char **argv) { } } - //get terminal size + /* 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 + /* 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 + /* hide cursor */ printf("\033[?25l"); - //make a new thread handling interrupts + /* make a new thread handling interrupts */ signal(SIGINT, interrupt_handler); - //init values + /* init values */ snakePositions = init_vectorp(malloc(5 * sizeof(vector_t)), 5); snakePositions.vectors[0] = init_vector(5, 5); snakePositions.vectors[1] = init_vector(5, 4); @@ -71,25 +72,25 @@ int main(int argc, char **argv) { foodPos = init_vector(7, 9); lastMoveDir = init_vector(0, 1); srand(time(NULL)); - //clear the screen so as to prevent artifacting + /* 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 + /* 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)); - //and the wrapper struct + /* and the wrapper struct */ threadArg_t threadArg; threadArg.returnV = (void*)lastInput; threadArg.cancellationToken = cancellationToken; - //create listener thread + /* create listener thread */ pthread_t listenerID; pthread_create(&listenerID, NULL, get_dir_loop, &threadArg); - //main game logic loop + /* main game logic loop */ while(1) { - //handle ctrl-c interrupt + /* handle ctrl-c interrupt */ if (INT_CTRL_C) { - //unhide cursor + /* unhide cursor */ printf("\033[?25h"); - //kill the listener thread + /* kill the listener thread */ *cancellationToken = 1; printf("Your score was %d. Press any key to exit\n", SCORE); pthread_join(listenerID, NULL); @@ -98,23 +99,23 @@ int main(int argc, char **argv) { return 0; } - //first, draw the current state of the board + /* first, draw the current state of the board */ draw_board(); - //handle player input - //wait for the specified interval + /* 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 + /* read the most recent value emitted by the listener */ vector_t dir = parse_dir(*(char*)lastInput); - //if the player inputted a valid direction, use it. otherwise, use his last valid input + /* 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; } else { } - //check if snake landed on the food + /* 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; @@ -122,10 +123,10 @@ int main(int argc, char **argv) { if (updateInterval > 50000000) { updateInterval -= 20000000; } - //move the food + /* move the food */ while(1) { foodPos = init_vector(1 + (rand() % (boardSize - 2)), 1 + (rand() % (boardSize - 2))); - //if food overlaps snake, try again + /* if food overlaps snake, try again */ int overlap = 0; for(int i = 0; i < snakePositions.length; i++) { if((snakePositions.vectors[i].mx == foodPos.mx) && (snakePositions.vectors[i].my == foodPos.my)) { @@ -137,15 +138,15 @@ int main(int argc, char **argv) { } } } - //move the snake + /* move the snake */ vectorp_t newSnakePositions = move_snake(lastMoveDir, ateFood); free(snakePositions.vectors); snakePositions = newSnakePositions; - //check if snake landed on self + /* check if snake landed on self */ int died = 0; for(int 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 + /* game over */ died = 1; } } @@ -153,7 +154,7 @@ int main(int argc, char **argv) { break; } } - //clear the last getch call from the listener + /* 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("\033[?25h"); @@ -168,7 +169,7 @@ void* get_dir_loop(void* threadArg) { } return NULL; } -//parses WASD or HJKL, returns the necessary deltas to move within a 2d array +/* parses WASD or HJKL, returns the necessary deltas to move within a 2d array */ vector_t parse_dir(char cDir) { if(cDir == 'w' || cDir == 'k') { return init_vector(0, -1); @@ -189,7 +190,7 @@ vector_t parse_dir(char cDir) { vectorp_t move_snake(vector_t dir, int lengthen) { int newLength = snakePositions.length + (lengthen ? 1 : 0); vector_t* newSnakePositions = malloc((newLength * sizeof(vector_t))); - //wrap around edges + /* 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; @@ -201,12 +202,12 @@ vectorp_t move_snake(vector_t dir, int lengthen) { return init_vectorp(newSnakePositions, newLength); } void draw_board() { - //move the cursor to the top left corner of the screen + /* move the cursor to the top left corner of the screen */ printf("\033[0;0H"); - //iterate through and write the board + /* iterate through and write the board */ for(int r = 0; r < boardSize; r++) { for(int c = 0; c < boardSize; c++) { - //determine the contents of the board position + /* determine the contents of the board position */ int type = 0; for(int i = 0; i < snakePositions.length; i++) { if(snakePositions.vectors[i].mx == c && snakePositions.vectors[i].my == r) { @@ -220,21 +221,21 @@ void draw_board() { if(foodPos.mx == c && foodPos.my == r) { type = 3; } - //append the appropriate value + /* append the appropriate value */ if(type == 0) { - //empty square + /* empty square */ printf("` "); } else if(type == 1) { - //snake head + /* snake head */ printf("%s@ %s", GRN, COLOR_RESET); } else if(type == 2) { - //snake body + /* snake body */ printf("%s* %s", GRN, COLOR_RESET); } else if(type == 3) { - //food + /* food */ printf("%sF %s", MAG, COLOR_RESET); } } |
