2017-03-17 10 views
-2

방금 ​​Java에서 다른 클래스를 사용하는 방법을 배웠습니다.하지만 방법을 명확히 알지 못합니다. 나는 내 게임을 시작하고 멈추는 데 충분한 정비공을 만드는 법을 궁금해했다. 누군가가 당신이 가정하지 않은 것을 입력 할 때와 같이, 다시 입력 할 수있게합니다. 이 작은 게임으로 끝나지 않았다는 것을 명심하십시오.조직화에 어려움이 있습니다.

package Horror; 

import java.util.Scanner; 

public class main { 
    public static void main (String[] args){ 
     //Creating a scanner to get user input 
     Scanner input = new Scanner(System.in); 

     //Game Story | Introduction 
     System.out.println("You find yourself in a car, but you have no idea how you got there. It's raining outside and you spot a house through your cold window. You see something in the glovebox."); 
     System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
     String contlol = input.nextLine(); 
     System.out.println("Choices:"); 
     System.out.println("'Leave'"); 
     System.out.println("'Open Glovebox'"); 
     String firstchoice = input.nextLine(); 
     //Choice of leaving the car without opening the glovebox 
     if (firstchoice.equalsIgnoreCase("Leave")){ 
      System.out.println("You get outside of the car. It's dark and rainy and you have no other choice than to enter the safest place that's closest to you. The house"); 
      System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
      String cont1 = input.nextLine(); 
      System.out.println("Choices:"); 
      System.out.println("'Walk'"); 
      String in = input.nextLine(); 
      //Choice of Walking to the house 
      if (in.equalsIgnoreCase("Walk")){ 
       System.out.println("You begin to walk towards the house. The door creaks open, and you find yourself in a dusty, cold house. The house doens't seem like it has had anybody in it for years, and there is an upstairs and a kitchen."); 
       System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
       String cont2 = input.nextLine(); 
       System.out.println("Choices:"); 
       System.out.println("'Kitchen'"); 
       System.out.println("'Upstairs'"); 
       System.out.println("'Living Room'"); 
       String gimme = input.nextLine(); 
       if (gimme.equalsIgnoreCase("Kitchen")){ 
        System.out.println("You walk into the dark kitchen. You find a light switch to the side, and the room brightens up a tiny bit. You see that the stove is on. Someone is boiling water."); 
        System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
        String gay = input.nextLine(); 
        System.out.println("Choices"); 
        System.out.println("'Run'"); 
        System.out.println("'Stay'"); 
        String utters = input.nextLine(); 
        if (utters.equalsIgnoreCase("Run")){ 
         System.out.println("You run to the door helplessly. You find that the door is locked... From the outside."); 
         System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
         String k = input.nextLine(); 
         System.out.println("Choices:"); 
         System.out.println("'Upstairs'"); 
         System.out.println("'Living Room'"); 
         String kkk = input.nextLine(); 
         if (kkk.equalsIgnoreCase("Upstairs")){ 
          System.out.println("As you start your journey upstairs, the stairs start creaking, and you hear someone. After a while, you convince yourself it was your imagination."); 
          System.out.println("Once you get upstairs, you find a bathroom, and a bedroom."); 
          System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
          String rape = input.nextLine(); 
          System.out.println("Choices:"); 
          System.out.println("'Bathroom'"); 
          System.out.println("'Bedroom'"); 
        } 
       }else if (utters.equalsIgnoreCase("Stay")){ 
        System.out.println("You walk toward the boiling pot of water. Inside, you see someone, but not yourself..."); 
        System.out.println("You turn around, but not quick enough to evade the knife that is being hurled at you. It sticks inside your heart."); 
        System.out.println("You start to slowly die, watching the creature above you stare down into your eyes."); 
        System.out.println("YOU LOSE. . ."); 
        return; 
       } 
       }else if (gimme.equalsIgnoreCase("Living Room")){ 
        System.out.println("You walk into the small, but somehow spacious living room. There is a television, ceiling fan, record player, couches, and other living room items."); 
        System.out.println("As soon as you walk into the living room, the telvesion turns on to a news channel, and the ceiling fan starts spinning."); 
        System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
        String dick = input.nextLine(); 
        System.out.println("Choices:"); 
        System.out.println("'Run'"); 
        System.out.println("'Investigate'"); 
        String meme = input.nextLine(); 
        if (meme.equalsIgnoreCase("Investigate")){ 
         System.out.println("You circle around the living room. You find a cassette tape inside the telveison. You hear something behind you."); 
         System.out.println("You turn around, but no one's there. You turn back around and the cassette tape is gone."); 
         System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
         String gutknife = input.nextLine(); 
         System.out.println("Choices:"); 
         System.out.println("'Upstairs'"); 
         System.out.println("'Kitchen'"); 
         String search = input.nextLine(); 
         if (search.equalsIgnoreCase("Upstairs")){ 
          System.out.println("As you start your journey upstairs, the stairs start creaking, and you hear someone. After a while, you convince yourself it was your imagination."); 
          System.out.println("Once you get upstairs, you find a bathroom, and a bedroom."); 
          System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
          String rape = input.nextLine(); 
          System.out.println("Choices:"); 
          System.out.println("'Bathroom'"); 
          System.out.println("'Bedroom'"); 
          String fight = input.nextLine(); 
          if (fight.equalsIgnoreCase("Bathroom")){ 
           System.out.println("As you walk into the bathroom, you flip on the light switch. You find a sink, toilet, mirror, and a bath tub with the shower curtains covering it."); 
           System.out.println("You hear water dripping."); 
           System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
           String poop = input.nextLine(); 
           System.out.println("Choices:"); 
           System.out.println("'Investigate'"); 
           System.out.println("'Look in Mirror'"); 
           String gimmedat = input.nextLine(); 
          }else if (fight.equalsIgnoreCase("Bedroom")){ 
           System.out.println("You walk in to a roomy bedroom. You think to yourself that it must be the master bedroom. You start looking around."); 
           System.out.println("After inspecting the room for a few minutes, you find a bloody knife on the drawer, it's right next to a dead body lying next to you feet."); 
           System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
           String cont0101 = input.nextLine(); 
           System.out.println("Choices:"); 
           System.out.println("'Scream'"); 
           System.out.println("'Examine'"); 
           String gaymemes = input.nextLine(); 
           if (gaymemes.equalsIgnoreCase("Examine")){ 

           } 
          } 
         } 
        } 
       } else if (gimme.equalsIgnoreCase("Upstairs")){ 
        System.out.println("As you start your journey upstairs, the stairs start creaking, and you hear someone. After a while, you convince yourself it was your imagination."); 
        System.out.println("Once you get upstairs, you find a bathroom, and a bedroom."); 
        System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
        String rape = input.nextLine(); 
        System.out.println("Choices:"); 
        System.out.println("'Bathroom'"); 
        System.out.println("'Bedroom'"); 
        String xdd = input.nextLine(); 
        if (xdd.equalsIgnoreCase("Bathroom")){ 
         System.out.println("As you walk into the bathroom, you flip on the light switch. You find a sink, toilet, mirror, and a bath tub with the shower curtains covering it."); 
         System.out.println("You hear water dripping."); 
         System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
         String poop = input.nextLine(); 
         System.out.println("Choices:"); 
         System.out.println("'Investigate'"); 
         System.out.println("'Look in Mirror'"); 
         String gimmedat = input.nextLine(); 
       }else if (xdd.equalsIgnoreCase("Bedroom")){ 
        System.out.println("You walk in to a roomy bedroom. You think to yourself that it must be the master bedroom. You start looking around."); 
        System.out.println("After inspecting the room for a few minutes, you find a bloody knife on the drawer, it's right next to a dead body lying next to you feet."); 
        System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
        String cont0101 = input.nextLine(); 
        System.out.println("Choices:"); 
        System.out.println("'Scream'"); 
        System.out.println("'Examine'"); 
        String rapeme = input.nextLine(); 
        if (rapeme.equalsIgnoreCase("Examine")){ 

        } 
       } 
      } 
      } 
     }else if (firstchoice.equalsIgnoreCase("Open Glovebox")){ 
      System.out.println("You find a note in the glovebox. It reads: 'They've taken it away from me. They told me I was crazy. But I'll prove them wrong. I will take it back. I will make this right. You don't know me. But I am there. Watching."); 
      System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
      String cont3 = input.nextLine(); 
      System.out.println("Choices:"); 
      System.out.println("Leave"); 
      String getin = input.nextLine(); 
      if (getin.equalsIgnoreCase("Leave")){ 
       System.out.println("You get outside of the car. It's dark and rainy and you have no other choice than to enter the safest place that's closest to you. The house"); 
       System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
       String cont69 = input.nextLine(); 
       System.out.println("Choices:"); 
       System.out.println("'Walk'"); 
       String fat = input.nextLine(); 
       if (fat.equalsIgnoreCase("Walk")){ 
        System.out.println("You begin to walk towards the house. The door creaks open, and you find yourself in a dusty, cold house. The house doens't seem like it has had anybody in it for years, and there is an upstairs and a kitchen."); 
        System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
        String cont2 = input.nextLine(); 
        System.out.println("Choices:"); 
        System.out.println("'Kitchen'"); 
        System.out.println("'Upstairs'"); 
        System.out.println("'Living Room'"); 
        String gimme = input.nextLine(); 
        if (gimme.equalsIgnoreCase("Kitchen")){ 
         System.out.println("You walk into the dark kitchen. You find a light switch to the side, and the room brightens up a tiny bit. You see that the stove is on. Someone is boiling water."); 
         System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
         String gay = input.nextLine(); 
         System.out.println("Choices"); 
         System.out.println("'Run'"); 
         System.out.println("'Stay'"); 
         String utters = input.nextLine(); 
         if (utters.equalsIgnoreCase("Run")){ 
          System.out.println("You run to the door helplessly. You find that the door is locked... From the outside."); 
          System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
          String k = input.nextLine(); 
          System.out.println("Choices:"); 
          System.out.println("'Upstairs'"); 
          System.out.println("'Living Room'"); 
          String kkk = input.nextLine(); 
          if (kkk.equalsIgnoreCase("Upstairs")){ 
           System.out.println("As you start your journey upstairs, the stairs start creaking, and you hear someone. After a while, you convince yourself it was your imagination."); 
           System.out.println("Once you get upstairs, you find a bathroom, and a bedroom."); 
           System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
           String rape = input.nextLine(); 
           System.out.println("Choices:"); 
           System.out.println("'Bathroom'"); 
           System.out.println("'Bedroom'"); 
           String xdd = input.nextLine(); 
           if (xdd.equalsIgnoreCase("Bathroom")){ 
            System.out.println("As you walk into the bathroom, you flip on the light switch. You find a sink, toilet, mirror, and a bath tub with the shower curtains covering it."); 
            System.out.println("You hear water dripping."); 
            System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
            String poop = input.nextLine(); 
            System.out.println("Choices:"); 
            System.out.println("'Investigate'"); 
            System.out.println("'Look in Mirror'"); 
            String gimmedat = input.nextLine(); 
            }else if (xdd.equalsIgnoreCase("Bedroom")){ 
             System.out.println("You walk in to a roomy bedroom. You think to yourself that it must be the master bedroom. You start looking around."); 
             System.out.println("After inspecting the room for a few minutes, you find a bloody knife on the drawer, it's right next to a dead body lying next to you feet."); 
             System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
             String cont0101 = input.nextLine(); 
             System.out.println("Choices:"); 
             System.out.println("'Scream'"); 
             System.out.println("'Examine'"); 
             String ugh = input.nextLine(); 
             if (ugh.equalsIgnoreCase("Examine")){ 

             } 
            } 
           } 
          }else if (utters.equalsIgnoreCase("Stay")){ 
           System.out.println("You walk toward the boiling pot of water. Inside, you see someone, but not yourself..."); 
           System.out.println("You turn around, but not quick enough to evade the knife that is being hurled at you. It sticks inside your heart."); 
           System.out.println("You start to slowly die, watching the creature above you stare down into your eyes."); 
           System.out.println("YOU LOSE. . ."); 
           return; 
          } 
         }else if (gimme.equalsIgnoreCase("Upstairs")){ 
          System.out.println("As you start your journey upstairs, the stairs start creaking, and you hear someone. After a while, you convince yourself it was your imagination."); 
          System.out.println("Once you get upstairs, you find a bathroom, and a bedroom."); 
          System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
          String rape = input.nextLine(); 
          System.out.println("Choices:"); 
          System.out.println("'Bathroom'"); 
          System.out.println("'Bedroom'"); 
          String xdd = input.nextLine(); 
          if (xdd.equalsIgnoreCase("Bathroom")){ 
           System.out.println("As you walk into the bathroom, you flip on the light switch. You find a sink, toilet, mirror, and a bath tub with the shower curtains covering it."); 
           System.out.println("You hear water dripping."); 
           System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
           String poop = input.nextLine(); 
           System.out.println("Choices:"); 
           System.out.println("'Investigate'"); 
           System.out.println("'Look in Mirror'"); 
           String gimmedat = input.nextLine(); 
           } 
        }else if (gimme.equalsIgnoreCase("Living Room")){ 
         System.out.println("You walk into the small, but somehow spacious living room. There is a television, ceiling fan, record player, couches, and other living room items."); 
         System.out.println("As soon as you walk into the living room, the telvesion turns on to a news channel, and the ceiling fan starts spinning."); 
         System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
         String dick = input.nextLine(); 
         System.out.println("Choices:"); 
         System.out.println("'Run'"); 
         System.out.println("'Investigate'"); 
         String meme = input.nextLine(); 
         if (meme.equalsIgnoreCase("Investigate")){ 
          System.out.println("You circle around the living room. You find a cassette tape inside the telveison. You hear something behind you."); 
          System.out.println("You turn around, but no one's there. You turn back around and the cassette tape is gone."); 
          System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
          String gutknife = input.nextLine(); 
          System.out.println("Choices:"); 
          System.out.println("'Upstairs'"); 
          System.out.println("'Kitchen'"); 
          String search = input.nextLine(); 
          if (search.equalsIgnoreCase("Kitchen")){ 
           System.out.println("You walk into the dark kitchen. You find a light switch to the side, and the room brightens up a tiny bit. You see that the stove is on. Someone is boiling water."); 
           System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
           String gay = input.nextLine(); 
           System.out.println("Choices"); 
           System.out.println("'Run'"); 
           System.out.println("'Stay'"); 
           String utters = input.nextLine(); 
           if (utters.equalsIgnoreCase("Run")){ 
            System.out.println("You run to the door helplessly. You find that the door is locked... From the outside."); 
            System.out.println("PRESS ANY KEY TO CONTINUE. . ."); 
            String k = input.nextLine(); 
            System.out.println("Choices:"); 
            System.out.println("'Upstairs'"); 
            String kkk = input.nextLine(); 
            }else if (utters.equalsIgnoreCase("Stay")){ 
             System.out.println("You walk toward the boiling pot of water. Inside, you see someone, but not yourself..."); 
             System.out.println("You turn around, but not quick enough to evade the knife that is being hurled at you. It sticks inside your heart."); 
             System.out.println("You start to slowly die, watching the creature above you stare down into your eyes."); 
             System.out.println("YOU LOSE. . ."); 
             return; 
            } 
           } 
          } 
         } 
        } 
       } 
      } 
    } 
} 
+0

