- 这周做了什么
继续学习JAVA,做出了城堡游戏
点击查看代码
//Room
package castleV3;
import java.util.HashMap;
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);
}
@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);
}
}
//Handlerhelp
package castleV3;
public class HandlerHelp extends Handler {
public HandlerHelp(Game game ) {
super(game);
}
@Override
public void docmd(String word) {
System.out.println("迷路了吗? 你可以做的命令有 :go bye help");
System.out.println("如 \t go east");
}
}
//Handlergo
package castleV3;
public class HandlerGo extends Handler {
public HandlerGo(Game game ) {
super(game);
}
@Override
public void docmd(String word) {
game.goRoom(word);
}
}
//Handlerbye
package castleV3;
public class HandlerBye extends Handler {
public HandlerBye(Game game ) {
super(game);
}
@Override
public boolean isBye() {
return true;
}
}
//Handler
package castleV3;
public class Handler {
protected Game game;
public Handler(Game game ) {
this.game=game;
}
public void docmd(String word) {
}
public boolean isBye() {
return false;
}
}
//Game
package castleV3;
//V2 基础上 增加框架+数据来提高拓展性
import java.util.HashMap;
import java.util.Scanner;
public class Game {
private Room currentRoom;
private HashMap<String,Handler>handlers= new HashMap<String,Handler>();
public Game()
{
handlers.put("bye", new HandlerBye(this));
handlers.put("help", new HandlerHelp(this));
handlers.put("go", new HandlerGo(this));
createRooms();
}
private void createRooms()
{
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("easst",bedroom);
bedroom.setExit("west",study);
// 增加 拓展
lobby.setExit("up", pub);
pub.setExit("down", lobby);
currentRoom = outside; // 从城堡门外开始
}
private void printWelcome() {
System.out.println();
System.out.println("欢迎来到城堡!");
System.out.println("这是一个超级无聊的游戏。");
System.out.println("如果需要帮助,请输入 'help' 。");
System.out.println();
showPrompt();
}
public void showPrompt() {
System.out.println("现在你在" + currentRoom);
System.out.print("出口有:");
System.out.println(currentRoom.getExitDesc());
}
// 以下为用户命令
public void goRoom(String direction)
{
Room nextRoom = currentRoom.getExit(direction);
if (nextRoom == null) {
System.out.println("那里没有门!");
}
else {
currentRoom = nextRoom;
showPrompt();
}
}
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("感谢您的光临。再见!");
}
}
运行结果:
/
/
/
- 下周打算做什么
1.深入学习JAVA
2.看完《大道至简》
/
/
/
- 有哪些问题
感觉自己的沟通能力需要提高,目前进行暑假授课,希望表达能力能得到提升。