城堡的Handler类
在第5周的城堡程序那里,我们做的Handler是需要在构造的时候注入Game的。那么,在学习了内部类之后,能否用Game的内部类来实现这些Handler呢?应该怎么做?方法:Handler为接口,在Game.java构造函数里用匿名类完成go,bye,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("bye", new Handler() { @Override public void doCmd(String word) { } @Override public boolean isBye() { return true; } }); handlers.put("help", new Handler() { @Override public void doCmd(String word) { System.out.println("迷路了吗?你可以做的命令有:" + cmds); System.out.println("如:tgo east"); } @Override public boolean isBye() { return false; } }); handlers.put("go", new Handler() { @Override public void doCmd(String word) { goRoom(word); } @Override public boolean isBye() { return false; } }); 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("感谢您的光临。再见!"); } }
Handler.java
package castle; public class Handler { public void doCmd(String word) { } public boolean isBye() { return false; } }
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); } }
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算