gpt4 book ai didi

javascript - 使用开关添加附加条件

转载 作者:行者123 更新时间:2023-11-28 18:56:56 25 4
gpt4 key购买 nike

我正在开发一个包含 4 个房间的文字游戏。房间的布局就像一个盒子,分为 4 个部分(西北、东北、东南、西南)。您将从西北房间开始遍历房间,然后顺时针移动到西南房间结束。

无论如何,我已经掌握了开关中包含的基本 Action :

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;

这是我的问题 - 需要复制“西”的方向,因为您应该能够在上层房间和下层房间中向东/西移动。我怎么做?我最初想到的是 OR bool 值:

if (currentRoom == "swRoom"|| "nwRoom")

但是剩下的代码就没有意义了。由于第一次使用时出现中断,我无法复制整个语句。如果可能的话,我想坚持在大部分代码中使用 SWITCH,因为这比大量的 if else 语句干净得多。

最终,我想继续使用它来将其发展成一个更大的游戏,但我正在使用这 4 个房间来自学基础知识。

谢谢!

最佳答案

基于 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]];

现在,如果这碰巧没有返回任何内容,即。方向为“east”的路径在 currentRoom 中不存在,我们的变量将为 undefined ,其计算结果为 false

这对于下一步很重要,因为它允许我们检查是否能够到达新房间。 false 评估意味着我们没有,这就是我们告诉玩家的。

如果返回了一个房间,我们将其设置为当前房间。显示房间的名称,我们就完成了。然后,您可以从 switch 语句内运行此旅行函数:

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();
}
};

关于javascript - 使用开关添加附加条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33427608/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com