리팩토링 개념을 살펴 봅니다. 대부분의 게임은 어떤 유형의 루프에서도 수행됩니다. while 루프가 좋을지도 모릅니다. –

+1

이 질문은 topic : http://codereview.stackexchange.com/ –

+0

에 나와 있습니다. 대부분의 게임은 while 루프와 같은 루프에서 처리되지만 초보자이며 하나 만드는 방법을 모르겠습니다. – Sve

답변

3

여기에있는 내용은 기본적으로 state machine입니다. 당신은 무언가를 인쇄 한 다음 몇 가지 선택을 제시합니다. 사용자는 이러한 선택 사항 중 하나를 입력하면 다른 "상태"(즉, 다른 장소)로 이동하여 무언가를 인쇄하고 선택 사항을 제시하고 새로운 상태로 이동합니다.

이런 상태의 것을 나타냅니다

class State { 
    String message; 
    Map<String, State> choices = new LinkedHashMap<>(); 
} 

(왼쪽 의도적으로 노출, 선명도)

와 같은 일부 주를 구성 :

State start = new State(); 
start.message = "You find yourself in a car..."; 

State leaveTheCar = new State(); 
leaveTheCar.message = "You get outside of the car..."; 

State openGlovebox = new State(); 
openGlovebox.message = "You find a note..."; 

