Results 1 to 8 of 8
  1. #1
    jcbmack is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Loose Coupling and Cohesion

    The IDE is BlueJ and the purpose of this exercise is to be able to reduce code duplication and make more editable code while adding a method call known as printLocation as a separate method under the heading of private void printLocation(). Now this is just the first step in a variety of exercises so the assignment does not ask to delete the extra code within printWelcome or goRoom. However, as to no waste your valuable time I did try deleting those after placing this method and I still received the same error message which were illegal start of expression for private void printLocation and after trying to balance my braces I received an error message of could not find the given method. This is what I have to go on at the moment... I will keep working at it but tips on how to remove/add a brace or improve syntax so it will compile would be helpful. Again, the code is supposed to be bad at first with duplications as per BlueJ 7.4 pp. 206-210 2009 by Barnes and Kolling.

    The code is below

    Java Code:
    /**
     *  This class is the main class of the "World of Zuul" application. 
     *  "World of Zuul" is a very simple, text based adventure game.  Users 
     *  can walk around some scenery. That's all. It should really be extended 
     *  to make it more interesting!
     * 
     *  To play this game, create an instance of this class and call the "play"
     *  method.
     * 
     *  This main class creates and initialises all the others: it creates all
     *  rooms, creates the parser and starts the game.  It also evaluates and
     *  executes the commands that the parser returns.
     * 
     * @author  Jacob Mack
     * @version 2012.08/09
     */
    
    public class Game 
    {
        private Parser parser;
        private Room currentRoom;
            
        /**
         * Create the game and initialise its internal map.
         */
        public Game() 
        {
            createRooms();
            parser = new Parser();
        }
    
        /**
         * Create all the rooms and link their exits together.
         */
        private void createRooms()
        {
            Room outside, theater, pub, lab, office;
          
            // create the rooms
            outside = new Room("outside the main entrance of the university");
            theater = new Room("in a lecture theater");
            pub = new Room("in the campus pub");
            lab = new Room("in a computing lab");
            office = new Room("in the computing admin office");
            
            // initialise room exits
            outside.setExits(null, theater, lab, pub);
            theater.setExits(null, null, null, outside);
            pub.setExits(null, outside, null, null);
            lab.setExits(outside, office, null, null);
            office.setExits(null, null, null, lab);
    
            currentRoom = outside;  // start game outside
        }
    
        /**
         *  Main play routine.  Loops until end of play.
         */
        public void play() 
        {            
            printWelcome();
    
            // Enter the main command loop.  Here we repeatedly read commands and
            // execute them until the game is over.
                    
            boolean finished = false;
            while (! finished) {
                Command command = parser.getCommand();
                finished = processCommand(command);
            }
            System.out.println("Thank you for playing.  Good bye.");
        }
    
        /**
         * Print out the opening message for the player.
         */
        private void printWelcome()
        {
            System.out.println();
            System.out.println("Welcome to the World of Zuul!");
            System.out.println("World of Zuul is a new, incredibly boring adventure game.");
            System.out.println("Type 'help' if you need help.");
            System.out.println();
            System.out.println("You are " + currentRoom.getDescription());
            System.out.print("Exits: ");
            if(currentRoom.northExit != null) {
                System.out.print("north ");
            }
            if(currentRoom.eastExit != null) {
                System.out.print("east ");
            }
            if(currentRoom.southExit != null) {
                System.out.print("south ");
            }
            if(currentRoom.westExit != null) {
                System.out.print("west ");
            }
            System.out.println();
        }
    
        /**
         * Given a command, process (that is: execute) the command.
         * @param command The command to be processed.
         * @return true If the command ends the game, false otherwise.
         */
        private boolean processCommand(Command command) 
        {
            boolean wantToQuit = false;
    
            if(command.isUnknown()) {
                System.out.println("I don't know what you mean...");
                return false;
            }
    
            String commandWord = command.getCommandWord();
            if (commandWord.equals("help")) {
                printHelp();
            }
            else if (commandWord.equals("go")) {
                goRoom(command);
            }
            else if (commandWord.equals("quit")) {
                wantToQuit = quit(command);
            }
    
            return wantToQuit;
        }
    
        // implementations of user commands:
    
        /**
         * Print out some help information.
         * Here we print some stupid, cryptic message and a list of the 
         * command words.
         */
        private void printHelp() 
        {
            System.out.println("You are lost. You are alone. You wander");
            System.out.println("around at the university.");
            System.out.println();
            System.out.println("Your command words are:");
            System.out.println("   go quit help");
        }
    
        /** 
         * Try to go in one direction. If there is an exit, enter
         * the new room, otherwise print an error message.
         */
        private void goRoom(Command command) 
        {
            if(!command.hasSecondWord()) {
                // if there is no second word, we don't know where to go...
                System.out.println("Go where?");
                return;
            }
    
            String direction = command.getSecondWord();
    
            // Try to leave current room.
            Room nextRoom = null;
            if(direction.equals("north")) {
                nextRoom = currentRoom.northExit;
            }
            if(direction.equals("east")) {
                nextRoom = currentRoom.eastExit;
            }
            if(direction.equals("south")) {
                nextRoom = currentRoom.southExit;
            }
            if(direction.equals("west")) {
                nextRoom = currentRoom.westExit;
            }
    
            if (nextRoom == null) {
                System.out.println("There is no door!");
            }
            else {
                currentRoom = nextRoom;
                System.out.println("You are " + currentRoom.getDescription());
                System.out.print("Exits: ");
                if(currentRoom.northExit != null) {
                    System.out.print("north ");
                }
                if(currentRoom.eastExit != null) {
                    System.out.print("east ");
                }
                if(currentRoom.southExit != null) {
                    System.out.print("south ");
                }
                if(currentRoom.westExit != null) {
                    System.out.print("west ");
                }
                System.out.println();
                }
                
            
                private void printLocationInfo()
                }
                    System.out.println("you are" +
                        currentRoom.getDescription());
                    System.out.print("Exits:");
                    if(currentRoom.northExit != null) {
                        System.out.print("north ");
                    }
                    if(currentRoom.eastExit != null) {
                        System.out.print("east ");
                    }
                    if(currentRoom.southExit != null) {
                        System.out.print("south ");
                    }
                    if(currentRoom.eastExit != null){
                        System.out.print("west "){;
                    }
                    System.out.println();
                }
            
    
        /** 
         * "Quit" was entered. Check the rest of the command to see
         * whether we really quit the game.
         * @return true, if this command quits the game, false otherwise.
         */
        private boolean quit(Command command) 
        {
            if(command.hasSecondWord()) {
                System.out.println("Quit what?");
                return false;
            }
            else {
                return true;  // signal that we want to quit
            }
        }
    }
    Last edited by Fubarable; 08-10-2012 at 03:47 AM. Reason: quote tags changed to code tags

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Loose Coupling and Cohesion

    Moderator edit: [quote] [/quote] changed to [code] [/code] tags. Original poster, please don't use quote tags when posting code as it makes the code lose all of its formatting.

    As for your problem, carefully check all your curly braces near and at the offending method, and you'll find some big problems. Also, note that methods cannot be nested within methods.

  3. #3
    jcbmack is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: Loose Coupling and Cohesion

    I apologize for the oversight. I was able to remove the method from being nested within other methods and now the new error message states cannot find symbol variable command, on this line of code is where the indicated error is contained: String direction = command.getSecondWord(); Below is the whole code within the code tag convention and thanks for your help.
    Java Code:
     /**
     *  This class is the main class of the "World of Zuul" application. 
     *  "World of Zuul" is a very simple, text based adventure game.  Users 
     *  can walk around some scenery. That's all. It should really be extended 
     *  to make it more interesting!
     * 
     *  To play this game, create an instance of this class and call the "play"
     *  method.
     * 
     *  This main class creates and initialises all the others: it creates all
     *  rooms, creates the parser and starts the game.  It also evaluates and
     *  executes the commands that the parser returns.
     * 
     * @author  Michael K÷lling and David J. Barnes
     * @version 2011.07.31
     */
    
    public class Game 
    {
        private Parser parser;
        private Room currentRoom;
            
        /**
         * Create the game and initialise its internal map.
         */
        public Game() 
        {
            createRooms();
            parser = new Parser();
        }
    
        /**
         * Create all the rooms and link their exits together.
         */
        private void createRooms()
        {
            Room outside, theater, pub, lab, office;
          
            // create the rooms
            outside = new Room("outside the main entrance of the university");
            theater = new Room("in a lecture theater");
            pub = new Room("in the campus pub");
            lab = new Room("in a computing lab");
            office = new Room("in the computing admin office");
            
            // initialise room exits
            outside.setExits(null, theater, lab, pub);
            theater.setExits(null, null, null, outside);
            pub.setExits(null, outside, null, null);
            lab.setExits(outside, office, null, null);
            office.setExits(null, null, null, lab);
    
            currentRoom = outside;  // start game outside
        }
    
        /**
         *  Main play routine.  Loops until end of play.
         */
        public void play() 
        {            
            printWelcome();
    
            // Enter the main command loop.  Here we repeatedly read commands and
            // execute them until the game is over.
                    
            boolean finished = false;
            while (! finished) {
                Command command = parser.getCommand();
                finished = processCommand(command);
            }
            System.out.println("Thank you for playing.  Good bye.");
        }
    
        /**
         * Print out the opening message for the player.
         */
        private void printWelcome()
        {
            System.out.println();
            System.out.println("Welcome to the World of Zuul!");
            System.out.println("World of Zuul is a new, incredibly boring adventure game.");
            System.out.println("Type 'help' if you need help.");
            System.out.println();
            System.out.println("You are " + currentRoom.getDescription());
            System.out.print("Exits: ");
            if(currentRoom.northExit != null) {
                System.out.print("north ");
            }
            if(currentRoom.eastExit != null) {
                System.out.print("east ");
            }
            if(currentRoom.southExit != null) {
                System.out.print("south ");
            }
            if(currentRoom.westExit != null) {
                System.out.print("west ");
            }
            System.out.println();
        }
    
        /**
         * Given a command, process (that is: execute) the command.
         * @param command The command to be processed.
         * @return true If the command ends the game, false otherwise.
         */
        private boolean processCommand(Command command) 
        {
            boolean wantToQuit = false;
    
            if(command.isUnknown()) {
                System.out.println("I don't know what you mean...");
                return false;
            }
    
            String commandWord = command.getCommandWord();
            if (commandWord.equals("help")) {
                printHelp();
            }
            else if (commandWord.equals("go")) {
                goRoom(command);
            }
            else if (commandWord.equals("quit")) {
                wantToQuit = quit(command);
            }
    
            return wantToQuit;
        }
    
        // implementations of user commands:
    
        /**
         * Print out some help information.
         * Here we print some stupid, cryptic message and a list of the 
         * command words.
         */
        private void printHelp() 
        {
            System.out.println("You are lost. You are alone. You wander");
            System.out.println("around at the university.");
            System.out.println();
            System.out.println("Your command words are:");
            System.out.println("   go quit help");
        }
    
        /** 
         * Try to go in one direction. If there is an exit, enter
         * the new room, otherwise print an error message.
         */
        private void goRoom(Command command) 
        {
            if(!command.hasSecondWord()) 
                // if there is no second word, we don't know where to go...
                System.out.println("Go where?");
                return;
         }
            
            private void printLocationInfo()
            {
        
            System.out.println("You are " + currentRoom.getDescription());
            System.out.println("Exits: ");
            if(currentRoom.northExit != null )
            {
                System.out.println("north ");
            }
            if(currentRoom.eastExit != null)
            {
                System.out.print("east ");
            }
            if(currentRoom.southExit != null)
            {
                System.out.print("south ");
            }
            if(currentRoom.westExit != null)
            {
                System.out.print("west ");
            }
            System.out.println();
        }
            String direction = command.getSecondWord();
            {
    
            // Try to leave current room.
            Room nextRoom = null;
            if(direction.equals("north")) {
                nextRoom = currentRoom.northExit;
            }
            if(direction.equals("east")) {
                nextRoom = currentRoom.eastExit;
            }
            if(direction.equals("south")) {
                nextRoom = currentRoom.southExit;
            }
            if(direction.equals("west")) {
                nextRoom = currentRoom.westExit;
            }
    
            if (nextRoom == null) {
                System.out.println("There is no door!");
            }
            else {
                currentRoom = nextRoom;
                System.out.println("You are " + currentRoom.getDescription());
                System.out.print("Exits: ");
                if(currentRoom.northExit != null) {
                    System.out.print("north ");
                }
                if(currentRoom.eastExit != null) {
                    System.out.print("east ");
                }
                if(currentRoom.southExit != null) {
                    System.out.print("south ");
                }
                if(currentRoom.westExit != null) {
                    System.out.print("west ");
                }
                System.out.println();
            }
        }
    
        /** 
         * "Quit" was entered. Check the rest of the command to see
         * whether we really quit the game.
         * @return true, if this command quits the game, false otherwise.
         */
        private boolean quit(Command command) 
        {
            if(command.hasSecondWord()) {
                System.out.println("Quit what?");
                return false;
            }
            else {
                return true;  // signal that we want to quit
            }
        }
    }

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Loose Coupling and Cohesion

    Indent your code properly, matching braces.
    Because that is your problem.
    How do you expect to see where there is a basic problem with braces if your indentation is all over the place?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    jcbmack is offline Member
    Join Date
    Aug 2012
    Posts
    10
    Rep Power
    0

    Default Re: Loose Coupling and Cohesion

    Quote Originally Posted by Tolls View Post
    Indent your code properly, matching braces.
    Because that is your problem.
    How do you expect to see where there is a basic problem with braces if your indentation is all over the place?
    I will try that but this is the format of the textbook and the BlueJ format. I also used the code tags, as told to.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Loose Coupling and Cohesion

    No, that is not the format.
    The code that is untouched by you is properly indented.
    The code that is causing you problems (printLocationInfo()) is not.
    So fix that, then you will probably see that the braces do not match.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gack1234 is offline Member
    Join Date
    Jun 2014
    Posts
    2
    Rep Power
    0

    Default Re: Loose Coupling and Cohesion

    Java Code:
    package*zuul;*
    public*interface*Action*{*
    ***/***
    *****This*method*should*be*implemented*by*a*class*that*denotes*a*command*
    ****/*
    ***public*void*doAction(Player*player,*Command*command);*
    }*
    package*zuul;*
    /***
    **This*class*is*part*of*the*"World*of*Zuul"*application.*
    **"World*of*Zuul"*is*a*very*simple,*text*based*adventure*game.*
    **
    **This*class*holds*information*about*a*command*that*was*issued*by*the*user.*
    **A*command*currently*consists*of*two*strings:*a*command*word*and*a*second*
    **word*(for*example,*if*the*command*was*"take*map",*then*the*two*strings*
    **obviously*are*"take"*and*"map").*
    **
    **The*way*this*is*used*is:*Commands*are*already*checked*for*being*valid*
    **command*words.*If*the*user*entered*an*invalid*command*(a*word*that*is*not*
    **known)*then*the*command*word*is*<null>.*
    **
    **If*the*command*had*only*one*word,*then*the*second*word*is*<null>.*
    **
    **@author**Michael*K├Âlling*and*David*J.*Barnes*
    **@version*2011.08.08*
    */*
    public*class*Command*
    {*
    ***private*String*firstWord;*
    ***private*String*secondWord;*
    ***protected*static*boolean*wantQuit*=*false;//ŕ│*ý╣�ŕŞ░*
    ***/***
    *****Constructor*of*class*Command*with*fristWord,*secondWord*
    ****/*
    ***public*Command(String*firstWord,*String*secondWord)*
    ***{*
    ******this.firstWord*=*firstWord;*
    ******this.secondWord*=*secondWord;*
    ***}*
    ***/***
    *****getter*the*first*command*Word*
    ****/*
    ***public*String*getFirstWord()*
    ***{*
    ******return*firstWord;*
    ***}*
    ***/***
    *****getter*the*second*command*Word*
    ****/*
    ***public*String*getSecondWord()*
    ***{*
    ******return*secondWord;*
    ***}*
    ***/***
    *****@return*true*if*firstword*is*empty*
    ****/*
    ***public*boolean*noFristWord()*
    ***{*
    ******return*(firstWord*==*null);*
    ***}*
    ***/***
    *****@return*true*if*the*command*has*a*second*word.*
    ****/*
    ***public*boolean*hasSecondWord()*
    ***{*
    ******return*(secondWord*!=*null);*
    ***}*
    ***/***
    *****static*method*
    *****getter*quitstate*
    ****/*
    ***public*static*boolean*getWantQuit()*
    ***{*
    ******return*wantQuit;*
    ***}*
    ***/***
    *****static*method*
    *****setter*quitstate*
    ****/*
    ***public*static*void*setWantQuit(boolean*aa)*
    ***{*
    ******wantQuit*=*aa;*
    ***}*
    }*
    package*zuul;*
    import*java.util.HashMap;*
    /***
    **This*class*is*part*of*the*"World*of*Zuul"*application.*
    **"World*of*Zuul"*is*a*very*simple,*text*based*adventure*game.*
    **
    **This*class*holds*an*enumeration*of*all*command*words*known*to*the*game.*
    **It*is*used*to*recognise*commands*as*they*are*typed*in.*
    **
    **@author**Michael*K├Âlling*and*David*J.*Barnes*
    **@version*2011.08.08*
    */*
    public*class*CommandWords*{*
    ***private*HashMap<String,*Action>*commandList;*
    ***/***
    *****Constructor*class*command*words.*
    ****/*
    ***public*CommandWords()*{*
    ***}*
    ***/***
    *****make*each*action*object*and*put*the*each*action*object*in*the*HashMap*
    *****
    ****/*
    ***public*void*buildCommands()*{*
    ******commandList*=*new*HashMap<String,*Action>();*
    ******Action*runHelp*=*new*Help();*
    ******commandList.put("help",*runHelp);*
    ******Action*runQuit*=*new*Quit();*
    ******commandList.put("quit",*runQuit);*
    ******Action*runGo*=*new*Go();*
    ******commandList.put("go",*runGo);*
    ******Action*runTake*=*new*Take();*
    ******commandList.put("take",*runTake);*
    ******Action*runDrop*=*new*Drop();*
    ******commandList.put("drop",*runDrop);*
    ******Action*runTrade*=*new*Trade();*
    ******commandList.put("trade",*runTrade);*
    ******Action*runLock*=*new*Lock();*
    ******commandList.put("lock",*runLock);*
    ******Action*runUnlock*=*new*Unlock();*
    ******commandList.put("unlock",*runUnlock);*
    ***}*
    ***/***
    *****Check*whether*action*command,*player*type*is*exsit*in*
    *****action*commandList*
    ****/*
    ***public*boolean*isCommand(String*aString)*{*
    ******if*(commandList.containsKey(aString))*{*
    *********return*true;*
    ******}else*
    ******{*
    *********return*false;//*no*such*a*that*action*command*
    ******}*
    ***}*
    ***/***
    *****getter*action*command*
    ****/*
    ***public*Action*getCommand(String*command)*{*
    ******return*commandList.get(command);*
    ***}*
    ***/***
    *****Print*all*valid*commands*to*System.out.*
    ****/*
    ***public*static*void*showAll()*{*
    ******System.out.println("go,*help,*quit,*trade,*take,*drop,*lock,*unlock.*");*
    ***}*
    }*
    package*zuul;*
    /***
    **class*Drop*implements*Action*
    **Drop*is*one*of*action*command.*
    **Drop*provide*action*that*player*can*drop*an*item*in*current*room*
    */*
    public*class*Drop*implements*Action*
    {*
    ***/***
    *****Constructor*for*class*Drop*
    ****/*
    ***public*Drop()*
    ***{*
    ***}*
    ***public*void*doAction(Player*player,*Command*command)*
    ***{*
    ******Item*item;*
    ******//check*if*player*typed*secondword*or*not.*
    ******if(!command.hasSecondWord())*
    ******{*
    *********System.out.println("what*do*you*want*to*drop?");*
    ******}*
    ******else{*
    *********String*itemName*=*command.getSecondWord();*
    *********//check*if*player*has*the*item*in*the*player*itemlist*
    *********//*if*it*dose*remove*the*item*on*the*player*item*list*
    *********//and*add*the*item*to*the*currnet*room*
    *********if(player.existItem(itemName))*
    *********{*
    ************item*=*player.getItem(itemName);*
    ************player.removeItem(item);*
    ************player.getCurrentRoom().addItem(item);*
    ************}else{*
    ************System.out.println("Sorry,you*do*not*have*this*item..");*
    *********}*
    *********//show*current*information*
    *********State.currentState(player);*
    ******}*
    ***}*
    }*
    package*zuul;*
    /***
    **Class*Exit*
    **exit-*door*of*room*
    **exit*exists*in*room*
    **one*room*can*have*many*exits,*and*exit*can*be*lock*unlock*
    */*
    public*class*Exit*extends*GameObject*
    {*
    ***//*instance*variables*-*replace*the*example*below*with*your*own*
    ***private*Room*currentRoom;*
    ***private*Room*room;*
    ***private*boolean*isLocked;*
    ***private*boolean*canLock;*
    ***/***
    *****Constructor*for*class*Exit*
    ****/*
    ***public*Exit(String*name,*String*description,*Room*current,*Room*next,boolean*isLocked)*
    ***{*
    ******super(name,*description);*
    ******this.setCurrentRoom(current);*
    ******this.setNextRoom(next);*
    ******this.setLocked(isLocked);*
    ******this.canLock=*true;///////*some*exits*can*be*locked..*
    ***}*
    ***public*Exit(String*name,*String*description,*Room*current,*Room*next)*
    ***{*
    ******super(name,*description);*
    ******this.setCurrentRoom(current);*
    ******this.setNextRoom(next);*
    ******this.setLocked(isLocked);*
    ******this.canLock=*false;*
    ***}*
    ***public*void*setName(String*name)*
    ***{*
    ******super.setName(name);*
    ***}*
    ***public*String*getName()*
    ***{*
    ******return*super.getName();*
    ***}*
    ***public*void*setNextRoom(Room*room)*
    ***{*
    ******this.room=*room;*
    ***}*
    ***public*Room*getNextRoom()*
    ***{*
    ******return*room;*
    ***}*
    ***public*void*setLocked(boolean*isLocked)*
    ***{*
    ******this.isLocked*=*isLocked;*
    ***}*
    ***public*boolean*isLocked()*
    ***{*
    ******return*isLocked;*
    ***}*
    ***public*boolean*canLock()*
    ***{*
    ******return*this.canLock;*
    ***}*
    ***public*Room*getCurrentRoom()*
    ***{*
    ******return*currentRoom;*
    ***}*
    ***public*void*setCurrentRoom(Room*currentRoom)*
    ***{*
    ******this.currentRoom*=*currentRoom;*
    ***}*
    }*
    package*zuul;*
    /***
    ***This*class*is*the*main*class*of*the*"World*of*Zuul"*application.*
    ***"World*of*Zuul"*is*a*very*simple,*text*based*adventure*game.**Users*
    ***can*walk*around*some*scenery.*That's*all.*It*should*really*be*extended*
    ***to*make*it*more*interesting!*
    **
    ***To*play*this*game,*create*an*instance*of*this*class*and*call*the*"play"*
    ***method.*
    **
    ***This*main*class*creates*and*initialises*all*the*others:*it*creates*all*
    ***rooms,*creates*the*parser*and*starts*the*game.**It*also*evaluates*and*
    ***executes*the*commands*that*the*parser*returns.*
    **
    **@author**Michael*K├Âlling*and*David*J.*Barnes*
    **@version*2011.08.08*
    */*
    public*class*Game*{*
    ***private*Parser*parser;*
    ***private*Player*player;*
    ***/***
    *****game*constructor*call*initialseGame*
    ****/*
    ***public*Game()*{*
    ******initialiseGame();*
    ***}*
    ***/***
    *****initialiseGame()*initialises*the*game,*sets*a*player,Rooms,*exits*in*this*game*
    *****some*room*can*contain*one*NPC.*
    *****when*NPC*defines,*one*NPC*item*also*set.*
    ****/*
    ***private*void*initialiseGame()*{*
    ******parser*=*new*Parser();*
    ******player*=*new*Player("Load.Anthory",*"This*is*Player1.");*
    ******Room*outside,*theatre,cistern,bathHouse,storeRoom,eastTower,dinningRoom,kitchen,westTower;*
    ******Item*key*=*new*Item("key",*"secret*Door*key",4);*
    ******Item*gold*=*new*Item("gold",*"This*is*gold*coin",5);*
    ******Item*juice*=*new*Item("juice",*"This*is*energy*juice",2);*
    ******Item*paper*=*new*Item("stamp",*"This*is*information*paper",1);*
    ******//*create*the*rooms*
    ******outside*=*new*Room("Outside",*
    ******"outside*the*main*entrance*of*the*castle");*
    ******cistern*=*new*Room("Cistern",*"in*a*lecture*theatre");*
    ******bathHouse*=*new*Room("BathHouse",*"in*a*Bath-house");*
    ******storeRoom*=*new*Room("StoreRoom",*"in*a*Store*room");*
    ******eastTower*=*new*Room("EastTower",*"in*a*eastTower");*
    ******dinningRoom=*new*Room("DinningRoom",*"in*a*Dinning*Room");*
    ******kitchen*=*new*Room("Kitchen",*"in*a*kitchen");*
    ******westTower*=*new*Room("WestTower",*"in*a*westTower");*
    ******theatre*=*new*Room("Theatre",*"in*a*castle*theatre");*
    ******//*initialise*outside*room*exits/////*
    ******outside.setExit("east",*new*Exit("east",*"This*is*the*east*exit",*
    ******outside,*storeRoom,*false));*
    ******outside.setExit("north",*new*Exit("north",*"This*is*the*south*exit",*
    ******outside,*kitchen,*false));*
    ******outside.setExit("west",*new*Exit("west",*"This*is*the*west*exit",*
    ******outside,*theatre,*false));*
    ******//*Add*items*to*room*outside*
    ******outside.addItem(juice);*
    ******outside.addItem(gold);*
    ******//*Create*NPC*at*outside*
    ******NPC*outsideNPC*=*new*NPC("Karl",*"I*am*a*dwarf*at*main*Gate",*
    ******key);*
    ******outside.setNPC(outsideNPC);*
    ******//*initialise*theatre*room*exits//////*
    ******theatre.setExit("east",*new*Exit("east",*"This*is*the*east*exit",*
    ******theatre,*outside,*false));*
    ******theatre.setExit("north",*new*Exit("north",*"This*is*the*north*exit",*
    ******theatre,*westTower,*false));*
    ******//*Add*theatre*items*
    ******theatre.addItem(key);*
    ******theatre.addItem(gold);*
    ******//*Create*NPC*in*the*theatre*
    ******NPC*theatreNPC*=*new*NPC("Jery",*"I*am*a*dwarf*in*the*theatre*room",*
    ******gold);*
    ******theatre.setNPC(theatreNPC);*
    ******//*initialise*theatre*room*exits/////*
    ******storeRoom.setExit("west",*new*Exit("west",*"This*is*the*west*exit",*
    ******storeRoom,*outside,*false));*
    ******//*Add*storeRoom*items*
    ******storeRoom.addItem(paper);*
    ******storeRoom.addItem(gold);*
    ******//*Create*NPC*in*storeRoom*
    ******NPC*storeRoomNPC*=*new*NPC("Mike",*"I*am*a*dwarf*in*the*storeRoom",*
    ******gold);*
    ******storeRoom.setNPC(storeRoomNPC);*
    ******//*initialise*west*tower*exits/////*
    ******westTower.setExit("south",*new*Exit("south",*"This*is*the*south*exit",*
    ******westTower,*theatre,*false));*
    ******westTower.setExit("north",*new*Exit("north",*"This*is*the*north*exit",*
    ******westTower,*bathHouse,*false));*
    ******//*Add**westTower*items*
    ******westTower.addItem(paper);*
    ******//*initialise*kitchen*exits/////*
    ******kitchen.setExit("east",*new*Exit("east",*"This*is*the*east*exit",*
    ******kitchen,*westTower,*false));*
    ******kitchen.setExit("north",*new*Exit("north",*"This*is*the*north*exit",*
    ******kitchen,*dinningRoom,*true));*
    ******kitchen.setExit("south",*new*Exit("south",*"This*is*the*south*exit",*
    ******kitchen,*outside*,*false));*
    ******//*Add*kitchen*items*
    ******kitchen.addItem(juice);*
    ******//*initialise*theatre*room*exits/////*
    ******eastTower.setExit("west",*new*Exit("west",*"This*is*the*west*exit",*
    ******eastTower,*kitchen,*false));*
    ******//*Add*kitchen*items*
    ******eastTower.addItem(gold);*
    ******//*Create*NPC*in*storeRoom*
    ******NPC*eastTowerNPC*=*new*NPC("Mike",*"I*am*a*dwarf*in*the*eastTower",*
    ******key);*
    ******eastTower.setNPC(eastTowerNPC);*
    ******//*initialise*dinningRoom*exits/////*
    ******dinningRoom.setExit("south",*new*Exit("south",*"This*is*the*south*exit",*
    ******dinningRoom,*kitchen,*false));*
    ******dinningRoom.setExit("west",*new*Exit("west",*"This*is*the*west*exit",*
    ******dinningRoom,*bathHouse,*true));*
    ******//*Add**dinningRoom*items*
    ******dinningRoom.addItem(paper);*
    ******//*initialise*bathHouse*exits/////*
    ******bathHouse.setExit("east",*new*Exit("east",*"This*is*the*east*exit",*
    ******bathHouse,*dinningRoom,*false));*
    ******bathHouse.setExit("west",*new*Exit("west",*"This*is*the*west*exit",*
    ******bathHouse,*cistern,*true));*
    ******bathHouse.setExit("south",*new*Exit("south",*"This*is*the*south*exit",*
    ******bathHouse,*westTower*,*false));*
    ******//*Add*bathHouse*items*
    ******bathHouse.addItem(juice);*
    ******//*initialise*bathHouse*exits/////*
    ******cistern.setExit("east",*new*Exit("east",*"This*is*the*east*exit",*
    ******bathHouse,*dinningRoom,*true));*
    ******//*Add*bathHouse*items*
    ******cistern.addItem(juice);*
    ******NPC*cisternNPC*=*new*NPC("earl",*"I*am*a*dwarf*in*the*cistern",*
    ******gold);*
    ******cistern.setNPC(cisternNPC);*
    ******player.setCurrentRoom(outside);*//*start*game*outside*
    ******player.addItem(new*Item("sore",*"This*is*a*sore*item",4));*
    ******player.addItem(new*Item("id",*"This*is*a*id*item"));*
    ***}*
    ***/***
    *****Main*play*routine.*Loops*until*end*of*play.*
    ****/*
    ***public*void*play()*{*
    ******printWelcome();*
    ******//*Enter*the*main*command*loop.*Here*we*repeatedly*read*commands*and*
    ******//*execute*them*until*the*game*is*over.*
    ******boolean*wantQuit*=*Command.getWantQuit();*
    ******while*(!wantQuit)*{*
    *********Command*command*=*parser.getCommand();*
    *********wantQuit*=*processCommand(command);*
    ******}*
    ******System.out.println("Thank*you*for*playing.**Good*bye.");*
    ***}*
    ***/***
    *****Print*out*the*opening*message*for*the*player.*
    ****/*
    ***private*void*printWelcome()*{*
    ******System.out.println();*
    ******System.out.println("Welcome*to*the*World*of*Zuul!");*
    ******System.out.println("World*of*Zuul*is*a*new,*boring*adventure*game.");*
    ******System.out.println("Type*'help'*if*you*need*help.");*
    ******System.out.println();*
    ******System.out.println(player.getCurrentRoom().getLongDescription());*
    ***}*
    ***/***
    *****Given*a*command,*process*(that*is:*execute)*the*command.*If*this*command*
    *****ends*the*game,*true*is*returned,*otherwise*false*is*returned.*
    ****/*
    ***private*boolean*processCommand(Command*command)*{*
    ******if*(command.noFristWord())*{*
    *********System.out.println("I*don't*know*what*you*mean...");*
    *********return*false;*
    ******}*
    ******String*commandWord*=*command.getFirstWord();*
    ******CommandWords*cw*=*new*CommandWords();*
    ******//*Create*valid*commands*
    ******cw.buildCommands();*
    ******//*If*command*exists...*
    ******if*(cw.isCommand(commandWord))*{*
    *********//*...execute*command*
    *********cw.getCommand(commandWord).doAction(player,*command);*
    *********}*else*{*
    *********System.out.println("I*don't*know*what*you*mean...");*
    ******}*
    ******return*Command.getWantQuit();*
    ***}*
    ***public*static*void*main(String[]*args)*{*
    ******Game*game*=*new*Game();*
    ******game.play();*
    ***}*
    }*
    package*zuul;*
    /***
    **Abstract*class*GameObject*-*provides*initialising*object*with*name*description*
    **and*setter,*getter*of*name,*description*
    **in*this*game,*there*are*several*game*objects*which*are*player,*item,*NPC,*and*Room.*
    **
    */*
    public*abstract*class*GameObject*
    {*
    ***//*instance*variables*-*replace*the*example*below*with*your*own*
    ***private*String*name;*
    ***private*String*description;*
    ***/***
    *****Constructor*class*game*object.*
    ****/*
    ***public*GameObject(String*name,*String*description)*
    ***{*
    ******this.name*=*name;*
    ******this.description*=*description;*
    ***}*
    ***public*String*getDescription()*
    ***{*
    ******return*description;*
    ***}*
    ***public*void*setDescription(String*description)*
    ***{*
    ******this.description*=*description;*
    ***}*
    ***public*String*getName()*
    ***{*
    ******return*name;*
    ***}*
    ***public*void*setName(String*name)*
    ***{*
    ******this.name*=*name;*
    ***}*
    }*
    package*zuul;*
    /***
    ***class*Go*implements*Action*
    ***Go*is*one*of*action*command*
    ***class*go*excute*player*go*command*with*second*word*of*command,*direction*
    **
    */*
    public*class*Go*implements*Action*{*
    ***/***
    *****Constructor*of*class*Go*
    ****/*
    ***public*Go()*{*
    ***}*
    ***/***
    *****Try*to*go*to*one*direction.*If*there*is*an*exit,*enter*the*new*room,*
    *****otherwise*print*an*error*message.*
    *****
    ****/*
    ***public*void*doAction(Player*player,*Command*command)*{*
    ******if*(!command.hasSecondWord())*{*
    *********//*if*player*didn't*type*second*word,*then*ask*Go*where?*
    *********System.out.println("Go*where?");*
    *********return;*
    ******}*
    ******String*direction*=*command.getSecondWord();*
    ******try*{*
    *********//*put*player*current*room's*exit*user*want*to*go*to*'exit'*
    *********Exit*exit*=*player.getCurrentRoom().getExit(direction);*
    *********//*get*nextroom*from*exit*info.*
    *********Room*nextRoom*=*exit.getNextRoom();*
    *********//*show*error*if*nextroom*doesnt*exsit*
    *********if*(nextRoom*==*null)*
    *********System.out.println("There*is*no*door!");*
    *********//*Check,*Exit*is*locked.*
    *********else*if*(exit.isLocked())*{*
    ************System.out*
    ************.println("This*door*is*Locked.*Try*to*unlock*it*first!");*
    ************}*else*{*
    ************player.setCurrentRoom(nextRoom);*
    ************System.out*
    ************.println(player.getCurrentRoom().getLongDescription());*
    *********}*
    *********}*catch*(NullPointerException*exit)*{*
    *********System.out.println("There*is*no*such*exit*to*"*+*direction*+*".");*
    ******}*
    ******//print*out*current*state*on*screen*
    ******State.currentState(player);*
    ***}*
    }*
    package*zuul;*
    /***
    **class*Help*implements*Action*
    **Go*is*one*of*action*command*
    **it*will*give*adivce*to*player*on*screen*
    **
    */*
    public*class*Help*implements*Action*{*
    ***CommandWords*wrds*=*new*CommandWords();*
    ***Parser*parser*=*new*Parser();*
    ***/***
    *****Constructor*of*class*Help*
    ****/*
    ***public*Help()*{*
    ***}*
    ***/***
    *****This*method*prints*help*messages*
    *****
    *****@param*player*
    *****@param*command*
    ****/*
    ***public*void*doAction(Player*player,*Command*command)*{*
    ******System.out.println("You*are*lost.*You*are*around*at*the*castle.\n");*
    ******System.out.println("Your*command*words*are:\n");*
    ******CommandWords.showAll();*
    ***}*
    }*
    package*zuul;*
    /***
    **class*item*extends*GameObject*
    **An*item*has*a*description*(a*String)*and*a*name*(a*1-word*String).*
    **
    */*
    public*class*Item*extends*GameObject*
    {*
    ***//*instance*variables*-*replace*the*example*below*with*your*own*
    ***private*int*value;//*item*value*or*weight*
    ***/***
    *****Constructor*class*item*.*
    *****make*an*item*with*name,*descripton*
    *****then*value*automatically*set*1*
    ****/*
    ***public*Item(String*name,*String*description)*
    ***{*
    ******//*initialise*instance*variables*
    ******super(name,description);*
    ******value=1;*
    ***}*
    ***/***
    *****Constructor*class*item.*
    *****make*an*item*with*name,*descripton,value*
    ****/*
    ***public*Item(String*name,*String*description,*int*value)*
    ***{*
    ******//*initialise*instance*variables*
    ******super(name,description);*
    ******this.value=value;*
    ***}*
    ***/***
    ****getter*item*name*
    ****/*
    ***public*String*getName()*
    ***{*
    ******return*super.getName();*
    ***}*
    ***/***
    ****getter*item*Description*
    ****/*
    ***public*String*getDescription()*
    ***{*
    ******return*super.getDescription();*
    ***}*
    ***/***
    ****getter*item*value*
    ****/*
    ***public*int*getValue(){*
    ******return*value;*
    ***}*
    }*
    package*zuul;*
    /***
    **class*Lock*implements*Action*
    **Lock*is*one*of*action*command.*
    **Lock*provide*action*that*player*can*locked*one*of*exit*in*a*curret*room*
    **
    */*
    public*class*Lock*implements*Action*{*
    ***/***
    *****constructor*of*lock*Class*
    ****/*
    ***public*Lock()*{*
    ***}*
    ***/***
    *****Try*to*lock*a*door*in*the*room*
    *****
    ****/*
    ***public*void*doAction(Player*player,*Command*command)*{*
    ******Exit*exit*=*null;*
    ******boolean*existKey*=*false;*
    ******if*(!command.hasSecondWord())*{*
    *********//*if*player*didn't*type*second*word,*then*ask*detail.*
    *********System.out.println("which*exits*Lock?");*
    *********return;*
    ******}*
    ******String*exitToLock*=*command.getSecondWord();*
    ******exit=*player.getCurrentRoom().getExit(exitToLock);*
    ******existKey*=*player.existItem("key");*
    ******//*Check*if*the*player*has*a*key.*
    ******try*{*
    *********if*(existKey*==*true*&&*exit.isLocked()*==*false)*{*
    ************//*Check*that*can*Exit*lock?*
    ************if*(exit.canLock())*{*
    ***************//*lock*the*Exit*
    ***************player.getCurrentRoom().getExit(exitToLock).setLocked(true);*
    ***************}*else*{*
    ***************//show*message*that*door*is*already*locked*
    ***************System.out*
    ***************.println("This*exit*is*already*be*locked!*");*
    ************}*
    ************}*else*{//*show*error*message*
    ************System.out*
    ************.println("The*player*do*not*have*a*key*or*this*exit*is*locked*already!");*
    *********}*
    *********}*catch*(NullPointerException*e)*{*
    *********//*show*error*message*about*nullpoint*
    *********System.out.println("This*Exit*does*not*exist!*Try*another*Exit!");*
    ******}*
    ******//show*current*information*
    ******State.currentState(player);*
    ***}*
    }*
    package*zuul;*
    /***
    **class*NPC*-*
    **An*NPC*is*like*an*item*in*that*it*has*a*name*and*a*description.*
    **However,*unlike*items*the*player*cannot*pick*up*or*drop*an*NPC.*
    **An*NPC*is*like*the*player*in*that*it*can*hold*an*item*but*is*different*
    **in*that*it*can*hold*only*one*item*at*a*time*and*doesn't*take*actions.*
    **NPC*will*accept*any*item*in*trade*from*the*player.*
    **There*can*be*only*1*NPC*or*non*in*each*room,*
    */*
    public*class*NPC*extends*GameObject*{*
    ***private*Item*item;*
    ***/***
    *****Constructor*of*the*NPC*class*
    *****make*a*Non*Player*Character*
    *****Name*name,*Description*description,*@parameter*item*
    ****/*
    ***public*NPC(String*name,*String*description,*Item*item)*{*
    ******super(name,*description);*
    ******//*Assign*an*Item*to*the*NPC*
    ******setItemName(item);*
    ***}*
    ***/**this*function*can*offer*
    *****trading*an*item*to*the*player*and*this*NPC*
    *****@return*exchangedItem*
    ****/*
    ***public*Item*tradeItem(Item*item)*{*
    ******Item*NPCItem=*this.item;*
    ******this.item*=*item;*
    ******return*NPCItem;*
    ***}*
    ***/***
    *****Setter*the*NPC*character*name*
    *****@parameter*NPC*name*
    ****/*
    ***public*void*setName(String*name)*{*
    ******super.setName(name);*
    ***}*
    ***/***
    *****Getter*for*the*NPC*character*name*
    *****
    *****@return*NPC*name.*
    ****/*
    ***public*String*getName()*{*
    ******return*super.getName();*
    ***}*
    ***/***
    *****Getter*NPC's*item*
    *****
    *****@return*-->*Item*name*
    ****/*
    ***public*String*getItemName()*{*
    ******return*this.item.getName();*
    ***}*
    ***/***
    *****Setter*for*the*NPC*item*
    ****/*
    ***public*void*setItemName(Item*item)*{*
    ******this.item*=*item;*
    ***}*
    ***/***
    *****Setter*for*NPC*description*
    ****/*
    ***public*void*setDescription(String*description)*{*
    ******super.setDescription(description);*
    ***}*
    ***/***
    *****Getter*for*NPC*description*
    ****/*
    ***public*String*getDescription()*{*
    ******return*super.getDescription();*
    ***}*
    ***/***
    *****print*NPC*name*and*NPC*item*
    ****/*
    ***public*void*printNPCInfo()*{*
    ******System.out.println("There*is*a*Non-Player*Character*which*is*"*
    ******+*super.getName()*+*"in*this*room*and*has*a/an*"*+*this.getItemName());*
    ***}*
    }*
    package*zuul;*
    import*java.util.Scanner;*
    /***
    **This*class*is*part*of*the*"World*of*Zuul"*application.*
    **"World*of*Zuul"*is*a*very*simple,*text*based*adventure*game.*
    **
    **This*parser*reads*user*input*and*tries*to*interpret*it*as*an*"Adventure"*
    **command.*Every*time*it*is*called*it*reads*a*line*from*the*terminal*and*
    **tries*to*interpret*the*line*as*a*two*word*command.*It*returns*the*command*
    **as*an*object*of*class*Command.*
    **
    **The*parser*has*a*set*of*known*command*words.*It*checks*user*input*against*
    **the*known*commands,*and*if*the*input*is*not*one*of*the*known*commands,*it*
    **returns*a*command*object*that*is*marked*as*an*unknown*command.*
    **
    **@author**Michael*K├Âlling*and*David*J.*Barnes*
    **@version*2011.08.08*
    */*
    public*class*Parser*
    {*
    ***private*CommandWords*commands;**//*holds*all*valid*command*words*
    ***private*Scanner*reader;*********//*source*of*command*input*
    ***/***
    *****Create*a*parser*to*read*from*the*terminal*window.*
    ****/*
    ***public*Parser()*
    ***{*
    ******commands*=*new*CommandWords();*
    ******reader*=*new*Scanner(System.in);*
    ***}*
    ***/***
    *****@return*The*next*command*from*the*user.*
    ****/*
    ***public*Command*getCommand()*
    ***{*
    ******String*playerCommand;***//*will*hold*the*full*input*line*
    ******String*word1*=*null;*
    ******String*word2*=*null;*
    ******System.out.print(">*");*****//*print*prompt*
    ******playerCommand*=*reader.nextLine();*
    ******Scanner*scan*=*new*Scanner(playerCommand);*
    ******if*(scan.hasNext())*{*
    *********word1*=*scan.next();*//*get*first*word*
    *********}*else*{*
    *********word1*=*null;*//if*scan*has*no*next,*then*put*null*to*word1*
    ******}*
    ******if*(scan.hasNext())*{*
    *********word2*=*scan.next();*//*get*second*word*
    *********}*else*{*
    *********word2*=*null;*//if*scan*has*no*next,*then*put*null*to*word2*
    ******}*
    ******//*return*commands;*
    ******return*new*Command(word1,*word2);*
    ***}*
    ***/***
    *****Print*out*a*list*of*valid*command*words.*
    ****/*
    ***public*void*showCommands()*{*
    ******commands.showAll();*
    ***}*
    }*
    package*zuul;*
    import*java.util.ArrayList;*
    /***
    **
    **Player*is*a*user*character*which*can*play*this*game*
    **Player*is*in*some*room*and*can*have*items.*
    **The*class*Player*extends*the*Class*GameObject*
    **this*class*
    */*
    public*class*Player*extends*GameObject*
    {*
    ***//*instance*variables*-*replace*the*example*below*with*your*own*
    ***private*Room*currentRoom;*
    ***//*itemsList*is*an*array,*it*will*contain*items*in*this*player*
    ***private*ArrayList<Item>*itemList;*
    ***/***
    *****Constructor*for*class*Player*
    *****creat*a*player*with*name,*description*
    ****/*
    ***public*Player(String*name,*String*description)*
    ***{*
    ******super(name,*description);*
    ******//*initialise*player*item*Arraylist*
    ******itemList*=*new*ArrayList<Item>(0);*
    ******//playerItemList*=*new*ItemList();*
    ***}*
    ***/***
    *****print*out*itmes*data*of*this*palyer*
    ****/*
    ***public*void*printPlayerItemInfo(){*
    ******String*itemStr="You*have";*
    ******if(itemList.isEmpty()){*
    *********itemStr+="*no*items";*
    ******}*
    ******else{*
    *********for(Item*item:itemList){*
    ************itemStr+="*"*+item.getName()*+",";*
    *********}*
    ******}*
    ******System.out.println(itemStr+".");*
    ***}*
    ***/***
    *****set*location*room*of*this*player*
    ****/*
    ***public*void*setCurrentRoom(Room*room)*
    ***{*
    ******currentRoom*=*room;*
    ***}*
    ***/***
    *****get*a*room*where*the*player*is*
    ****/*
    ***public*Room*getCurrentRoom()*
    ***{*
    ******return*this.currentRoom;*
    ***}*
    ***/***
    *****ask*the*item*exists*in*this*player*
    *****true/false*
    ****/*
    ***public*boolean*existItem(String*itemName)*
    ***{*
    ******boolean*takenItem=false;*
    ******String*name;*
    ******for(Item*item:itemList){*
    *********name=item.getName();*
    *********if(itemName.compareTo(name)==0)*
    *********{*
    ************takenItem=true;*
    *********}*
    ******}*
    ******return*takenItem;*
    ***}*
    ***/***
    *****find*a*item*in*this*player*item*list*
    ****/*
    ***public*Item*getItem(String*itemName)*
    ***{*
    ******Item*returnedItem=null;*
    ******String*name;*
    ******for(Item*item:itemList){*
    *********name=item.getName();*
    *********if(itemName.compareTo(name)==0)*
    *********{*
    ************returnedItem=*item;*
    *********}*
    ******}*
    ******return*returnedItem;*
    ***}*
    ***/***
    *****add*an*item*in*this*player's*item*arraylist*
    ****/*
    ***public*void*addItem(Item*item)*
    ***{*
    ******itemList.add(item);*
    ***}*
    ***/***
    *****delete*an*item*in*this*player's*item*arraylist*
    ****/*
    ***public*void*removeItem(Item*item)*
    ***{*
    ******for(int*i=0;*i<itemList.size();i++)*
    ******{*
    *********if(itemList.get(i).equals(item))*
    *********{*
    ************itemList.remove(i);*
    ************break;*
    *********}*
    ******}*
    ***}*
    }*
    package*zuul;*
    /***
    **class*Quit*implements*Action*
    **Quit*is*one*of*action*command*
    **this*class*set*quit*state*to*ture*or*show*error*commnad*message*
    **
    */*
    public*class*Quit*implements*Action*{*
    ***/***
    *****constructor*of*the*quit*class*
    ****/*
    ***public*Quit()*{*
    ***}*
    ***/***
    *****This*method*sets*quitstate*to*ture*or*show*error*commnad*message*
    ****/*
    ***public*void*doAction(Player*player,*Command*command)*{*
    ******if*(!command.hasSecondWord())*{*
    *********Command.setWantQuit(true);*//*set*ture*at*setwanttoQuit*
    *********}*else*{*
    *********System.out.println("if*you*want*to*Quit,*type*quit*only*please");*
    ******}*
    ***}*
    }*
    package*zuul;*
    import*java.util.Set;*
    import*java.util.HashMap;*
    import*java.util.ArrayList;*
    /***
    **Class*Room*-*a*room*in*an*adventure*game.*
    **
    **This*class*is*part*of*the*"World*of*Zuul"*application.*
    **"World*of*Zuul"*is*a*very*simple,*text*based*adventure*game.*
    **
    **A*"Room"*represents*one*location*in*the*scenery*of*the*game.**It*is*
    **connected*to*other*rooms*via*exits.**For*each*existing*exit,*the*room*
    **stores*a*reference*to*the*neighboring*room.*
    **
    **class*Room*extends*GameObject*
    **Room*might*have*some*items*or*NPC.*
    **
    **@author**Michael*K├Âlling*and*David*J.*Barnes*
    **@version*2011.08.08*
    */*
    public*class*Room*extends*GameObject*
    {*
    ***private*String*description;*
    ***private*HashMap<String,*Exit>*exits;//*stores*exits*of*this*room.*
    ***//*stores*items*of*this*room.*
    ***private*ArrayList<Item>*roomItemList;*
    ***private*NPC*dwarf;//*NPC*in*this*room*
    ***/***
    *****constructor*for*class*room*
    *****Create*a*room*described*"description".*Initially,*it*has*
    *****no*exits.*"description"*is*something*like*"a*kitchen"*or*
    *****"an*open*court*yard".*
    *****@param*description*The*room's*description.*
    ****/*
    ***public*Room(String*name,*String*description)*
    ***{*
    ******super(name,*description);*
    ******exits*=*new*HashMap<String,*Exit>();*
    ******//*Exits*is*a*HashMap,**it*will*store*exits*of*this*room*
    ******roomItemList*=*new*ArrayList<Item>(0);*
    ******//*itemsList*is*an*array,*it*will*contain*items*in*this*room*
    ***}*
    ***/***
    *****Define*an*exit*from*this*room.*
    *****@param*direction*The*direction*of*the*exit.*
    *****@param*neighbor**The*room*to*which*the*exit*leads.*
    *****setter*eixts*
    ****/*
    ***public*void*setExit(String*direction,*Exit*neighborExits)*
    ***{*
    ******exits.put(direction,*neighborExits);*
    ***}*
    ***/***
    *****@return*The*short*description*of*the*room*
    *****(the*one*that*was*defined*in*the*constructor).*
    ****/*
    ***public*String*getShortDescription()*
    ***{*
    ******return*super.getDescription();*
    ***}*
    ***/***
    *****Return*a*description*of*the*room*in*the*form:*
    *********You*are*in*the*kitchen.*
    *********Exits:*north*west*
    *****@return*A*long*description*of*this*room*
    ****/*
    ***public*String*getLongDescription()*
    ***{*
    ******return*"You*are*"*+*super.getDescription()*+*".\n"*+*getExitString();*
    ***}*
    ***/***
    *****print*out*exits*data*of*this*room*
    ****/*
    ***public*void*printRoomInfo()*{*
    ******String*str*=*"Exits:";*
    ******Set<String>*keys*=*exits.keySet();*
    ******for*(String*exit*:*keys)*
    ******str*+=*"*"*+*exit;*
    ******System.out.println(str);*
    ***}*
    ***/***
    *****Return*a*string*describing*the*room's*exits,*for*example*
    *****"Exits:*north*west".*
    *****@return*Details*of*the*room's*exits.*
    ****/*
    ***private*String*getExitString()*
    ***{*
    ******String*returnString*=*"Exits:";*
    ******Set<String>*keys*=*exits.keySet();*
    ******for(String*exit*:*keys)*{*
    *********returnString*+=*"*"*+*exit;*
    ******}*
    ******return*returnString;*
    ***}*
    ***/***
    *****print*out*itmes*data*of*this*room*
    ****/*
    ***public*void*printRoomItemInfo(){*
    ******String*itemStr="This*room*has";*
    ******if(roomItemList.isEmpty()){*
    *********itemStr+="*no*items";*
    ******}*
    ******else{*
    *********for(Item*item:roomItemList){*
    ************itemStr+="*"*+item.getName()*+",";*
    *********}*
    ******}*
    ******System.out.println(itemStr+".");*
    ***}*
    ***/***
    *****ask*the*item*exists*in*this*room*
    *****true/false*
    ****/*
    ***public*boolean*existItem(String*itemName)*
    ***{*
    ******boolean*takenItem=false;*
    ******String*name;*
    ******for(Item*item:roomItemList){*
    *********name=item.getName();*
    *********if(itemName.compareTo(name)==0)*
    *********{*
    ************takenItem=true;*
    *********}*
    ******}*
    ******return*takenItem;*
    ***}*
    ***/***
    *****among*items*in*this*room,*player*can*take*one*of*them.*
    *****used*enhanced*for*loop*to*check*target*of*item*in*roomItemList*(arraylist)*
    *****compareTo*will*be*help*to*find*same*string*
    *****not*'=='*
    *****return*the*item*
    ****/*
    ***public*Item*getItem(String*itemName)*
    ***{*
    ******Item*returnedItem=null;*
    ******String*name;*
    ******for(Item*item:roomItemList){*
    *********name=item.getName();*
    *********if(itemName.compareTo(name)==0)*
    *********{*
    ************returnedItem=*item;*
    *********}*
    ******}*
    ******return*returnedItem;*
    ***}*
    ***/***
    *****add*an*item*in*this*room*to*item*arraylist*
    ****/*
    ***public*void*addItem(Item*item)*
    ***{*
    ******roomItemList.add(item);*
    ***}*
    ***/***
    *****delete*an*item*in*this*room*to*item*arraylist*
    ****/*
    ***public*void*removeItem(Item*item)*
    ***{*
    ******for(int*i=0;*i<roomItemList.size();i++)*
    ******{*
    *********if(roomItemList.get(i).equals(item))*
    *********{*
    ************roomItemList.remove(i);*
    ************break;*
    *********}*
    ******}*
    ***}*
    ***/***
    *****Getter*exit*in*this*room*
    *****return*direction*
    ****/*
    ***public*Exit*getExit(String*direction)*{*
    ******return*exits.get(direction);*
    ***}*
    ***/***
    *****Getter*NPC*in*this*room*
    *****return*dawrf*in*this*room*
    ****/*
    ***public*NPC*getNPC()*{*
    ******return*dwarf;*
    ***}*
    ***/***
    *****Getter*NPC*in*this*room*
    *****@param*ncp*
    ****/*
    ***public*void*setNPC(NPC*npc)*{*
    ******this.dwarf*=*npc;*
    ***}*
    }*
    package*zuul;*
    /***
    **class*State*show*current*room,*player*and*dwarf*information*
    **printing*information*needs*after*excuting*an*acton.*
    **to*avoid*repeating*make*staic*fuction*
    **however*it*is*not*directly*used*so*that*satisfy*encapsulation*
    */*
    public*class*State*
    {*
    ***public*State()*
    ***{*
    ***}*
    ***public*static*void*currentState*(Player*player)*
    ***{*
    ******player.getCurrentRoom().printRoomInfo();*
    ******player.getCurrentRoom().printRoomItemInfo();*
    ******player.printPlayerItemInfo();*
    ******//check*if*a*dwarf*exsit*in*current*room,*
    ******//if*it*does,*then*print*out*infomation*
    ******if(player.getCurrentRoom().getNPC()!=null)*
    ******{*
    *********player.getCurrentRoom().getNPC().printNPCInfo();*
    ******}*
    ***}*
    }*
    package*zuul;*
    /***
    **class*Take*implements*Action*
    **Take*is*one*of*action*command.*
    **Takerop*provide*action*that*player*can*have*an*item*in*current*room*
    **
    */*
    public*class*Take*implements*Action*{*
    ***/***
    *****constructor*of*the*Take*class.*
    ****/*
    ***public*Take()*{*
    ***}*
    ***public*void*doAction(Player*player,*Command*command)*{*
    ******Item*item;*
    ******//check*if*player*typed*secondword*or*not.*
    ******if*(!command.hasSecondWord())*{*
    *********//*
    *********System.out.println("What*do*you*want*to*take?");*
    *********return;*
    ******}*
    ******String*itemName*=*command.getSecondWord();*
    ******//check*if*player*has*the*item*in*the*player*itemlist*
    ******if*(player.getCurrentRoom().existItem(itemName))*{*
    *********boolean*hasItem;*
    *********hasItem=player.existItem(itemName);*
    *********//if*player*has*same*name*of*item,*just*show*comment*
    *********//but*player*can*take*same*name*item*as*muchas*they*can.*
    *********if(hasItem==true){*
    ************System.out.println("You*have*the*same*item*but*you*can*take*more");*
    *********}*
    *********item*=*player.getCurrentRoom().getItem(itemName);*
    *********//*remove*item*from*current*Room*item*list.*
    *********player.getCurrentRoom().removeItem(item);*
    *********//*Add*Item*in*Player's*Items*List*
    *********player.addItem(item);*
    *********}*else*{*
    *********System.out.println("The*item,*you*require,*doesnt*exist*");*
    *********return;*
    ******}*
    ******//show*current*information*
    ******State.currentState(player);*
    ***}*
    }*
    package*zuul;*
    /***
    **class*Trade*implements*Action*
    **Trade*is*one*of*action*command.*
    **between*player*and*dwarf*to*swap*items*
    **
    */*
    public*class*Trade*implements*Action*{*
    ***/***
    ******constructor*of*the*Trade*Class*
    ****/*
    ***public*Trade()*{*
    ***}*
    ***/***
    *****The*doAction*function*trades*an*item*between*the*current*player*and*the*
    *****existing*NPC*in*the*room.*if*the*specified*item*is*not*owned*by*the*
    *****player,*an*error*message*is*printed*
    *****
    *****@param*player*
    ****************-->*the*current*player*
    ****/*
    ***public*void*doAction(Player*player,*Command*command)*{*
    ******Item*item,*dwarfitem;*
    ******if(player.getCurrentRoom().getNPC()!=null)*
    ******{*
    *********NPC*dwarf*=*player.getCurrentRoom().getNPC();*
    *********//check*if*a*dwarf*exsit*in*current*room*
    *********if*(!command.hasSecondWord())*{*
    ************//*if*player*didn't*type*second*word,*then*ask*detail*
    ************System.out.println("What*do*you*want*to*trde?");*
    ************return;*
    *********}*
    *********String*itemName*=*command.getSecondWord();*
    *********//check*if*the*item*user*want*to*swap,*is*exist*in*player*list*first*
    *********//then,*exchange*item*with*dwarf.*
    *********if*(player.existItem(itemName))*{*
    ************item*=*player.getItem(itemName);*
    ************player.removeItem(item);*
    ************player.addItem(dwarf.tradeItem(item));*
    ************}*else*{*
    ************System.out.println("Sorry,*you*don't*have*this*item!");*
    *********}*
    *********State.currentState(player);*
    *********}else{*
    *********//show*error*as*there*is*not*dwarf*in*this*room*to*trade*with*
    *********System.out.println("Sorry,*you*can't*trade*because*no*dwarf*trades*with");*
    ******}*
    ***}*
    }*
    package*zuul;*
    /***
    **class*Unlock*implements*Action*
    **Unlock*is*one*of*action*command.*
    **Unlock*provide*action*that*player*can*locked*one*of*exit*in*a*curret*room*
    **need*to*an*key*to*unlock*the*exit*
    */*
    public*class*Unlock*implements*Action*{*
    ***/***
    *****constructor*of*the*Unlock*class*
    ****/*
    ***public*Unlock()*{*
    ***}*
    ***/***
    *****Try*to*unlock*a*door/exit*in*the*room*
    *****
    ****/*
    ***public*void*doAction(Player*player,*Command*command)*{*
    ******boolean*existKey*=*false;*
    ******Exit*exit*=*null;*
    ******if*(!command.hasSecondWord())*{*
    *********//*if*player*didn't*type*second*word,*then*ask*detail*
    *********System.out.println("which*exits*unLock?");*
    *********return;*
    ******}*
    ******//*Get*the*second*word*of*the*Command*
    ******String*exitToUnlock*=*command.getSecondWord();*
    ******exit*=*player.getCurrentRoom().getExit(exitToUnlock);*
    ******//*Check*if*the*player*has*a*key*Item*
    ******existKey*=*player.existItem("key");*
    ******try*{*
    *********//*if*there*is*such*Exit*and*the*Player*has*a*key*Item*
    *********if*(existKey*==*true)*{*
    ************if*(exit.isLocked()*==*true)*{*
    ***************//*Unlock*the*door*
    ***************player.getCurrentRoom().getExit(exitToUnlock)*
    ***************.setLocked(false);*
    ***************}*else*{*
    ***************//show*message*that*door*is*already*unlocked*
    ***************System.out.println("The*Exit*is*already*unlocked!");*
    ************}*
    ************}*else*{*
    ************//*show*error*message*that*player*does*not*have*a*key*
    ************System.out.println("The*player*do*not*have*a*key!");*
    *********}*
    *********}*catch*(NullPointerException*e)*{*
    *********//*show*error*message*about*nullpoint*
    *********System.out.println("This*Exit*does*not*exist!*Try*another*exit!");*
    ******}*
    ******//show*current*information*
    ******State.currentState(player);*
    ***}*
    }*
    Last edited by gack1234; 06-26-2014 at 11:58 AM.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: Loose Coupling and Cohesion

    What on earth is the point of that?

    I'm locking this.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Replies: 3
    Last Post: 01-13-2012, 11:45 AM
  2. Events and listeners - reduce coupling
    By Serpenthelm in forum AWT / Swing
    Replies: 6
    Last Post: 01-03-2012, 01:07 AM
  3. When do we draw the line for loose coupling.
    By h8alfred in forum Advanced Java
    Replies: 0
    Last Post: 03-27-2009, 03:05 AM
  4. coupling
    By srinuvericharla in forum Advanced Java
    Replies: 1
    Last Post: 05-16-2007, 04:06 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •