castle-城堡游戏

高玮
2023-12-01
package castle_test;

import java.util.*;

public class Room {
	private String description;
	private HashMap<String,Room> exits = new HashMap<String,Room>();
	public Room(String description) {
		this.description = description;
	}
	public void setExit(String dir,Room room) {
		exits.put(dir, room);
	}
	
	public String toString() {
		return description;
	}
	
	public String getExitDesc() {
		StringBuffer sb = new StringBuffer();
		for(String dir:exits.keySet()) {
			sb.append(dir);
			sb.append(" ");
		}
		return sb.toString();
	}
	
	public Room getExit(String direction) {
		return exits.get(direction);
	}
}

Room类

下面是Game类:

package castle_test;

import java.util.*;

public class Game {
	private Room currentRoom;
	public Game() {
		creatRooms();
	}
	public void creatRooms() {
		Room outside,lobby,pub,study,bedroom;
		outside = new Room("城堡外");
		lobby = new Room("大堂");
		pub = new Room("小酒吧");
		study = new Room("书房");
		bedroom = new Room("卧室");
		outside.setExit("east", lobby);
		outside.setExit("south", study);
		outside.setExit("west", pub);
		lobby.setExit("west", outside);
		pub.setExit("east",outside);
		study.setExit("north",outside);
		study.setExit("east",bedroom);
		bedroom.setExit("west", study);
		lobby.setExit("up", pub);
		pub.setExit("down",lobby);
		currentRoom = outside;
	}
	public void printWelcome() {
		System.out.println();
		System.out.println("欢迎来到城堡!");
		System.out.println("这是一个超级无聊的游戏。");
		System.out.println("如果需要帮助,请输入‘help’。");
		System.out.println();
		showPrompt();
	}
	private void printHelp() {
		System.out.print("迷路了吗?你可以做的命令有:go bye help");
		System.out.println("如:\tgo east");
	}
	
	private void goRoom(String direction) {
		Room nextRoom=  currentRoom.getExit(direction);
		
		if(nextRoom==null) {
			System.out.println("那里没有门!");
		}
		else {
			currentRoom= nextRoom;
	        showPrompt();
		}
	}
	public void showPrompt() {
		System.out.println("你在"+currentRoom);
		System.out.println("出口有:");
		System.out.println(currentRoom.getExitDesc());
		System.out.println();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Scanner in = new Scanner(System.in);
		Game game = new Game();
		game.printWelcome();
		while(true) {
			String line = in.nextLine();
			String[] words = line.split(" ");
			if(words[0].equals("help")) {
				game.printHelp();
			}
			else if(words[0].equals("go")) {
				game.goRoom(words[1]);
			}
			else if(words[0].equals("bye")) {
				break;
			}
			
		}
		System.out.println("感谢您的光临。再见!");
		in.close();
	}

}

欢迎来到城堡!
这是一个超级无聊的游戏。
如果需要帮助,请输入‘help’。

你在城堡外
出口有:
east south west 

help
迷路了吗?你可以做的命令有:go bye help如:	go east
go east
你在大堂
出口有:
west up 

go up
你在小酒吧
出口有:
east down 

go down
你在大堂
出口有:
west up 

bye
感谢您的光临。再见!

程序的封装、可扩展性。

修改其可扩展性,可以扩展生成其他指令:

package castle_test;

import java.util.*;

public class Room {
	private String description;
	private HashMap<String,Room> exits = new HashMap<String,Room>();
	public Room(String description) {
		this.description = description;
	}
	public void setExit(String dir,Room room) {
		exits.put(dir, room);
	}
	
	public String toString() {
		return description;
	}
	
	public String getExitDesc() {
		StringBuffer sb = new StringBuffer();
		for(String dir:exits.keySet()) {
			sb.append(dir);
			sb.append(" ");
		}
		return sb.toString();
	}
	
	public Room getExit(String direction) {
		return exits.get(direction);
	}
}


package castle_test;

public class Handler {
	protected Game game;
	
	public Handler(Game game) {
		this.game = game;
	}
	public void doCmd(String word) {
		
	}
	
	public boolean isBye() {
		return false;
	}
}


package castle_test;

public class HandlerHelp extends Handler{
	
	public HandlerHelp(Game game) {
		super(game);
	}
	@Override
	public void doCmd(String word) {
		System.out.print("迷路了吗?你可以做的命令有:go bye help");
		System.out.println("如:\tgo east");
	}

}


package castle_test;

public class HandlerGo extends Handler{
	public HandlerGo(Game game) {
		super(game);
	}
	@Override
	public void doCmd(String word) {
		// TODO Auto-generated method stub
		game.goRoom(word);
	}

}


package castle_test;

public class HandlerBye extends Handler{

	public HandlerBye(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
	}

	@Override
	public boolean isBye() {
		return true;
	}

}


package castle_test;

import java.util.*;

public class Game {
	private Room currentRoom;
	private HashMap<String, Handler> handlers = new HashMap<String, Handler>();
	public Game() {
		handlers.put("go",new HandlerGo(this));
		handlers.put("bye", new HandlerBye(this));
		handlers.put("help", new HandlerHelp(this));
		creatRooms();
	}
	public void creatRooms() {
		Room outside,lobby,pub,study,bedroom;
		outside = new Room("城堡外");
		lobby = new Room("大堂");
		pub = new Room("小酒吧");
		study = new Room("书房");
		bedroom = new Room("卧室");
		outside.setExit("east", lobby);
		outside.setExit("south", study);
		outside.setExit("west", pub);
		lobby.setExit("west", outside);
		pub.setExit("east",outside);
		study.setExit("north",outside);
		study.setExit("east",bedroom);
		bedroom.setExit("west", study);
		lobby.setExit("up", pub);
		pub.setExit("down",lobby);
		currentRoom = outside;
	}
	public void play() {
		Scanner in = new Scanner(System.in);
		while(true) {
			String line = in.nextLine();
			String[] words = line.split(" ");
			
			Handler handler = handlers.get(words[0]);
			String value = "";
			if(words.length>1) {
				value= words[1];
			}
			if(handler != null) {
				handler.doCmd(value);
				if(handler.isBye())
					break;
			}		
		}

	}
	public void printWelcome() {
		System.out.println();
		System.out.println("欢迎来到城堡!");
		System.out.println("这是一个超级无聊的游戏。");
		System.out.println("如果需要帮助,请输入‘help’。");
		System.out.println();
		showPrompt();
	}
	
	public void goRoom(String direction) {
		Room nextRoom=  currentRoom.getExit(direction);
		
		if(nextRoom==null) {
			System.out.println("那里没有门!");
		}
		else {
			currentRoom= nextRoom;
	        showPrompt();
		}
	}
	public void showPrompt() {
		System.out.println("你在"+currentRoom);
		System.out.println("出口有:");
		System.out.println(currentRoom.getExitDesc());
		System.out.println();
	}
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		Game game = new Game();
		game.printWelcome();
		game.play();
		System.out.println("感谢您的光临。再见!");
	}

}

运行结果:

欢迎来到城堡!
这是一个超级无聊的游戏。
如果需要帮助,请输入‘help’。

你在城堡外
出口有:
east south west 

help
迷路了吗?你可以做的命令有:go bye help如:	go east
go east
你在大堂
出口有:
west up 

go up
你在小酒吧
出口有:
east down 

go east
你在城堡外
出口有:
east south west 

go south
你在书房
出口有:
east north 

bye
感谢您的光临。再见!



 类似资料: