diff options
Diffstat (limited to 'snake.c')
| -rw-r--r-- | snake.c | 14 |
1 files changed, 13 insertions, 1 deletions
@@ -164,6 +164,7 @@ int main(int argc, char **argv) { while(1) { /* handle ctrl-c interrupt */ if(intCtrlC) { +EXIT: /* unhide cursor */ printf("\033[?25h"); /* kill the listener thread */ @@ -189,8 +190,14 @@ int main(int argc, char **argv) { 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; } - /* check if snake landed on the food */ + /* check if snake lands on obstacle */ + for (int i = 0; i < obstaclePositions.length; i ++) { + if ((snakePositions.vectors[0].mx + lastMoveDir.mx == obstaclePositions.vectors[i].mx) && (snakePositions.vectors[0].my + lastMoveDir.my == obstaclePositions.vectors[i].mx)) { + goto EXIT; + } + } int ateFood = 0; + /* check if snake landed on the food */ if((snakePositions.vectors[0].mx + lastMoveDir.mx == foodPos.mx) && (snakePositions.vectors[0].my + lastMoveDir.my == foodPos.my)) { ateFood = 1; score += 1; @@ -207,6 +214,11 @@ int main(int argc, char **argv) { overlap = 1; } } + for (int i = 0; i < obstaclePositions.length; i ++) { + if ((obstaclePositions.vectors[i].mx == foodPos.mx) && (obstaclePositions.vectors[i].my == foodPos.my)) { + overlap = 1; + } + } if(!overlap) { break; } |
