0

So I am writing a program where somebody draws cards from a deck. So I wrote a while loop that loops through and checks to see if there are over 4 of the randomly created card drawn, and if there is, change the card.

here is my code:

String card = (int)Math.ceil(Math.random() * 13) + " ";
String[] used2 = used.split(" ");
//used is a String like "12 3 7 8 4 ... # etc" such that it is all the previously drawn cards.
boolean checking = true;
boolean isIn = false;
int in = 0;
int check = 0;
while(checking){
    for(int q = 0; q < used2.length; q++){
        check += 1;
        if(card.equals(used2[q] + " ")){
            in += 1;
            if(in == 4){
                System.out.println(check); //debugging line
                check += 1;
                card = (int)Math.ceil(Math.random() * 13) + " ";
                card_val = (int)Math.ceil(Math.random() * 13);
                isIn = true;
                in = 0;
                break;
            }
        }
    }
    if(isIn){
        //will execute if there is 4 of the cards already drawn so the while loop continues with a different card
        checking = true;
    }
    else{
        //breaks out of while loop because the card can be drawn
        checking = false;
    }
}
used += card;

Now this runs, but when I put it inside a for loop and set it to run like 40 times, about 2/3 times it creates an of infinite loop.

I discovered that the infinite loop is only created if the if(in == 4) statement comes out to be true.

Why is this? I have been debugging since last night and I cannot figure this out.

5
  • 1
    A cleaner way of writing your if-else statement: checking = isIn; Commented Sep 23, 2013 at 20:23
  • 1
    Because you set isIn to true in that block, which means checking will never get set to false. Commented Sep 23, 2013 at 20:24
  • Also a comment to (int)Math.ceil(Math.random() * 13) + " ". I suspect your reason for " " is to convert the int to String, however you can only do +"" which makes the code simpler afterwards.. Commented Sep 23, 2013 at 20:27
  • @KubaSpatny: I am not sure what you mean by that Commented Sep 23, 2013 at 20:28
  • @RyanSaxe well what is the reason for the +" "? Commented Sep 23, 2013 at 20:29

2 Answers 2

6

Once you set isIn to true, you never set it back to false. So, the if statement at the bottom will keep setting checking to true, resulting in an infinite loop.

Set isIn to false at the beginning of the while loop.

while(checking){
    isIn = false;  // Add this line.
    for(int q = 0; q < used2.length; q++){
Sign up to request clarification or add additional context in comments.

Comments

0

Seems like a complex algorithm for a simple task.

Why not change the implementation to use simple subtraction? Initially allocate a List of all 52 values, then as you "draw" cards out of the deck, remove it from the list.

Then you can use Math.random() * list.size() to get an appropriately-ranged index?

1 Comment

I haven't learned lists yet, this is a hw problem, we haven't even learned arrays, I've just known the split method so I used it to make manipulating the string easier

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.