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.
Since the default says room = 1, the room1(compass,room1V) function will start.
If I go 'north,' the compass will update to say what direction I am viewing the room from, and door4(room,door4V) will run.
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.
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);
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
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
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: