0

Hello, I am currently doing an assignment which requires the use of hashmaps. The game is called "zuul" which is a text based game. A player class can pick up and drop items and if an item is picked up, the item is taken away from the rooms hashmap and is stored in the player. If the player drops the item, vica versa, but to the rooms class. The player may hold in total (as an int) 100. The problem im havin is when I pik up an item with a player weight of 100, it says item cannot be found and the item is deleted from the room hashmap. When I add an item with a playerweight of 99, it says item doesnt exist? I just need a couple of pointers if ou can figure it out. There are no syntax errors.

p.s. HashMap is compulsory for this assignment.

Room Class

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  
 * @version 05.05.2009
 */

public class Room 
{
    private String description;
    private HashMap<String, Room> exits;      // stores exits of this room.
    private HashMap<String, Item> items;      //set items  
    private String roomName;
    
    /**
     * 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>();
        items = 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);
    }

    
    /**
     * Creates an item in the Item room list.
     */
    public void addItem(String name, String description, int weight)   
    {
        items.put(name, new Item(name, description, weight));
    }
       
     /**
     * Looks for item in room, returns true if found, false if it doesn't exist.
     */
    public boolean sameName(String name)
    {
        Set<String> keys = items.keySet();
        for(Iterator<String> i=keys.iterator();i.hasNext();) 
        {
            if(i.next().equals(name))
            {
                return true;
            }
        }
        return false;
    }
    
   /**
    * @returns items description.
    */
    public Item getItemValue(String name)
    {
        return (Item)items.get(name);
    }
    
    /**
     * removes an item from inventory
     */
    public void giveUpItem(String name)
    {
        items.remove(name);   
    }
    
     /**
     * lists all items in the Rooms inventory.
     */
     public void printRoomItemList()   
    {   
           
        System.out.println(description + " Items");   
        
        Set<String> keys = items.keySet();   
        for(Iterator<String> i=keys.iterator();i.hasNext();)   
        {   
            System.out.println();
            String name = i.next();   
            Item it = getItemValue(name);   
            System.out.println("----------------------------------------------");
            System.out.println("Name:        " + it.getItemName());   
            System.out.println("Description: " + it.getItemDescription());
            System.out.println("Weight:      " + it.getItemWeight());
            System.out.println("----------------------------------------------");
        }   
    }   
    
    
    /**
     * @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 in the " + 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 (Room)exits.get(direction);
    }
    
   
    /**
     * 
     */
    public String getRoomName()
    {
        return roomName;
    } 
  

}
______________________________________________________

Player Class

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

/**
 * Write a description of class Player here.
 * 
 * @author 
 * @version 05.05.2009
 */
public class Player
{
    private String name;
    private int weight;
    private HashMap <String, Item> items;
    private Room currentRoom;
    private static int maxWeight = 100;
    private int playerWeight;

    /**
     * Constructor for objects of class Player
     */
    public Player(String name, Room room)
    {
        this.name = name;
        items = new HashMap <String, Item>();
        currentRoom = room;
        playerWeight = 0;
    
    }

    public int getPlayerWeight()
    {
        return playerWeight;
    }
    
    public static int getMaxWeight()
    {
        return maxWeight;
    }
   
    /**
     * @return current position
     */ 
    public Room getRoom()
    {
        return currentRoom;
    }
    
    /**
     * Sets Player current position.
     * @param room
     */
    public void setRoom(Room room)
    {
        currentRoom = room;
    }
    
    
    /**
     * @returns the players name
     */
    public String getName()
    {
        return name;
    }
    
    /**
     * @returns the players maximum weight 
     */
    public int getMaximumWeight()
    {
        return maxWeight;
    }

    /**
     * returns the number of items in inventory
     */
     public  int inventorySize()
     {
         return items.size();
    
     }

    
    /**
     * Looks for an item in a room, return true if found.
     * If item does not exist, return false.
     * @return
     */
    public boolean sameName(String name)   
    {   
        Set<String> keys = items.keySet();   
        for(Iterator<String> i=keys.iterator();i.hasNext();)   
    {   
            if(i.next().equals(name))   
            {   
                return true;   
            }   
        }   
        return false;   
    }   

