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

Module #2 (Nowshin Owishi) #29

Open
wants to merge 11 commits into
base: module-2
Choose a base branch
from

Conversation

owishiboo
Copy link

No description provided.

pages/index.js Outdated
}, []);
}, [dir, func]);

useEffect(() => {
Copy link
Author

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')'

Copy link
Contributor

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

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

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

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
Copy link
Contributor

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) => {

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.

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.

@owishiboo

pages/index.js Outdated
if (isPoison(newHead)) {
newSnake.pop();
}
if (isSnake(newHead) || score < 0) {
Copy link

@royantar0311 royantar0311 Mar 30, 2022

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)) {

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)
);

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 =
Copy link
Author

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

@MahdiMurshed MahdiMurshed Apr 16, 2022

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

@owishiboo

Copy link
Author

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(
Copy link
Author

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

@MahdiMurshed MahdiMurshed Apr 17, 2022

Choose a reason for hiding this comment

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

fix naming convention

Copy link
Author

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);
},

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?

Copy link
Author

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.

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

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);

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);

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);
},

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) => {

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]
  );

Copy link
Author

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
);
};

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]
  );

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),

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]);

Copy link
Author

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!

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.

4 participants