前言:城堡游戏是mooc浙江大学面向对象编程第五周的代码,要在翁恺老师的带领下修改这些代码,使代码具有可扩展性。所以在改造代码值钱,必须要深入理解代码,才能看懂后面的改造
先上城堡的地图:
要先根据代码画出成败地图,才能测试改造后的代码是否正确。、
理解代码要先从程序的入口main函数开始,一条一条的写上注释:
Game类
import java.util.Scanner;
//Game类
public class Game {
private Room currentRoom;//定义了一个Room的对象currentRoom作为Game的成员函数
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("卧室");
//初始化房间的出口,创建城堡地图
outside.setExits(null, lobby, study, pub);
lobby.setExits(null, null, null, outside);
pub.setExits(null, outside, null, null);
study.setExits(outside, bedroom, null, null);
bedroom.setExits(null, null, null, study);
currentRoom = outside; // 从城堡门外开始
}
private void printWelcome() {//欢迎函数
//欢迎部分
System.out.println();
System.out.println("欢迎来到城堡!");
System.out.println("这是一个超级无聊的游戏。");
System.out.println("如果需要帮助,请输入 'help' 。");
System.out.println();
System.out.println("现在你在" + currentRoom);
System.out.print("出口有:");
if(currentRoom.northExit != null)//城堡北方不为空,输出north+空格
System.out.print("north ");
if(currentRoom.eastExit != null)//城堡东方不为空,输出east+空格
System.out.print("east ");
if(currentRoom.southExit != null)//城堡南方不为空,输出south+空格
System.out.print("south ");
if(currentRoom.westExit != null)//城堡西方不为空,输出west+空格
System.out.print("west ");
System.out.println();
}
// 以下为用户命令
private void printHelp()
{
System.out.print("迷路了吗?你可以做的命令有:go bye help");
System.out.println("如:\tgo east");
}
private void goRoom(String direction)
{
Room nextRoom = null;//定义一个新的Room变量储存变化之后的位置
if(direction.equals("north")) {
nextRoom = currentRoom.northExit;//向北走
}
if(direction.equals("east")) {
nextRoom = currentRoom.eastExit;//向东走
}
if(direction.equals("south")) {
nextRoom = currentRoom.southExit;//向南走
}
if(direction.equals("west")) {
nextRoom = currentRoom.westExit;//向西走
}
if (nextRoom == null) {
System.out.println("那里没有门!");//没有房间
}
else {
currentRoom = nextRoom;//有房间,当前位置发生更爱
System.out.println("你在" + currentRoom);//toString函数,直接输出特定返回值
System.out.print("出口有: ");
//计算出口
if(currentRoom.northExit != null)
System.out.print("north ");
if(currentRoom.eastExit != null)
System.out.print("east ");
if(currentRoom.southExit != null)
System.out.print("south ");
if(currentRoom.westExit != null)
System.out.print("west ");
System.out.println();
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Game game = new Game();//创建一个Game的对象
game.printWelcome();//调用Game 的printWelcome 函数
while ( true )
{
String line = in.nextLine();//定义字符串接收命令行
String[] words = line.split(" ");//以空格为间隔返回数组
//split方法,以括号的内容为间隔返回一个数组
//特殊字符需要加斜杠作为转义字符(如* ^ . #)
//在引号内要写成这样,\\* \\^ \\. \\#
/*案例:
* String[] aa = "aaafbbbfccc".split("f");
* for (int i = 0 ; i <aa.length ; i++ )
* {
* System.out.println("--"+aa[i]);
* }
* 输出结果:
* --aaa
* --bbb
* --ccc
*/
if ( words[0].equals("help") ) {
game.printHelp();//第一个字符是help调用help函数
}
else if (words[0].equals("go") ) {
game.goRoom(words[1]);//第一个字符是go调用goRoom函数
}
else if ( words[0].equals("bye") ) {
break;//第一个字符是bye,离开循环
}
}
System.out.println("感谢您的光临。再见!");
in.close();
}
}
Room类
//Room类
public class Room {
//成员变量
public String description;//描述,初始化对象时候的位置
//定义四个Room类型的成员变量用来储存方位
public Room northExit;
public Room southExit;
public Room eastExit;
public Room westExit;
//构造函数,初始化对象的时候,声明位置
public Room(String description)
{
this.description = description;
}
//成员函数,传入四个Room变量
public void setExits(Room north, Room east, Room south, Room west)
{
if(north != null)
northExit = north;
if(east != null)
eastExit = east;
if(south != null)
southExit = south;
if(west != null)
westExit = west;
}
@Override
public String toString()
{
return description;
}
}
split函数是之前没有遇到的函数,要先去查一下函数的作用:
public class MainTest {
public static void main(String[] args) {
// TODO Auto-generated method stub
// String[] aa = "aaa|bbb|ccc".split("|");
String[] aa = "aaafbbbfccc".split("\\|"); //这样才能得到正确的结果
for (int i = 0 ; i <aa.length ; i++ ) {
System.out.println("--"+aa[i]);
}
}
}
然后根据函数的程序画出关系图:
关系图可以帮你理清的了解两个类之间的关系,一定要画一下。
从关系图中我们可以看到两个类之间的耦合程度非常高。
弄明白两个类之间的关系之后就可以跟着老师继续改造这下代码了。