Difference between revisions of "The Labyrinth"

From CompSciWiki
Jump to: navigation, search
Line 49: Line 49:
 
An image or By Students section
 
An image or By Students section
  
|Solution=
+
|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:
 +
 
 +
#Getting input
 +
#Doing something with that input
 +
#Displaying output
 +
 
 +
In this program you will do all three things, repeatedly.
 +
 
 +
First of course, you will need variables to store your input:
 +
<pre>
 +
String input;
 +
int answer;
 +
</pre>
  
 
|SolutionCode=<pre>
 
|SolutionCode=<pre>

Revision as of 11:49, 6 April 2010

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!

 

SideSectionTitle

float
Taken from http://www.flickr.com/photos/daniello/565304023/

An image or By Students section

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;

Code

Solution Code

Back to the Program-A-Day homepage