简体   繁体   English

如何写多个条件?

[英]How to write multiple conditions?

I'm trying to make a simple javascript text-based adventure game for school assignment. 我正在尝试制作一个简单的基于JavaScript文本的冒险游戏,用于学校作业。 My map is a 3x3 grid and the locations are called mapLocation . 我的地图是3x3网格,位置称为mapLocation The player types a command into a text box and the game then functions based on the command. 玩家在文本框中输入命令,然后游戏根据该命令运行。 One of those commands is the "north" command which is supposed to set the mapLocation -= 3 . 这些命令之一是“北”命令,该命令应该设置mapLocation -= 3 I'm supposed to have multiple conditions as to when the player can go north. 关于玩家何时可以向北走,我应该有多个条件。 If the player is in mapLocation4 he cannot go north, if they are in mapLocations 0 , 1 , 2 they cannot go north. 如果玩家在mapLocation4他不能北上,如果他们在mapLocations 012 ,他们不能去北方。 If the player is in mapLocation3 but has not used a card that they can pick up from mapLocation 4 , they cannot go north. 如果玩家在mapLocation3但没有使用他们可以从mapLocation 4拿起的卡,则他们不能向北走。 The only way a player is supposed to go north is if they are in mapLocation s 5 and 7 . 玩家应该往北走的唯一方法是,如果他们位于mapLocation57 (6 and 8 don't exist as mapLocations .) or if they have used the card in mapLocation 3 , which allows them to go to mapLocation0 . (6和8不作为mapLocations存在。),或者如果他们已在mapLocation 3使用该卡,则允许他们转到mapLocation0
Here is the non-functional code for my "north" command. 这是我的“北方”命令的非功能代码。 blockedPathMessage is just a message the player receives if they use the "north" command in a mapLocation that does not support "north". blockedPathMessage只是玩家会收到一条消息,如果他们在使用“北”命令mapLocation不支持“北”。

Here's the code I have for the "north" command. 这是我为“北方”命令准备的代码。 I'm not sure how to set up multiple conditions for the if statements. 我不确定如何为if语句设置多个条件。 Would it be better to use a switch statement in this case (Not sure how to do that). 在这种情况下使用switch语句会更好(不确定如何执行此操作)。 If you are going to correct me or advise me, please only use javascript and html since they're the only languages I have a mediocre understanding of. 如果您要纠正我或建议我,请仅使用javascript和html,因为它们是我仅有的中等理解水平的唯一语言。

case "north":
    if(mapLocation >= 3 && mapLocation != 4 && mapLocation != 3) {
        mapLocation -= 3;
    }
    else if(mapLocation = 4) {
        gMessage = blockedPathMessage[mapLocation];
        mapLocation = 4;
    }
    else if(mapLocation = 3 && door === false) {
        gMessage = blockedPathMessage[mapLocation];
        mapLocation = 3;
    }
    else if(mapLocation = 3 && door === true) {
        mapLocation -= 3;
    }
    else {
        gMessage = blockedPathMessage[mapLocation];
    }

You probably want a "break;" 您可能想要“休息”; at the end of the code block. 在代码块的末尾。 Otherwise, the following case will also be executed. 否则,将执行以下情况。

Your code contains an error. 您的代码包含错误。 else if(mapLocation = 4) is invalid; else if(mapLocation = 4)无效; it should be a == . 它应该是== You have the same mistake in each else if statement. 您在else if语句中也有相同的错误。

You can simplify your code quite a lot. 您可以简化很多代码。 Take, for example, if(mapLocation >= 3 && mapLocation != 4 && mapLocation != 3) . if(mapLocation >= 3 && mapLocation != 4 && mapLocation != 3)为例。 That says "if mapLocation not less than three, AND mapLocation is not 4, AND mapLocation is not three, then...". 这就是说“如果mapLocation不小于3,并且AND mapLocation不是4,并且mapLocation不是3,那么...”。 Well, if mapLocation cannot be less than 3 and it cannot be 3, then you can express that as mapLocation > 3 && mapLocation != 4 . 好吧,如果mapLocation不能小于3并且不能为3,那么可以将其表示为mapLocation > 3 && mapLocation != 4 If mapLocation is an integer, then the next integer larger than 3 is 4, so you can express it as mapLocation > 4 . 如果mapLocation是一个整数,则下一个大于3的整数是4,因此可以将其表示为mapLocation > 4

Also, you have "if mapLocation is three and door is false, then ... mapLocation = 3". 此外,您还有“如果mapLocation为3,而door为false,则... mapLocation = 3”。 Since mapLocation must be 3 to hit that line, you don't need it. 由于mapLocation必须为3才能到达该行,因此您不需要它。 Further, because the else statement calls gMessage = blockedPathMessage[mapLocation]; 此外,因为else语句调用gMessage = blockedPathMessage[mapLocation]; , you can eliminate the else if(mapLocation == 3 && door === false) block. ,则可以消除else if(mapLocation == 3 && door === false)块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM