diff options
| author | Kai Stevenson <kai@kaistevenson.com> | 2023-02-06 14:46:00 -0800 |
|---|---|---|
| committer | Kai Stevenson <kai@kaistevenson.com> | 2023-02-06 14:46:04 -0800 |
| commit | 88e2805ed74df0ec8b41c4bdd1978a15396674c3 (patch) | |
| tree | 6bc013e65ce9d5373c5c555d7e707b1621be0a5d | |
| parent | 213b95227a18809476bababef72ab3861bfa0e2d (diff) | |
added obstacles except they don't work
| -rw-r--r-- | snake.c | 64 |
1 files changed, 61 insertions, 3 deletions
@@ -20,6 +20,8 @@ int speed = 3; int score = 0; int boardSize = 15; int updateInterval = 300000000L; +int obstacleSize = 2; +vectorp_t obstaclePositions; vectorp_t snakePositions; vector_t foodPos; vector_t lastMoveDir; @@ -36,6 +38,7 @@ int main(int argc, char **argv) { /* parsing arguments */ char *last_flag; int was_flag = 0; + int obstacles = 0; for(int i = 1; i < argc; i++) { if(strcmp("--version", argv[i]) == 0) { printf("snake3, version 1.0\n"); @@ -48,7 +51,7 @@ 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 || strcmp("--obstacles", argv[i]) == 0) { last_flag = argv[i]; was_flag = 1; } @@ -66,18 +69,30 @@ int main(int argc, char **argv) { fprintf(stderr, "%sspeed level must be greater than zero and integer. Please try again.%s\n", RED, COLOR_RESET); exit(1); } - if(speed > 10) { + else if(speed > 10) { fprintf(stderr, "%sspeed must not be greater than ten. Please try again.%s\n", RED, COLOR_RESET); exit(1); } } + else if(strcmp("--obstacles", last_flag) == 0) { + obstacles = atoi(argv[i]); + if(obstacles < 0) { + fprintf(stderr, "%sobstacle count cannot be negative. Please try again.%s\n", RED, COLOR_RESET); + exit(1); + } + } was_flag = 0; } else { printf("usage: snake3 [--version] [--help]\n"); - return 1; + exit(1); } } + /* check argument compatibility */ + if(obstacles * 2 > boardSize) { + fprintf(stderr, "%stoo many obstacles. Please try again.%s\n", RED, COLOR_RESET); + exit(1); + } /* get terminal size */ struct winsize win; ioctl(fileno(stdout), TIOCGWINSZ, &win); @@ -99,7 +114,40 @@ int main(int argc, char **argv) { snakePositions.vectors[4] = init_vector(3, 3); foodPos = init_vector(7, 9); lastMoveDir = init_vector(0, 1); + int opc = (obstacleSize ^ 2) * obstacles; + obstaclePositions = init_vectorp(malloc(opc * sizeof(vector_t)), opc); srand(time(NULL)); + /* place the obstacles */ + /* TODO: make this less recursive */ + int opi = 0; + for(int i = 0; i < obstacles; i++) { + while(1) { + vector_t basePos = init_vector((rand() % (boardSize - (obstacleSize - 1))), (rand() % (boardSize - (obstacleSize - 1)))); + /* if obstacle overlaps snake, try again */ + int overlap = 0; + for(int r = 0; r < obstacleSize; r++) { + for(int c = 0; c < obstacleSize; c++) { + vector_t voxelPos = init_vector(basePos.mx + c, basePos.my + r); + for(int j = 0; j < snakePositions.length; j++) { + if((snakePositions.vectors[j].mx == voxelPos.mx) && (snakePositions.vectors[j].my == voxelPos.my)) { + overlap = 1; + } + } + } + } + if(overlap) { + continue; + } + for(int r = 0; r < obstacleSize; r++) { + for(int c = 0; c < obstacleSize; c++) { + vector_t voxelPos = init_vector(basePos.mx + c, basePos.my + r); + obstaclePositions.vectors[opi] = voxelPos; + opi++; + } + } + break; + } + } /* 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 */ @@ -274,6 +322,12 @@ void draw_board() { if(foodPos.mx == c && foodPos.my == r) { type = 3; } + for(int i = 0; i < obstaclePositions.length; i++) { + if(obstaclePositions.vectors[i].mx == c && obstaclePositions.vectors[i].my == r) { + type = 4; + break; + } + } /* append the appropriate value */ if(type == 0) { /* empty square */ @@ -296,6 +350,10 @@ void draw_board() { /* food */ printf("%sF %s", MAG, COLOR_RESET); } + else if(type == 4) { + /* obstacle */ + printf("%s# %s", MAG, COLOR_RESET); + } } printf("\n"); } |
