Skip to content

Module #1 (Omar)#18

Open
riyadomf wants to merge 3 commits intorecruit-org:module-1from
riyadomf:module-1
Open

Module #1 (Omar)#18
riyadomf wants to merge 3 commits intorecruit-org:module-1from
riyadomf:module-1

Conversation

@riyadomf
Copy link
Copy Markdown

No description provided.


return () => clearInterval(timer);
}, [direction, food]);
}, [direction, foods]);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foods state isn't used anywhere inside the useEffect so we can remove it.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know why but removing foods state from dependency list causes the game to reset after a certain period of time

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@riyadomf can you check again? I ran your branch and couldn’t reproduce the issue you mentioned after removing the foods state

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@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.

pages/index.js Outdated
// 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 );
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

pages/index.js Outdated
setScore((score) => {
return score + 1;
});
addNewFood();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor

@Corei13 Corei13 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks really good!

pages/index.js Outdated
const isSnake = ({ x, y }) =>
snake.find((position) => position.x === x && position.y === y);

return [score, isFood, isSnake];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants