城堡游戏
我们在尝试了之前的简单媒体库构造之后,试着整合一下之前学到的关于类,继承,多态等知识,制作一个简单的城堡游戏,城堡游戏是一个简单的文字游戏,通过输入命令可以在地图上不同的房间进行移动。
一、城堡游戏介绍:
1.这个程序的任务是通过玩家的输入的方向(纯文字)在虚构的城堡内移动(以纯文字作为移动后的返回结果)。
2.这个程序接受help、bye、go south、go north、go west、go east六种命令,要求命令单独一行输入并在结束时敲回车,另外如果接受go xxx的不合规信息会输出不存在这样的房间。
3.help提供帮助信息,bye结束游戏,go后面空一格加south、north、west、east表示在虚构的城堡中移动。
4.有五个地点,分别是:小酒吧,大厅,书房,卧室,次卧。
5.地图:
二、类的设计
首先我们要有三个类,第一个是Game类,用来表示游戏本身和执行各种游戏命令,第二个是Room类,用来表示游戏中的所有房间。可见整个城堡游戏只由这两个类构成,具体怎么运行就要先构造这两个大类。
1.Game类
Game类是整个游戏运行的核心,具有构造房间,开始游戏,执行命令三个功能。
2.Room类
城堡游戏中是由很多个小房间构成的,分别是小酒吧,大堂,书房,卧室,次卧这五个地点。每个房间只有一种属性,即description(房间名)。
三、具体实现
1.类的创建
首先我们要创造一个Game类,用来作为游戏的主体。我们知道Game类有三个功能,构造房间,开始游戏,执行命令。那么在创建好Game类之后,我们再着手从构造房间这个功能开始实现,所以我们要再创建一个Room类,用来表示游戏中的所有房间。
2.Room类
首先创造好Room的description属性,然后创建构造表。
public String description;
public Room(String description) {
this.description = description;
}
然后我们在Game类中构造createRoom函数,用来创造我们已知的五个房间(酒吧,大厅,书房,卧室,次卧)。
private void createRoom(){
Room pub,lobby,study,bedroom,bedroom1;
}
在创建好房间之后,我们要开始考虑5个房间的连通性,也就是五个房间分别的四个方向的出口通往哪个房间,我们在Room类中创建setExit函数来完成,同时我们需要在Room类中先创建4个对象,分别表示当前房间通往的下一个房间,当setExit函数得到null时,说明该房间的对应方向不存在新的房间。
public String description;
public Room northExit;
public Room southExit;
public Room eastExit;
public Room westExit;
public Room(String description) {
this.description = description;
}
public void setExits(Room north,Room south,Room west,Room east){
if ( north != null )
northExit = north;
if ( south != null )
southExit = south;
if ( west != null )
westExit = west;
if ( east != null )
eastExit = east;
}
3.构造房间
接下来我们在Game类中创造五个房间,并初始化五个房间分别对应的出口。同时创建一个Room对象currentRoom,用来表示初始房间,并将初始房间设置为小酒馆。
private Room currentRoom;
private void createRoom(){
Room pub,lobby,study,bedroom,bedroom1;
//创造房间
pub = new Room("小酒馆");
lobby = new Room("大厅");
study = new Room("书房");
bedroom = new Room("卧室");
bedroom1 = new Room("次卧");
//设置出口
pub.setExits(null,null,null,lobby);
lobby.setExits(null,study,pub,bedroom1);
study.setExits(lobby,null,null,bedroom);
bedroom.setExits(null,null,study,null);
bedroom1.setExits(null,null,lobby,null);
currentRoom = pub;
}
4.游戏设计
构造好房间之后,就可以开始设计游戏的运行逻辑了。游戏分为开始游戏,游戏过程,游戏结束三个部分,我们来一个一个完成。
①开始游戏
首先我们需要在开始游戏的时候介绍一下城堡游戏,然后输出初始化的地点,再执行游戏的具体操作。
public void printWelcome(){
System.out.println("***********************************");
System.out.println("欢迎来到城堡游戏!");
System.out.println("这是一个超级无聊的小游戏。");
System.out.println("如果需要帮助,请输入help。");
System.out.println("那么我们开始游戏吧!");
System.out.println("***********************************");
System.out.println("现在你在"+currentRoom);
}
这里要注意到的是,我们直接输出currentRoom对象的话只能得到null结果,要想得到currentRoom中的description字符串的话,必须要使用toString方法,在Room类设置一个toString函数,来返回description值。
public String toString()
{
return description;
}
可以看到我们在开始游戏的介绍中加入了一个help可以得到游戏帮助的小命令,用来提示玩家可以进行怎样的操作,那么我们现在开始设计这个help。
private void printHelp()
{
System.out.println("迷路了吗?你可以做的命令有:go | bye | help");
System.out.println("如:\tgo east");
}
②游戏过程
private void goRoom(String direction) {
Room nextRoom = null;
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("那里没有门!");
}
}
构造goRoom函数,用来表示当我们收到一个前往下个方向的命令时,创造一个nextRoom对象,将相对应方向的房间的对象交给它,以此来实现从一个房间到下一个对应房间的移动。同时我们考虑到当玩家不按常理出牌,如果某个当前房间并没有对应方向的房间的时候,玩家输入这个方向,我们要输出“那里没有门!”,然后将其他方向的房间说明给玩家。
else{
currentRoom = nextRoom;
System.out.println("你在"+ currentRoom);
System.out.println("出口有:");
if(currentRoom.northExit != null)
System.out.println("north");
if(currentRoom.eastExit != null)
System.out.println("east");
if(currentRoom.southExit != null)
System.out.println("south");
if(currentRoom.westExit != null)
System.out.println("west");
System.out.println();
}
然后我们在Game类的main函数中设计get命令的while循环,设置一个死循环,不停地获取命令,知道得到bye命令时结束循环,并结束游戏。
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("***********************************");
System.out.println("游戏到此结束!");
System.out.println("欢迎下次光临!");
System.out.println("谢谢!");
System.out.println("***********************************");
四、代码测试
五、详细代码
1.Game:
package castlegame;
import java.util.Scanner;
public class Game {
private Room currentRoom;
public Game()
{
createRooms();
}
private void createRooms(){
Room pub,lobby,study,bedroom,bedroom1;
//创造房间
pub = new Room("小酒馆");
lobby = new Room("大厅");
study = new Room("书房");
bedroom = new Room("卧室");
bedroom1 = new Room("次卧");
//设置出口
pub.setExits(null,null,null,lobby);
lobby.setExits(null,study,pub,bedroom1);
study.setExits(lobby,null,null,bedroom);
bedroom.setExits(null,null,study,null);
bedroom1.setExits(null,null,lobby,null);
currentRoom = pub;
}
public void printWelcome(){
System.out.println("***********************************");
System.out.println("欢迎来到城堡游戏!");
System.out.println("这是一个超级无聊的小游戏。");
System.out.println("如果需要帮助,请输入help。");
System.out.println("那么我们开始游戏吧!");
System.out.println("***********************************");
System.out.println("现在你在"+currentRoom);
}
private void printHelp()
{
System.out.println("迷路了吗?你可以做的命令有:go | bye | help");
System.out.println("如:\tgo east");
}
private void goRoom(String direction) {
Room nextRoom = null;
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);
System.out.println("出口有:");
if(currentRoom.northExit != null)
System.out.println("north");
if(currentRoom.eastExit != null)
System.out.println("east");
if(currentRoom.southExit != null)
System.out.println("south");
if(currentRoom.westExit != null)
System.out.println("west");
}
}
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("***********************************");
System.out.println("游戏到此结束!");
System.out.println("欢迎下次光临!");
System.out.println("谢谢!");
System.out.println("***********************************");
}
}
2.Room:
package castlegame;
public class Room {
public String description;
public Room northExit;
public Room southExit;
public Room westExit;
public Room eastExit;
public Room(String description) {
this.description = description;
}
public void setExits(Room north,Room south,Room west,Room east){
if ( north != null )
northExit = north;
if ( south != null )
southExit = south;
if ( west != null )
westExit = west;
if ( east != null )
eastExit = east;
}
public String toString()
{
return description;
}
}