-
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 (Nowshin Owishi) #29
base: module-2
Are you sure you want to change the base?
Conversation
pages/index.js
Outdated
}, []); | ||
}, [dir, func]); | ||
|
||
useEffect(() => { |
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.
@Corei13 why does removefood() doesn't work? it says 'Cannot read properties of undefined (reading 'find')'
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.
@owishiboo because on line 136-138, you are setting setFoods(undefined). Why? Because you are calling setFoods(currentFoords => { expression }
. Correct form would be either
a. setFoods(currentFoords => expression
(without {} or
b. setFoods(currentFoords => { return expression }
pages/index.js
Outdated
} | ||
}, [isFood, snake]); | ||
|
||
const UseInterval = (func, dir) => { |
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.
move this outside of useSnake
pages/index.js
Outdated
}, []); | ||
}, [dir, func]); | ||
|
||
useEffect(() => { |
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.
@owishiboo because on line 136-138, you are setting setFoods(undefined). Why? Because you are calling setFoods(currentFoords => { expression }
. Correct form would be either
a. setFoods(currentFoords => expression
(without {} or
b. setFoods(currentFoords => { return expression }
pages/index.js
Outdated
@@ -200,6 +201,7 @@ const UseSnake = () => { | |||
); | |||
} | |||
if (isPoison(head)) { |
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.
@Corei13
Thanks for the previous help.
Whenever the snake eats one poison, other poisons of the same row OR same column get deleted. But I'm checking both row AND column every time. The same happens with the food also. How to fix it? Everything else is working fine.
pages/index.js
Outdated
console.log("ate poison"); | ||
setPoison((currentPoison) => | ||
currentPoison.filter( | ||
(poison) => poison.x !== head.x && poison.y !== head.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.
@owishiboo poison.x !== head.x && poison.y !== head.y
is equivalent to !(poison.x === head.x || poison.y === head.y)
See the issue? It will remove a poison even when it matches either x or y.
pages/index.js
Outdated
const Snake = () => { | ||
//custom hook | ||
//controller | ||
const UseInterval = (func, dir) => { |
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.
custom hooks name should start with use with a lowercase u and a react components name should start with a capital letter.
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.
custom hooks name should start with use with a lowercase u and a react components name should start with a capital letter.
pages/index.js
Outdated
if (isPoison(newHead)) { | ||
newSnake.pop(); | ||
} | ||
if (isSnake(newHead) || score < 0) { |
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.
how weird would that be to play with a snake of length < 3 ? haha
also one more thing, you are popping the snake here but does the score gets updated here? no, you will get the updated value of score only on the next rerender. so you cannot rely on the score here to get the snake length, use newSnake.length here to avoid potential bugs.
pages/index.js
Outdated
//add new food | ||
const addFood = () => { | ||
let newFood = getRandomCell(); | ||
while (isSnake(newFood) || isFood(newFood) || isPoison(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);
// console.log("delete poison auto"); | ||
setPoison((currentPoison) => | ||
currentPoison.filter((poison) => (Date.now() - poison.start) < 10000) | ||
); |
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.
if (isFood({ x, y })) { | ||
type = CellType.Food; | ||
remaining = |
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.
@royantar0311 I forgot to add the remaining time to the food, so I tried it out today. But the time shows undefined. I can't figure out the problem :")
pages/index.js
Outdated
10 - | ||
Math.round( | ||
(Date.now() - | ||
foods.find((food) => food.x === x && food.y === y).createdAt) / |
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.
as your cell looks like
{
x: Math.floor(Math.random() * Config.width),
y: Math.floor(Math.random() * Config.width),
start: Date.now()
}
.createdAt should be
poisons.find((poison) => poison.x === x && poison.y === y)
.start) /
1000
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.
thanks, bro @MahdiMurshed
const [objects, setObjects] = useState([]); | ||
const score = snake.length - 3; | ||
|
||
const finding = ({ x, y }, arr, type) => { | ||
return arr.find((position) => position.x === x && position.y === y && position.type===type); | ||
return arr.find( |
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.
@royantar0311 @MahdiMurshed Check this commit. Is it good? Do I need to modify anything?
pages/index.js
Outdated
return () => clearInterval(interval); | ||
}, [createCallback]); | ||
}; | ||
const UseSnake = () => { |
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.
fix naming convention
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.
UseSnake is not a hook here. I'm not sure, should I remove the term 'Use'?
else if (type === CellType.Poison) | ||
return finding({ x, y }, objects, type); | ||
else if (type === CellType.Snake) return finding({ x, y }, snake); | ||
}, |
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.
If we just want to know whether the cell contains an object or not, do we need to know the type too?
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.
My 'objects' array contains both poison and food. So, I need to find out whether an object is a food or poison when the snake eats it. I need to update the length and the score according to the type of the object. I've been using the function in this purpose, so I need it.
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.
I don't think you are taking full advantage of objects state
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.
isObject can be more simpler
const isObject = useCallback(
({ x, y }) =>
objects.find((position) => position.x === x && position.y === y),
[objects]
);
pages/index.js
Outdated
UseInterval(() => addObject(CellType.Poison), 1000); | ||
UseInterval(runSingleStep, 300); | ||
UseInterval(() => removeObject(CellType.Food), 50); | ||
UseInterval(() => removeObject(CellType.Poison), 100); |
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.
removeObject doesnt take any parameter
UseInterval(() => removeObject(CellType.Food), 50);
UseInterval(() => removeObject(CellType.Poison), 100);
would be
UseInterval(removeObject, 100);
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.
this is because removeObject function removes any object that is at least 10s old right? so we don't need to provide the type here.
else if (type === CellType.Poison) | ||
return finding({ x, y }, objects, type); | ||
else if (type === CellType.Snake) return finding({ x, y }, snake); | ||
}, |
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.
I don't think you are taking full advantage of objects state
|
||
const [food, setFood] = useState({ x: 4, y: 10 }); | ||
const [score, setScore] = useState(0); | ||
const finding = ({ x, y }, arr, type) => { |
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.
doesn't it look cleaner ?
const isObjectOfType = useCallback(
({ x, y }, type) => {
const _obj = isObject({ x, y });
return _obj && _obj.type === type;
},
[isObject]
);
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.
I'm using the same function for snake, food, and poison to find. So I think this is better -
const finding = ({ x, y }, arr, type) => {
//for snake
if (type === undefined)
return arr.find((position) => position.x === x && position.y === y);
return arr.find(
(position) =>
position.x === x && position.y === y && position.type === type
);
};
//checking cells
const isObjectOrSnake = useCallback(
({ x, y }, type) => {
if (type === CellType.Snake) return finding({ x, y }, snake);
else return finding({ x, y }, objects, type);
},
[objects, snake]
);
(position) => | ||
position.x === x && position.y === y && position.type === type | ||
); | ||
}; | ||
|
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.
as snake state is different from objects state,it makes sense to have a different function for checking snake.And it also makes the code more readable
const isSnake = useCallback(
({ x, y }) =>
snake.find((position) => position.x === x && position.y === y),
[snake]
);
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.
yeah I give an upvote to this. @owishiboo your function can work with all three types but loses simplicity. poison and food are similiar and we put them to one object state, but snake is different so we can keep it seperate fully.
well there is no right or wrong here, it comes down to your opinion.
(cell) => | ||
isObject(cell, CellType.Snake) || | ||
isObject(cell, CellType.Food) || | ||
isObject(cell, CellType.Poison), |
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.
const isBlocked = useCallback(isObject(cell) || isSnake(cell),[isObject,isSnake]);
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.
I chnaged these in the next commit. Thanks for your help!
No description provided.