Results 1 to 10 of 10
  1. #1
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Making Items in World of Zuul

    Hi,

    I got a exercise for school. I need to progam a text based game. Now i am stucked with making items in a room.
    This is the code i got. Error is named below.
    Class Game
    Java Code:
    import java.util.Stack;
    /**
     * This class is part of the "World of Zuul" application. 
     * "World of Asus" 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  Steven Zonnenberg & Roan Christiaanse & Thomas Wijnholds
     * @version 2012
     */
    
    public class Game 
    {
        private Parser parser;
        private Room currentRoom;
        private Stack<Room> backStack;
    
        /**
         * Create the game and initialise its internal map.
         */
        public Game() 
        {
            createRooms();
            parser = new Parser();
            backStack = new Stack();
        }
    
        public static void main (String args[])
        {
            Game game = new Game();
            game.play();
        }
    
        /**
         * Create all the rooms and link their exits together.
         */
        private void createRooms()
        {
            Room woodsNorth, woodsWest, woodsEast, woodsSouth, smallshack, hallway, livingroom, kitchen, libery, office, upstairshallway, bedroom, bathroom;
    
            //outside
            woodsNorth = new Room("outside the main entrance of the hounted house");   //entrance
            woodsWest = new Room("in the woods on the west side of the house");    //
            woodsEast = new Room("in the woods on the east side of the house");    //tp
            woodsSouth = new Room("in the woods leading to the main road");   //exit house
            smallshack = new Room("in a small shack somewhere in the woods, behind the house");
    
            //downstairs
            hallway = new Room("the entrance of the house");
            livingroom = new Room("the livingroom, there is a lot of stuff on the ground");
            kitchen = new Room("in the kitchen of the house, there is something lying on the floor");
            libery = new Room("in a libery, ");
            office = new Room("in the office, the door is locked maby i have to find a key");
    
            //upstairs
            upstairshallway = new Room("a scary dark hallway, with holes in the floor");
            bedroom = new Room("the master bedroom, there is blood on the bed");
            bathroom = new Room("a smelly dark place, with bones in the bathtub");
    
            // initialise room exits
            //outside
            woodsNorth.setExit("south", hallway);
            woodsWest.setExit("east", kitchen);
            //woodsEast.setExit("west", office);
            //woodsSouth.setExit("", ); //endgame
    
            //downstaris
            hallway.setExit("south", libery);
            hallway.setExit("west", livingroom);
            hallway.setExit("east", upstairshallway);//naam miss korter
    
            livingroom.setExit("east", hallway);
            livingroom.setExit("south", kitchen);
    
            kitchen.setExit("north", livingroom);
            kitchen.setExit("east", libery);
    
            libery.setExit("north", hallway);
            libery.setExit("west", kitchen);
    
            //office.setExit("west", hallway);
    
            //upstairs
            upstairshallway.setExit("west", bedroom);
            upstairshallway.setExit("east", hallway);
    
            bedroom.setExit("south", bathroom);
            bedroom.setExit("east", upstairshallway);
    
            bathroom.setExit("north", bedroom);
    
            Item golden_key;
            woodsNorth.addItem("golden key","a_golden_key_is_the_key_for_the_golden_door",1);
            //start game outside
            currentRoom = woodsNorth;  
        }
    
        /**
         *  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, text based adventure game.");
            System.out.println("Type 'help' if you need help.");
            System.out.println();
            System.out.println(currentRoom.getLongDescription());
        }
    
        /**
         * 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("look")) {
                look();
            }
            else if (commandWord.equals("quit")) {
                wantToQuit = quit(command);
            }
            else if (commandWord.equals("eat")){
                eat();
            }
            else if (commandWord.equals("jump")){
                jump();
            }
            else if (commandWord.equals("back")){
                back();
            }
          
    
            // else command not recognised.
            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 hounted house.");
            System.out.println();
            System.out.println("Your command words are:");
            parser.showCommands();
        }
    
        /** 
         * Try to go to 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 = currentRoom.getExit(direction);
            backStack.push(currentRoom);
    
            if (nextRoom == null) {
                System.out.println("There is no door!");
            }
            else {
                currentRoom = nextRoom;
                System.out.println(currentRoom.getLongDescription());
            }
        }
    
        /** 
         * "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
            }
        }
    
        /**
         * command look
         */
        private void look()
        {
            System.out.println(currentRoom.getLongDescription());
        }
    
        /**
         * command eat
         */
        private void eat()
        {
            System.out.println("You nommed on a zombiebrain");
        }
    
        /**
         * command jump
         */
        private void jump()
        {
            System.out.println("You jumped over a gap");
        }
    
        /**
         * returns to the previous room
         */
        private void back()
        {
            if(backStack.empty() == true){
                System.out.println("You are already in the first room");
                System.out.println(currentRoom.getLongDescription());
            }
            else{
                System.out.println("You went back to the room you came from");
                currentRoom = backStack.pop();
                System.out.println(currentRoom.getLongDescription());
            }
        }
    
      
    
    }
    Class Item
    Java Code:
    import java.util.HashMap;
    /**
     * Write a description of class Item here.
     * 
     * @author (your name) 
     * @version (a version number or a date)
     */
    public class Item
    {
        // instance variables 
        private HashMap<String,Item> itemList;
        private String name;
        private String itemDescription;
        private int weight;
    
        /**
         * 
         */
        public Item(String name, String itemDescription, int weight)
        {
            this.name = name;
            this.itemDescription = itemDescription;
            this.weight = weight;
            itemList = new HashMap<String,Item>();
        }
        
        /**
         * 
         */
        private void addItems()
        {
            itemList.put("golden_key", new Item("golden_key", "a_golden_key_is_the_key_for_the_golden_door", 1));
            itemList.put("silver_key", new Item("silver_key", "a_silver_key_is_the_key_for_the_silver_door", 1));
            itemList.put("bronze_key",new Item("bronze_key", "a_bronze_key_is_the_key_for_the_bronze_door", 1));
            itemList.put("machete",new Item("manchete", "a_melee_weapon", 5));
            itemList.put("golden_apple", new Item("golden_apple", "a_golden_apple_is_an_apple_for_health_regen", 2));
            itemList.put("backpack", new Item("backpack", "a_backpack_increases_your_inventory_capacity", 2));
        }
    
        /**
         * 
         */
        public String getItemDescription()
        {
            return itemDescription;
        }
    
    }

    Class Room
    Java Code:
    import java.util.Set;
    import java.util.HashMap;
    import java.util.Iterator;
    
    /**
     * 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.
     * 
     * @author  Michael Kolling and David J. Barnes
     * @version 2008.03.30
     */
    
    public class Room 
    {
        private String description;
        private HashMap<String, Room> exits;        // stores exits of this room.
        private HashMap<String,Item> itemList;
        private String name;
        private String itemDescription;
        private int weight;
    
        /**
         * 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 description) 
        {
            this.description = description;
            exits = new HashMap<String, Room>();
            itemList = new HashMap<String,Item>();
            this.name = name;
            this.itemDescription = itemDescription;
            this.weight = weight;
            itemList = new HashMap<String,Item>();
        }
    
        /**
         * Define an exit from this room.
         * @param direction The direction of the exit.
         * @param neighbor  The room to which the exit leads.
         */
        public void setExit(String direction, Room neighbor) 
        {
            exits.put(direction, neighbor);
        }
    
        /**
         * @return The short description of the room
         * (the one that was defined in the constructor).
         */
        public String getShortDescription()
        {
            return description;
        }
    
        /**
         * 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 " + description + ".\n" + getExitString() + "There is a " + getItemDescription() + " in the room";
        }
    
        /**
         * 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;
        }
    
        /**
         * Return the room that is reached if we go from this room in direction
         * "direction". If there is no room in that direction, return null.
         * @param direction The exit's direction.
         * @return The room in the given direction.
         */
        public Room getExit(String direction) 
        {
            return exits.get(direction);
        }
        
        public void addItem(String name, String description, int weight)   
        {
            itemList.put(name, new Item(name, description, weight));
        }
    
        /**
         * 
         */
        public String getItemDescription()
        {
            return itemDescription;
        }
        
        
    }
    ErrorText:

    None, but in-game it gives a "null" back instead of golden_key when i enter the room Woodsnorth.
    Last edited by awesom; 01-16-2012 at 10:50 AM.

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Making Items in World of Zuul

    Moved from 'Advanced Java'

    Please keep homework questions in 'New to Java'. There's nothing advanced about them.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,455
    Rep Power
    20

    Default Re: Making Items in World of Zuul

    i got a compile error at Item
    And where did you post the error text?

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Making Items in World of Zuul

    Did post the error text;)

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

    Default Re: Making Items in World of Zuul

    Quote Originally Posted by awesom View Post
    Did post the error text;)
    Please don't paste it as an image, and especially of an image none of us can see. Please instead post the error text as text.

  6. #6
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Making Items in World of Zuul

    Oke did post the errorText

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

    Default Re: Making Items in World of Zuul

    The message is telling you that you're trying to call itemList's put method by passing in a single parameter, an Item parameter. itemList is a HashMap, and does HashMap have a put(...) method that takes only one parameter?

  8. #8
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Making Items in World of Zuul

    Hi,

    I know what the problem is, but i;m looking for other options to put Items in a room.
    I tried to Put a line in Game:
    Java Code:
            Item golden_key;
            woodsNorth.addItem("golden key","a_golden_key_is_the_key_for_the_golden_door",1);
            //start game outside
            currentRoom = woodsNorth;
    But when i run the game and i try to take the golden_key it says "null"

  9. #9
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Default Re: Making Items in World of Zuul

    No one got a solution?

  10. #10
    awesom is offline Member
    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

Similar Threads

  1. Replies: 8
    Last Post: 12-10-2011, 05:54 PM
  2. ListView Sub Items
    By rolledback in forum Android
    Replies: 2
    Last Post: 09-21-2011, 10:21 PM
  3. Replies: 10
    Last Post: 02-07-2011, 10:08 AM
  4. Trying to create a map for a Zuul game using JFrame
    By alpdog14 in forum New To Java
    Replies: 1
    Last Post: 04-18-2009, 01:24 AM
  5. different sort of items
    By jrleonard in forum SWT / JFace
    Replies: 0
    Last Post: 12-23-2008, 03:41 PM

Posting Permissions

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