Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Module #1 (Nowshin Owishi) #17

Open
wants to merge 6 commits into
base: module-1
Choose a base branch
from

Conversation

owishiboo
Copy link

No description provided.

pages/index.js Outdated

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

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.

pages/index.js Outdated
if(direction!==Direction.Left)
return Direction.Right;
return direction;
})

Choose a reason for hiding this comment

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

these 4 cases almost do the same thing and we can create a function that calculates the next direction based on the previous direction and the ArrowDirection.

Copy link
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.

I will recommend using VS Code editor and installing prettier to make sure your code are formatted.

Everything else looks great!

pages/index.js Outdated
}, [snake]);
setFoods(currentFoods => [...currentFoods,newFood]);
setTimeout(() => {
setFoods((f) => f.filter(e => e.x !=newFood.x && e.y !=newFood.y))
Copy link
Author

Choose a reason for hiding this comment

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

@Corei13 is this a good approach? registering a timeout while creating to execute a removal after 10 seconds

Choose a reason for hiding this comment

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

@owishiboo think of this,
on 11th second you registered a timeout to remove a food on 21th second on a coordinate.
the snake eats the food within 14th second and another food appears on the exact coordinate that should get deleted on 24th second.
but the later food will get deleted on 21th second because the timeout clears that coordinate, it doesn't care about which food. right?

Copy link
Author

Choose a reason for hiding this comment

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

@royantar0311 I'm so sorry I didn't think of this. I thought the code was working fine.
How can I solve it? I'm really out of ideas.

Choose a reason for hiding this comment

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

one idea is to assign an unique id to each of the foods, and then remove the food by id inside setInterval callback
another is to keep the created time with the food and each second keep only foods that were created within last 10s

pages/index.js Outdated
setFoods((f) =>
f.filter(
(e) =>
e.start!==newFood.start
Copy link
Author

Choose a reason for hiding this comment

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

@royantar0311 I've attached an id for each food and now removing food after 10s by using the id. But the game keeps restarting. How can I fix it?

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