If I understand you correctly, you are trying to implement some kind of State Pattern for you game. Instead of calling while(game.getGameState() == State.INCOMPLETE)
during each loop cycle, I recommend to move the game state logic entirely away from the main loop and have an abstract state class instead:
public abstract class AbstractGameState{
protected abstract void update(float delta);
}
Then you could have another abstract state class for your turns, that only executes the updates, if the game is not yet won. Otherwise it will end the game.
public abstract class TurnGameState extends AbstractGameState{
protected final void updateState(float delta){
if (isWinConditionSatisfied()) {
// end the game by setting the final state
TurnBasedGame.currentState = new EndGameState();
} else{
update(delta);
}
}
protected abstract void update(float delta);
private boolean isWinConditionSatisfied() {
// somehow check if the game is won
return false;
}
}
Each part of your turn-based logic can represent one TurnGameState
, that will be updated from the main game loop, and you have an additional EndGameState
, that is entered once the game is won:
public class EndGameState extends AbstractGameState{
@Override
protected void updateState(float delta) {
// this simple implementation just ends the game loop
TurnBasedGame.gameIsRunning = false;
}
}
Here is a very simplified game loop, that uses this concept:
public class TurnBasedGame {
private static boolean gameIsRunning;
private static AbstractGameState currentState;
public static void main(String[] args) {
while (gameIsRunning) {
// very simplified game loop...
// somehow handle the delta for the game loop...
float delta;
// update the game logic
doGameUpdates(delta);
// render game graphics here...
render();
}
}
private static void doGameUpdates(float delta) {
currentState.update(delta);
}
}
Of course, all the code I have posted is very simplified and just tries to illustrate the general concept. E.g. you may perhaps want to have a GameStateManager
class, that handles adding and removing of states, and you will of course have a more complex main game loop etc.
For an overview of the involved concepts, have a look at the following resources: