Ejemplo n.º 1
0
  /** Generates the thread that runs the main game loop */
  private void makeGameThread() {
    Thread t =
        new Thread() {
          @Override
          public void run() {
            // run the loop when the game has started,
            // hasn't been stopped, and isn't finished
            while (!isFinished && hasStarted && !gameStopped) {
              // if paused, sleep thread
              while (paused) {
                // sleep for a while and wait to see if paused state has changed
                try {
                  sleep(100);
                } catch (InterruptedException e) {
                }
              }

              // run main gameLoop
              model.getNextRobot();
              model.doTurn();
              isFinished = model.isFinished();

              // update board
              gameBoard.repaint();

              // update results
              gameInfo.revalidate();
              gameInfo.repaint();

              // sleep and wait to see if user has given any input
              try {
                sleep((long) waitTime);
              } catch (InterruptedException e) {
              }
            }

            // code to build updates and display results when game finishes
            if (isFinished) {
              gameInfo.repaint();
              gameBoard.repaint();

              model.buildUpdate();
            }
          }
        };

    // start up the thread
    t.start();
  }