Difference between revisions of "The Labyrinth"

From CompSciWiki
Jump to: navigation, search
m (fixed some grammar issues in 'By Students' section.)
m (Changed to codeblocks)
Line 57: Line 57:
  
 
First of course, you will need variables to store your input:
 
First of course, you will need variables to store your input:
<pre>
+
{{CodeBlock|Code=
 
String input;
 
String input;
 
int answer;
 
int answer;
</pre>
+
}}
  
 
The input variable will receive the raw text (String) directly from JOptionPane.showInputDialog.  The answer variable will store the choice that the user will make as an integer.  To begin the program, we will ask the user for their first choice:
 
The input variable will receive the raw text (String) directly from JOptionPane.showInputDialog.  The answer variable will store the choice that the user will make as an integer.  To begin the program, we will ask the user for their first choice:
<pre>
+
{{CodeBlock|Code=
 
input = JOptionPane.showInputDialog(null,"You have arrived at the entrance of the labyrinth.\n"+
 
input = JOptionPane.showInputDialog(null,"You have arrived at the entrance of the labyrinth.\n"+
 
"Large doors stand in front of you. A wooden sign reads \"Death Awaits\".\n"+  
 
"Large doors stand in front of you. A wooden sign reads \"Death Awaits\".\n"+  
 
"Choose your fate: 1. Go inside 2. Leave");
 
"Choose your fate: 1. Go inside 2. Leave");
</pre>
+
}}
  
 
There are a couple of things that may seem strange to you.  One is that the showInputDialog method spans three lines.  This is perfectly fine.  In fact, it is preferred so that your string doesn't stretch outside the bounds of your editor, requiring you to scroll to see it.  You can split your string into multiple lines in the editor by ending the string with a quotation mark, adding the '+' character, and then continuing the string on the next line.
 
There are a couple of things that may seem strange to you.  One is that the showInputDialog method spans three lines.  This is perfectly fine.  In fact, it is preferred so that your string doesn't stretch outside the bounds of your editor, requiring you to scroll to see it.  You can split your string into multiple lines in the editor by ending the string with a quotation mark, adding the '+' character, and then continuing the string on the next line.
Line 76: Line 76:
  
 
Once we have the raw input from the user, we need to convert it into an integer:
 
Once we have the raw input from the user, we need to convert it into an integer:
<pre>
+
{{CodeBlock|Code=
 
answer = Integer.parseInt(input);
 
answer = Integer.parseInt(input);
</pre>
+
}}
  
 
Now that we have received the input and converted it into an integer, we must do something with the input.  We must check to see which option the user picked:
 
Now that we have received the input and converted it into an integer, we must do something with the input.  We must check to see which option the user picked:
<pre>
+
{{CodeBlock|Code=
 
if(answer == 1)
 
if(answer == 1)
 
{
 
{
Line 95: Line 95:
 
JOptionPane.showMessageDialog(null,"Before you can leave, a hungry tiger attacks and kills you. Game Over.");
 
JOptionPane.showMessageDialog(null,"Before you can leave, a hungry tiger attacks and kills you. Game Over.");
 
}
 
}
</pre>
+
}}
  
 
If the user answers 1, the program will continue.  We will receive more input from the user to continue the quest.  If they answer 2, a message dialog is displayed (rather than an input dialog), and the program ends.
 
If the user answers 1, the program will continue.  We will receive more input from the user to continue the quest.  If they answer 2, a message dialog is displayed (rather than an input dialog), and the program ends.
Line 102: Line 102:
 
Now it is time for the tricky part.  You will use your knowledge of nested-if statements to continue the quest.  We've already received the new input from the user inside of the if statement.  Now we add a new if-else clause inside of the if block that we are currently in:
 
Now it is time for the tricky part.  You will use your knowledge of nested-if statements to continue the quest.  We've already received the new input from the user inside of the if statement.  Now we add a new if-else clause inside of the if block that we are currently in:
  
<pre>
+
{{CodeBlock|Code=
 
if(answer == 1)
 
if(answer == 1)
 
{
 
{
Line 130: Line 130:
 
JOptionPane.showMessageDialog(null,"Before you can leave, a hungry tiger attacks and kills you. Game Over.");
 
JOptionPane.showMessageDialog(null,"Before you can leave, a hungry tiger attacks and kills you. Game Over.");
 
}
 
}
</pre>
+
}}
  
 
Continue the quest using nested-if statements until you get to the end.  See below for the entire solution code.
 
Continue the quest using nested-if statements until you get to the end.  See below for the entire solution code.

Revision as of 15:06, 4 December 2011

Back to the Program-A-Day homepage

Problem

For this problem, you will create a labyrinth. The user will be given a series of decisions to make in hope of making it to the treasure. For simplicity sake, the labyrinth will very small. The input will be recorded using JOptionPane.showInputDialog and the output will be done with JOptionPane.showMessageDialog. The user will enter an integer based on their choice. If the user makes the wrong choice, the game will be over and the program will end. To complete this problem, you should have a good understanding of Nested if statements as well as JOptionPane Methods. Design the labyrinth exactly as described below:


Input Dialog: You have arrived at the entrance of the labyrinth. Large doors stand in front of you. A wooden sign reads "Death Awaits". Choose your fate: 1. Go inside 2. Leave


