【问题标题】:Branching storyline with Classes使用类分支故事情节
【发布时间】:2018-03-01 15:23:46
【问题描述】:

所以我正在做一个班级项目,它正在制作一个分支故事情节游戏。这是我所拥有的:

    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.");

如您所见,我试图通过利用其他类从本质上简化主类。我可能完全错了,因为我称我的另一个类为“PathOne”并且有一些文本可以打印出来,但它没有。一旦选择了某种选择,有没有办法完全从一个班级转移到另一个班级?或者,我应该如何插入我的类,以便该类中的故事情节发挥作用?

【问题讨论】:

  • 有什么办法可以从一个班级转移到另一个班级 - 这到底是什么意思?
  • 你能提供一些 PathOne 类的源代码吗?我很好奇为什么它没有打印出你声称它应该有的文本......有很多方法可以设计这样的东西,所以系统最终取决于你作为设计师/开发人员。
  • 你应该创建类来合并概念,而不是细节(PathOne、PathWto 等)
  • 所以我有了我的主类,它就叫游戏。我准备好了第二节课,叫做 PathOne。一旦在 Game 中给出了某个响应,我希望下一个要打印出来的东西来自 PathOne,并且在返回第一类之前,所有其他打印和输入都从第二类完整运行。我对 Java 还很陌生,所以如果这听起来很疯狂或没有任何意义,请原谅我,哈哈,或者如果有更好的方法可以做到这一点。我只是不希望在一个类中有 1000 个 switch/case 语句,我希望将其中一些降级到其他类并在需要时调用它们。
  • public static void main(String[] args) 这是 java 程序中主类的特殊语法。你不应该把它复制到其他类。

标签: java


【解决方案1】:

这真的只是一个建议(因为有很多不同的方式来设计这种类型的应用程序),所以请接受它的价值......

您将需要某种游戏状态来跟踪以前的决策/库存/等。

public class GameState {
  List<Item> inventory;
}

您可能希望拥有许多不同的房间或场景。每个房间/场景都可以根据游戏状态管理用户输入。最好创建一个界面来定义房间/场景的职责。

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);
}

例子:

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();
    }
  }
}

您的 Game 类可能会管理某种循环,该循环应在游戏结束时终止。

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());
    }
  }
}

这实际上只是一个外壳,因为你是设计师,所以你可以拿走它。我讨厌提供学校问题的答案,但是如果有你不熟悉的概念,这应该会给你很多研究和学习的机会。这可能不是最好的方法,其他人会有他们的意见。这只是一种“入门”的设计。操纵它,随心所欲地扭曲它。祝你好运!

【讨论】:

    【解决方案2】:

    这都是很棒的帮助!谢谢你的回答。事实证明一切正常,只是我需要 input.nextLine() 而不是 next。我的教授上课不太好,所以有些东西我不太擅长,哈哈。但总的来说,它现在按我的预期运行。再次感谢!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-22
      相关资源
      最近更新 更多