Search code examples
javaooploopshardcode

Java hardcoded repeating process


I am making a basic hardcoded game that has 2 users that will fight each other. All my methods are set and work as expected. I am now trying to figure out a way after looping through the main hardcode, to give the option to fight again and continue the game, instead of just stopping after 1 fight.

public static void main(String[] args) {

        Scanner in = new Scanner(System.in);
        System.out.print("What is your name: ");
        String myName = in.nextLine();

        Fighter a = new Warrior();
        Fighter b = new Dragon();
        a.pickOpponent(b);

        a.setName(myName);
        b.setName("Onyxia");

        System.out.print(getWelcome());     
        while(in.hasNextLine()) 
        {
            switch(in.nextLine()) 
            {
                case "no":
                    System.out.println("Wow, you are not even gonna try, you have lost!");
                    break;
                case "yes":
                    System.out.println("Let the fight begin! ");
                    while(a.isAlive() && b.isAlive()) 
                    {
                        System.out.println("Do you want to punch, kick, or headbutt the other fighter? ");
                        switch(in.nextLine()) 
                        {
                            case "punch":
                                System.out.println(a.getPunch(b));
                                System.out.println(b.getOpponentAttack(a));
                                break;
                            case "kick":
                                System.out.println(a.getKick(b));
                                System.out.println(b.getOpponentAttack(a));
                                break;
                            case "headbutt":
                                System.out.println(a.getHeadbutt(b));
                                System.out.println(b.getOpponentAttack(a));
                                break;
                            default :
                                System.out.println(invalidInput());
                                break;
                        }
                    }
                default:
                    System.out.println(a.getWinner(b));
                    break;  
            }//end of first switch statement
        }//end of first while loop
    }//end of main   

Solution

  • Try moving all of the code in main into a new function.

    You can put that function into a while loop in your main method, like so:

    while(playGame()) {}
    

    and have playGame() return true if the came should be played again, and false if it should end.


    Sample:

    public static boolean playGame() {
    
            Scanner in = new Scanner(System.in);
            System.out.print("What is your name: ");
            String myName = in.nextLine();
    
            Fighter a = new Warrior();
            Fighter b = new Dragon();
            a.pickOpponent(b);
    
            a.setName(myName);
            b.setName("Onyxia");
    
            System.out.print(getWelcome());     
            while(in.hasNextLine()) 
            {
                switch(in.nextLine()) 
                {
                    case "no":
                        System.out.println("Wow, you are not even gonna try, you have lost!");
                        break;
                    case "yes":
                        System.out.println("Let the fight begin! ");
                        while(a.isAlive() && b.isAlive()) 
                        {
                            System.out.println("Do you want to punch, kick, or headbutt the other fighter? ");
                            switch(in.nextLine()) 
                            {
                                case "punch":
                                    System.out.println(a.getPunch(b));
                                    System.out.println(b.getOpponentAttack(a));
                                    break;
                                case "kick":
                                    System.out.println(a.getKick(b));
                                    System.out.println(b.getOpponentAttack(a));
                                    break;
                                case "headbutt":
                                    System.out.println(a.getHeadbutt(b));
                                    System.out.println(b.getOpponentAttack(a));
                                    break;
                                default :
                                    System.out.println(invalidInput());
                                    break;
                            }
                        }
                    default:
                        System.out.println(a.getWinner(b));
                        break;  
                }//end of first switch statement
            }//end of first while loop
        }//end of playGame  
    
    public static void main(String[] args) {
    
        while(playGame()) {}
    
    }