Skip to content

module#2 (Mahmud)#38

Open
Mahmud-007 wants to merge 2 commits intorecruit-org:module-2from
Mahmud-007:module-2
Open

module#2 (Mahmud)#38
Mahmud-007 wants to merge 2 commits intorecruit-org:module-2from
Mahmud-007:module-2

Conversation

@Mahmud-007
Copy link
Copy Markdown

added poison and if snake eats a poison game will restart

@Mahmud-007 Mahmud-007 changed the title add poison feature add poison feature (Mahmud) Apr 16, 2022
@Mahmud-007 Mahmud-007 changed the title add poison feature (Mahmud) module#2 (Mahmud) Apr 16, 2022
setFoods((currentFoods) => [...currentFoods, newFood]);
}, [isFood, isSnake]);
}, [isAllowedCell]);

Copy link
Copy Markdown

@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.

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

)
);
}, []);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

we can create a reusable removeObject(type) function like addObject

[foods]
);
const isPoison = useCallback(
({ x, y }) => poisons.find((poison) => poison.x === x && poison.y === y),
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

food and poison are basically the same but different types. We can check food or poison with the same function.

useInterval(removePoisons, 100);
useInterval(runSingleStep, 200);
useInterval(()=>addObject(setFoods), 3000);
useInterval(()=>removeObject(setFoods), 100);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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.

// );
// }, []);

const removeObject = useCallback((setState)=>{
Copy link
Copy Markdown

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.

Copy link
Copy Markdown
Author

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.

Copy link
Copy Markdown

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.

removeObject(type){
if(type===CellType.Food)//do something for food
else if (type===CellType.Poison)//do something for poison
}

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.

Copy link
Copy Markdown

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 .

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.

3 participants