
// ZuulGame.java
// Andrew Davison, Nov 2018, ad@fivedots.coe.psu.ac.th


/* "World of Zuul" is the beginnings of a simple, text based adventure game.  
   Users can walk around rooms, and that's all.

   This version contains some **VERY BAD** design. It should **NOT**
   be used as a basis for a real game. It is meant to be an example to discuss 
   good and bad design. Look in the Zuul2\ directory for a better version.

   -----
   This class creates all the rooms, creates the parser, and starts the game.  
   It evaluates and executes the commands that the parser returns.
 
 * @author  Michael Kolling and David J. Barnes
 * @version 2006.03.30
*/


public class ZuulGame 
{
  private Parser parser;
  private Room currRoom;  // the room where the user is currently located
        

  public ZuulGame() 
  { createRooms();
    parser = new Parser();
  }


  private void createRooms()
  // Create all the rooms and link them together.
  {
    // create the rooms
    Room outside = new Room("outside the main entrance of the university");
    Room theatre = new Room("in a lecture theatre");
    Room pub = new Room("in the campus pub");
    Room lab = new Room("in a computing lab");
    Room office = new Room("in the computing admin office");
        
    // link the room exits
    outside.setExits(null, theatre, lab, pub);
    theatre.setExits(null, null, null, outside);
    pub.setExits(null, outside, null, null);
    lab.setExits(outside, office, null, null);
    office.setExits(null, null, null, lab);

    currRoom = outside; // the user starts in the 'outside' room
  }  // end of createRooms()


  public void play() 
  {            
    printWelcome();

    Command cmd;
    boolean isFinished = false;
    while (!isFinished) {
      cmd = parser.getCommand();       // get a command
      isFinished = processCommand(cmd);  // process it
    }
    System.out.println("Thank you for playing. Goodbye.");
  }  // end of play()


  private void printWelcome()
  {
    System.out.println();
    System.out.println("Welcome to the World of Zuul!");
    System.out.println("Type 'help' if you need help.");
    System.out.println();
    System.out.println("You are " + currRoom.description);
    System.out.print("Exits: ");
    if (currRoom.northExit != null)
      System.out.print("north ");
    if (currRoom.eastExit != null)
      System.out.print("east ");
    if (currRoom.southExit != null)
      System.out.print("south ");
    if (currRoom.westExit != null)
      System.out.print("west ");
    System.out.println();
  }  // end of printWelcome()


  private boolean processCommand(Command cmd) 
  {
    boolean isFinished = false;

    if (cmd.isUnknown()) {
      System.out.println("Sorry, I don't know what you mean.");
      return false;
    }

    String cmdWord = cmd.getFirstWord();
    if (cmdWord.equals("help"))
      printHelp();
    else if (cmdWord.equals("go"))
      goDirection(cmd);
    else if (cmdWord.equals("quit"))
      isFinished = tryQuit(cmd);
    // else ignore any other command words

    return isFinished;
  }  // end of processCommand()


  // ------------------ process user commands ----------------------


  private void printHelp() 
  {
    System.out.println("Please wander around at the university.");
    System.out.println();
    System.out.println("Your command words are:");
    System.out.println("   go  quit  help");
  }


  private void goDirection(Command command) 
  /* Try to go in the specified direction. If there is an exit, enter
     the new room, otherwise print an error message. */
  {
    if (!command.hasSecondWord()) {
      // if there is no second word, we don't know where to go...
      System.out.println("Go where?");
      return;
    }

    String dir = command.getSecondWord();

    Room nextRoom = null;       // try to leave the current room
    if (dir.equals("north"))
      nextRoom = currRoom.northExit;
    if (dir.equals("east"))
      nextRoom = currRoom.eastExit;
    if (dir.equals("south"))
      nextRoom = currRoom.southExit;
    if (dir.equals("west"))
      nextRoom = currRoom.westExit;

    if (nextRoom == null)
      System.out.println("There is no room in that direction!");
    else {
      currRoom = nextRoom;
      System.out.println("You are " + currRoom.getInfo());

      System.out.print("Exits: ");
      if (currRoom.northExit != null)
        System.out.print("north ");
      if (currRoom.eastExit != null)
        System.out.print("east ");
      if (currRoom.southExit != null)
        System.out.print("south ");
      if (currRoom.westExit != null)
        System.out.print("west ");
      System.out.println();
    }
  }  // end of goDirection()


  private boolean tryQuit(Command command) 
  // Check whether we really can quit the game
  {
    if (command.hasSecondWord()) {
      System.out.println("Quit what?");
      return false;
    }
    else
      return true;
  }  // end of tryQuit()


  // -------------------------------------------------------

  public static void main(String[] args) 
  {
    ZuulGame game = new ZuulGame();
    game.play();
  }

}  // end of ZuulGame class
