简体   繁体   中英

How to write multiple conditions?

I'm trying to make a simple javascript text-based adventure game for school assignment. My map is a 3x3 grid and the locations are called 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 . 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. If the player is in mapLocation3 but has not used a card that they can pick up from mapLocation 4 , they cannot go north. The only way a player is supposed to go north is if they are in mapLocation s 5 and 7 . (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 .
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".

Here's the code I have for the "north" command. I'm not sure how to set up multiple conditions for the if statements. Would it be better to use a switch statement in this case (Not sure how to do that). 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.

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; it should be a == . You have the same mistake in each else if statement.

You can simplify your code quite a lot. Take, for example, 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...". Well, if mapLocation cannot be less than 3 and it cannot be 3, then you can express that as 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 .

Also, you have "if mapLocation is three and door is false, then ... mapLocation = 3". Since mapLocation must be 3 to hit that line, you don't need it. Further, because the else statement calls gMessage = blockedPathMessage[mapLocation]; , you can eliminate the else if(mapLocation == 3 && door === false) block.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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