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 mands 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 pletely Text-based. In order to progress through the game, you will need to type in mands that you think the character you are playing as should do.Example: look around.')
}
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 mands 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 pletely Text-based. In order to progress through the game, you will need to type in mands that you think the character you are playing as should do.Example: look around.')
}
Share
Improve this question
asked Jul 24, 2014 at 2:11
user3871114user3871114
111 silver badge4 bronze badges
4
-
1
Wrap a
while(true)
around the whole code. Or change the true, so that you can actually end the game. (while(stillPlaying)
), and within one of your conditions, you can setstillPlaying
to false. As for the caps problem, just change begin to begin.toLowerCase(), and make sure the strings you are paring it to are always lowercase. – Dave Chen Commented Jul 24, 2014 at 2:14 - I would also convert your string values to lowercase before you check their value. That way it doesn't matter if it is LOOK, lOok, or look. If these values are being pulled from some type of mand line, I would convert them as soon as you retrieve them. – Sean Cox Commented Jul 24, 2014 at 2:16
- Instead of using if-else, you could simply use an array with all your options and indexOf to check if an option exists in the array. – elclanrs Commented Jul 24, 2014 at 2:16
- Use recursive function calls. – pdoherty926 Commented Jul 24, 2014 at 2:17
5 Answers
Reset to default 3I would reend a switch inside a while(true) and if something goes on, just break.
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
- Switch Statements
- While Loops
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 pletely Text-based. In order to progress through the game, you will need to type in mands that you think the character you are playing as should do.Example: look around.');
break;
}
}
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);
}
}
}
}
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 pletely Text-based. In order to progress through the game, you will need to type in mands that you think the character you are playing as should do.Example: look around.')
t=false; // if you want to e out of the loop
}
}
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.