Indie Dev

Hello Guest!. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, sell your games, upload content, as well as connect with other members through your own private inbox!

Prevent Variables from Resetting in Do/While

Sethorion

Towns Guard
Xy$
0.00
If you want to see a jsfiddle for this instead, let me know.

SUMMARY OF WHAT I"M TRYING TO DO:
((You can read or ignore this p)) I've put together a very simple game that uses a do/while function and switch to check what room you are in. If you are in room one, the switch selects room1 and runs the room1() function. Inside the room, you can choose which direction to go. If you choose north or east, a door1() or door4() function will run to say "Would you like to open this door?

WHAT IS BROKEN: This is all working beautifully and the functions are working as they should. The one major issue if that my variables are resetting, so I am always in room 1, my compass always equals 0 and none of the doors of rooms equal "visited."

Basically, I walk through a door and I end up in room 1 again, and the program treats everything as though I have just restarted the game.

Code:
var room1V = 0; //room1  // these variables tell the computer whether I have 'visited' a room before.
var room2V = 0; //room2
var room3V = 0; //room3
var room4V = 0; //room4

var door1V = 0; //room1 - room2 // these variables tell the computer whether I have used a door before.
var door2V = 0; //room2 - room3
var door3V = 0; //room3 - room4
var door4V = 0; //room4 - room1

var compass = 0; // which side of the room am I on?
var room = 1; // what room am I currently in?
var reply = 1; // this is re-declared as a local variable in each function, and it works fine
win = 0; // this will eventually tell the room-check do/while to stop

// This do/while check what room I am in:
do {
    quit = 0;
    switch(room) {
        case '1':
            room1(compass,room1V);
            break;
        case '2':
            room2(compass,room2V);
            break;
        case '3':
            room3(compass,room3V);
            break;
        case '4':
            room4(compass,room4V);
            break;
    }
} while (win != 1);
Since the default says room = 1, the room1(compass,room1V) function will start.

Code:
function room1(compass,room1V) {
    if (room1V === 1) {
        console.log("You are in room 1 again.");
        document.write("You are in room 1 again." + "<br>");
        var reply = prompt("Where would you like to go? EAST, NORTH?");
        switch(reply.toLowerCase()) {
            case 'east':
                compass = "east"; //because you are trying to open the east door, you will now see room1 from the east. If you make it through, your compass will update to 'west' because you will be on the west side of room2.
                door1(compass,door1V);
                break;
            case 'north':
                compass = "north";
                door4(compass,door4V);
                break;
            default:
                  console.log("Something went wrong.");
                  document.write("Something went wrong." + "<br>");
        }
    } else {
        console.log("You are in room 1.");
        document.write("You are in room 1." + "<br>");
        room1V = 1;
        reply = prompt("Where would you like to go? EAST, NORTH?");
        switch(reply.toLowerCase()) {
            case 'east':
                compass = "east";
                door1(compass,door1V);
                break;
            case 'north':
                compass = "north";
                door4(compass,door4V);
                break;
            default:
                  console.log("Something went wrong.");
                document.write("Something went wrong." + "<br>");
        }
    }
}   // Working
If I go 'north,' the compass will update to say what direction I am viewing the room from, and door4(room,door4V) will run.

Code:
function door4(room,door4V) {
    if (door1V === 1) {
        console.log("You approach door 4 again.");
        document.write("You approach door 4 again." + "<br>");
        var reply = prompt("What would you like to do? OPEN, QUIT?");
        switch(reply.toLowerCase()) {
            case 'open':
                if (room === 4) {
                   room = 1;
                   compass = "north";
                   console.log("You walk through door 4 into room 1...");
                   document.write("You walk through door 4 into room 1..." + "<br>");
                } else {
                   room = 4;
                   compass = "south";
                   console.log("You walk through door 4 into room 4...");
                   document.write("You walk through door 4 into room 4..." + "<br>");
                }
                quit = 1;
                break;
            case 'quit':
                  quit = 1;
                break;
            default:
                  console.log("Something went wrong.");
                document.write("Something went wrong." + "<br>");
        }
    } else {
        console.log("You approach door 4.");
        document.write("You approach door 4." + "<br>");
        var reply = prompt("What would you like to do? OPEN, QUIT?");
        switch(reply.toLowerCase()) {
            case 'open':
                if (room === 4) {
                   room = 1;
                   compass = "north";
                   console.log("You walk through door 4 into room 1...");
                   document.write("You walk through door 4 into room 1..." + "<br>");
                } else {
                   room = 4;
                   compass = "south";
                   console.log("You walk through door 4 into room 4...");
                   document.write("You walk through door 4 into room 4..." + "<br>");
                }
                quit = 1;
                break;
            case 'quit':
                  quit = 1;
                break;
            default:
                  console.log("Something went wrong.");
                  document.write("Something went wrong." + "<br>");
        }
    }
}       // Working
At this point, the do/while function is supposed to say, "Oh! Since room = 4, you are now in room 4." But that isn't what happens.
Room does = 4, but when the do/while re-runs, I am back in room1, and all the variables appear to have reset.
 
Last edited:

eivl

Local Hero
Xy$
0.00
If you use the JavaScript code so i can read it easily that would be great ;)
[doublepost=1452094857,1452094282][/doublepost]Your case switch values are wrong, upload the complete code if you want me to look at it more. I am on mobile now so it would be later
[doublepost=1452161510][/doublepost]You could just upload the code to pastebin and send me the link or use any other method that works for you, PM, email also works! ;)
 
Top