Exemplo n.º 1
0
  @Override
  public void start() {
    // save the number of characters in the game to a local int variable
    numCharactersInGame = allCharacters.size();

    /*store a local copy of the shared Random instance from the Utils class
    we will use this to decide who is attacking and who is defending on each turn of the game*/
    Random sharedRandomGen = Utils.getSharedRandomGen();
    Utils.printSystemMessage(
        "How many turns to you want to give characters playing the game? \n\tEnter a number.");

    if (userInputScanner.hasNext()) {
      userInput = userInputScanner.next();
    }

    // use a try-catch block to catch any errors that occur with user input
    try {
      // try to parse the number from the user input string
      setNumTurns(Integer.parseInt(userInput));
    } catch (NumberFormatException e) {
      // this error block will execute if the user does not enter a number
      Utils.printSystemMessage("Error! You were supposed to enter a number ...");

      // show yourself the error so you can find and fix it during development
      e.printStackTrace();

      Utils.printSystemMessage(
          "ConsoleGame will startInConsole with "
              + DEFAULT_NUM_TURNS_TO_TAKE
              + " turns for the characters to interact.");
      // but, let the user continue with a default number of turns
      setNumTurns(1000);
    }

    play(numTurnsToTake);
  }