State dead = new State(); 
dead.message = "You're dead."; 

을 그리고 벗어나서 마치 상태를 가입 이 같은 그녀 : 그래서

start.choices.put("Leave", leaveTheCar); 
start.choices.put("Open glovebox", openGlovebox); 

leaveTheCar.choices.put("Get back in car", start); 
leaveTheCar.choices.put("Blah", dead); 

openGlovebox.choices.put("Bloo", dead); 

, 당신은 시작에있을 때, 당신은 선택 "남겨"할 수있는 말을하는지, 당신은 "차를두고"상태 또는 "열기로 이동합니다 글로브 박스 "상태; 차 안에서 돌아올 수도 있고, 죽어가는 결과를 가져올 수도 있습니다.

는 선택 당신은 "노드"상태 및 "가장자리"되는 state transition diagram로이 그릴 수

있습니다 :

enter image description here

당신이 State 인스턴스의 부하를 만들어야하고 위의 게임을 위해 함께 참여하십시오. 당신은하지만, 일단 "재생"할 수있는 논리는 매우 간단합니다 State design pattern을 확인,이를 구현하기위한 관련하지만 약간 다른 방법을

State state = start; 
while (true) { 
    System.out.println(state.message); 
    if (state.choices.isEmpty()) { 
    break; // No choices: game over. 
    } 

    while (true) { // Keep going until user enters a valid choice. 
    System.out.println("Choices:"); 
    for (String key : state.choices.keySet()) { 
     System.out.println("'%s'%n", key); 
    } 
    String choice = input.nextLine(); 
    if (state.choices.containsKey(choice)) { 
     state = state.choices.get(choice); // Go to the next state. 
     break; 
    } 

    System.out.println("Choose again!"); 
    } 
} 

.

+0

본질적으로 주제를 벗어난 질문에 대한 답변이 많습니다 (+1). [상태 다이어그램] (https://en.wikipedia.org/wiki/State_diagram) 그리기를 시작할 수 있습니다. –

+0

정보를 제공해 주셔서 감사합니다. 확실히 상태 다이어그램을 학습 할 것입니다. – Sve