13

I am creating my first game in Java. The game is Monopoly. I am struggling with how I should design the game to model its turn-based structure (managing player turns). I want to allow for both a single human-controlled and one or multiple AI-controlled players to play the game.

My specific issue is that I do not know whether to implement a game loop or not, meaning a loop which can manage the players and the variables directly related to the game of Monopoly, (think of things such as prompting each player for their turn, incrementing the turn to the next player, or getting dice rolls from each player—in turn). I am not referring to the more low-level meaning of the term “game loop” which relates more to drawing frames on the screen, updating physics, or updating AI at a specific rate of time.

My understanding is that my options for trying to implement what I need are either to:

  1. Implement a completely event-driven program which has no such game loop, or
  2. Implement a game loop—something that is long-running in the background and basically never-ending as long as the game is running. This would be the more procedural approach.

When I first started trying to solve this issue, I ran into problems of my UI freezing because my game loop was never-ending and was completely consuming the thread on which it was running (I just made a very simple while loop to illustrate this). So I went to the effort of creating a SwingWorker to encapsulate my game loop. That solved the issue of UI freezes, but still left me wondering if I was going down the wrong path.

As a general rule, I found that most advice on the web generally seems to favor any approach which is event-driven, and thus my current implementation utilizing a SwingWorker could be a step in the wrong direction. But I cannot fully grasp how I would implement a completely event-driven system for this specific task (meaning no game loop present). It seems to me that a loop has to exist somewhere for managing the player turns.

Here are my specific questions:

  1. Are game loops (as I am describing them) appropriate for turn-based games such as Monopoly—specifically for queuing the player turns and prompting the appropriate player for their turn, one player at a time (and queuing the entire procedure of/sequence of steps that comprise a turn)?
  2. If a purely event-driven system were to be created for managing player turns, how do you iterate through each player to prompt them for their turn and keep iterating until the game ends?
  3. If a game loop were to be used to solve the specific problem described above, does it have to be run within its own thread (possibly using SwingWorker) in order to avoid freezing the UI? My situation is Java-specific, but I suppose I would be interested in answers for non-Java-specific situations as well.

Currently, I have my code organized using the MVC pattern. My controller is where my game loop (the actual SwingWorker thread) resides. It is far from complete, but it helps illustrate how I am managing player turns in what I am calling a "game loop".

SwingWorker code from controller:

swingWorker = new SwingWorker<Void, Model>() {
@Override
protected Void doInBackground() throws InterruptedException {
gameLoopRunning = true;
while (gameLoopRunning) {

    //to do: use a timer instead of thread.sleep
    Thread.sleep(1000);

    //user turn prompt
    if (model.getActivePlayer().isUserControlled()) {

        boolean userRolled = false;
        while(!userRolled) {
            System.out.println("Roll the dice please...");
            Thread.sleep(3000);
        }

    }
    //bot turn prompt
    else {
        //insert code for bot rolling dice here
        model.rollDice();
    }

    publish(model);

    Thread.sleep(1000);
    model.incrementPlayerTurn();
    publish(model);

}
return null;
}

@Override
protected void process(List<Model> chunks) {
Model gameModel = chunks.get(chunks.size() - 1);
//hard-coded for 6 players
for (int i = 0; i < 6; i++) {
    view.getPlayerPanel(i).setTurn(gameModel.getPlayers().get(i).isTurn());
}
view.getGamePanel().getDice().setDie1(model.getDie1());
view.getGamePanel().getDice().setDie2(model.getDie2());
}

};
swingWorker.execute();
nairware
  • 2,776
  • 8
  • 31
  • 56
  • Monopoly is not much different from chess, to me it makes more sense to let it be eventdriven as it will allow the implementation of the rules to be much easier, like in chess, because each event tries to affect some state. – arynaq Jul 25 '13 at 21:54
  • No matter how you implement it, you probably don't want your gameplay CPU cycles being done on the UI thread. – Tim Jul 25 '13 at 21:56
  • 1
    what about using a finite state machine to represente game state changes ? I once used a crude one to develop a checkers game, with event-based UI code. – SirDarius Jul 25 '13 at 22:22

1 Answers1

11

The comment from SirDarius is spot on.

Though, for something as simple as advancing player turns, you don't really need to bother implementing a full fledged finite state machine.

In terms of MVC, this is what you should do for human players:

  • The Model: Provide methods for advancing the active player to the next player and for running through the "turn process" (i.e. rolling the dice, moving the active player's token, etc.). Because much of the turn process is event driven, these method calls will be made from event listeners in the controller.

  • The View: Raise an event when the active player finishes their turn, as well as raising events on various other input.

  • The Controller: Whenever a player finishes their turn, tell the model to advance to the next player, and start the "turn process" again. Whenever a player provides input, an event will be fired that tells the model to advance to the next stage of the turn.

For AI players, the majority of the same code can be used, but it does not make sense to have the turn progression be driven by the view. Instead, the model would need to have another "turn process" method which would be specifically for AI players. The only difference being that the code would execute in succession without waiting for input from the view, instead of being a series of event listeners.

Luke
  • 7,719
  • 3
  • 43
  • 74
  • I do not follow your comment about the view. I can understand the view firing events when buttons are clicked, but as far as turns beginning and ending in Monopoly, there may not be a dedicated button for starting or ending a turn. In addition to that, what about AI bots starting and ending their turns? Clearly they do not interact with the view for managing turns, do they? – nairware Jul 26 '13 at 01:30
  • @nairware A player's turn in monopoly follows a natural progression. There are only so many tasks to perform, and one of them is the last task for any given turn. In general, the player will roll the dice, move their piece, perform an action (buy the property, pay a fine, draw a card, etc.), buy houses, and (optionally) perform trades. The turn process changes if the player is in jail, but there is still a final action for the player to perform. When the player finishes their final action (or maybe clicks a contextual "end turn" button), the turn may be ended. – Luke Jul 26 '13 at 15:00
  • I follow. Certainly an event for ending the turn makes sense. I was just unclear about why the view necessarily had anything to do with that event. I think you are assuming there is an "end turn" button, in which case your description of the view makes sense. An AI-controlled player would not click such a button though, so you are also assuming only human-controlled players. The same end turn event would fire for AI-controlled players, but not from a button click or anything view related (in my estimation anyway). – nairware Jul 26 '13 at 15:37
  • I agree. For a human player, it does make sense that the turn progression would be UI driven, but an AI player would be driven from the controller. I'll update my answer to specify tasks for AI turns. – Luke Jul 26 '13 at 15:47