Module #1 (Omar) - #18
Conversation
|
|
||
| return () => clearInterval(timer); | ||
| }, [direction, food]); | ||
| }, [direction, foods]); |
There was a problem hiding this comment.
foods state isn't used anywhere inside the useEffect so we can remove it.
There was a problem hiding this comment.
I don't know why but removing foods state from dependency list causes the game to reset after a certain period of time
There was a problem hiding this comment.
@riyadomf can you check again? I ran your branch and couldn’t reproduce the issue you mentioned after removing the foods state
There was a problem hiding this comment.
@royantar0311 When the snake starts from it's initial position, don't change it's direction. It will cross the right side boundary and will return to it's initial position; At that time, the game resets.
| // return the foods remaining after it's dinner including the new food | ||
| setFoods( (foods) => { | ||
| const head = snake[0]; | ||
| const remainingFoods = foods.filter(food => food.x != head.x || food.y != head.y ); |
There was a problem hiding this comment.
the function addNewFood describes that it will add a new food regardless of the snakes current state. so this function should only add a food and nothing else.
| setScore((score) => { | ||
| return score + 1; | ||
| }); | ||
| addNewFood(); |
There was a problem hiding this comment.
here along with addNewFood() we can call another method removeFood() that takes in the coordinates of the current snake head and removes the food from there.
| const isSnake = ({ x, y }) => | ||
| snake.find((position) => position.x === x && position.y === y); | ||
|
|
||
| return [score, isFood, isSnake]; |
There was a problem hiding this comment.
Return an object instead of an array. Object is preferred because of the named keys. When you have an object, say { a: 5, b: 6 } you know what value 5 or 6 are. But with an array like [5, 6] you don't know what 5 or 6 are supposed to be, which is prone to errors.
No description provided.