title: Turn-based Game Design: Event-Driven vs. Game Loop
body: <p>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.</p>
  
  <p>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.</p>
  
  <p>My understanding is that my options for trying to implement what I need are either to:</p>
  
  <ol>
  <li>Implement a completely event-driven program which has no such game loop, or</li>
  <li>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.</li>
  </ol>
  
  <p>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 <code>SwingWorker</code> 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.</p>
  
  <p>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 <code>SwingWorker</code> 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.</p>
  
  <p>Here are my specific questions:</p>
  
  <ol>
  <li>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)?</li>
  <li>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?</li>
  <li>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 <code>SwingWorker</code>) 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.</li>
  </ol>
  
  <p>Currently, I have my code organized using the MVC pattern.  My controller is where my game loop (the actual <code>SwingWorker</code> 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".</p>
  
  <p><strong><code>SwingWorker</code> code from controller:</strong></p>
  
  <pre><code>swingWorker = new SwingWorker&lt;Void, Model&gt;() {
  @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&lt;Model&gt; chunks) {
  Model gameModel = chunks.get(chunks.size() - 1);
  //hard-coded for 6 players
  for (int i = 0; i &lt; 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();
  </code></pre>