Beispiel #1
0
  /**
   * Main method to start a new Sudoku game. When the game starts the user is asked if they would
   * like to load a premade board from our library, or have a random board made for them. The main
   * method only creates a new instance of the class that is needed. If the user request to solve
   * the game themselves then a new HumanPlayer instance is created and the methods from that class
   * are called. If the user request to have an AI solve the game then a new AIPlayer instance is
   * created and methods from that class are called.
   *
   * @param args command line arguments
   */
  public static void main(String[] args) {
    Boolean keepPlaying = true;

    // repeat while the user wishes to play
    while (keepPlaying) {
      Sudoku game = new Sudoku();

      game.currentState.printBoard();

      // keep this here. It ask the user for an AI or Human Player
      int playOrSolve = game.playOrSolve();

      // case where the user chooses to play themselves
      if (playOrSolve == 1) {
        HumanPlayer humanPlayer = new HumanPlayer(game.currentState);
        humanPlayer.play();
      }
      // case where the user chooses to have the game solved for them
      else {
        AIPlayer aiPlayer = new AIPlayer(game.currentState);
        aiPlayer.play();
      }

      // after the user has completed the board or the AI has solved
      // it, the user is given the option to play again or quit.
      System.out.println("Would you like to play again?");

      while (game.scan.hasNext()) {
        String input = game.scan.next();
        if (input.startsWith("y") || input.startsWith("Y")) {
          break;
        }
        if (input.startsWith("n") || input.startsWith("N")) {
          keepPlaying = false;
          System.exit(0);
        } else {
          System.out.println("Please answer with either yes or no.");
        }
      }
    }
  }