/* Detects if snake hits a wall or itself */ bool CollisionDetector::collisionDetected(Snake snake) { Cell header = snake.getHeader(); int x = header.getX(); int y = header.getY(); //Iteration snakes cells, detecting self hit for(int i = 1; i < snake.getCells().size(); i++) { Cell currCell = snake.getCells().at(i); if(currCell.getX() == x && currCell.getY() == y) return true; } if(x >= width || y >= height || x < 0 || y < 0) return true; return false; }
/* Checks if food has been eaten */ bool CollisionDetector::foodEaten(Snake snake, vector<Cell> &foodList) { Cell header = snake.getHeader(); //Iterates foodlist for(int j = 0; j < foodList.size(); j++) { Cell foodCell = foodList.at(j); //If header hits a food element, then clear foodlist and return if(foodCell.getX() == header.getX() && foodCell.getY() == header.getY()) { vector<Cell>::iterator i = foodList.begin() + j; foodList.erase(i); return true; } } //No food eaten return false; }