面向对象程序设计——城堡游戏

面向对象程序设计——Java语言 浙江大学 翁恺

城堡游戏框架+数据,程序和一点笔记

翁恺老师的慕课课程,java进阶课程中的城堡游戏,源码可以自行下载:城堡游戏源码下载
B站指路:课程
课程后续对源码进行了一系列改造,在这里放上我的程序,没有跟上课程的同学可以参考一下:程序下载(提取🐎:tvhd)
我放的是castle的包,需要同学们自己新建文件夹,把下载的包放到src。我在程序里做了一些笔记,应该是比较好理解的。

我也是新手,如有不足希望可以一起讨论讨论

下面放上我的程序

Game类

package castle;import java.util.HashMap;
import java.util.Scanner;public class Game {private Room current_room;private HashMap<String, Handler> handlers = new HashMap<String,Handler>();// 构造器,game new出来之后自动初始化,并在handlers表里加载用户指令映射// this指这个被new出来的gamepublic Game() {setRoom();// 事实上后面这些new出来的value就是函数,操作都在构造器里是自动调用的// 相当于this.go\this.help,根据key判断调用哪个函数,bye不是构造器,不过使用上差不多handlers.put("go", new HandlerGo(this));handlers.put("help", new HandlerHelp(this));handlers.put("bye", new HandlerBye(this));}// 房间初始化public void setRoom() {//一个房间一张表Room outside = new Room("城堡外");Room lobby = new Room("大堂");;Room study = new Room("书房");Room bedroom = new Room("卧室");Room pub = new Room("小酒吧");// 一个房间有几个出口,他的表里就有几个键值对outside.setExit("east", lobby);outside.setExit("south", study);outside.setExit("west", pub);lobby.setExit("west", outside);study.setExit("north", outside);study.setExit("east", bedroom);bedroom.setExit("west", study);pub.setExit("east", outside);		lobby.setExit("up", pub);pub.setExit("down", lobby);current_room=outside;}// 封装:打印当前房间的位置、打印函数返回的当前房间所具有的出口public void print_info(Room current_room) {System.out.println("当前位置:"+current_room);System.out.print("可走方向有:");System.out.println(current_room.getExitDescription());}// 欢迎界面public void welcome() {System.out.println("欢迎来到迷宫游戏!");System.out.println("需要帮助可输入help。");print_info(current_room);		}// 确定下一房间,把下一房间变成当前房间,并打印信息,这函数在HandlerGo里调用public void goGame(String direction) {Room next_room = current_room.nextRoomDetermine(direction);//如果都不符合,说明没有这个出口if(next_room==null){System.out.println("没有这个出口!");}else{current_room=next_room;print_info(current_room);}}// 根据用户命令操作public void run() {Scanner in=new Scanner(System.in);while (true) {String[] order = in.nextLine().split(" ");// 把命令分割存入字符串数组Handler handler = handlers.get(order[0]);// 根据返回的value的映射关系创建这个handler,里面放了这次的命令和执行方法String value="";if(order.length>1){value = order[1];}if(handler!=null){// value两种情况:空(help\bye)、非空(go direction)// doCmd用于help和go两种情况,根据handler的具体属性调用(第二个属性)handler.doCmd(value);// 如果用order[1],不存在时会报错ArrayIndexOutOfBoundsException// bye子类重写了isBye,所以会返回HandlerBye类里的结果if(handler.isBye())// 另外两个类没有重写这个函数,会返回父类的false{break;}				}}in.close();		}public static void main(String[] args) {Game game = new Game();game.welcome();game.run();System.out.println("感谢您的光临。再见!");}}

Room类

package castle;import java.util.HashMap;public class Room {private String description;// 做了一个映射private HashMap<String, Room> exits=new HashMap<String,Room>();// <方向,这个方向上的房间>// print房间打印出名字public Room(String description) {this.description = description;}	// 用来初始化各房间各出口,也是建立映射表的过程   方位---该方位出去的房间public void setExit(String direction, Room room) {exits.put(direction, room);}// 一个房间一张map,有几个出口,这个map就有几个键值对,返回当前房间所具有的出口的方位public String getExitDescription() {StringBuffer sb = new StringBuffer();for(String dir : exits.keySet()){sb.append(dir);sb.append(" ");}return sb.toString();}// 根据direction这个键,返回对应的值,这个值就是对应的房间public Room nextRoomDetermine(String direction) {return exits.get(direction);}@Overridepublic String toString() {return description;}
}

Handler类

package castle;public class Handler {protected Game game;public Handler(Game game) {this.game = game;}public void doCmd(String direction) {		}public boolean isBye() {return false;}
}

HandlerBye类

package castle;public class HandlerBye extends Handler{public HandlerBye(Game game) {super(game);}@Overridepublic boolean isBye() {return true;}
}

HandlerHelp类

package castle;public class HandlerHelp extends Handler {public HandlerHelp(Game game) {super(game);}@Overridepublic void doCmd(String direction) {System.out.println("你可以选择的功能有:help,bye,go(如 go east)");}
}

HandlerGo类

package castle;public class HandlerGo extends Handler {public HandlerGo(Game game) {super(game);}@Overridepublic void doCmd(String direction) {game.goGame(direction);}}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部