Here's the gist of my code and it's function. Essentially, it's a pick-where-you-go game to choose your path. And in that it works and performs as expected. For example, if you choose path a at the start, you get to choose between path d and e, and if you chose d you can move to f and g and so on. However, here's where I run into a wall. I want to add the ability to backtrack down the paths, which my current code doesn't allow for. For instance, if I choose a in the beginning and go all the way to f, I want the user to be able to go back to d and have the choice between f and g again, or go all the way back to the starting point and choose b.
How would I go about this? My initial thought was to use something to tell the code to back to a certain line of code when I need to backtrack, but there's no goto type thing in java to my understanding (and judging from what I've read, with good reason). I have an inkling to use loops (I'm thinking while loops in particular), but for the life of me I cannot figure out HOW to structure the loops to backtrack. Any help would be very much appreciated.
Here's my code:
public class PathGame {
public static void main (String[] args) {
String name = JOptionPane.showInputDialog("Hello! Welcome to my paths! What is your name, adventurer?");
JOptionPane.showMessageDialog(null, "Well then " + name + ", here's how this works...some generic instructions");
String startingChoice = JOptionPane.showInputDialog("Choose your path, a, b, or c.");
if (startingChoice.equals("a")){
String aChoice = JOptionPane.showInputDialog("Choose path d or path e");
if (aChoice.equals("d")) {
String dExamineChoice = JOptionPane.showInputDialog("path f or g?");
if (dExamineChoice.equals("f")) {
JOptionPane.showMessageDialog(null, name + "...!");
}
else if (dExamineChoice.equals("g")) {
JOptionPane.showMessageDialog(null, "Stuff g");
}
}
else if (aChoice.equals("e")) {
JOptionPane.showMessageDialog(null, "Stuff e");
}
else if (aChoice.equals("goBack")) {
///Backtrack back to start
}
}
else if (startingChoice.equals("b")) {
String bChoice = JOptionPane.showInputDialog("Path h or i?");
if (bChoice.equals("h")) {
String hChoice = JOptionPane.showInputDialog("Path j, k, or l?");
if (hChoice.equals("j")) {
String jExamine = JOptionPane.showInputDialog("m or n?");
if (jExamine.equals("m")) {
JOptionPane.showMessageDialog(null,"Stuff m");
}
else if (jExamine.equals("n")) {
JOptionPane.showMessageDialog(null,"Stuff n");
}
}
else if (hChoice.equals("k")) {
JOptionPane.showMessageDialog(null,"Stuff k");
}
else if (hChoice.equals("l")) {
JOptionPane.showMessageDialog(null,"Stuff l");
}
}
else if (bChoice.equals("i")) {
JOptionPane.showMessageDialog(null,"Stuff i");
}
}
}
}
Thanks in advance.
Aucun commentaire:
Enregistrer un commentaire