Skip to content

Module #1 (Nowshin Owishi)#17

Open
owishiboo wants to merge 6 commits intorecruit-org:module-1from
owishiboo:module-1
Open

Module #1 (Nowshin Owishi)#17
owishiboo wants to merge 6 commits intorecruit-org:module-1from
owishiboo:module-1

Conversation

@owishiboo
Copy link
Copy Markdown

No description provided.

pages/index.js Outdated

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.

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

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

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
Copy Markdown
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

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

4 participants