Skip to content

module #2 (debakor)#30

Open
Dibakor20 wants to merge 6 commits intorecruit-org:mainfrom
Dibakor20:snake-2
Open

module #2 (debakor)#30
Dibakor20 wants to merge 6 commits intorecruit-org:mainfrom
Dibakor20:snake-2

Conversation

@Dibakor20
Copy link
Copy Markdown

No description provided.

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

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]);
};
Copy link
Copy Markdown

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 }) =>
Copy link
Copy Markdown

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