DragonCoder DragonCoder - 6 months ago 11
Java Question

My program does one extra loop, how to fix?

I am programming a card game of higher lower. The goal is to guess whether the second card will be bigger or smaller than the first one. The program works fine except one problem. In the game the player starts with 5 credits. Each guess he earns one credit up to 10 or if he did not guess he loses 1 credit, down to 0.
At 0 or 10, he is supposed to get a You Win ! or You Lose ! message, which he does, but the problem is the program does one extra draw anyway afterwards.

I used a while loop first, then I tried a do while loop, but the issue is the same.

Here is the main method :

public static void main(String[] args)
{
HigherLower h = new HigherLower();

boolean higher; // higher = true; lower = false

Draw draw = new Draw(); Deck deck = new Deck();
Card card1; Card card2;

int credits = 5;

boolean playing = true;

while(playing)
{
if(credits == 0)
{
System.out.println("\nYou lose !");
playing = false;
}
else if(credits == 10)
{
System.out.println("\nYou win !");
playing = false;
}

System.out.println("\n\n\n\n NEW DRAW : ");
card1 = draw.drawCard(deck);

System.out.print(card1.getName());

boolean choice = h.getRightInput();

card2 = draw.drawCard(deck);

if((card2.getValue() > card1.getValue() && choice == true)
|| (card2.getValue() < card1.getValue() && choice == false))
{
credits++;
System.out.printf("Second card is %s", card2.getName());
System.out.printf("\n\nYou guessed right ! "
+ "Now you have %d credits", credits);
}
else
{
credits--;
System.out.printf("Second card is %s", card2.getName());
System.out.printf("\n\nYou guessed wrong ! "
+ "Now you have %d credits", credits);
}




}


}


Here are the other classes , I will just pile them next to each other :

public class Card
{
String name;
int value;

Card(String name, int value)
{
this.name = name;
this.value = value;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}

public class Deck
{
Card Joker = new Card("Joker", 1);
Card Two = new Card("2", 2);
Card Three = new Card("3", 3);
Card Four = new Card("4", 4);
Card Five = new Card("5", 5);
Card Six = new Card("6", 6);
Card Seven = new Card("7", 7);
Card Eight = new Card("8", 8);
Card Nine = new Card("9", 9);
Card Ten = new Card("10", 10);
Card Jack = new Card("J", 11);
Card Queen = new Card("Q", 12);
Card King = new Card("K", 13);
Card Ace = new Card("A", 14);

Card[] deck = new Card[]{Joker, Two, Three, Four, Five,
Six, Seven, Eight, Nine, Ten,
Jack, Queen, King, Ace};

}


import java.util.Random;

public class Draw
{

Random random = new Random();

public Card drawCard(Deck deck)
{
int maxNumber = deck.deck.length ;

int draw = random.nextInt(maxNumber);

Card drawedCard = deck.deck[draw];

return drawedCard;

}


}

public class HigherLower
{

Scanner sc = new Scanner(System.in);

public boolean getRightInput()
{
while(true)
{
System.out.println("\n (H)igher or (L)ower ? ");

String s = sc.next();
char c = s.charAt(0);

if(c == 'H' || c == 'h')
{
return true;
}
else if(c == 'L' || c == 'l')
{
return false;
}
else
{
System.out.println("Give valid input ! H or L");
}

}

}
}

Answer

A player starts the game with 5 credits, meaning the game won't end the moment it starts. You should flip the parts of your loop around - first perform a drawing and then check if the game ended:

 while (playing) {
     // Code to do another drawing

     if (credits == 0) {
         System.out.println("\nYou lose !");
         playing = false;
     } else if (credits == 10) {
         System.out.println("\nYou win !");
         playing = false;
     }
}
Comments