将项目添加到基于文本的Java游戏

时间:2014-04-12 15:30:34

标签: java text items

我目前正在为课程制作基于文本的游戏,无法弄清楚如何向游戏类添加项目。游戏和房间之间的移动工作得很好,但这一部分让我感到困惑。这是与这个sitch有关的三个类。

    public class Item{
private String itemName;

public Item(String itemName){
this.itemName = itemName;

}

public String getItemName(){
return itemName;
}
}

    import java.util.Set;
import java.util.HashMap;
import java.util.Iterator;
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.
 * 
 * @author  Michael Kölling and David J. Barnes
 * @version 2011.08.09
 */

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 ArrayList<Item> 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, Room>();
        items = new ArrayList<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()
    {
        String longDescription = "You are " + description + ".\n" + getExitString();
        if(items.size() > 0) {
            longDescription += "\nThe following things are here: \n";
            for (Item item : items) {
                longDescription += "\t" + item.getItemName() + "\n";
            }
        }
        return longDescription;

    }

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

    /**
     * Add an item to the room 
     */
    public void addItem(Item i){
        items.add(i);
    }
    public String getItem() {

    //THIS IS THE METHOD I CANT FIGURE OUT
    }
}



    import java.util.ArrayList;
/**
 * 
 */

public class Game 
{
    private Parser parser;
    private Room currentRoom;
    private ArrayList<Item> inventory;
    private Item seaWeed;
    /**
     * Create the game and initialise its internal map.
     */
    public Game() 
    {
        createRooms();
        inventory = new ArrayList<Item>();
        parser = new Parser();
        seaWeed = new Item("Seaweed");
    }

    /**
     * Create all the rooms and link their exits together.
     */
   private void createRooms()
    {
        Room oceanGrotto, outsideFortress, fortressWallEast, fortressWallWest,
                insideFortress, secretTunnel, tunnelEnd, bavesStable, sunkenShip, 
                caviarUniversity, parkingLot, murkeyWaters, otterOasis,
                mysteriousCloud, dryLands, caveEntrance, caveInside,
                darkCrevace, crevaceDown, crevaceBottom;

        // create the rooms
        oceanGrotto = new Room("in the whales territory");
        outsideFortress = new Room("the gate of the sharks fortress");
        fortressWallEast = new Room("The eastern stone wall of the fortress");
        fortressWallWest = new Room("western stone wall of the fortress");
        insideFortress = new Room("inside the fortress");
        secretTunnel = new Room("inside the hidden tunnel");
        tunnelEnd = new Room("end of the tunnel");
        bavesStable = new Room("the seahorses house");
        sunkenShip = new Room("a sunken ship");
        caviarUniversity = new Room("Fish school");
        parkingLot = new Room("parking lot behind the school");
        murkeyWaters = new Room("Polluted waters");
        otterOasis = new Room ("Home of the otters");
        mysteriousCloud = new Room("a mysterious cloud above the water");
        dryLands = new Room("area above the ocean");
        caveEntrance = new Room("entrance to the abandoned cave");
        caveInside = new Room("inside  the cave");
        darkCrevace = new Room("a dark hole in the ground");
        crevaceDown = new Room ("deep in the dark crevace");
        crevaceBottom = new Room("the bottom of the crevace");

        // initialise room exits
        oceanGrotto.setExit("east", murkeyWaters);
        oceanGrotto.setExit("south", caviarUniversity);
        oceanGrotto.setExit("west", bavesStable);
        oceanGrotto.setExit("north", outsideFortress);

        //Add extra exit DOWN later
        bavesStable.setExit("east", oceanGrotto);
        bavesStable.setExit("south", sunkenShip);
        bavesStable.setExit("north", fortressWallWest);

        secretTunnel.setExit("north", tunnelEnd);
        secretTunnel.setExit("up", bavesStable);

        tunnelEnd.setExit("south", secretTunnel);

        fortressWallWest.setExit("south", bavesStable);
        fortressWallWest.setExit("east", outsideFortress);

        fortressWallEast.setExit("south", murkeyWaters);
        fortressWallEast.setExit("west", outsideFortress);

        //Add extra Exit North later
        outsideFortress.setExit("south", oceanGrotto);
        outsideFortress.setExit("east", fortressWallEast);
        outsideFortress.setExit("west", fortressWallWest);

        sunkenShip.setExit("north", bavesStable);
        sunkenShip.setExit("east", caviarUniversity);

        //Add extraExit south later
        caviarUniversity.setExit("north", oceanGrotto);
        caviarUniversity.setExit("east", otterOasis);
        caviarUniversity.setExit("west", sunkenShip);

        parkingLot.setExit("north", caviarUniversity);

        murkeyWaters.setExit("north", fortressWallEast);
        murkeyWaters.setExit("east", caveEntrance);
        murkeyWaters.setExit("south", otterOasis);
        murkeyWaters.setExit("west", oceanGrotto);

        otterOasis.setExit("north", murkeyWaters);
        otterOasis.setExit("east", darkCrevace);
        otterOasis.setExit("south", mysteriousCloud);
        otterOasis.setExit("west", caviarUniversity);

        mysteriousCloud.setExit("north", otterOasis);
        mysteriousCloud.setExit("up", dryLands);

        caveEntrance.setExit("east", caveInside);
        caveEntrance.setExit("west", murkeyWaters);

        caveInside.setExit("west", caveEntrance);

        darkCrevace.setExit("west", otterOasis);
        darkCrevace.setExit("down", crevaceDown);

        //Add extra exit DOWN later
        crevaceDown.setExit("up", darkCrevace);

        crevaceBottom.setExit("up", crevaceDown);

        dryLands.setExit("down", mysteriousCloud);

        //add items
        oceanGrotto.addItem(seaWeed);//THIS IS WHERE I AM TRYING TO USE THE METHOD 
        currentRoom = oceanGrotto;``


    }


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

        CommandWord commandWord = command.getCommandWord();

        switch (commandWord) {
            case UNKNOWN:
                System.out.println("I don't know what you mean...");
                break;

            case HELP:
                printHelp();
                break;

            case SWIM:
                swimRoom(command);
                break;

            case QUIT:
                wantToQuit = quit(command);
                break;

            case LOOK:
                System.out.println(currentRoom.getLongDescription());
                system.out.println(currentRoom.getItem());
                break;
        }
        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 the ocean flo'.");
        System.out.println();
        System.out.println("Your command words are:");
        parser.showCommands();
    }

    /** 
     * Try to go in one direction. If there is an exit, enter the new
     * room, otherwise print an error message.
     */
    private void swimRoom(Command command) 
    {
        if(!command.hasSecondWord()) {
            // if there is no second word, we don't know where to go...
            System.out.println("Swim where?");
            return;
        }

        String direction = command.getSecondWord();

        // Try to leave current room.
        Room nextRoom = currentRoom.getExit(direction);

        if (nextRoom == null) {
            System.out.println("The water is too polluted, you cant go there!");
        }
        else {
            currentRoom = nextRoom;

    }
}
    /** 
     * "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
        }
    }

}

抱歉,如果我第一次使用此网站时没有正确格式化

1 个答案:

答案 0 :(得分:0)

public String getItem(int i) {
    if(i<items.size()&&>=0)
        return items.get(i).toString();
    else
        return "item does not exist";
}