private void startSaveGame() {
    this.console.println("\nEneter the file path for the file where the " + "game is to be saved.");

    String filePath = this.getInput();

    try {
      GameControl.saveGame(PiratesOfTheSevenSeas.getCurrentGame(), filePath);
    } catch (Exception ex) {
      ErrorView.display("MainMenuView", ex.getMessage());
    }

    GameMenuView gameMenu = new GameMenuView();
    gameMenu.display();
  }
    @Override
    public String getInput() {

      // Scanner keyboard = new Scanner(System.in); //get infile for keyboard
      String value = ""; // value to be returned
      boolean valid = false; // initialize to not valid

      try {
        while (!valid) { // loop while an invalid is entered
          this.console.println(this.displayMessage);

          // value = keyboard.nextLine();// get next line typed on keyboard
          value = this.keyboard.readLine(); // get next line typed on keyboard
          value = value.trim(); // trim of leading and trailing blanks

          if (value.length() < 1) { // value is blank
            ErrorView.display(
                this.getClass().getName(),
                "Invalid value: value cannot be blank, please enter a value");
            continue;
          }
          break; // end loop
        }
      } catch (Exception e) {
        ErrorView.display(this.getClass().getName(), "Error reading input:" + e.getMessage());
      }

      return value; // return value entered
    }
  @Override
  public boolean doAction(Object obj) {
    String value = (String) obj;

    value = value.toUpperCase(); // convert to all upper case

    int newChoice = 0;

    try {
      newChoice = Integer.parseInt(value);
    } catch (NumberFormatException nf) {
      ErrorView.display(
          this.getClass().getName(),
          "\nYou must enter a valid number." + " Try again of enter Q to quit.");
    }

    switch (newChoice) {
      case 1: // Choice A
        this.console.println("\n***Choice 1 Selected***");
        break;
      case 2: // Choice B
        this.console.println("\n***Choice 2 Selected***");
        break;
      default:
        ErrorView.display(this.getClass().getName(), "\n***Invalid Selection.  Try Again.***");
        break;
    }
    return true;
  }
  public String getInput() {

    boolean valid = false;
    String value = null;

    try {
      while (!valid) {
        // loop while an invalid value is entered
        this.console.println("\nChoose an Option:");

        value = this.keyboard.readLine(); // get next lyne typed on keyboard
        value = value.trim(); // trim off leading and trailing blanks

        if (value.length() < 1) { // value is blank
          ErrorView.display(this.getClass().getName(), "You must enter a value");
          continue;
        }

        break; // end the loop
      }

    } catch (IOException e) {
      ErrorView.display(this.getClass().getName(), "Error reading input: " + e.getMessage());
    }

    return value; // return the value entered
  }
 @Override
 public void display() {
   // display instructions
   this.console.println(this.menu);
   // get position
   Planet currentLocation = andromeda260.Andromeda260.getPlayer().getLocation();
   int curow = currentLocation.getRow();
   int cucolumn = currentLocation.getColumn();
   int column = 0;
   int row = 0;
   double fuelN = 0;
   boolean valid = false;
   while (!valid) {
     try {
       try {
         // display map
         this.console.println(andromeda260.Andromeda260.getGame().getMap().getMapString());
         // display position
         this.console.println(
             "You are currently at row " + (curow + 1) + " in column " + (cucolumn + 1));
         // get row destination
         this.console.println(
             "What row of planets would you like to venture towards? (1, 2, or 3)");
         row = (int) (getNumberInput() - 1);
         // get column destination
         this.console.println(
             "What column of planets in row " + (row + 1) + " would you like to visit?");
         column = (int) (getNumberInput() - 1);
         // calculate distance to new location
         double distance = MapControl.distanceToPlanet(curow, cucolumn, row, column);
         // checks to verify you have enough fuel
         fuelN = MapControl.fuelNeeded(distance);
         double fuelA = ShipControl.fuelAvailable();
         if (fuelA >= fuelN) {
           this.console.println("You have enough fuel to get there");
         } else {
           ErrorView.display(
               this.getClass().getName(),
               ("You don't have enough fuel to get there." + "\nTry again."));
         }
       } catch (NumberFormatException nfe) {
         ErrorView.display(this.getClass().getName(), "Invalid option, Lets try that again.");
       }
       andromeda260.Andromeda260.getPlayer().getLocation().setColumn(column);
       andromeda260.Andromeda260.getPlayer().getLocation().setRow(row);
       Planet destination = ShipControl.getCurrentPlanet();
       valid = true;
     } catch (ArrayIndexOutOfBoundsException aioobe) {
       ErrorView.display(
           this.getClass().getName(),
           "\nThe coordinates you wish to explore do not exist."
               + "\nYour navigator looks at you in disbelief");
       andromeda260.Andromeda260.getPlayer().getLocation().setColumn(cucolumn);
       andromeda260.Andromeda260.getPlayer().getLocation().setRow(curow);
     }
   }
   ShipControl.useFuel(fuelN);
 }
  @Override
  public boolean doAction(String choice) {

    choice = choice.toUpperCase();

    switch (choice) {
      case "R":
        {
          try {
            this.repairTheShipMenu();
          } catch (IOException ex) {
            ErrorView.display("repairTheShipMenu", ex.getMessage());
          } catch (SpaceShipControlException ex) {
            ErrorView.display("repairTheShipMenu", ex.getMessage());
          }
        }
        break;
      case "F":
        {
          try {
            this.refuelTheShipMenu();
          } catch (SpaceShipControlException | IOException ex) {
            ErrorView.display("refuelTheShipMenu", ex.getMessage());
          }
        }
        break;
      case "J":
        this.jobBoardMenu();
        break;
      case "S":
        this.shopMenu();
        break;
      case "T":
        this.trainingRoomMenu();
        break;
      case "C":
        {
          try {
            this.checkValue();
          } catch (ItemControlException | IOException ex) {
            ErrorView.display("checkValue", ex.getMessage());
          }
        }
        break;
      case "X":
        this.checkShipValue();
        break;
      default:
        this.console.println("\n*** Invalid selection *** Try again");
        break;
    }
    return false;
  }
  public SpaceEventView() {
    super("\nYou have arrived in Sector X");

    // This is currently showing up after the event - fix this later.

    Random rand = new Random();
    RandomControl event = new RandomControl();
    int spaceevent = event.showRandomInteger(0, 2, rand);

    switch (spaceevent) {
      case 0: // Asteroid Hit
        this.console.println(
            "An asteroid has hit your ship!"
                + "\n\n 1. Call all hands to go investigate the strike point!"
                + "\n\n 2. Send one engineer to investigate.");
        break;
      case 1: // Alien Attack
        this.console.println(
            "Little Green Men are attacking your ship!"
                + "\n\n 1. Battle Stations! Let's fight them off!"
                + "\n\n 2. Run away! Run Away");
        break;
      case 2: // Space Disease
        this.console.println(
            "A virus has infected the ship, your crew are dying!"
                + "\n\n 1. Let's look for a cure down on the planet!"
                + "\n\n 2. Let the Doctor handle it, he knows what he's doing.");
        break;
      default:
        ErrorView.display(this.getClass().getName(), "\n***Invalid Selection.  Try Again.***");
        break;
    }
  }
  public void printReport(String outputLocation) {

    SuspectList[] suspectList = SuspectListControl.createSuspectList();

    try (PrintWriter out = new PrintWriter(outputLocation)) {

      // print title and column headings
      out.println("\n\n          CASE & SUSPECT LISTS          ");
      out.printf("%n%-15s%15s", "Cases Involved", "Suspects");
      out.printf("%n%-15s%15s", "--------------", "--------");

      for (int i = 0; i < suspectList.length; i++) {
        out.printf(
            "%n%-15s%15s", suspectList[i].getCaseInvolved(), suspectList[i].getSuspectName());

        if (suspectList[i] == suspectList[suspectList.length - 1])
          this.console.println("Report printed successfuly");
      }
    } catch (IOException ex) {
      ErrorView.display(this.getClass().getName(), "I/O Error: " + ex.getMessage());
    }

    GamePlayMenuView gamePlayMenu = new GamePlayMenuView();
    gamePlayMenu.display();
  }
  public boolean doAction(char input) {
    try {
      switch (input) {
        case 'N':
          moveNorth();
          break;
        case 'G':
          moveEast();
          break;
        case 'H':
          moveSouth();
          break;
        case 'S':
          moveWest();
          break;
        case 'Q': // Quit the program
          return true;
        default:
          System.out.println("\n*** This is an invalid selection *** Please try again");
          break;
      }
      return true;
    } catch (Exception e) {
      e.printStackTrace();

      System.out.print("Error reading input: " + e.getMessage());
      ErrorView.display(this.getClass().getName());
    } finally {
      System.out.close();
    }
    return false;
  }
 @Override
 public boolean doAction(Object obj) {
   String value = (String) obj;
   char choice = value.charAt(0);
   if (choice == 'R') {
     return false;
   } else {
     ErrorView.display(getClass().getName(), "\n*** Invalid Input Please Try Again ***");
   }
   return true;
 }
  private void displaySaveGameMenu() {
    this.console.println("\n\nEnter the file path for the file where the game is to be saved.");
    String filePath = this.getInput();

    try {
      // save the game to the specified file
      GameControl.saveGame(TheGreatEscape.getCurrentGame(), filePath);
    } catch (Exception ex) {
      ErrorView.display("MainMenuView", ex.getMessage());
    }
  }
  private void startLoadGame() {
    this.console.println("\nEnter the file path for the file where the " + "game was saved.");

    String filePath = this.getInput();

    try {
      GameControl.getSavedGame(filePath);
    } catch (Exception ex) {
      ErrorView.display("MainMenuView", ex.getMessage());
    }

    GameMenuView gameMenu = new GameMenuView();
    gameMenu.display();
  }
