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 #2 (Nowshin Owishi) #29

Open
wants to merge 11 commits into
base: module-2
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions pages/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,15 +178,16 @@ const UseSnake = () => {

//remove food
const removeFood = useCallback(() => {
console.log("delete");
// console.log("delete food auto");
setFoods((currentFoods) =>
currentFoods.filter((food) => Date.now() - food.start < 10000)
currentFoods.filter((food) => (Date.now() - food.start) < 10000)
);
}, []);
//remove poison
const removePoison = useCallback(() => {
// console.log("delete poison auto");
setPoison((currentPoison) =>
currentPoison.filter((poison) => Date.now() - poison.start < 10000)
currentPoison.filter((poison) => (Date.now() - poison.start) < 10000)
);

Choose a reason for hiding this comment

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

addFoods and addPoison, removeFood and removePoison works the same, you can merge them to have one addObject function, and one removeObject function.

}, []);

Expand All @@ -200,6 +201,7 @@ const UseSnake = () => {
);
}
if (isPoison(head)) {
Copy link
Author

Choose a reason for hiding this comment

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

@Corei13
Thanks for the previous help.
Whenever the snake eats one poison, other poisons of the same row OR same column get deleted. But I'm checking both row AND column every time. The same happens with the food also. How to fix it? Everything else is working fine.

console.log("ate poison");
setPoison((currentPoison) =>
currentPoison.filter(
(poison) => poison.x !== head.x && poison.y !== head.y
Copy link
Contributor

Choose a reason for hiding this comment

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

@owishiboo poison.x !== head.x && poison.y !== head.y is equivalent to !(poison.x === head.x || poison.y === head.y)

See the issue? It will remove a poison even when it matches either x or y.

Expand All @@ -208,7 +210,7 @@ const UseSnake = () => {
}
}, [isFood, isPoison, snake]);

UseInterval(addFood, 2000);
UseInterval(addFood, 3000);
UseInterval(runSingleStep, 300);
UseInterval(removeFood, 50);
UseInterval(addPoison, 4000);
Expand Down