-1

So I'm working on a class project, which is making a branching storyline game. Here's what I have:

    System.out.println("Welcome to _____. \nThe way you play this game is by typing your response exactly as shown in the question.\n\nGood luck.\n\n\nYou wake up in a dark room. \nWhat would you like to do? \n(search for a light switch, cry, wait it out)");
    String response1 = input.next();

    switch (response1) {
    case "search for a light switch": PathOne.main(args);
        break;
    case "wait it out":
        break;
    case "cry": System.out.println("Weak. You cry yourself to death. Good going. This is the end for you.");

As you see, I'm trying to essentially simplify the main class by utilizing other classes. I may be doing this TOTALLY wrong, as I called my other class "PathOne" and had some text for it to print out, which it didn't. Is there any way to kind of move from one class to another entirely once a certain choice is chosen? Or, how should I be inserting my classes so that the storyline inside that class plays out?

  • 1
    *Is there any way to kind of move from one class to another* - what does this mean exactly? – Tim Mar 01 '18 at 15:25
  • 1
    Can you provide some source from the PathOne class? I'm curious why it didn't print out text you claim it should have... There are soooo many ways to design something like this, so the system is ultimately up to you as the designer/developer. – pacifier21 Mar 01 '18 at 15:28
  • You should create classes to incorporate concepts, not specifics (PathOne, PathWto, etc) – Lance Toth Mar 01 '18 at 15:29
  • So I have my main class, which is just called Game. And I have a second class ready, called PathOne. Once a certain response is given in Game, I would like the next thing to print out to come from PathOne, and every other print and input to run in it's entirety from the second class before returning to the first class. I'm fairly new to Java so forgive me if this sounds crazy or doesn't make any sense, haha, or if there's a better way to do this. I just don't want 1000 switch/case statements in one class, I was hoping to relegate some of them to other classes and call them when needed. – Melissa Mar 01 '18 at 15:31
  • public class PathOne { public static void main(String[] args) { System.out.print("test"); } } (this is all I had for path one, I thought it should print but nothing happened) – Melissa Mar 01 '18 at 15:31
  • 2
    `public static void main(String[] args)` this is a special syntax for the main class in a java program. You should not copy it to other classes. – Tim Mar 01 '18 at 15:34
  • Does the program do anything other than not print out the expected text? Is there an error message or anything? What happens when you step through with a debugger? – Brandon McKenzie Mar 01 '18 at 15:35
  • How should I correct this then so it properly prints out? – Melissa Mar 01 '18 at 15:36
  • The program prints nothing. It skips a line and a debugger offers nothing, which is odd to me. – Melissa Mar 01 '18 at 15:37
  • What version of Java are you using? It may be trouble with using "switch" with Strings. Try adding a "default:" to the end of your switch block. Something like this: switch(response1) { case ...; case ...; default: System.out.println("Sorry, I didn't understand your request. Looks like you said " + response1); } – pacifier21 Mar 01 '18 at 15:40
  • I added 'default: System.out.println("I don't know what you mean. (You may have spelled something wrong)");' and when i entered in the light switch option it printed out the default. I'm using JavaSE-9, if that helps. – Melissa Mar 01 '18 at 15:53
  • It would probably help if you first learn some basics of Java OOP. Try creating a toy example, play with classes. Create a `Room` class with an abstract `Furniture` class and extending `Chair` and `Table` classes. The room then may have some kind of `List inventory` with corresponding `add` and `remove` methods. After that you probably gained enough experience to know how to use classes and methods in Java. Currently it is hard to help you since you directly go for something harder without knowing the basics. – Zabuzard Mar 01 '18 at 15:56
  • Okay, so I realized it is only taking the first word, so I changed case 1 to just "search" and now it printed out. So new problem, how would I collect all of the words inputted so I can make it actually take the entire phrase? – Melissa Mar 01 '18 at 15:57
  • There is definitely some opportunity here to do some research into OOP as @Zazuba suggests. As I said, there are many many ways to implement the full architecture. As far as reading the full line, I'm not an expert with Scanner (which I'm assuming you are using), but you may want to use input.nextLine() instead of input.next() (see [this answer](https://stackoverflow.com/a/20190577/3586783)) – pacifier21 Mar 01 '18 at 16:08
  • `Scanner.next()` has a default delimiter on all whitespaces. To get the full line, you would probably want to do `input.nextLine()` instead of `input.next()` – Asthor Mar 01 '18 at 16:48

2 Answers2

0

This is really just a suggestion (since there are so many different ways to design this type of application), so take it for what it's worth...

You will want to have some kind of Game State that keeps track of previous decisions/inventory/etc.

public class GameState {
  List<Item> inventory;
}

You will likely want to have many different rooms or scenes. Each room/scene can manage user input based on the game state. It is probably best to create an interface that defines what a room/scene is responsible for.

public interface Room {
  // This will provide the text for the room, including any questions
  public String getRoomPrompt(GameState state);
  // This will return the next Room object based on the user input. "null" might indicate game-over?
  public Room processUserInput(String userInput);
}

Example:

public class OpeningRoom implements Room {
  public String getRoomPrompt(GameState state) {
    // return all the stuff you have in  your current Game class
  }

  public Room processUserInput(String userInput) {
    // using the switch block, return an instance of the next room to enter (like RoomOne, etc)
    switch(userInput){
      default: return new RoomOne();
    }
  }
}

Your Game class will likely manage some kind of loop which should terminate when the game is over.

public class Game {

  public static void main(String[] args) {
    /* (initialize "input" Scanner here as you already are */

    Room currentRoom = new OpeningRoom();
    while (currentRoom != null) {
      System.out.println(currentRoom.getRoomPrompt());
      currentRoom = currentRoom.processUserInput(input.nextLine());
    }
  }
}

This is really just a shell, and you can take it or leave it since you are the designer. I hate to provide answers to school problems, but this should give you a lot to research and learn if there are concepts you are not familiar with. It may not be the best approach, and others will have their opinions. This is just a "get started" kind of design. Manipulate it, twist it as you wish. Good luck!

pacifier21
  • 813
  • 1
  • 5
  • 13
0

This is all fantastic help! Thank you for the answers. It turns out everything worked fine, it was just that I needed the input.nextLine() instead of next. My professor hasn't gone over classes too well so there's some things i'm not too strong with, haha. But overall, it's now running as I expected. Thanks again!