I'm creating a game in Android Studio. The game has a Easy mode and a Hard mode (respectively hard=False and hard=True).
Both the Easy mode and the Hard mode have their own class. Both these classes contain the same functions, but their implementation differs. I'm calling the functions in these classes from my Gameplay class. This means that in every function in my Gameplay class, I have to do the following:
if (hard == True):
HardGameplay gameplayMode = new HardGameplay();
else:
EasyGameplay gameplayMode = new EasyGameplay();
functionResult = gameplayMode.myFunction();
However, instead of performing this if-else check in each function, I'd like to check this just once. Therefore, I have created a checkDifficulty() function, which I call at the start of my onCreate:
public ??? gameplayMode;
public Boolean easyOrHard() {
if (hard == True):
HardGameplay gameplayMode = new HardGameplay();
else:
EasyGameplay gameplayMode = new EasyGameplay();
}
The problem is, I don't know what type I should place at the ???. It should be either HardGameplay or EasyGameplay, but I don't know which of these two in advance. Putting Object there doesn't work either, because then, each function I call gives the error Cannot resolve method 'myFunction()'.
Does anybody know what to do? Or maybe there's an easier implementation I'm overlooking? Any help is greatly appreciated!