实现了隐形耦合,help输出内容与help的输出函数无关,使得新的命令加入的时候,help能自动输出新的内容。
添加了新元素:添加了简单的故事场景。
城堡游戏演示:
代码如下:
Game.java
城堡游戏的主函数
package castle;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;
public class Game {
private Room currentRoom;
private HashMap<String, Handler> handlers = new HashMap<String, Handler>();
protected ArrayList<String> cmds = new ArrayList<String>();
public Game()
{
// handlers.put("go", new HandlerGo());
handlers.put("bye", new HandlerBye(this));
handlers.put("help", new HandlerHelp(this));
handlers.put("go", new HandlerGo(this));
for (String cmd : handlers.keySet()) {
cmds.add(cmd);
}
createRooms();
}
private void createRooms()
{
Room outside, lobby, pub, study, bedroom, inside, bedroomup;
// 制造房间
outside = new Room("城堡外");
inside = new Room("城堡内");
lobby = new Room("大堂");
pub = new Room("小酒吧");
study = new Room("书房");
bedroom = new Room("卧室");
bedroomup = new Room("卧室二楼");
// hoteldown = new Room("宾馆一楼");
// hotelup = new Room("宾馆二楼");
// 设置房间故事
outside.setNarration("城堡外","恭喜你逃出了城堡");
inside.setNarration("城堡内", "你现在在城堡的一个恐怖黑暗的广场上");
lobby.setNarration("大堂", "你来到了城堡的大堂,大堂里面有着恐怖的石像");
pub.setNarration("小酒吧", "这是一间幽深的酒吧,傍边到处是黑色的酒瓶,还有这恐怖的音乐在播放着");
study.setNarration("书房", "这是城堡的书房,里面记载着这个城堡的过去");
bedroom.setNarration("卧室", "你来到了城堡主人的卧室,这里是最黑暗的地方,你的心跳跳的越来越快,快点离开这里吧!");
bedroomup.setNarration("卧室二楼", "恭喜你来到了城堡主人的卧室二楼,并且发现了一个箱子,你打开箱子后找到了钥匙,请拿着钥匙快速逃出城堡吧!");
// 初始化房间的出口
outside.setExit("south", inside);
inside.setExit("north", outside);
inside.setExit("east", lobby);
inside.setExit("south", study);
inside.setExit("west", pub);
lobby.setExit("west", inside);
pub.setExit("east", inside);
study.setExit("north", inside);
study.setExit("east", bedroom);
bedroom.setExit("west", study);
bedroom.setExit("up", bedroomup);
bedroomup.setExit("down", bedroom);
currentRoom = inside; // 从城堡门外开始
}
private 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() {
String currentnarration = currentRoom.getNarration(currentRoom.toString());
System.out.println("你在" + currentRoom + "\n场景描述:" + currentnarration);
System.out.print("出口有: ");
System.out.print(currentRoom.getExitDesc());
System.out.println();
}
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;
}
}
}
in.close();
}
public static void main(String[] args) {
Game game = new Game();
game.printWelcome();
game.play();
System.out.println("感谢您的光临。再见!");
}
}
Room.java
城堡游戏中的房间模块
package castle;
import java.util.HashMap;
public class Room {
private String description;
private HashMap<String, Room> exits = new HashMap<String, Room>();
private HashMap<String, String> narration = new HashMap<String, String>();
public Room(String description)
{
this.description = description;
}
public void setExit(String dir, Room room) {
exits.put(dir, room);
}
public void setNarration(String room, String narr) {
narration.put(room, narr);
}
@Override
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);
}
public String getNarration(String direction) {
return narration.get(direction);
}
}
Handler.java
城堡游戏中的添加各种功能的父类模块
package castle;
public class Handler {
protected Game game;
public Handler(Game game) {
this.game = game;
}
public void doCmd(String word) {}
public boolean isBye() {return false;}
}
HandlerBye.java
功能模块的子类实现结束功能
package castle;
public class HandlerBye extends Handler{
public HandlerBye(Game game) {
super(game);
}
@Override
public boolean isBye() {
return true;
}
}
HandlerHelp.java
功能模块的子类实现帮助功能
package castle;
public class HandlerHelp extends Handler {
public HandlerHelp(Game game) {
super(game);
}
@Override
public void doCmd(String word) {
System.out.println("迷路了吗?你可以做的命令有:" + game.cmds);
System.out.println("如:\tgo east");
}
}
HandlerGo.java
功能模块的子类实现go功能
package castle;
public class HandlerGo extends Handler {
public HandlerGo(Game game) {
super(game);
}
@Override
public void doCmd(String word) {
game.goRoom(word);
}
}
学习心得:
-
消除代码复制
- 提取出来,改为函数。
-
增加可扩展性
- 可以运行的代码 != 良好的代码
- 对代码做维护的时候最能看出代码的质量
-
用封装来降低耦合
-
Sring每次都String做+时,都会产生新的对象,系统开销较大。
-
StringBuffer是可以不断修改的对象。
- StringBuffer sb = new StringBuffer();
- sb.append(“haha”);