Skip to content

Screening 01#1

Open
KongkaMozumderSUST wants to merge 1 commit intorecruit-org:mainfrom
KongkaMozumderSUST:snake1
Open

Screening 01#1
KongkaMozumderSUST wants to merge 1 commit intorecruit-org:mainfrom
KongkaMozumderSUST:snake1

Conversation

@KongkaMozumderSUST
Copy link
Copy Markdown

@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();
Copy link
Copy Markdown

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

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

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

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