Choice 2:


Message Dialog: Before you can leave, a hungry tiger attacks and kills you. Game Over.


Choice 1:


Input Dialog: You enter the labyrinth, well aware that your life may be in jeopardy. Almost instantly your suspicions are confirmed. There are two doors with two dragons guarding them. One dragon is good, one is evil. Choose your fate: 1. Red dragon 2. Blue dragon.


Choice 2:


Message Dialog: The blue dragon claws your face off. You need that to live. Game Over.


Choice 1:


Input Dialog: The red dragon likes your style. He moves out of the way and allows you to enter his door. You walk into the next room. In the distance you see what you came here for, the treasure. Unfortunately, the treasure is contained in a cage. There are two buttons: One button opens the cage, one button undoubtably seals your doom. Choose your fate: 1. Left button 2. Right button


Choice 1:


Message Dialog: A trap door opens up beneath you and you fall into a pit of spikes and die. Game Over.


Choice 2:


Message Dialog: The cage opens. The treasure is yours. Congratulations!

 

By Students...

For many new programmers, the ability to make a game is their motivation to learn how to program. This was originally my motivation. A program such as this is one of the first games that you can make, even without knowing anything substantial about programming. Once you have more experience, you will be able to make bigger and better games. Also once you gain more skills you will also be able to vastly improve on the code for this game. Once you have finished Introduction To Computer Science, I would suggest going back and doing just that.

Solution

This problem may seem daunting at first, but once you have the general idea of how it's supposed to work, it becomes much more clear.

Like any other program, you must consider three things:

  1. Getting input
  2. Doing something with that input
  3. Displaying output

In this program you will do all three things, repeatedly.

First of course, you will need variables to store your input:

 String input;
int answer; 

The input variable will receive the raw text (String) directly from JOptionPane.showInputDialog. The answer variable will store the choice that the user will make as an integer. To begin the program, we will ask the user for their first choice:

 input = JOptionPane.showInputDialog(null,"You have arrived at the entrance of the labyrinth.\n"+
					"Large doors stand in front of you. A wooden sign reads \"Death Awaits\".\n"+ 
					"Choose your fate: 1. Go inside 2. Leave"); 

There are a couple of things that may seem strange to you. One is that the showInputDialog method spans three lines. This is perfectly fine. In fact, it is preferred so that your string doesn't stretch outside the bounds of your editor, requiring you to scroll to see it. You can split your string into multiple lines in the editor by ending the string with a quotation mark, adding the '+' character, and then continuing the string on the next line.


This does not, however, split the string up into three lines when displayed to the user. That is what the '\n' character is for. '\n' is the new line character. If you do not include any of these characters in your string, the string will be on one line and stretch the dialog box over the bounds of the screen, preventing the user from seeing it. Try removing the new line characters and see what happens.


Once we have the raw input from the user, we need to convert it into an integer:

 answer = Integer.parseInt(input); 

Now that we have received the input and converted it into an integer, we must do something with the input. We must check to see which option the user picked:

 if(answer == 1)
{
	input = JOptionPane.showInputDialog(null, "You enter the labyrinth, "+
		"well aware that your life may be in jeopardy.\nAlmost instantly your suspicions are confirmed. "+
		"There are two doors with two dragons guarding them.\nOne dragon is good, one is evil.\n"+
		"Choose your fate: 1. Red dragon 2. Blue dragon.");
	answer = Integer.parseInt(input);
        ... <---The program continues
}
else
{
	JOptionPane.showMessageDialog(null,"Before you can leave, a hungry tiger attacks and kills you. Game Over.");
} 

If the user answers 1, the program will continue. We will receive more input from the user to continue the quest. If they answer 2, a message dialog is displayed (rather than an input dialog), and the program ends.


Now it is time for the tricky part. You will use your knowledge of nested-if statements to continue the quest. We've already received the new input from the user inside of the if statement. Now we add a new if-else clause inside of the if block that we are currently in:

 if(answer == 1)
{
	input = JOptionPane.showInputDialog(null, "You enter the labyrinth, "+
		"well aware that your life may be in jeopardy.\nAlmost instantly your suspicions are confirmed. "+
		"There are two doors with two dragons guarding them.\nOne dragon is good, one is evil.\n"+
		"Choose your fate: 1. Red dragon 2. Blue dragon.");
	answer = Integer.parseInt(input);
			
	if(answer == 1)
	{
		input = JOptionPane.showInputDialog(null, "The red dragon likes your style. He moves out of the way\n"+
			"and allows you to enter his door. You walk into the next room.\nIn the distance "+
			"you see what you came here for, the treasure. \nUnfortunately, the treasure is contained "+
			"in a cage.\nThere are two buttons: One button opens the cage, one button undoubtably seals your doom.\n"+
			"Choose your fate: 1. Left button 2. Right button");
		answer = Integer.parseInt(input);
                ...<---The program continues
        }
        else
	{
		JOptionPane.showMessageDialog(null,"The blue dragon claws your face off. You need that to live. Game Over.");
	}
}
else
{
	JOptionPane.showMessageDialog(null,"Before you can leave, a hungry tiger attacks and kills you. Game Over.");
} 

Continue the quest using nested-if statements until you get to the end. See below for the entire solution code.

Code

Solution Code

Back to the Program-A-Day homepage