2016-06-07 7 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

'extend'는 동작을 개선하고 "is-a"관계 (바나나는 과일 임)에서 사용됩니다. 인트로와의 관계에 당신의 시절이 있습니까? – zapl

+0

기술적으로 인트로가 독립 실행 형 것은 아닙니다. 그 말이 맞는다면. 인트로에서 게임 메뉴를 사용하여 게임을하거나 종료 할 수 있습니다. system.quit (1) 행을 종료하면 트리거되지만 분명히 게임을 실행합니다. 제 질문은 여기에서 어떻게 시작하나요. 나는 하루 하나를 별도의 수업 또는 별도의 방법으로 만들어야합니까? –

+0

나는 매일 수업에서 상속 받겠다. 외부 수업에서 날마다 움직이는 논리를 처리하고, 일들이 공유 할 수있는 변수를 추적하십시오. –

답변

0

당신의 일 유사한 방식으로 진행하고 비슷한 동작이있는 경우, 당신은 SchoolDay 인터페이스를 정의하고이 인터페이스를 구현하는 클래스로 열두 일을 만드는 고려할 수 있습니다. 주 Intro 클래스의 변수는 플레이어가 수행 한 작업을 추적 할 수 있습니다.

다음은 예입니다. 여기에, 나는 숙제를 추적을 유지하는 것은 게임의 결과에 필수적이라고 결정했다 (I 지루 해요) :

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 
     ... 

설명이 다소 모호하고 여기에 특정 질문이 없기 때문에이 설정은 사용자의 요구에 완벽하게 맞지 않을 수 있습니다. 당신의 하루 객체가 모든 날에 공통적 인 변수를 필요로한다고 생각한다면, 인터페이스 대신에 부모 클래스 (아마도 추상 클래스)를 사용하십시오.

+0

이것은 실제로 많은 도움을줍니다. –