Home > Software design >  i just did switch-case in a switch-case... in a switch-case. There maybe be a better way to do it ri
i just did switch-case in a switch-case... in a switch-case. There maybe be a better way to do it ri

Time:12-18

import java.io.*;
import java.util.Scanner;

public class MyGame {
  public static void main(String argv[]) throws IOException {
    
    String prompt;
    
    Scanner scanner = new Scanner(System. in); 
    
//TITLE ASCII text art 


    do {
      
      BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
      System.out.print("What is your name?: ");
      String name = reader.readLine();
      System.out.println("");
      System.out.println("Hello, comrade "   name   ". Do you want to be a rebel?");
      System.out.println("");
      
      int i ;
      System.out.println("[1] Yes, i want to fight for Freedom! ");
      System.out.println("[2] No, i want to live oppressed.");
      i = Integer.parseInt(reader.readLine());
      
      switch (i) {
        case  1: 
              System.out.println("");
          System.out.println("/// 00:13 ///");
          System.out.println(">> You go to a abandoned industrial building.");
          System.out.println(">> You go inside, because you think there is the man you need to contact.");
 // ASCII text art
          System.out.println("");
          System.out.println(">> A man comes to you. He is young, dressed in an uniform.");
          System.out.println("???: Here");
          System.out.println(">> He passes you an military uniform.");
          System.out.println("Grogor: My name is Grigor. You should be " name "?");
          
          int n ;
          System.out.println("[1] Yes");
          System.out.println("[2] No");
          n = Integer.parseInt(reader.readLine());
          
          switch (n) {
            case  1: 
                  System.out.println("Grigor: So you are the right one!");
                  System.out.println("Grigor: You have your first mission. You need to get documents for me.");
              System.out.println("Grigor: The documents are in the mayor's bureau. You need to infiltrate there in the night.");
              System.out.println("Grigor: We think that there are in Room B102. The documents are about the payments of the city for 12.1989");
              System.out.println("Grigor: I wish you luck! Don't forget... GOLDEN RING");
              System.out.println("");
              System.out.println(">> You are ready to do the job. It will be dangerous.");
              System.out.println("/// 23:46 /// on the next day.");
              System.out.println(">> You stand in front of the political bureau of city.");
              System.out.println("");
// ASCII text art
              System.out.println(">> You break in the building. Sweat runs down your back. It's cold");
              System.out.println(">> You go to room B102...");
              System.out.println(">> It's locked");
              System.out.println(">> What do you do now?");
              System.out.println("");
          int lock ;
          System.out.println("[1] Try to break in with force. You kick the door.");
          System.out.println("[2] Look around for a key.");
          System.out.println("");
          lock = Integer.parseInt(reader.readLine());
              
              switch (lock) {
                case  1: 
                  System.out.println(">> You kick the door.");
                  System.out.println(">> The sound echoes in the corridors.");
                  System.out.println(">> You enter Room B102. You see a window in front of you. Outside you see a block of flats");
                  System.out.println(">> On one of the balkons is a man smoking. Does he heared you?");
                  System.out.println(">> You search for the documents in the desk. Not long after you hear steps in the corridor.");
                  System.out.println(">> You panic and try to open the window and jump outside. Someone is coming!");
                  System.out.println("Police Officer: STAY RIGHT THERE! YOU ARE ARRESTED, BASTER!");
                  System.out.println("");
                  System.out.println("");
                  
                  System.out.println(">> They send you to a prison camp. ");
               
               System.out.println(">> You are hungry. You need to work. You feel sick.");
// ASCII text art
        System.out.println("");
                  break;
                case  2: 
                  System.out.println("");
                  System.out.println(">> You look around. You see down the dark corridor the staff room.");
                  System.out.println(">> The door is open. You enter the room. There are keys hanging on the wall.");
                  System.out.println(">> One of them has B102 writen on.");
                  System.out.println(">> You unlock Room B102");
                  System.out.println(">> You enter and see a desk on the right, windows in front, on the wall is picture of the prime minister. ");
                  System.out.println(">> On the desk are scraps of paper");
                  System.out.println(">> Next to it a cabinet with folders. You search for the documents.");
                  System.out.println(">> You need to choose, which folder you need? Titles: ");
                  System.out.println("");
                  int doc ;
          System.out.println("[1] Budget - City GOBROVSKA XXII.1989");                     //RIGHT ONES
          System.out.println("[2] Deposits and Withdrawals - City GOBROVSKA XXI.1989");    //FALSE DOCS
          System.out.println("[3] Budget - Cityhall Project Nr. 423984");                  //FALSE DOCS
          System.out.println("");
          doc = Integer.parseInt(reader.readLine());
                  switch (doc) {
                    case  1:                               //RICHTIGE
                          System.out.println("doc 1");
                      System.out.println(">> You get the document and run away.");
                      System.out.println(">> You walt to your home as fast as possible. You feel your breath in the throat");
                          System.out.println(">> You enter the elevator of your block of flats. You throw the folder on the floor. You go to sleep with your clothes on.");
                          System.out.println(" ");
                      System.out.println(" ");
                      System.out.println(" ");
                      System.out.println("/// 09:26 ///");
                      System.out.println(">> Knocking on the door.");
                      System.out.println(">> Who can it be? DID THEY CATCH YOU?");
                      System.out.println("You: Who is there?");
                      System.out.println(name   " It's me. You remember... GOLDEN RING" );
                      System.out.println("");
                      System.out.println(">> You open the door.");
                      System.out.println("");
// ASCII text art
                      System.out.println("");
                      System.out.println("Grigor: It's me "  name  ". Do you have the documents?");
                      System.out.println(">> You pass Grigor the documents.");    // RIGHT DOCS
                      System.out.println("Grigor: ...");
                      System.out.println("Grigor: That are the right one.");
                      System.out.println("");
                      System.out.println(">> Grigor pulls out a lighter. He sets the Folder in flames");
                      System.out.println("Grigor: We don't need evidence, don't we?");
                      System.out.println("I will be honest with you... we actually didn't need these documents. Let's say it was a test");
                      System.out.println("Ah don't look at me like that. We just need to know if you are spy or not.");
                      System.out.println("You need that one. Wait for next instructions... And get this...");
                      int gun ;
          System.out.println("[1] You strech your hand...");                     //RIGHT DOCS
          System.out.println("[2] You look at him...");   
          System.out.println("");
          gun = Integer.parseInt(reader.readLine());
                      System.out.println(">> Grigor passes you a handgun");
                      System.out.println(">> He laughs at you, while pushing the button of the elevator. He has one teeth missing.");
                      System.out.println("I needed to kill you with that one... hahaha welcome in small one.");
                      System.out.println(">> He lefts. You look at the gun in your hand");
                      
                      System.out.println("");                                                                      
                    
                    
                      break;
                    case  2: 
                          System.out.println(">> You get the document and run away.");
                      System.out.println(">> You walt to your home as fast as possible. You feel your breath in the throat");
                          System.out.println(">> You enter the elevator of your block of flats. You throw the folder on the floor. You go to sleep with your clothes on.");
                          System.out.println(" ");
                      System.out.println(" ");
                      System.out.println(" ");
                      System.out.println("/// 09:26 ///");
                      System.out.println(">> Knocking on the door.");
                      System.out.println(">> Who can it be? DID THEY CATCH YOU?");
                      System.out.println("You: Who is there?");
                      System.out.println(name   " It's me. You remember... GOLDEN RING" );
                      System.out.println("");
                      System.out.println(">> You open the door.");
                      System.out.println("");
                      System.out.println("");
// ASCII text art
                      System.out.println("");
                      System.out.println("Grigor: It's me "  name  ". Do you have the documents?");
                      System.out.println(">> You pass Grigor the documents.");
                      System.out.println("Grigor: Deposits and Withdrawals - City GOBROVSKA ... The Date is XXI.1989.");
                      System.out.println("Grigor: WE NEEDED THE ONE FOR 12.1989, NOT FOR 11.");
                      System.out.println("Grigor: ARE YOU A SPY YOU BASTERED!? DO YOU WANT TO SABOTAGE US?");
                      System.out.println("");
                      int grigor ;
          System.out.println("[1] >> NO. IT WAS A FAILURE!...");
          System.out.println("[2] >> Stay in silence");
          System.out.println("");
          grigor = Integer.parseInt(reader.readLine());
        switch (grigor) {
          case  1: 
                  System.out.println("");
                      System.out.println(">> GRIGOR PULLS OUT A GUN");
        // ASCII text art
            break;
          default: 
                  System.out.println("");
                      System.out.println(">> GRIGOR PULLS OUT A GUN");
       // ASCII text art
        System.out.println("");
        } // end of switch
                      
                      break;
                    case  3: 
                        System.out.println(">> You get the document and run away.");
                      System.out.println(">> You walt to your home as fast as possible. You feel your breath in the throat");
                          System.out.println(">> You enter the elevator of your block of flats. You throw the folder on the floor. You go to sleep with your clothes on.");
                          System.out.println(" ");
                      System.out.println(" ");
                      System.out.println(" ");
                      System.out.println("/// 09:26 ///");
                      System.out.println(">> Knocking on the door.");
                      System.out.println(">> Who can it be? DID THEY CATCH YOU?");
                      System.out.println("You: Who is there?");
                      System.out.println(name   " It's me. You remember... GOLDEN RING" );
                      System.out.println("");
                      System.out.println(">> You open the door.");
                      System.out.println("");
                      System.out.println("");
// ASCII text art
                      System.out.println("");
                      System.out.println("Grigor: It's me "  name  ". Do you have the documents?");
                      System.out.println(">> You pass Grigor the documents.");
                      System.out.println("Grigor: WE NEEDED THE BUDGET OF THE CITY NOT FOR ONE PROJECT");
                      System.out.println("Grigor: ARE YOU A SPY YOU BASTERED!? DO YOU WANT TO SABOTAGE US?");
                      System.out.println("");
          int grigorr ;
          System.out.println("[1] >> NO. IT WAS A FAILURE!...");
          System.out.println("[2] >> Stay in silence");
          System.out.println("");
          grigorr = Integer.parseInt(reader.readLine());
        switch (grigorr) {
          case  1: 
                  System.out.println("");
                      System.out.println(">> GRIGOR PULLS OUT A GUN");
        /ASCII text art
            break;
          default: 
                  System.out.println("");
                      System.out.println(">> GRIGOR PULLS OUT A GUN");
       // ASCII text art
        System.out.println("");
        } // end of switch
                      break;
                    default: 
              System.out.println(">> You didn't deside right. Only [1,2 or 3] is possible. You leave...");
                  System.out.println(">> You failed the mission.");
                  System.out.println("");
               System.out.println(">> You live your live.");
               System.out.println(">> One day the secret police comes to your house.");
               System.out.println(">> They send you to a prison camp. ");
               System.out.println(">> You found out that your neighbor called on you.");
               System.out.println(">> You are hungry. You need to work. You feel sick.");
        //ASCII text art
                  } // end of switch
                  break;
                default: 
                  System.out.println(">> You didn't deside right. Only [1 or 2] is possible. You leave");
                  System.out.println(">> You failed the mission.");
                  System.out.println("");
               System.out.println(">> You live your live.");
               System.out.println(">> One day the secret police comes to your house.");
               System.out.println(">> They send you to a prison camp. ");
               System.out.println(">> You found out that your neighbor called on you.");
               System.out.println(">> You are hungry. You need to work. You feel sick.");
        // ASCII text art
        System.out.println("");
                  
              } // end of switch
           
              break;
            
            default: 
                  System.out.println("Grigor: You are not the one we need. What are you doing here than? Go away.");
                  System.out.println(">> You are send outside.");
                  System.out.println("");
               System.out.println(">> You live your live like always.");
               System.out.println(">> One day the secret police comes to your house.");
               System.out.println(">> They send you to a prison camp. ");
               System.out.println(">> You found out that your neighbor called on you.");
               System.out.println(">> You are hungry. You need to work. You feel sick.");
        // ASCII text art
        System.out.println("");

          } // end of switch
          
        
        
          break;
        case  2: 
               System.out.println("");
               System.out.println(">> You live your live.");
               System.out.println(">> One day the secret police comes to your house.");
               System.out.println(">> They send you to a prison camp. ");
               System.out.println(">> You found out that your neighbor called on you.");
               System.out.println(">> You are hungry. You need to work. You feel sick.");
        // ASCII text art


          break;
        default: 
               System.out.println("ERROR. Only 1 or 2 are possible!");
      } // end of switch
      
      System.out.println(name   " , Do you want to play one more time? If Yes tipp [1] and ENTER, if not [0]");
      prompt = scanner.next().toUpperCase();;    
    }while(prompt.equals("1")); 
  }
}

