一个java游戏,急,(1)
一个java游戏,要求用hashmap
游戏如下:
假设你在一个大屋子里,屋子里有很多房子,你迷路了不知道如何出去,你到处走(go east/go west/go south/go north).你可以进别的屋子通过这个go命令,但是你没有地图只能打字,如果那个方向没有屋子就显示"no door!".每个屋子里可以有物件(0个或多个都可以),你通过拿起这些物件(例如钥匙,手电等等)才可以进入某些屋子或才可以开某些屋子的门. 游戏者不可以无限制的拿物件,要看游戏者最多可承受的重量(也就是说每个物件都有一个自己的重量),游戏者如果在拿起新物件的同时发现超重,就不能拿新的了.这时,游戏者要用"dropItem()"命令来卸下多余的不需要的物件,当然,游戏里必须要有"getItem()"的命令让游戏者可以拿起物件.最后,游戏者在游走于各个房间的同时必须有一个"back()"方法,也就是一个back命令让游戏者可以回到刚出来的那个房间.另外,最后还要有一个"look"命令,让游戏者可以查看屋子里是否有物件,不过这个"look"命令不是必须的.最后,还有"quit"命令,让游戏者退出.游戏者一旦找到了最后的出口,游戏胜利.
提供了部分的程序,
class Command
{
private String commandWord;
private String secondWord;
public Command(String firstWord, String secondWord)
{
commandWord = firstWord;
this.secondWord = secondWord;
}
public String getCommandWord()
{
return commandWord;
}
public String getSecondWord()
{
return secondWord;
}
public boolean isUnknown()
{
return (commandWord == null);
}
public boolean hasSecondWord()
{
return (secondWord != null);
}
}
class Room
{
private String description;
private HashMap exits; // stores exits of this room.
private HashMap existMapItems;
//public boolean itemExist;
public Room(String description, Item item)
{
this.description = description;
exits = new HashMap();
existMapItems = new HashMap();
setItem(item);
}
//setItem() method
private void setItem(Item existItem)
{
if (existItem != null)
{
existMapItems.put (existItem.getShortItemDescription (),existItem);
}
}
public void setExit(String direction, Room neighbor)
{
exits.put(direction, neighbor);
}
public String getShortDescription()
{
return description;
}
public String getLongDescription()
{
return "You are " + description + ".\n" + getExitString() + ".\n" + getItemString();
}
private String getExitString()
{
String returnString = "Exits:";
Set keys = exits.keySet();
for(Iterator iter = keys.iterator(); iter.hasNext(); )
returnString += " " + iter.next();
return returnString;
}
private String getItemString()
{
String returnString = "Items:";
Set keys = existMapItems.keySet();
for(Iterator iter = keys.iterator(); iter.hasNext(); )
returnString += " " + iter.next();
return returnString;
}
public Room getExit(String direction)
{
return (Room)exits.get(direction);
}
}class CommandWords
{
// a constant array that holds all valid command words
private static final String validCommands[] = {
"go", "/", "back", "/", "get", "/", "quit", "/", "help"
};
public CommandWords()
{
// nothing to do at the moment...
}
public boolean isCommand(String aString)
{
for(int i = 0; i < validCommands.length; i++) {
if(validCommands[i].equals(aString))
return true;
}
// if we get here, the string was not found in the commands
return false;
}
public void showAll()
{
for(int i = 0; i < validCommands.length; i++) {
System.out.print(validCommands[i] + " ");
}
System.out.println();
}
}
class Game
{
private Parser parser;
private Room currentRoom;
private Room outsideRoom, restRoom, toolRoom, darkRoom, securityRoom, fuelRoom, exitRoom;
private Item emptyIn, torch, ladder, idCard, key, gasBottle;
public Game()
{
createRooms();
parser = new Parser();
}
public void createRooms()
{
//create the items
emptyIn = new Item("no item",0);
torch = new Item("torch",4);
ladder = new Item("ladder",10);
idCard = new Item("ID card",1);
key = new Item("key",2);
gasBottle = new Item("gas bottle",20);
// create the rooms with items
outsideRoom = new Room("at outside any rooms of the factory", emptyIn);
restRoom = new Room("in the rest room", torch);
toolRoom = new Room("in the tool room", ladder);
darkRoom = new Room("in a dark room without light", idCard);
securityRoom = new Room("in the security room", key);
fuelRoom = new Room("in the fuel room", gasBottle);
exitRoom = new Room("in the exit room and you need to get the door access to get out", emptyIn);
//exitRoom.itemExist = false;
//initialise item arributes
//torch.ableToBeCarried (true);
//ladder.ableToBeCarried (true);
//idCard.ableToBeCarried (true);
//key.ableToBeCarried (true);
//gasBottle.ableToBeCarried (false);
// initialise room exits
outsideRoom.setExit("east", restRoom);
outsideRoom.setExit("west", darkRoom);
restRoom.setExit("north", toolRoom);
restRoom.setExit ("west", outsideRoom);
darkRoom.setExit("north", securityRoom);
darkRoom.setExit ("east", outsideRoom);
securityRoom.setExit("north", fuelRoom);
fuelRoom.setExit("west", exitRoom);
currentRoom = outsideRoom; // start game outside
}
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.");
}
private void printWelcome()
{
System.out.println();
System.out.println("Welcome to Adventure!");
System.out.println("Adventure is a new, incredibly boring adventure game.");
System.out.println("Type 'help' if you need help.");
System.out.println();
System.out.println(currentRoom.getLongDescription());
}
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 ("get"))
// getItem(command);
//else if (commandWord.equals ("back"))
// getBack(command);
else if (commandWord.equals("quit")) {
wantToQuit = quit(command);
}
return wantToQuit;
}
// implementations of user commands:
private void printHelp()
{
System.out.println("You are lost. You are alone. You wander");
System.out.println("around at the factory.");
System.out.println();
System.out.println("Your command words are:");
parser.showCommands();
}