Beispiel #13
0
  @Override
  public char getInput() {
    //        boolean valid = false;
    String input = "";
    char rtn = 0;

    try {
      while (input.length() < 1) {

        input = keyboard.readLine();
        input = input.trim();

        if (input.length() < 1) {
          ErrorView.display(this.getClass().getName(), "Please select an option");
        } else {
          rtn = input.toUpperCase().charAt(0);
        }
      }
    } catch (Exception e) {
      ErrorView.display(this.getClass().getName(), "Error reading input: " + e.getMessage());
    }

    return rtn;
  }
  @Override
  public boolean doAction(Object obj) {
    String value = (String) obj;
    value = value.toUpperCase();
    char choice = value.charAt(0);
    Actor actor = null;

    switch (choice) {
      case 'S': // start a new game
      case 's':
        {
          try {
            this.startNewGame();
          } catch (MapControlException mce) {
            ErrorView.display("MainMenuView", mce.getMessage());
          }
        }
        break;
      case 'C': // continue previous game
      case 'c':
        this.continueGame();
        break;
      case 'H': // display the help menu
      case 'h':
        this.displayHelpMenu();
        break;
      case 'E': // exit the program
      case 'e':
        return true;
      default:
        ErrorView.display(
            this.getClass().getName(), "\n*** Invalid Menu selection *** Try Again ***");
        break;
    }
    return false;
  }
  private void movePlayer() {
    Game game = PemberleyGame.getCurrentGame();
    String roomDescription = "";
    Player player = game.getPlayerOne();
    String prompt = "Which direction do you want to go? Type X to exit.";
    String playerSelection;
    do {
      playerSelection = this.getStringInput(prompt);
      MapControl mapControl = new MapControl();
      try {
        roomDescription = mapControl.playerNavigate(playerSelection, player);
      } catch (MapControlException me) {
        ErrorView.display(this.getClass().getName(), me.getMessage());
      }

      this.console.println(roomDescription);
    } while (!playerSelection.equalsIgnoreCase("x"));
  }
  private void continueGame() {
    // call function to recall saved game
    // prompt for and get the name of the file to save the game in
    System.out.println("\n\nEnter the file path for file where the game " + "was saved.");

    String filePath = this.getInput();

    try {
      // start a saved game
      GameControl.getSavedGame(filePath);

    } catch (Exception ex) {
      ErrorView.display("MainMenuView", ex.getMessage());
    }

    // display the game menu
    GameMenuView gameMenu = new GameMenuView();
    gameMenu.display();

    // LoadSavedGameView loadSavedGameView = new LoadSavedGameView();
    // loadSavedGameView.LoadSavedGame();
  }
  // author Sheila
  private void speakToActor() {
    Game game = PemberleyGame.getCurrentGame();

    int itemSelection = 1; // start at one so first item in list isn't zero
    String playerSelection = " ";

    Actor[] actorNames = game.getLocalActorArray(); // get list of actors in the current location

    for (int i = 0; i < actorNames.length; i++) {
      if (actorNames.length == 0) {
        this.console.print("NO ONE IS HERE");
        return; // no selection possible, return to menu
      }
      // actors available to speak - return list
      else {
        this.console.println(
            "These actors are here: \n" + itemSelection + " " + actorNames[i].getName());
        itemSelection++; // increments number
        String prompt = "\nType the number of the actor you want to speak to? Type X to exit";
        do {
          playerSelection = this.getStringInput(prompt);
          if (playerSelection.equalsIgnoreCase(
              "x")) { // without this here, the 'x' is seen as an error.
            break;
          }

          try {
            itemSelection = Integer.parseInt(playerSelection); // converts string input to int
            this.console.println(
                actorNames[itemSelection - 1].getCharacterScript()); // -1 returns selection to zero
          } catch (NumberFormatException nf) {
            ErrorView.display(this.getClass().getName(), nf.getMessage());
          }
        } while (!playerSelection.equalsIgnoreCase("x"));
      }
    }
  }
  private void takeItem() {
    // declare variables
    Game game = PemberleyGame.getCurrentGame();
    Player player = game.getPlayerOne();
    ItemControl itemControl = new ItemControl();
    Item[] localItemArray = game.getLocalItemArray();
    String gameMessage = " ";
    int indexOfItem;
    int quantityOfItem = 1;
    String playerSelection;
    InventoryControl inventoryControl = new InventoryControl();
    // designate the inventory
    Inventory inventory = player.getInventory();
    // while player's selection is not X loop

    do {
      // if there is nothing in the localItemArray say there is nothing to take.
      if (localItemArray.length == 0) {
        this.console.println("Nothing to Take");
        break;
      } else {
        String prompt = "What do you want to take? Type X to exit.";
        // get input from the player
        playerSelection = this.getStringInput(prompt);
        // see if the player's selected item is in the room. return it's index in the array.
        indexOfItem = itemControl.getItemIndex(playerSelection, player, localItemArray);

        if (indexOfItem != -1) { // run this code if there was an index match
          // set the item to whatever item matched the players selection
          Item selectedItem = localItemArray[indexOfItem];
          // if the item's multiple attribute is true run this code
          if (selectedItem.isMultiple() == true) {
            prompt = "How Many do you want to take? (1 - 9)";
            // get the players selection and make sure it is an integer
            playerSelection = this.getStringInput(prompt); // gets a string
            // make the player's selection an integer
            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.");
            }
            // call the takeMultipleItem function.
            try {
              gameMessage =
                  inventoryControl.takeMultipleItem(selectedItem, quantityOfItem, inventory);
            } catch (InventoryControlException ie) {
              ErrorView.display(this.getClass().getName(), ie.getMessage());
            }
          } else { // run this code if the item's multiple attribue is not true
            quantityOfItem = 1;

            // call the takeSingleItem function
            try {
              gameMessage =
                  inventoryControl.takeSingleItem(selectedItem, quantityOfItem, inventory);
            } catch (InventoryControlException ie) {
              ErrorView.display(this.getClass().getName(), ie.getMessage());
            }
          }

        } else {
          // if the player's selection is not in the array send this message
          gameMessage = "Not sure what you are trying to take.";
        }
      }
      this.console.println(gameMessage);

    } while (!playerSelection.equalsIgnoreCase("X")); // loop
  }
  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"));
  }