The thing is, I'm not sure how to do it right... it will be also awsome, if after a false answer you could just loop to the question youve answered wrong and not loop to the beginning. First i thought hmmm i could make some more do-while loops, but won't be overkill?

I am total nood and the whole code is a mess.your text

CodePudding user response:

I am having difficulties in reading the code but I believe you are using multiple nested switch statements to create a game.

This approach can work, but it can also make your code difficult to read and maintain, especially if you have many different options and branches (i.e. switch in another switch and on and on.)

I can suggest in order to improve your code, use functions to organize your code into logical blocks.

For example, you could define a function for each action that the player can take, and call that function from the appropriate case in the switch statement. This would make it easier to see the different options available to the player and to modify the behavior of the game. Read more about Functions.

switch (choice) {
        case 1: 
          // Player wants to join the rebellion
          handleJoinRebellion(reader, name);
          break;
        case 2:
          // Player does not want to join the rebellion
          handleDeclineRebellion();
          break;
      }

void handleJoinRebellion(String reader, String name) {
   // Something here
}
void handleDeclineRebellion(input) {
   // Something here
}

You could also consider using an Object-Oriented (OOP) approach, where an object with its own behavior represents each action. This would allow you to encapsulate the behavior of each action in a separate class, which can make your code more modular and easier to understand (Just like what I said, I am having difficulties in reading it).

Correct me if I am wrong. I hope this helps!

  • Related