    /**
     * If there is an item in the current room that has the same name
     * as the item you are trying to obtain, returns the items attributes
     * else, return null.
     * @return
     * @param name used to searching HashMap for items.
     */
    public Item checkTake(String name)
    {
        if(currentRoom.sameName(name))
        return (Item)currentRoom.getItemValue(name);
        else
        return null;
    }
    
    
    /**
     * If there is an item with the same name as your given name in Player,
     * return true and attributes of that item.
     * If it doesn't exist, return null.
     * @return
     * @param name used to search for HashMap items.
     */
    public Item checkDrop(String name)
    {
        if(sameName(name))
        return (Item)getItemValue(name);
        else
        return null;
        
    }
    
    /**
     * Searches the room for that given item, if HashMap contains the same name as
     * the given name, Item from current room deletes and is added to Player collection, 
     * return true. Returns false if item isn't found and if player inventory weighs more
     * than 100.
     * @return
     * @param name used to search HashMap for items
     */
    public boolean getItem(String name)
    {
        
        Item it = checkTake(name);
        if(it!=null)
        {
            playerWeight = playerWeight + weight;
            if(!sameName(name) && ((playerWeight + weight)  <= maxWeight))
            
            {
                addItem(it.getItemName(),it.getItemDescription(),it.getItemWeight());
                currentRoom.giveUpItem(name);
                return true;
            }
                else
                System.out.println("You can not have item with same name: " + name);
            }
                System.out.println("There is no " + name + " in this Room.");
                return false;
            }
            
            
           
           
          
      
    
    /**
     * Search the HashMap items in player inventory, if item is found and the
     * given name equals that items name, return true, Item is removed from Player 
     * inventory into that current room. If item isn't found in Player inventory,
     * return false.
     * @return
     * @param name used to search HashMap for items
     */
    public boolean removeItem(String name)
    {
        Item it = checkDrop(name);
        if(it!=null)
        {
            
            if(!currentRoom.sameName(name))
            {
                playerWeight = playerWeight - it.getItemWeight();
                currentRoom.addItem(it.getItemName(),it.getItemDescription(),it.getItemWeight());
                giveUpItem(name);
                
                return true;
            }
        }
        return false;
    }
                
    
  
    
    
     /**
     * add an item to your inventory
     */
    public void addItem(String name, String description, int weight)   
    {   

       
        
        if((playerWeight + weight)  <= maxWeight)
        
        {
        playerWeight = playerWeight + weight;
        items.put(name, new Item(name,description,weight)); 

        }
        else if(playerWeight + weight > maxWeight)
        { 
          System.out.println("you are too weak to carry this item!");   
          
        }
            
    }
    
    
    
    
    /**
     * @returns an items name, description and weight given its name.
     */
    public Item getItemValue(String name)   
    {   
        return (Item)items.get(name);   
    }   

    /**
     * Permanently removes an item from that HashMap.
     */
     public void giveUpItem(String name)   
    {   
        items.remove(name);        
    }   
           
    /**
     * lists all items in the Players inventory.
     */
     public void printPlayerInventory()   
    {   
           
        System.out.println(name + "'s" + " Inventory");   
        
        Set<String> keys = items.keySet();   
        for(Iterator<String> i=keys.iterator();i.hasNext();)   
        {   
            System.out.println();
            String name = (String)i.next();   
            Item it = (Item)items.get(name);   
            System.out.println("----------------------------------------------");
            System.out.println("Name:        " + it.getItemName());   
            System.out.println("Description: " + it.getItemDescription());
            System.out.println("Weight:      " + it.getItemWeight());
            System.out.println("----------------------------------------------");    
       
        }   
    }   
 
    
       
}
______________________________________________________

Game class

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

/**
 * Write a description of class Player here.
 * 
 * @author 
 * @version 05.05.2009
 */
public class Player
{
    private String name;
    private int weight;
    private HashMap <String, Item> items;
    private Room currentRoom;
    private static int maxWeight = 100;
    private int playerWeight;

    /**
     * Constructor for objects of class Player
     */
    public Player(String name, Room room)
    {
        this.name = name;
        items = new HashMap <String, Item>();
        currentRoom = room;
        playerWeight = 0;
    
    }

    public int getPlayerWeight()
    {
        return playerWeight;
    }
    
    public static int getMaxWeight()
    {
        return maxWeight;
    }
   
    /**
     * @return current position
     */ 
    public Room getRoom()
    {
        return currentRoom;
    }
    
    /**
     * Sets Player current position.
     * @param room
     */
    public void setRoom(Room room)
    {
        currentRoom = room;
    }
    
    
    /**
     * @returns the players name
     */
    public String getName()
    {
        return name;
    }
    
