-
Notifications
You must be signed in to change notification settings - Fork 15
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
base: main
Are you sure you want to change the base?
Conversation
// food add function | ||
const addNewFood = () => { | ||
let newFood = getRandomCell(); | ||
while (isSnake(newFood) || isFood(newFood)) { |
There was a problem hiding this comment.
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]); | ||
}; |
There was a problem hiding this comment.
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 }) => |
There was a problem hiding this comment.
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
No description provided.