All pastes #373856 Raw Edit

MikeH

public text v1 · immutable
#373856 ·published 2007-02-27 01:37 UTC
rendered paste body
import java.util.Scanner;

public class MazeTUI{
  
  private Scanner in = new Scanner(System.in);
  public Explorer player;
  private Room tempRoom = new Room("Dummy Room",false,false);
  public static final int EXIT = 9;
  
  public MazeTUI(){
    int zero = 0;
  }
  
  /** This is the main game loop that allows the game to run until the
   * player either finds the exit or gives up and chooses to quit */
  public void start(){
    System.out.println("Enter a name for the player: ");
    String tempName = in.next();
    player = CreateMap.start(tempName);
    
    int choice = -1;
    while (choice != EXIT){
      tempRoom = player.whatRoom();
      System.out.println("You are in the " + tempRoom.roomDescription());
      System.out.flush();
      if (tempRoom.escape()) System.out.println("You have found the exit!");
      if ((tempRoom.monster() != null) && (tempRoom.monster().health() > 0)) System.out.println("There is a Denizen in the Room!");
      System.out.flush();
      displayMenu(tempRoom);
      choice = readIntWithPrompt(player.explorerName() + " please enter choice: ");
      executeChoice(choice, tempRoom);
      if ((choice == 1) && (tempRoom.escape())) choice = EXIT;
    }
  }
  
  /** This is a dynamic menu that is passed a room and prints out the available
   * options the player can do, based on the room that they are in. 
   * <p>
   * For example
   * if there is no north connection but there is an east, west, and south
   * connection in the room. It will not print the option to go north but it
   * will print the options to go east, west, south. */
  public void displayMenu(Room tempRoom){
    if (tempRoom.escape()) System.out.println("Exit the Maze......1");
    System.out.flush();
    if (tempRoom.pNorth() != null) System.out.println("Go North...........7");
    System.out.flush();
    if (tempRoom.pSouth() != null) System.out.println("Go South...........2");
    System.out.flush();
    if (tempRoom.pEast() != null) System.out.println("Go East............6");
    System.out.flush();
    if (tempRoom.pWest() != null) System.out.println("Go West............4");
    System.out.flush();
    if ((tempRoom.monster() != null) && (tempRoom.monster().health() > 0)) System.out.println("Attack Denizen.....5");
    System.out.flush();
    System.out.println("Quit...............9");
    System.out.flush();
  }
  
  /** This is a dynamic command executer which is given the choice
   * that was input by the player and the room that the player is in
   * and if the choice exists for that room, it executes the choice.
   * If the choice does not exist for that room (i.e. they try to go
   * north when there is no north), it displays "please enter a valid
   * option"*/
  public void executeChoice(int choice, Room tempRoom){
    if((choice == 7) && (tempRoom.pNorth() != null)) {
      LockablePassageway tempPass = tempRoom.pNorth();
      player.move(tempPass.otherRoom(tempRoom));
    }
    else if((choice == 2) && (tempRoom.pSouth() != null)) {
      LockablePassageway tempPass = tempRoom.pSouth();
      player.move(tempPass.otherRoom(tempRoom));
    }
    else if((choice == 6) && (tempRoom.pEast() != null)) {
      LockablePassageway tempPass = tempRoom.pEast();
      player.move(tempPass.otherRoom(tempRoom));
    }
    else if((choice == 4) && (tempRoom.pWest() != null)) {
      LockablePassageway tempPass = tempRoom.pWest();
      player.move(tempPass.otherRoom(tempRoom));
    }
    else if((choice == 5) && (tempRoom.monster() != null)) {
      Explorer.explorerAttack(player, tempRoom.monster());
      if (tempRoom.monster().health() > 0) {
        Denizen.denizenAttack(tempRoom.monster(),player);
        System.out.println("The monster has attacked you!");
        System.out.println("Your life is now: " + player.health() + "%");
        System.out.println(" ");
        
      }
      else
        System.out.println("You killed the Denizen!");
    }
    else if(choice == EXIT){
      System.out.println("You have chose to quit. Goodbye!");
    }
    else if ((choice == 1) && (tempRoom.escape())) {
      System.out.println("Congratulations! You escaped the maze.");
    }
    else
      System.out.println("You did not enter a valid option");
  }
  
  /** This method continues to loop an input prompt until the player
   * enters an integer.*/
  public int readIntWithPrompt(String toDisplay){
    System.out.println(toDisplay);
    while(!in.hasNextInt()){
      System.out.println("Please enter an integer choice from the menu");
      in.next();
    }
    return in.nextInt();
  }
  
}