Conversation
pages/index.js
Outdated
| // reset the game if the snake hit itself or hit poison | ||
| if (isSnake(newHead) || isPoison(newHead)) { | ||
| resetGame(); | ||
| return getDefaultSnake(); |
There was a problem hiding this comment.
Is there any particular reason to keep 'getDefaultSnake()' here instead of resetting the snake state inside 'resetGame()' ?
and how the snake state is resetted here without using setSnake() ?
There was a problem hiding this comment.
you have a setSnake on line number 185 and providing a function inside that. now whatever you return from this function will be the new snakes value. that's why returning getDefaultSnake() resets the snake without any setSnake (actually there is a setSnake)
There was a problem hiding this comment.
it is more convenient to reset the snake inside resetGame function but we are already inside a setState and need to return a value there, otherwise, the newValue of the snake state will be undefined. If anyone else need to consume the resetSnake function, then it is a must to reset the snake inside the function.
pages/index.js
Outdated
| setPoisons((currentPoisons) => | ||
| currentPoisons.filter((poison) => Date.now() - poison.createdAt <= 10 * 1000) | ||
| ); | ||
| }, []) |
There was a problem hiding this comment.
instead of seperate removeFoods and removePoisons we could create one removeObjects and remove any food/poison that lasted for 10s
pages/index.js
Outdated
| const addFood = useCallback(() => { | ||
| let newFood = getRandomCell(); | ||
| while (isSnake(newFood) || isFood(newFood)) { | ||
| while (isSnake(newFood) || isFood(newFood) || isPoison(newFood)) { |
There was a problem hiding this comment.
an object can be put to a cell only if it isn't occupied yet.
why not create a reusable function like
const isOccupied = (cell) => isSnake(cell) || isFood(cell) || isPoison(cell);
pages/index.js
Outdated
| // reset the game if the snake hit itself | ||
| if (isSnake(newHead)) { | ||
| // reset the game if the snake hit itself or hit poison | ||
| if (isSnake(newHead) || isPoison(newHead)) { |
There was a problem hiding this comment.
oh, poor snake! dies on one unit of poison!
Well, this works, there is nothing wrong with your approach. But you could improve it by decreasing the snake size by one after getting hit by poison, otherwise, the game would be extremely difficult for the player.
No description provided.