Java设计原则3—城堡游戏之可扩展性

什么叫可扩展性?
可扩展性的意思就是代码的某些部分不需要经过修改就能适应将来可能的变化。
比如:现在要给城堡游戏中添加up和down两个方向,该怎么做呢?

这就要基于设计的城堡游戏中,在Java设计原则2—城堡游戏之封装中,把Room内部成员变量设为私有,然后提供了两个接口(实现了内部私有成员的操作)以便外部访问;然后需要考虑在Room类内部应该如何表达出口,如何出口和从出口出去之后所对应的房间之间的关系,这个在Room内部,Room自己说了算,所以可以用容器来实现灵活性,即:用容器来表达,方向和这个方向所对应的房子的关系。

用容器实现灵活性:目前城堡游戏中的问题:Room的方向是用成员变量表示的,增加或减少方向就要改变代码。策略:如果用Hash表示方向,那么方向就不是“硬编码”的了。

看下面的代码:

public class Room {
    private String description;
  private HashMap<String, Room> exits = new HashMap<String, Room>(); //用HashMap表示方向

    public Room(String description) 
    {
        this.description = description;
    }

    public void setExits(String dir, Room room) {
        exits.put(dir, room);//调用put方法填写元素
    }

    @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 goExit(String direction) {   
        return exits.get(direction);
    }
}
public class Game {
    private Room currentRoom;

    public Game() 
    {
        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("卧室");

        //  初始化房间的出口(ps:可以方便的加入或删除任何方向)
        outside.setExits("east", lobby);
        outside.setExits("south", study);
        outside.setExits("west", pub);
        lobby.setExits("west", outside);
        pub.setExits("east", outside);
        study.setExits("north", outside);
        study.setExits("east", bedroom);
        bedroom.setExits("west", study);
        lobby.setExits("up", pub);
        pub.setExits("down", lobby);

        currentRoom = outside;  //  从城堡门外开始
    }

    private 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.goExit(direction);
        if (nextRoom == null) {
            System.out.println("那里没有门!");
        }
        else {
            currentRoom = nextRoom;
            showPrompt();
        }
    }

    public void showPrompt() {
         System.out.println("你在" + currentRoom);
         System.out.print("出口有: ");
         System.out.println(currentRoom.getExitDesc());
         System.out.println();
    }

    public static void main(String[] args) {
        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();
    }
}
  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值