    /**
     * @returns the players maximum weight 
     */
    public int getMaximumWeight()
    {
        return maxWeight;
    }

    /**
     * returns the number of items in inventory
     */
     public  int inventorySize()
     {
         return items.size();
    
     }

    
    /**
     * Looks for an item in a room, return true if found.
     * If item does not exist, return false.
     * @return
     */
    public boolean sameName(String name)   
    {   
        Set<String> keys = items.keySet();   
        for(Iterator<String> i=keys.iterator();i.hasNext();)   
    {   
            if(i.next().equals(name))   
            {   
                return true;   
            }   
        }   
        return false;   
    }   

    /**
     * If there is an item in the current room that has the same name
     * as the item you are trying to obtain, returns the items attributes
     * else, return null.
     * @return
     * @param name used to searching HashMap for items.
     */
    public Item checkTake(String name)
    {
        if(currentRoom.sameName(name))
        return (Item)currentRoom.getItemValue(name);
        else
        return null;
    }
    
    
    /**
     * If there is an item with the same name as your given name in Player,
     * return true and attributes of that item.
     * If it doesn't exist, return null.
     * @return
     * @param name used to search for HashMap items.
     */
    public Item checkDrop(String name)
    {
        if(sameName(name))
        return (Item)getItemValue(name);
        else
        return null;
        
    }
    
    /**
     * Searches the room for that given item, if HashMap contains the same name as
     * the given name, Item from current room deletes and is added to Player collection, 
     * return true. Returns false if item isn't found and if player inventory weighs more
     * than 100.
     * @return
     * @param name used to search HashMap for items
     */
    public boolean getItem(String name)
    {
        
        Item it = checkTake(name);
        if(it!=null)
        {
            playerWeight = playerWeight + weight;
            if(!sameName(name) && ((playerWeight + weight)  <= maxWeight))
            
            {
                addItem(it.getItemName(),it.getItemDescription(),it.getItemWeight());
                currentRoom.giveUpItem(name);
                return true;
            }
                else
                System.out.println("You can not have item with same name: " + name);
            }
                System.out.println("There is no " + name + " in this Room.");
                return false;
            }
            
            
           
           
          
      
    
    /**
     * Search the HashMap items in player inventory, if item is found and the
     * given name equals that items name, return true, Item is removed from Player 
     * inventory into that current room. If item isn't found in Player inventory,
     * return false.
     * @return
     * @param name used to search HashMap for items
     */
    public boolean removeItem(String name)
    {
        Item it = checkDrop(name);
        if(it!=null)
        {
            
            if(!currentRoom.sameName(name))
            {
                playerWeight = playerWeight - it.getItemWeight();
                currentRoom.addItem(it.getItemName(),it.getItemDescription(),it.getItemWeight());
                giveUpItem(name);
                
                return true;
            }
        }
        return false;
    }
                
    
  
    
    
     /**
     * add an item to your inventory
     */
    public void addItem(String name, String description, int weight)   
    {   

       
        
        if((playerWeight + weight)  <= maxWeight)
        
        {
        playerWeight = playerWeight + weight;
        items.put(name, new Item(name,description,weight)); 

        }
        else if(playerWeight + weight > maxWeight)
        { 
          System.out.println("you are too weak to carry this item!");   
          
        }
            
    }
    
    
    
    
    /**
     * @returns an items name, description and weight given its name.
     */
    public Item getItemValue(String name)   
    {   
        return (Item)items.get(name);   
    }   

    /**
     * Permanently removes an item from that HashMap.
     */
     public void giveUpItem(String name)   
    {   
        items.remove(name);        
    }   
           
    /**
     * lists all items in the Players inventory.
     */
     public void printPlayerInventory()   
    {   
           
        System.out.println(name + "'s" + " Inventory");   
        
        Set<String> keys = items.keySet();   
        for(Iterator<String> i=keys.iterator();i.hasNext();)   
        {   
            System.out.println();
            String name = (String)i.next();   
            Item it = (Item)items.get(name);   
            System.out.println("----------------------------------------------");
            System.out.println("Name:        " + it.getItemName());   
            System.out.println("Description: " + it.getItemDescription());
            System.out.println("Weight:      " + it.getItemWeight());
            System.out.println("----------------------------------------------");    
       
        }   
    }   
 
    
       
}
2
Contributors
1
Reply
4
Views
7 Years
Discussion Span
Last Post by __avd
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.