一个java游戏,急,(1)

scarletg 2006-04-11 06:19:28
一个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();
}


...全文
369 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyan010 2006-04-12
  • 打赏
  • 举报
回复
文字mud
jobs002 2006-04-12
  • 打赏
  • 举报
回复
帮顶,关注最后的代码,解决了把完整代码贴贴..............
sail988 2006-04-12
  • 打赏
  • 举报
回复
迷宫游戏?
bgceft 2006-04-11
  • 打赏
  • 举报
回复
真牛
看不懂
帮顶
lydvqq 2006-04-11
  • 打赏
  • 举报
回复
可惜现在没什么时间。UP
water621 2006-04-11
  • 打赏
  • 举报
回复
帮顶
scarletg 2006-04-11
  • 打赏
  • 举报
回复
里面的文档齐全,都是注释啊
sgdgoodboy 2006-04-11
  • 打赏
  • 举报
回复
老兄,你认为写了这么多的代码,并且没加任何文档,有多少free man 会来看啊?
zx2002027 2006-04-11
  • 打赏
  • 举报
回复
友情UP
scarletg 2006-04-11
  • 打赏
  • 举报
回复
class Player
{
int itemNum;
String name;

public Player(String name)
{
this.name = name;
itemNum = 0;
}

public boolean canCarry(Item item)
{
boolean canCarry;
if (itemNum > 2)
{
canCarry = false;
System.out.println("You have carried two items, can not carry more.");
itemNum = itemNum;
}
else
{
canCarry = true;
System.out.println("You have got a" + " " + item);
}

return canCarry;
}

public void getItem(Item item)
{
++ itemNum;
}

public void dropItem(Item item)
{
-- itemNum;

if (itemNum < 0)
{
System.out.println("You have no items to drop.");
itemNum = itemNum;
}
else
{
System.out.println("You have dropped a" + " " + item);
}
}
}
scarletg 2006-04-11
  • 打赏
  • 举报
回复
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);

if (nextRoom == null)
System.out.println("There is no door!");
else {
currentRoom = nextRoom;
System.out.println(currentRoom.getLongDescription());

}
}

/*private void getItem(Command command)
{
if(!command.hasSecondWord()) {
// if there is no second word, we don't know where to go...
System.out.println("Get what?");
return;
}

String itemName = command.getSecondWord();

// Try to leave current room.
Item nextItem = existItem.getExit(itemName);

if (nextItem == null)
System.out.println("Sorry, there is no item here.");
else {
existItem = nextItem;
System.out.println(existItem.getLongItemDescription());
}
}
*/

private boolean quit(Command command)
{
if(command.hasSecondWord()) {
System.out.println("Quit what?");
return false;
}
else
return true; // signal that we want to quit
}

private boolean back(Command command)
{
if(command.hasSecondWord()) {
System.out.println("Illegal input form!");
return false;
}
else
return true; // signal that we want to quit
}

/*private void getBack(Command command)
{
if(!command.hasSecondWord ()) {
currentRoom = backWardRoom;
}

}
*/
}
class Item
{
private String itemName;
private int itemWeight;

public Item(String itemName, int weight)
{
this.itemName = itemName;
this.itemWeight = weight;
}

public String getShortItemDescription()
{
return itemName;
}

public int getWeitht()
{
return itemWeight;
}
}
public class JavaGame
{

public JavaGame()
{
}

public static void main(String args[])
{
Game mainEngine = new Game();
mainEngine.createRooms ();
mainEngine.play ();
}
}
class Parser
{

private CommandWords commandswords; // holds all valid command words

public Parser()
{
commandswords = new CommandWords();
}

public Command getCommand()
{
String inputLine = ""; // will hold the full input line
String word1;
String word2;

System.out.print("> "); // print prompt

BufferedReader reader =
new BufferedReader(new InputStreamReader(System.in));
try {
inputLine = reader.readLine();
}
catch(java.io.IOException exc) {
System.out.println ("There was an error during reading: "
+ exc.getMessage());
}

StringTokenizer tokenizer = new StringTokenizer(inputLine);

if(tokenizer.hasMoreTokens())
word1 = tokenizer.nextToken(); // get first word
else
word1 = null;
if(tokenizer.hasMoreTokens())
word2 = tokenizer.nextToken(); // get second word
else
word2 = null;

// note: we just ignore the rest of the input line.

// Now check whether this word is known. If so, create a command
// with it. If not, create a "null" command (for unknown command).

if(commandswords.isCommand(word1))
return new Command(word1, word2);
else
return new Command(null, word2);
}

public void showCommands()
{
commandswords.showAll();
}
}

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