SeanHub SeanHub - 19 days ago 7
Java Question

Fixing my loop and needing assistance on Input Checker, Java

so recently i posted a question asking about my game of Higher and Lower, it was all over the place and just not working, I've taken another approach at this game, and it's working, with just a minor issue this time, it's easier to show than explain so, here is my code:

String noReplay = "\nThank you for playing.";
String highLow = "(H)igher or (L)ower: ";
String loseHigh = "Card is higher, you lose. Would you like to play again? Y/N: ";
String loseLow = "Card is lower, you lose. Would you like to play again? Y/N: ";

String playAgain;

boolean replay = false;

Random rand = new Random();
Scanner scan = new Scanner(System.in);

do {

int card1 = rand.nextInt(13) + 1;
int card2 = rand.nextInt(13) + 1;

System.out.println("Card is: " + card1);
System.out.printf(highLow);
String guess = scan.nextLine().toUpperCase();

if(card2 > card1 && guess.equalsIgnoreCase("H")) {
System.out.println("Card is: " + card2);
System.out.printf(highLow);
replay = true;
guess = scan.nextLine().toUpperCase();

}
else if(card2 > card1 && guess.equalsIgnoreCase("L")) {
System.out.println("Card is: " + card2);
System.out.printf(loseHigh);
playAgain = scan.nextLine().toUpperCase();
if(playAgain.equalsIgnoreCase("Y")) {
replay = true;
}
else if(playAgain.equalsIgnoreCase("N")) {
replay = false;
System.out.println(noReplay);
}
}
else if(card2 < card1 && guess.equalsIgnoreCase("H")) {
System.out.println("Card is: " + card2);
System.out.printf(loseLow);
playAgain = scan.nextLine().toUpperCase();
if(playAgain.equalsIgnoreCase("Y")) {
replay = true;
}
if(playAgain.equalsIgnoreCase("N")) {
replay = false;
System.out.println(noReplay);
}
}
else if(card2 < card1 && guess.equalsIgnoreCase("L")) {
System.out.println("Card is: " + card2);
System.out.printf(highLow);
replay = true;
guess = scan.nextLine().toUpperCase();
}

} while(replay != false);


The aim of my project here is to create a simple game, that when the user is to enter Higher or Lower to the card number above, the program will respond with the correct answer, either replaying the game from that stage, or asking if the user would like to play again as they failed.

This works fine, however from my output :

Card is: 9
(H)igher or (L)ower: h
Card is: 11
(H)igher or (L)ower: l
*Card is: 6*
*(H)igher or (L)ower: l*
*Card is: 7*
Card is higher, you lose. Would you like to play again? Y/N: y
*Card is: 13*
*(H)igher or (L)ower: h*
*Card is: 10*
(H)igher or (L)ower:


We can see on the starred lines, it isn't doing what it should be doing.
When i say higher, if the number is lower it should ask whether i would like to play again because i got it wrong. In this case it doesnt?

So that is my issue ^

Another thing whilst im at it, i'd like to ask for assistance with Input Checker, i only want the user to be able to input Y, N, H, L (Ive sorted the lower or upper case problem out).

Thank you for reading!

Answer

This should hopefully make sense and help to lessen the amount of complexity of the game. Take a look at my comparison for the card1 vs. card2, you want your guess to be based on what the card2 is compared to card1 and not the other way around. if that makes sense. Hope this helps!

     String highLow = "(H)igher or (L)ower: ";
     String loseHigh = "Card is higher, you lose.";
     String loseLow = "Card is lower, you lose.";
     String loseMessageDetails = "";
     boolean gameLost = false;
     boolean askAgain = true;
     Scanner scan = new Scanner(System.in);
     String guess = "";
     Random rand = new Random();

     int card1;
     int card2;

     while(askAgain)
     {   
         if(gameLost)
         {
             System.out.println(loseMessageDetails + "\nWould you like to play again? Y/N");
             guess = "";
             while(!guess.equals("Y") && !guess.equals("N"))
             {
                 guess = scan.nextLine().toUpperCase();
             }

             if(guess.equals("Y"))
             {
                 gameLost = false;
                 askAgain = true;
             }
             else
             {
                 askAgain = false;
             }
         }

         if(!gameLost)
         {
             card1 = rand.nextInt(13) + 1;
             card2 = rand.nextInt(13) + 1;
             System.out.println("Card is: " + card1);
             guess = "";
             //loop until correct type of input
             while(!guess.equals("H") && !guess.equals("L"))
             {
                 System.out.printf(highLow);
                 guess = scan.nextLine().toUpperCase();
             }
             //now we either correctly guessed or incorrectly guessed
             //here is a correct guess
             if((card1 < card2 && guess.equalsIgnoreCase("H")) ||
                (card1 > card2 && guess.equalsIgnoreCase("L")))
             {
                 askAgain = true;
             }
             else
             {
                 loseMessageDetails = guess.equalsIgnoreCase("L") ? loseHigh : loseLow;
                 gameLost = true;
             }
         }
     }

This will also ensure only valid input and continue to ask for input until the user enters the correct letters.