Reputation: 11
Ok, so I am creating a text-based exploration type game. I want to know how to loop an if/else statement along with resetting the variable. I have it so the character you play as starts in his bedroom and you have to type in commands for your character to follow such as looking around. If you could also help me with java recognizing the same word in all types of forms(CAPS in any place).
if (begin === 'look around')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === 'look')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === 'examine room')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === 'examine the room')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === '?')
{
confirm('If you have not already noticed, this game is completely Text-based. In order to progress through the game, you will need to type in commands that you think the character you are playing as should do.Example: look around.')
}
Upvotes: 1
Views: 9601
Reputation: 241
Gave it a little structure so you have something to go off of. Lot of people here at stackoverflow have given me more then enough to work with in the past and can't thank them enough, bout time I gave back a little bit :)
A good way is to put it all in a function and have it call itself. Gives the game some dimension and lets you return to previous locations.
Careful when editing though, need to make sure you can exit the function/loop.
You can also have prefilled values inside the prompt by doing:
prompt("Text", "Text in box");
Makes it easier for the user when they know what you're expecting.
var place = "room";
var playing = true;
// Get user's first choice
var choice = prompt("You are in a room with some choices");
// Call the function once to start the game
gameLoop(playing, place, choice);
// your main game loop
function gameLoop (playing, place, choice) {
// first "room"
while(playing && place == "room") {
// where the users "choice" gets evaluated
switch(choice) {
case "mirror":
choice = prompt("You have chosen to look at the mirror. Nothing, where to now?");
break;
case "leave":
place = "hallway";
choice = prompt("You have chosen to leave the room. In the doorway you can go left or right, which way?");
break;
case "dresser":
choice = prompt("You have chosen to look at the dresser");
break;
case "exit":
playing = false;
break
default:
choice = prompt("Yo what do you wanna do?");
}
}
// second "room"
while (playing && place == "hallway") {
switch(choice) {
case "left":
choice = confirm("You went left down the hallway and fell through the floor and met an untimely death.");
playing = false;
break;
case "back":
place = "room";
var choice = prompt("You went back and are in a room with some choices");
break;
case "right":
confirm("A smiling Unicorn, you won!");
break;
default:
playing = false;
}
}
// loop the game with the place and choice if playing is true, else game is over
if (playing) {
gameLoop(playing, place, choice);
}
}
If you'd like this without the annoying script box, give me a holler, believe I could make it work.
Upvotes: 0
Reputation: 14044
Probably this might help you
var t=true;
while(t)
{
if (begin === 'look around')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === 'look')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === 'examine room')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === 'examine the room')
{
confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
}
else if (begin === '?')
{
confirm('If you have not already noticed, this game is completely Text-based. In order to progress through the game, you will need to type in commands that you think the character you are playing as should do.Example: look around.')
t=false; // if you want to come out of the loop
}
}
Upvotes: 0
Reputation: 71
Consider better structuring your code.
Place your data on an appropriate structure and use a while
statement as your main game loop.
See Example:
var game = [
{
input: ['look', 'look around'],
answer: 'You see a lot of cool stuff'
},
{
input: ['?', 'help'],
answer: 'Good luck'
}
]
var entry = "";
while (entry !== "exit") {
entry = prompt("What now?", "help").toLowerCase();
for (var i = 0; i < game.length; i++) {
for (var j = 0; j < game[i].input.length; j++) {
if (game[i].input[j] === entry) {
confirm(game[i].answer);
}
}
}
}
Upvotes: 0
Reputation: 6170
You can use a while
loop and a variable to do this.
I would also get rid of all these if
statements and replace with a switch
statement
Here's some info
I've also changed some of your confirm
messages with alert
Here's the results
var exit = false;
while (!exit) {
switch (begin)
{
case 'look around':
alert('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed');
break;
case 'look':
alert('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed');
break;
case 'examine room':
alert('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed');
break;
case 'examine the room':
alert('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed');
break;
case 'exit':
if (confirm('Are you sure you want to exit this game?')){
exit = true;
}
break;
case '?':
default:
alert('If you have not already noticed, this game is completely Text-based. In order to progress through the game, you will need to type in commands that you think the character you are playing as should do.Example: look around.');
break;
}
}
Upvotes: 0
Reputation: 41
I would recomend a switch inside a while(true) and if something goes on, just break.
Upvotes: 3