Exemplo n.º 1
0
  private void giveItem() {
    Game game = PemberleyGame.getCurrentGame();

    if (game.getInventoryItemNames().length == 0 || game.getLocalActorNames().length == 0) {
      this.console.println("You have nothing to give or there is no one to give something to.");
      return;
    }

    ItemControl itemControl = new ItemControl();
    ActorControl actorControl = new ActorControl();
    InventoryControl inventoryControl = new InventoryControl();
    String prompt = "What do you want to give?";
    Player player = game.getPlayerOne();
    Item[] inventoryItems = game.getInventoryItemArray();
    Actor[] actorArray = game.getLocalActorArray();
    String gameMessage = " ";
    int quantityOfItem = 1;
    Item itemToGive;
    Actor actorToGive;
    // check to see if there is anything to use.

    this.console.println("These things are in your inventory\n");

    for (String i : game.getInventoryItemNames()) {
      this.console.print(i + "\n");
    }

    String playerSelection;
    do {
      playerSelection = this.getStringInput(prompt);

      int indexOfItem = itemControl.getItemIndex(playerSelection, player, inventoryItems);

      try {
        itemToGive = inventoryItems[indexOfItem];
      } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) {
        ErrorView.display(this.getClass().getName(), "\nNot Sure what you are trying to give.");
        return;
      }

      if (itemToGive.getQuantity() > 1) {
        prompt = "How many " + itemToGive.getName() + " do you want to give?";
        playerSelection = this.getStringInput(prompt);

        try {
          quantityOfItem = Integer.parseInt(playerSelection); // converts string to int

        } catch (NumberFormatException nf) {
          ErrorView.display(
              this.getClass().getName(),
              "\nYou must enter a valid number." + " Try again or type X to exit.");
          break;
        }

      } else {
        quantityOfItem = 1;
      }

      this.console.print("\nThese people are here:");
      prompt = "Who do you want to give the " + itemToGive.getName() + " to?\n";
      for (String i : game.getLocalActorNames()) {
        this.console.print(i + "\n");
      }

      playerSelection = this.getStringInput(prompt);

      int indexOfActor = actorControl.getActorIndex(playerSelection, player, actorArray);

      try {
        actorToGive = actorArray[indexOfActor];
      } catch (ArrayIndexOutOfBoundsException arrayIndexOutOfBoundsException) {
        ErrorView.display(
            this.getClass().getName(),
            "\nNot Sure who you are trying to give the " + itemToGive.getName() + " to.");
        return;
      }

      this.console.println(inventoryControl.giveItem(itemToGive, quantityOfItem, actorToGive));

      break;

    } while (!playerSelection.equalsIgnoreCase("x"));
  }