Click here to Skip to main content
15,867,488 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Java
public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private Person person;
    private Items item;
        
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        createItems();
        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.setExit("east", theater);
        outside.setExit("south", lab);
        outside.setExit("west", pub);
        theater.setExit("west", outside);
        pub.setExit("east", outside);
        lab.setExit("north", outside);
        lab.setExit("east", office);
        office.setExit("west", lab);
        
        
        
        
        Items book, pencilcase, pen, laptop;
        
        book = new Items("book");
        pencilcase = new Items("pencilcase");
        pen = new Items("pen");
        laptop = new Items("laptop");
        
        theater.setItem("book", book);
        pub.setItem("pencilcase", pencilcase);
        lab.setItem("pen", pen);
        office.setItem("laptop", laptop);   
        
                
        currentItem = null;
           
        currentRoom = outside;  // start game outside
    }

----------------------------------------------------------------------------------

import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;

/**
 * Write a description of class Items here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class Items
{
    private String description;
    private HashMap<string,> item;
    
    public Items(String description)
    {
        this.description = description;
        item = new HashMap<string,>();
    }
    
    //public String getDescription()
    //{
      //  return description;
    //}
    
     public void setItem(String description, Items thing)
    {
        item.put(description, thing);
    }
    
    /**
     * @return The short description of the room
     * (the one that was defined in the constructor).
     */
    public String getDescription()
    {
        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 "There is " + description + ".\n" + getItemString();
    }

    /**
     * Return a string describing the room's exits, for example
     * "Exits: north west".
     * @return Details of the room's exits.
     */
    private String getItemString()
    {
        String returnString = "Item:";
        Set<string> keys = item.keySet();
        for(String item : keys) {
            returnString += " " + item;
        }
        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 Items getItem(String i) 
    {
        return item.get(i);
    }
}

-----------------------------------------------------------------------------

public class Room 
{
    private String description;
    private HashMap<string,> exits;        // stores exits of this room.
    //private Items item;
    //private HashMap<string,>items;

    /**
     * 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,>();
    }
    
    //public Items()
    //{
        //this.item = item;
      //  items = new HashMap<string,>();
    //}

    /**
     * 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();
    }

    /**
     * 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);
    }
}

--------------------------------------------------------

the error appears in the setItem for the Items class but it has the same concept of the Room class and it does not show the error can someone tell me how to fix this? i want to add the items to the rooms so when room is created the item is created as well but i dont want to use arraylist.
Posted
Updated 24-Nov-14 9:15am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900