Пример #1
0
  /**
   * Run the game until finished. If GUI is not initialized, the output will be sent to the console.
   */
  public void runGame() {
    while (!isGameOver()) {
      // Checking to see that the move can be made (not overflowing a column)
      boolean moveIsSafe = false;
      Move nextMove = null;
      while (!moveIsSafe) {
        Move[] bestMoves = activePlayer.getMoves(board);
        if (bestMoves.length == 0) {
          gui.setMsg("Game cannot continue until a Move is produced.");
          continue;
        } else {
          nextMove = bestMoves[0];
        }
        if (board.getTile(0, nextMove.getColumn()) == null) {
          moveIsSafe = true;
        } else {
          gui.setMsg("Illegal Move: Cannot place disc in full column. Try again.");
        }
      }

      board.makeMove(nextMove);
      if (gui == null) {
        System.out.println(nextMove);
        System.out.println(board);
      } else {
        gui.updateGUI(board, nextMove);
      }
      activePlayer = (activePlayer == player1 ? player2 : player1);

      // The following code causes a delay so that you can easily view the plays
      // being made by the AIs
      try {
        Thread.sleep(SLEEP_INTERVAL);
      } catch (InterruptedException e) {
        e.printStackTrace();
      }
    }

    if (gui == null) {
      if (winner == null) {
        System.out.println("Tie game!");
      } else {
        System.out.println(winner + " won the game!!!");
      }
    } else {
      gui.notifyGameOver(winner);
    }
  }