我正在研究一个包含4个房间的文字游戏。房间的布局就像一个盒子,分为四个部分(西北,东北,东南,西南)。您可以从NW房间开始走房间,然后顺时针方向走到SW房间。
无论如何,我在开关中包含一些基本动作:
switch (input) {
case "help":
$("#messageHelp").properDisplay();
break;
case "take sword":
if (currentRoom == "nwRoom") {
$("<p>You picked up a sword.</p>").properDisplay();
} else {
$("<p>The sword is not here.</p>").properDisplay();
}
break;
case "go east":
if (currentRoom == "nwRoom") {
currentRoom = "neRoom";
$("<p>You are now in the North East Room.</p>").properDisplay();
} else {
$("<p>You can't go that way.</p>").properDisplay();
}
break;
case "go west":
if (currentRoom == "neRoom") {
currentRoom = "nwRoom";
$("<p>You are now in the North West Room.</p>").properDisplay();
} else {
$("<p>You can't go that way.</p>").properDisplay();
}
break;
case "go south":
if (currentRoom == "neRoom") {
currentRoom = "seRoom";
$("<p>You are now in the South East Room.</p>").properDisplay();
} else {
$("<p>You can't go that way.</p>").properDisplay();
}
break;
case "go north":
if (currentRoom == "seRoom") {
currentRoom = "neRoom";
$("<p>You are now in the North East Room.</p>").properDisplay();
} else {
$("<p>You can't go that way.</p>").properDisplay();
}
break;
这是我的问题-WEST的方向需要重复,因为您应该能够在上层房间和下层房间中向东/向西行驶。我怎么做?我最初想到的是OR布尔值:
if (currentRoom == "swRoom" || "nwRoom")
但是,剩下的代码将毫无意义。由于第一次使用该语句,我无法复制整个语句。如果可能的话,我想坚持对大多数代码使用SWITCH,因为这比大量的if else语句干净得多。
最终,我想继续使用它来将其发展成更大的游戏,但是我正在使用这四个房间来教自己一些基础知识。
谢谢!
最佳答案
基于Keith Nicholas的想法,您可以将房间表示为单个对象,如下所示:
var rooms = {
northWest: {
name: "North East",
hasSword: true,
paths: {
east: "northEast",
south: "southWest"
}
},
northEast: {
name: "North East",
paths: {
west: "northWest",
south: "southEast"
}
},
southWest: {
name: "South West",
paths: {
east: "southEast",
north: "northWest"
}
},
southEast: {
name: "South East",
paths: {
west: "southWest",
north: "northEast"
}
},
};
在游戏开始时设置您的
currentRoom
var currentRoom = rooms["northWest"];
由于在任一方向上移动之间的功能都相对相似,因此您希望重用尽可能多的代码。您可以创建一个旅行功能,如下所示:
var travel = function(direction) {
var newRoom = rooms[currentRoom.paths[direction]];
if(!newRoom) {
$("<p>You can't go that way.</p>").properDisplay();
}
else {
currentRoom = newRoom;
$("<p>You are now in the " + currentRoom.name + " Room.</p>").properDisplay();
}
};
此函数将采用字符串方向,例如“ east”或“ west”。第一个变量赋值将尝试从上面创建的对象获取空间。举例来说,假设我们在西北房间,方向输入是东。这条线将有效地做到这一点
currentRoom.paths["east"]
由于我们在西北房间,因此将返回该字符串:
northWest: {
name: "North East",
paths: {
east: "northEast", // This string will be returned.
south: "southWest"
}
}
有了这个字符串,我们现在可以使用以下命令获取要移到的新房间:
rooms["northEast"]
所以放在一起是
rooms[currentRoom.paths[direction]];
现在,如果碰巧不返回任何内容,即。在currentRoom中不存在带有“东”方向的路径,我们的变量将为
undefined
,其值将为false
。这对于下一步很重要,因为它使我们能够检查是否能够新建一个房间。
false
评估意味着我们不是,这就是我们告诉玩家的信息。如果返回房间,我们将其设置为当前房间。显示房间的名称,我们完成了。然后,您可以从switch语句内部运行此travel函数:
var receiveInput = function(input) {
switch(input) {
case "help":
$("#messageHelp").properDisplay();
break;
case "take sword":
takeSword();
break;
case "go east":
travel("east");
break;
case "go west":
travel("west");
break;
case "go north":
travel("north");
break;
case "go south":
travel("south");
break;
}
}
附赠剑功能:
var takeSword = function() {
if(currentRoom.hasSword) {
$("<p>You picked up a sword.</p>").properDisplay();
}
else {
$("<p>The sword is not here.</p>").properDisplay();
}
};