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

Screening 01 #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

KongkaMozumderSUST
Copy link

@KongkaMozumderSUST KongkaMozumderSUST commented Mar 4, 2022

I have completed the tasks but the code is very messy :')

useEffect(() => {
const updateFood = () => {
setFood((food) => {
const foodCell = getRandomCell();

Choose a reason for hiding this comment

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

a random cell can be a food or a snake cell already, we should check if it is not and only allow a cell that isn't already a snake/food.
condition should look like this: isFood(foodCell) || isSnake(foodCell)

newHead.y = 0;
else if (newHead.x < 0)
newHead.x = 24;
else if (newHead.x > 24)

Choose a reason for hiding this comment

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

this checking works but a better approach would have been using mod operation on the newHeads coordinates so they always lie between 0 - 24
like this x = (head.x + direction.x) % Config.width
but this calculation can produce a bug, can you find out ?

setSnake(() => getDefaultSnake());
setDirection(() => Direction.Right);
setFood([{ x: 4, y: 10, createdAt: Date.now() }]);
setScore(() => 0);

Choose a reason for hiding this comment

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

when using a setState, if you need the previous value of the state, you can use a function like this
setState((previousValue) => returnCalculatedNewValue)
but if the new value is independant of the previous one, you can simply set it
setState(independantNewValue)

setDirection(Direction.Right);
case "ArrowDown":

setDirection((prevDirection) => {

Choose a reason for hiding this comment

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

by creating a helper function that takes in the directions and calculates the next direction, you can remove the redundancy

Corei13 added a commit that referenced this pull request Mar 21, 2022
Corei13 pushed a commit that referenced this pull request Mar 21, 2022
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