![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Jan 2005
Posts: 29
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#2 |
|
Professional Programmer
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3
![]() |
You never actually increment "dIndex" and "pIndex". Just change "dIndex" to "d" and "pIndex" to "p".
cpp Syntax (Toggle Plain Text)
__________________
Robotics @ Maryland AUV Team - Software Lead |
|
|
|
|
|
#3 |
|
Newbie
Join Date: Jan 2005
Posts: 29
Rep Power: 0
![]() |
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;
}
}
} |
|
|
|
|
|
#4 |
|
PFO Founder
![]() ![]() |
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. ![]()
__________________
BIG K aka Kyle Programming Forums Kyle K Online Please do not PM or email me programming questions. Post them in the forums instead. |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Jan 2005
Posts: 29
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 |
|
PFO Founder
![]() ![]() |
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.
__________________
BIG K aka Kyle Programming Forums Kyle K Online Please do not PM or email me programming questions. Post them in the forums instead. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Help in QBASIC (I think it's similar to VB) | phoenix987 | Visual Basic | 3 | May 9th, 2005 12:33 PM |
| Help with a QBASIC program | phoenix987 | Other Programming Languages | 4 | May 5th, 2005 12:27 PM |
| Multiple http-requests are stuck | MereMortal | C++ | 0 | May 4th, 2005 3:08 AM |
| WinSock accept() hangs program in Do loop... | layer | C++ | 5 | Apr 29th, 2005 11:28 AM |
| Timing loop problems | badbasser98 | C++ | 11 | Mar 10th, 2005 8:30 PM |