Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C++ (http://www.programmingforums.org/forum15.html)
-   -   C++ Getting stuck within a loop. (http://www.programmingforums.org/showthread.php?t=13866)

n3o_X Aug 29th, 2007 12:34 PM

C++ Getting stuck within a loop.
 
So I took a break from programming for a few months and am back at it. I am attempting to make a function that will shuffle a deck of cards (52) to 4 players. I have setup a 2d array (myDeal[4][52]) and set all values to 0 for the time being. I select the random card by using rand()%52. If the value is available for the current player, ie myDeal[1][22] I am setting the value to 1 to show that it is there. The problem I am having is that my code seems to be getting stuck within a loop for no reason that I can see, but like I said I am a bit rusty on my programming currently.

:

void myDeal()
{
            int myDeal[4][52] = {0,0};
        int p = 0;  // Player Index
        int d = 0; // Deck Index
        int card;                // Card Number (0-52)

        // Seed Random Generator
        srand((unsigned)time(NULL));

        for (d = 0; dIndex < 52; d++)
        {
                for(p = 0; pIndex < 4; p++)
                {

                        bool result;

                        do
                        {
                                card = rand()%52;

                                if (myDeal[0][card] == 1)
                                        result = false;
                                else if (myDeal[1][card] == 1)
                                        result = false;
                                else if (myDeal[2][card] == 1)
                                        result = false;
                                else if (myDeal[3][card] == 1)
                                        result = false;
                                else
                                        result = true;

                        }while (result == false);

                        myDeal[p][card] = 1;
                }
        }
}


I know I could use just one If, but since it has been giving me problems, I have broken it up for now.

It's just driving me insane, so any little help you can give is much appreciated.

Thanks

Game_Ender Aug 29th, 2007 1:31 PM

You never actually increment "dIndex" and "pIndex". Just change "dIndex" to "d" and "pIndex" to "p".

:

  1. for (d = 0; d < 52; d++)
  2.         {
  3.                 for(p = 0; p < 4; p++)
  4.                 {
  5.                         ...
  6.                 }
  7.         }


n3o_X Aug 29th, 2007 1:38 PM

Ok, that was my bad I was editing the code for web is all and missed that. In my code the variables are the same. The problem still occurs when the variables are correct, sorry for the messup on my end with posting the code.
Here is the code again with the typo fixed.

:

void myDeal()
{
            int myDeal[4][52] = {0,0};
        int p = 0;  // Player Index
        int d = 0; // Deck Index
        int card;                // Card Number (0-52)

        // Seed Random Generator
        srand((unsigned)time(NULL));

        for (d = 0; d < 52; d++)
        {
                for(p = 0; p < 4; p++)
                {

                        bool result;

                        do
                        {
                                card = rand()%52;

                                if (myDeal[0][card] == 1)
                                        result = false;
                                else if (myDeal[1][card] == 1)
                                        result = false;
                                else if (myDeal[2][card] == 1)
                                        result = false;
                                else if (myDeal[3][card] == 1)
                                        result = false;
                                else
                                        result = true;

                        }while (result == false);

                        myDeal[p][card] = 1;
                }
        }
}


big_k105 Aug 29th, 2007 2:45 PM

I think your problem is the fact that by the time you hit 13 you have dealt all your cards out, you have to remember that there are only 52 cards. So if for each time through the parent for loop you are dealing 4 cards by the time you hit 13 you have dealt out all 52 cards, so you will no longer have a free card to deal out. So if you change if from 52 to 13 you will finish the loop and deal all 52 cards out to the 4 players. :)

edit: Currently your function wants to deal out 208 cards, just wanted to point that out. ;)

n3o_X Aug 29th, 2007 2:55 PM

Ahh!! I got so stuck up on the fact it was a problem with my syntax or an extra loop or something I didn't think about my logic! Thank you for that observation and it is workin once again.

Again, thank you for helping me with such a stupid mistake, I will make sure to double check my logic next time.

Thanks

big_k105 Aug 29th, 2007 3:03 PM

Yeah, I actually spent more time on it, but then I compiled it and printed out the stuff and it was getting stuck in the 14th loop or when d was 13 so thats how I figured it out.


All times are GMT -5. The time now is 3:00 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC