Difference between revisions of "The Labyrinth"
Line 52: | Line 52: | ||
|SolutionCode=<pre> | |SolutionCode=<pre> | ||
+ | /* Class Labyrinth | ||
+ | * Created by Tyler Dielschneider | ||
+ | * Made in COMP3040 | ||
+ | * Navigate a treacherous maze. | ||
+ | */ | ||
+ | |||
+ | // Need this for JOptionPane | ||
+ | import javax.swing.*; | ||
+ | |||
+ | public class Labyrinth | ||
+ | { | ||
+ | |||
+ | public static void main (String args[]) | ||
+ | { | ||
+ | String input; | ||
+ | int answer; | ||
+ | |||
+ | 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\n"); | ||
+ | answer = Integer.parseInt(input); | ||
+ | |||
+ | 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); | ||
+ | |||
+ | if(answer == 2) | ||
+ | { | ||
+ | JOptionPane.showMessageDialog(null,"The cage opens. The treasure is yours. Congratulations!"); | ||
+ | } | ||
+ | else | ||
+ | { | ||
+ | JOptionPane.showMessageDialog(null,"A trap door opens up beneath you and you fall into a pit of spikes and die. Game Over."); | ||
+ | } | ||
+ | } | ||
+ | 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."); | ||
+ | } | ||
+ | } | ||
+ | } | ||
</pre> | </pre> | ||
}} | }} |
Revision as of 18:41, 5 April 2010
Back to the Program-A-Day homepage
ProblemFor 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 the 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:
|
SideSectionTitle
An image or By Students section | |
---|---|---|
Solution | ||
Code |