一、城堡游戏介绍:
1.这个程序的任务是通过玩家的输入的方向(纯文字)在虚构的城堡内移动(以纯文字作为移动后的返回结果)。
2.这个程序接受help、bye、go south、go north、go west、go east六种命令,要求命令单独一行输入并在结束时敲回车,另外如果接受go xxx的不合规信息会输出不存在这样的房间。
3.help提供帮助信息,bye结束游戏,go后面空一格加south、north、west、east表示在虚构的城堡中移动。
4.有五个地点,分别是:小酒吧,大厅,书房,卧室,次卧。
5.地图:
二:实现代码
package com.company;
import java.util.Scanner;
/*
城堡游戏介绍:
1.这个程序的任务是通过玩家的输入的方向(纯文字)在虚构的城堡内移动(以纯文字作为移动后的返回结果)。
2.这个程序接受help、bye、go south、go north、go west、go east六种命令,要求命令单独一行输入并在结束时敲回车,
另外如果接受go xxx的不合规信息会输出不存在这样的房间。
3.help提供帮助信息,bye结束游戏,go后面空一格加south、north、west、east表示在虚构的城堡中移动。
4.有五个地点,分别是:小酒吧,大厅,书房,卧室,次卧。
5.地图:...
*/
//定义一个Room类,表示每个房间的情况
class Room{
public String room_name;
public Room eastExist;
public Room southExist;
public Room westExist;
public Room northExist;
public Room(String room_name){
this.room_name=room_name;
}
public void setExist(Room east,Room south,Room west,Room north){
if(east!=null){
eastExist=east;
}
if(south!=null){
southExist=south;
}
if(west!=null){
westExist=west;
}
if(north!=null){
northExist=north;
}
}
public String toString(){
return room_name;
}
}
public class Game {
private static Room current_room;
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
Room pub=new Room("小酒馆");
Room hall=new Room("大厅");
Room study=new Room("书房");
Room bedroom=new Room("卧室");
Room bedroom1=new Room("次卧");
current_room=pub;
//设置各个房间之间连通的门
pub.setExist(hall,null,null,null);
hall.setExist(bedroom1,study,pub,null);
study.setExist(bedroom,null,null,hall);
bedroom.setExist(null,null,study,null);
bedroom1.setExist(null,null,hall,null);
System.out.println("欢迎来到城堡游戏!");
System.out.println("请输入您选择的前进方向(east,south,west,north):");
System.out.println("输入格式例如:go east");
System.out.println("如果需要帮助,请输入:help ,如果需要结束游戏,请输入:bye");
Game game=new Game();
System.out.println("您当前所在的房间为:"+current_room);
while(true) {
String str = in.nextLine();
String[] arr = str.split(" ");
if(arr[0].equals("help")) {
game.help();
} else if(arr[0].equals("bye")) {
break;
}
else if(arr[0].equals("go")){
game.go_ahead(arr[1]);
}
else{
System.out.println("不存在这样的房间!");
}
}
}
public void help(){
System.out.println("迷路了吗?你可以做的命令有:go | bye | help");
System.out.println("如:go east");
}
public void go_ahead(String direction){
if(direction.equals("east")){
if(current_room.eastExist==null){
System.out.println("输入的方向没有房间");
}
else {
current_room = current_room.eastExist;
System.out.println("你当前所在的房间为:"+current_room);
}
}
else if(direction.equals("south")){
if(current_room.southExist==null){
System.out.println("输入的方向没有房间");
}
else{
current_room=current_room.southExist;
System.out.println("你当前所在的房间为:"+current_room);
}
}
else if(direction.equals("west")){
if(current_room.westExist==null){
System.out.println("输入的方向没有房间");
}
else {
current_room = current_room.westExist;
System.out.println("你当前所在的房间为:"+current_room);
}
}
else if(direction.equals("north")){
if(current_room.northExist==null){
System.out.println("输入的方向没有房间");
}
else {
current_room = current_room.northExist;
System.out.println("你当前所在的房间为:"+current_room);
}
}
else {
System.out.println("不存在这样的房间");
return;
}
System.out.print("和当前房间连通的房间的方向有:");
if(current_room.eastExist!=null){
System.out.print("east ");
}
if(current_room.southExist!=null){
System.out.print("south ");
}
if(current_room.westExist!=null){
System.out.print("west");
}
if(current_room.northExist!=null){
System.out.print("north");
}
System.out.println();
}
}
以下文章是对本篇文章中代码的优化:
Java之城堡游戏的优化