翁恺老师java面向对象 城堡游戏

翁恺老师java面向对象 城堡游戏

import java.util.HashMap;
import java.util.Scanner;

public class Game {
	private Room currentRoom;
	private HashMap<String, Handle> sh = new HashMap<String, Handle>();
	Game() {
		sh.put("help", new HandleHelp(this) );
		sh.put("bye", new HandleBye(this) );
		sh.put("go", new HandleGo(this) );
		setRoom();
	}
	public void setRoom() {
		Room outside = new Room("城堡外");
		Room lobby = new Room("大堂");
		Room pub = new Room("小酒馆");
		Room study = new Room("书房");
		Room bedRoom = new Room("卧室");
		Room bathRoom = new Room("卫生间");
		outside.setExit("east", lobby);
		outside.setExit("west", pub);
		outside.setExit("south", study);
		pub.setExit("east", outside);
		lobby.setExit("west", outside);
		study.setExit("north", outside);
		study.setExit("east", bedRoom);
		bedRoom.setExit("west", study);
		bedRoom.setExit("up", bathRoom);
		bathRoom.setExit("down", bedRoom);
		currentRoom = outside;
	}
	void Welcome() {
		System.out.println("欢迎来到城堡游戏");
		System.out.println("你可以输入help来查看相关玩法");
		System.out.println("那么游戏开始啦");
		getExitSoc();
	}
	void getExitSoc() {
		System.out.println("你当前的位置在"+currentRoom);
		System.out.print("出口有:");
		System.out.println(currentRoom.getExit());
	}
	void goRoom( String loc) {
		Room nextRoom = currentRoom.getRoom(loc);
		if( nextRoom != null ) {
			currentRoom = nextRoom;
			getExitSoc();
		}
		else System.out.println("那里没有门");
	}
	void play() {
		Scanner in = new Scanner(System.in);
		while( true ) {
			String l = in.nextLine();
			String []words = l.split(" ");
			Handle handle = sh.get(words[0]);
			String value = "";
			if( words.length > 1 ) {
				value = words[1];
			}
			if( handle != null ) {
				handle.doCmd(value);
				if( handle.isBye() ) {
					break;
				}
			}
		}
	}
	public static void main(String []args) {
		Game game = new Game();
		game.Welcome();
		game.play();
	}
}
import java.util.HashMap;

public class Room {
	private String location;
	HashMap<String, Room> hm = new HashMap<String, Room>();
	Room(String location) {
		this.location = location;
	}
	void setExit( String dir, Room room) {
		hm.put(dir, room);
	}
	String getExit() {
		StringBuffer sb = new StringBuffer();
		sb.append(hm.keySet());
		return sb.toString();
	}
	Room getRoom( String dir ) {
		return hm.get(dir);
	}
	@Override
	public String toString() {
		return location;
	}
	
}

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

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

	@Override
	void doCmd(String word) {
		System.out.println("迷路了吗,你可以输入 go bye ");
		System.out.println("例如 go east");
	}

	

}
public class HandleBye extends Handle {
	
	public HandleBye(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
	}

	@Override
	boolean isBye() {
		return true;
	}

	@Override
	void doCmd(String word) {
		System.out.println("欢迎您的光临,再见。");
	}
	
}
public class HandleGo extends Handle {
	
	public HandleGo(Game game) {
		super(game);
		// TODO Auto-generated constructor stub
	}

	@Override
	void doCmd(String word) {
		game.goRoom(word);
	}

}
  • 5
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值