001 /** 002 * This class is the main class of the "World of Zuul" application. 003 * "World of Zuul" is a very simple, text based adventure game. Users 004 * can walk around some scenery. That's all. It should really be extended 005 * to make it more interesting! 006 * 007 * To play this game, create an instance of this class and call the "play" 008 * method. 009 * 010 * This main class creates and initialises all the others: it creates all 011 * rooms, creates the parser and starts the game. 012 * 013 * @author Michael Kolling and David J. Barnes 014 * @version 2011.07.31 015 */ 016 017 public class Game 018 { 019 private Parser parser; 020 private Player player; 021 022 /** 023 * Create the game and initialise its internal map. 024 */ 025 public Game() 026 { 027 player = new Player(); 028 parser = new Parser(); 029 createRooms(); 030 } 031 032 /** 033 * Create all the rooms and link their exits together. 034 */ 035 private void createRooms() 036 { 037 Room outside, theatre, pub, lab, office; 038 039 // create the rooms 040 outside = new Room("outside the main entrance of the university"); 041 theatre = new Room("in a lecture theatre"); 042 pub = new Room("in the campus pub"); 043 lab = new Room("in a computing lab"); 044 office = new Room("in the computing admin office"); 045 046 // initialise room exits 047 outside.setExit("east", theatre); 048 outside.setExit("south", lab); 049 outside.setExit("west", pub); 050 051 theatre.setExit("west", outside); 052 053 pub.setExit("east", outside); 054 055 lab.setExit("north", outside); 056 lab.setExit("east", office); 057 058 office.setExit("west", lab); 059 060 // the player starts the game outside 061 player.setCurrentRoom(outside); 062 } 063 064 /** 065 * Main play routine. Loops until end of play. 066 */ 067 public void play() 068 { 069 printWelcome(); 070 071 // Enter the main command loop. Here we repeatedly read commands and 072 // execute them until the game is over. 073 074 boolean finished = false; 075 while(! finished) { 076 Command command = parser.getCommand(); 077 if(command == null) { 078 System.out.println("I don't understand..."); 079 } 080 else { 081 finished = command.execute(player); 082 } 083 } 084 System.out.println("Thank you for playing. Good bye."); 085 } 086 087 /** 088 * Print out the opening message for the player. 089 */ 090 private void printWelcome() 091 { 092 System.out.println(); 093 System.out.println("Welcome to The World of Zuul!"); 094 System.out.println("The World of Zuul is a new, incredibly boring adventure game."); 095 System.out.println("Type 'help' if you need help."); 096 System.out.println(); 097 System.out.println(player.getCurrentRoom().getLongDescription()); 098 } 099 }