Advertisement
kuchuz

PBO-C 6 : Game()

Nov 26th, 2020
752
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.34 KB | None | 0 0
  1. public class Game
  2. {
  3.     private Parser parser;
  4.     private Room currentRoom;
  5.      
  6.     public Game()
  7.     {
  8.         createRooms();
  9.         parser = new Parser();
  10.     }
  11.      
  12.     private void createRooms()
  13.     {
  14.         Room outside, theater, pub, lab, office;
  15.         outside = new Room("outside the main entrance of the university");
  16.         theater = new Room("in a lecture theater");
  17.         pub = new Room("in the campus pub");
  18.         lab = new Room("in a computing lab");
  19.         office = new Room("in the computing admin office");
  20.         outside.setExits(null, theater, lab, pub);
  21.         theater.setExits(null, null, null, outside);
  22.         pub.setExits(null, outside, null, null);
  23.         lab.setExits(outside, office, null, null);
  24.         office.setExits(null, null, null, lab);
  25.         currentRoom = outside;
  26.     }
  27.      
  28.     public void play()
  29.     {            
  30.         printWelcome();
  31.         boolean finished = false;
  32.         while (! finished)
  33.         {
  34.             Command command = parser.getCommand();
  35.             finished = processCommand(command);
  36.         }
  37.         System.out.println("Thank you for playing.  Good bye.");
  38.     }
  39.      
  40.     private void printWelcome()
  41.     {
  42.         System.out.println();
  43.         System.out.println("Welcome to the World of Zuul!");
  44.         System.out.println("World of Zuul is a new, incredibly boring adventure game.");
  45.         System.out.println("Type 'help' if you need help.");
  46.         System.out.println();
  47.         System.out.println("You are " + currentRoom.getDescription());
  48.         System.out.print("Exits: ");
  49.         if(currentRoom.northExit != null)
  50.             System.out.print("north ");
  51.         if(currentRoom.eastExit != null)
  52.             System.out.print("east ");
  53.         if(currentRoom.southExit != null)
  54.             System.out.print("south ");
  55.         if(currentRoom.westExit != null)
  56.             System.out.print("west ");
  57.         System.out.println();
  58.     }
  59.      
  60.     private boolean processCommand(Command command)
  61.     {
  62.         boolean wantToQuit = false;
  63.         if(command.isUnknown())
  64.         {
  65.             System.out.println("I don't understand...");
  66.             return false;
  67.         }
  68.         String commandWord = command.getCommandWord();
  69.         if (commandWord.equals("help"))
  70.             printHelp();
  71.         else if (commandWord.equals("go"))
  72.             goRoom(command);
  73.         else if (commandWord.equals("quit"))
  74.             wantToQuit = quit(command);
  75.         return wantToQuit;
  76.     }
  77.      
  78.     private void printHelp()
  79.     {
  80.         System.out.println("You are lost. You are alone. You wander");
  81.         System.out.println("around at the university.");
  82.         System.out.println();
  83.         System.out.println("Your command words are:");
  84.         System.out.println("   go quit help");
  85.     }
  86.      
  87.     private void goRoom(Command command)
  88.     {
  89.         if(!command.hasSecondWord())
  90.         {
  91.             System.out.println("Go where?");
  92.             return;
  93.         }
  94.         String direction = command.getSecondWord();
  95.         Room nextRoom = null;
  96.         if(direction.equals("north"))
  97.             nextRoom = currentRoom.northExit;
  98.         if(direction.equals("east"))
  99.             nextRoom = currentRoom.eastExit;
  100.         if(direction.equals("south"))
  101.             nextRoom = currentRoom.southExit;
  102.         if(direction.equals("west"))
  103.             nextRoom = currentRoom.westExit;
  104.         if (nextRoom == null)
  105.             System.out.println("There is no door!");
  106.          
  107.         else
  108.         {
  109.             currentRoom = nextRoom;
  110.             System.out.println("You are " + currentRoom.getDescription());
  111.             System.out.print("Exits: ");
  112.             if(currentRoom.northExit != null)
  113.                 System.out.print("north ");
  114.             if(currentRoom.eastExit != null)
  115.                 System.out.print("east ");
  116.             if(currentRoom.southExit != null)
  117.                 System.out.print("south ");
  118.             if(currentRoom.westExit != null)
  119.                 System.out.print("west ");
  120.             System.out.println();
  121.         }
  122.     }
  123.      
  124.     private boolean quit(Command command)
  125.     {
  126.         if(command.hasSecondWord())
  127.         {
  128.             System.out.println("Quit what?");
  129.             return false;
  130.         }
  131.         else
  132.         {
  133.             return true;
  134.         }
  135.     }
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement