-2

I am doing a simple text based OOP java game. I am a bit stuck at the moment and hoping for a help

Here is the code

    while(true){

        if(question==1){
            String input1 = inputString("You have entered the Dungeon, here you find a " + t.getDesc() + " would you like to fight it? Type Yes/No");
        chooseDungeonbattle(input1);    


        }
          if(question == 2){

       String input2 = inputString("You have entered the Underworld, it is dark and creepy, but you are not scared...\n SNAP! There is a " + d.getDesc() + " do you wish to fight it?");
      chooseUnderworldBattle(input2);



        }else if(question == 3){
       askUser("You have entered the Dark Forest and you met the Evil Elf, do you wish to fight it? ");
       String input3 = inputString("You have entered the Underworld, here you find a " + t.getDesc() + " would you like to fight it? Type Yes/No");
       //chooseAbattle3(input3);


        }
   //If user choose to go to Dungeon



          public static void chooseDungeonbattle(String questionInput){
        if(questionInput.equals("Yes")||questionInput.equals("yes")){
            Battle b = new Battle();
            Troll enemy = new Troll(10);
            MainPlayer main = new MainPlayer( 100, 100,150,0 );
            BadPlayer p = new BadPlayer("",0,0,0);

            int goldReceivedDungeon;



          b.getOutcome(enemy, main);

         if(b.getOutcome(enemy, main).equals("You win")){
        System.out.println("You win and receive " + enemy.awardCarried());
        goldReceivedDungeon=main.getGold()+ enemy.awardCarried();
        System.out.println("You currently have " + enemy.awardCarried());

        }else{
        System.out.println("Sorry you lost");
        }

       }



        }


        public static void chooseUnderworldBattle(String questionInput){
        if(questionInput.equals("Yes")||questionInput.equals("yes")){
            Battle b = new Battle();
            Demon enemy = new Demon(20);
            MainPlayer main = new MainPlayer( 100, 100,150,0 );
            String asknext;
            int goldReceivedDungeon;



          b.getOutcome(enemy, main);

         if(b.getOutcome(enemy, main).equals("You win")){
        System.out.println("You win and receive " + enemy.awardCarried());
        goldReceivedDungeon=main.getGold()+ enemy.awardCarried();
        asknext = inputString("Would you like to move to you next quest? Press 1 for the Dungeon or 3 for The Dark forest");


        }else{
        System.out.println("Sorry you lost");
        }

       }



        }
public static void chooseForestBattle(String questionInput){
    if(questionInput.equals("Yes")||questionInput.equals("yes")){
        Battle b = new Battle();
        Elf enemy = new Elf(20);
        MainPlayer main = new MainPlayer( 100, 100,150,0 );

        int goldReceivedDungeon;



      b.getOutcome(enemy, main);

     if(b.getOutcome(enemy, main).equals("You win")){
    System.out.println("You win and receive " + enemy.awardCarried());
    goldReceivedDungeon=main.getGold()+ enemy.awardCarried();
    asknext = inputString("Would you like to move to you next quest? Press 1 for the Dungeon or 3 for The Dark forest");


    }else{
    System.out.println("Sorry you lost");
    }

   }





    }

What I am trying to do is a main Character to input a place where they want to go first (Dungeon, Underworld or Dark forest), If they choose to go to the underworld first and win there, to input where they want to go after that - Dungeon or Dark Forest. And that is all the way until the quests are finished and they collect reward from each quest. I was trying with a while loop, but just loops the same method unfortunately :( Any help is greatly appreciated :)

Demiah
  • 41
  • 8
  • You need to save progress(state), store it in some kind of container e.g. std::vector, then add or remove the things that was done by the player, make that container scope be accessible for all of your functions, then loop throughout your container for the remaining options. – Yan.F Apr 11 '20 at 17:56
  • Please add also method wrapping your while loop. – Sabina Orazem Apr 11 '20 at 17:59

2 Answers2

0

question variable is never assigned a new value, so after exiting one "world" you would have to update question with a new value. You loop is infinite, you need a break or return to exit the loop.

Sabina Orazem
  • 477
  • 4
  • 12
-1

@Yani if loops should be if-else ladder for better performance and the question variable should be reassigned with a new value to get to the other battles i.e. asknext should be used in every method i.e. in chooseDungeonBattle too. And in case of ending your quest you must Break the while loop after your last else to end the infinite loop.

ODEVIL
  • 1
  • 2