2

I used to program in TI-BASIC, and often used goto statements. I have moved to learning java, and can't figure out how to send the execution of a program to another line.
This is my full code:

package inputs;

import java.util.Scanner;

public class Inputs {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
        String greeting = "Welcome to a new choose your own adventure game!";
        String help = "Use commands such as up, down, left, and right to move around the world";
        String error = "You can't do that";
    room1();
}
public static String getNextLine() {
    Scanner scan = new Scanner(System.in);
    return scan.nextLine();
}
public static String getNextWord() {
    Scanner scan = new Scanner(System.in);
    String base = scan.nextLine();
    String command = base;
            //differentiate capital and lowcase
    if ("north".equals(base)){
        command = "North";
    }
    if ("south".equals(base)){
        command = "South";
    }
    if ("east".equals(base)){
        command = "East";
    }
    if ("west".equals(base)){
        command = "West";
    }
    return command;
}
public static void room1(){
    System.out.println("There is a faint smell of rotting flesh around you. you look hazily around the room. There is a door to the north. It may be your way out...");
    String command = getNextWord();
    while(1==1){
        if ("North".equals(command)){
            hall();
        }
        else{
            System.out.println("You can't do that.");
        }
    }
}
public static void hall(){
    System.out.println("You are in a hallway. The hallway continues in a northerly direction to another room. There is a room to your left");
    while(1==1){
    String command = getNextWord();
        if ("North".equals(command)){
            room3();
        }
        if ("East".equals(command)){
            room2();
        }
        if ("South".equals(command)) {
            room1();
        }
        else {
            System.out.println("You can't do that.");
        }
    }
}
public static void room2(){
    while(1==1){
    String command = getNextWord();
        if("get".equals(command)){
            System.out.println("You picked up the key. Maby there is a door somewhere");

        }
    }
}
public static void room3(){}

}

As you can see, I am attempting to create a text adventure game that has 4 rooms. If it's possible, I would also like to find a key in room2 that gets used in room3. Honestly, I have no ideea how to get this fixed...

*EDIT In response to being marked as a possible duplicate, my question asks for a specific method of action in regards to my particular program, rather than an over all method of action

  • 1
    Convert room to class and instanciate them in Main class – Aroniaina Oct 13 '15 at 15:36
  • 1
    https://en.wikipedia.org/wiki/Finite-state_machine – Sotirios Delimanolis Oct 13 '15 at 15:43
  • 1
    You should learn about the usage of `else if` as opposed to a series of `else` statements. No reason to do a bunch of compares after you've already found a match. – KevinDTimm Oct 13 '15 at 15:44
  • 1
    See the answer from Kevin Hooke below - you really need to follow the java tutorial as a first step. – KevinDTimm Oct 13 '15 at 15:51
  • The problem is that I want to be able to jump to the different methods without returning to the previous method. If there is a way to do this, I would much rather do that. – Confederate 3320 Oct 13 '15 at 15:51
  • simulate goto - https://groups.google.com/forum/#!topic/java-lang-fans/y4zwG4nzkWs – ZhongYu Oct 13 '15 at 16:02
  • Possible duplicate of [Alternative to a goto statement in Java](http://stackoverflow.com/questions/2430782/alternative-to-a-goto-statement-in-java) – Oleg Estekhin Oct 13 '15 at 16:07
  • It looks like you're struggling to translate procedural programming concepts to an OO language like Java. Start with a OO tutorial and learn how to design your solution thinking about it with an OO approach, and then implementing the solution with an OO language like Java will be a lot easier. – Kevin Hooke Oct 13 '15 at 16:23
  • Actually, the OP is struggling with a procedural programming issue as well; GOTO was identified as an a major cause of errors in programming before OO existed; one study found that a GOTO was 9 times more likely to be an error than any other statement. Not only are they not possible in Java, they aren't common anywhere any more -- not since the 70s. – arcy Oct 13 '15 at 16:41

1 Answers1

3

Java does not have a goto statement, and you don't need one.

Currently, you're representing rooms as methods. The current room as the topmost layer on the call stack. Every room visited beforehand is below that. This approach is sufficient for short adventure-style games. However:

  • You're learning Java, and Java supports object-oriented programming.
  • If someone played your game a really long time, eventually the call stack would grow so large a stack overflow occurred.

An alternative would be to consider what are the objects in your game. You have:

  • Rooms
  • Inventory
  • Inventory items (like the key)

These objects can be defined as Java classes or enums. The rooms and connections between rooms constitute the nodes and edges in a graph.

Your methods can be actions, such as transitioning the user from one room to another, or picking up or using an inventory item.

Andy Thomas
  • 84,978
  • 11
  • 107
  • 151