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 (debakor) #30

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open

module #2 (debakor) #30

wants to merge 6 commits into from

Conversation

Dibakor20
Copy link

No description provided.

// food add function
const addNewFood = () => {
let newFood = getRandomCell();
while (isSnake(newFood) || isFood(newFood)) {

Choose a reason for hiding this comment

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

an object can be put to a cell only if it isn't occupied yet.
why not create a reusable function like
const isOccupied = (cell) => isSnake(cell) || isFood(cell) || isPoison(cell);

newPoison = getRandomCell();
}
setPoisons((currentPoison) => [...currentPoison, newPoison]);
};

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.

const isFood = ({ x, y }) =>
foods.some((food) => food?.x === x && food?.y === y);

const isPoison = ({ x, y }) =>

Choose a reason for hiding this comment

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

you can also wrap these functions into a useCallback hook so that they don't get instantiated again

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.

2 participants