-
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 (Mahmud) #38
base: module-2
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -124,20 +124,28 @@ const useSnake = () => { | |
setPoisons([]); | ||
}, []); | ||
|
||
const removeFoods = useCallback(() => { | ||
// only keep those foods which were created within last 10s. | ||
setFoods((currentFoods) => | ||
currentFoods.filter((food) => Date.now() - food.createdAt <= 10 * 1000) | ||
); | ||
}, []); | ||
|
||
const removePoisons = useCallback(() => { | ||
setPoisons((currentPoisons) => | ||
// const removeFoods = useCallback(() => { | ||
// // only keep those foods which were created within last 10s. | ||
// setFoods((currentFoods) => | ||
// currentFoods.filter((food) => Date.now() - food.createdAt <= 10 * 1000) | ||
// ); | ||
// }, []); | ||
|
||
// const removePoisons = useCallback(() => { | ||
// setPoisons((currentPoisons) => | ||
// currentPoisons.filter( | ||
// (poison) => Date.now() - poison.createdAt <= 10 * 1000 | ||
// ) | ||
// ); | ||
// }, []); | ||
|
||
const removeObject = useCallback((setState)=>{ | ||
setState((currentPoisons) => | ||
currentPoisons.filter( | ||
(poison) => Date.now() - poison.createdAt <= 10 * 1000 | ||
) | ||
); | ||
}, []); | ||
},[]); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can create a reusable removeObject(type) function like addObject |
||
// ?. is called optional chaining | ||
// see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Optional_chaining | ||
|
@@ -160,21 +168,29 @@ const useSnake = () => { | |
(newCell) => isSnake(newCell) || isFood(newCell || isPoison(newCell)), | ||
[isFood, isPoison, isSnake] | ||
); | ||
const addFood = useCallback(() => { | ||
let newFood = getRandomCell(); | ||
while (isAllowedCell(newFood)) { | ||
newFood = getRandomCell(); | ||
} | ||
setFoods((currentFoods) => [...currentFoods, newFood]); | ||
}, [isAllowedCell]); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. basically addFood and addPoison does the same job .So we can make a reusable function like const addObject= useCallback((type) => {
let newObj= getRandomCell();
while (isAllowedCell(newObj)) {
newObj= getRandomCell();
}
if(type===CellType.Food){
setFoods((currentFoods) => [...currentFoods, newObj]);
}else{
setPoisons(currentPoison=>[...currentPoison,newObj]);
}
}, [isAllowedCell]); when calling useInterval(()=>addObject(CellType.Food), 3000);
useInterval(()=>addObject(CellType.Poison), 3000); |
||
const addPoison = useCallback(() => { | ||
let newPoison = getRandomCell(); | ||
while (isAllowedCell(newPoison)) { | ||
newPoison = getRandomCell(); | ||
const getEmptyCell = useCallback(() =>{ | ||
let newCell = getRandomCell(); | ||
while (isAllowedCell(newCell)) { | ||
newCell = getRandomCell(); | ||
} | ||
setPoisons((currentPoisons) => [...currentPoisons, newPoison]); | ||
}, [isAllowedCell]); | ||
return newCell; | ||
},[isAllowedCell]); | ||
|
||
// const addFood = useCallback(() => { | ||
// const newFood = getEmptyCell(); | ||
// setFoods((currentFoods) => [...currentFoods, newFood]); | ||
// }, [getEmptyCell]); | ||
|
||
// const addPoison = useCallback(() => { | ||
// const newPoison = getEmptyCell(); | ||
// setPoisons((currentPoisons) => [...currentPoisons, newPoison]); | ||
// }, [getEmptyCell]); | ||
|
||
const addObject = useCallback((setState)=>{ | ||
const newObject = getEmptyCell(); | ||
setState((currentPoisons) => [...currentPoisons, newObject]); | ||
},[getEmptyCell]) | ||
|
||
// move the snake | ||
const runSingleStep = useCallback(() => { | ||
|
@@ -213,12 +229,13 @@ const useSnake = () => { | |
return newSnake; | ||
}); | ||
}, [direction.x, direction.y, isFood, isPoison, isSnake, resetGame]); | ||
|
||
|
||
useInterval(runSingleStep, 1); | ||
useInterval(addFood, 3000); | ||
useInterval(removeFoods, 100); | ||
useInterval(addPoison, 3000); | ||
useInterval(removePoisons, 100); | ||
useInterval(runSingleStep, 200); | ||
useInterval(()=>addObject(setFoods), 3000); | ||
useInterval(()=>removeObject(setFoods), 100); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we can identify food or poison as an object, the only difference will be the type(the type of an object will be food or poison) and it will be less hassle to handle. |
||
useInterval(()=>addObject(setPoisons), 3000); | ||
useInterval(()=>removeObject(setPoisons), 100); | ||
|
||
useEffect(() => { | ||
const handleDirection = (direction, oppositeDirection) => { | ||
|
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.
we don't need to send the setStates as they're already available in this function. It would be better to use cellType to decide which setState you want to call.
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.
But this call the same addFood or addPoison function inside that. I wanted to generalize the adding and removing process for food and poison both.
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.
yes, and we can easily do it by checking the type of the object.
it is much simpler.
Also, you may consider food or poison as an object. The only thing that differs between them is the type. All the functionalities differ according to the type(for example, adding or removing is the same process for different arrays). We can easily use the type to differentiate.
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 in your code you almost did the same thing . You passed array as an argument for checking types in findings function .