2016-06-07 67 views
0

我目前正在制作一个游戏,发生在大四的10多天。我为这款游戏创建了一个介绍,但我不确定如何把它放在一起。我应该将介绍作为父类,并且每天都有不同的子类?这是我的代码到目前为止。基于文本的游戏与选择,发生在多天

package SeniorGame; 

import java.util.Scanner; 

public class Intro{ 
    public static void main (String[]args){ 
    Scanner sc = new Scanner(System.in); 

    System.out.println("Before the game begins I need to know a few things about yourself."); 
    System.out.println(); 

    String name; 
    System.out.println("What is your name? "); 
    name=sc.next(); 

    String crush; 
    System.out.println("Who was your High School crush? "); 
    crush=sc.next(); 

    String bfriend; 
    System.out.println("Who was your best friend in High School? "); 
    bfriend=sc.next(); 

    System.out.println(); 
    System.out.println(); 

    System.out.println("This is a story that consits of 12 days in your senior year of High School.\n" 
      + "The days are spread out from the beginning to the end of the year.\nThe choices you make" 
      + " will impact the way your story plays out."); 
} 

}

+0

'extends'提炼行为并用于“is-a”关系(香蕉是一种水果)。与你的介绍有关系吗? – zapl

+0

从技术上讲,介绍不是一个独立的东西。如果这是有道理的。从介绍开始,你将被提升为游戏菜单,以播放或退出。如果你退出system.quit(1)行触发器,但如果不行,你显然会玩游戏。我的问题是如何从这里开始第一天。我是否以单独的课程或单独的方法进行第一天课程? –

+0

我会让每一天继承Day类。让外部的课程处理日常移动的逻辑,记录日期可能会分享的变量等。 –

回答

0

如果以同样的方式你的日子进步,也有类似的行为,你可能会考虑定义SchoolDay接口,然后创建一两天为实现此接口的类。主要Intro课程中的变量可以跟踪玩家在整个过程中所做的事情。

下面是一个例子。在这里,我已经决定,保持做功课的轨道是对比赛的胜负至关重要的(我无聊):

public interface SchoolDay { 
    public void doMorningClass(); 
    public void doLunchTime(); 
    public void doAfternoonClass(); 
    public boolean doHomework(); //Sets a boolean to true, see below 
} 

然后,在你的主类(我将重新命名,说实话):

public class SchoolGame { 
    private static boolean[] homeworkDone; 
    //Your other global variables such as crush and name go here 
    //Make getters and setters for all of these so the day objects can 
    //access and change them. 

    public static void main(String[] args) { 
     homeworkDone = new boolean[12]; //One boolean for each day 

     //Move your intro stuff into a method and call it here 
     //The main method continues once they fill out the info 

     //Instantiate day objects and run their school time methods 
     //If the player decides to do homework during those times, for example 
     //Then that day's doHomework() method is called 
     //Which would affect the boolean of that day under some condition 
     ... 

此设置可能不完全适合您的需求,因为您的描述有些模糊,并且此处没有具体问题。如果你认为你的day对象需要所有日子都有的变量,那么使用父类(可能是抽象的)而不是接口。

+0

这实际上确实对你很有帮助。 –