Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Aug 29th, 2007, 11:34 AM   #1
n3o_X
Newbie
 
Join Date: Jan 2005
Posts: 29
Rep Power: 0 n3o_X is on a distinguished road
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
n3o_X is offline   Reply With Quote
Old Aug 29th, 2007, 12:31 PM   #2
Game_Ender
Professional Programmer
 
Game_Ender's Avatar
 
Join Date: May 2006
Location: Maryland, USA
Posts: 306
Rep Power: 3 Game_Ender is on a distinguished road
You never actually increment "dIndex" and "pIndex". Just change "dIndex" to "d" and "pIndex" to "p".

cpp Syntax (Toggle Plain Text)
  1. for (d = 0; d < 52; d++)
  2. {
  3. for(p = 0; p < 4; p++)
  4. {
  5. ...
  6. }
  7. }
__________________
Robotics @ Maryland AUV Team - Software Lead
Game_Ender is offline   Reply With Quote
Old Aug 29th, 2007, 12:38 PM   #3
n3o_X
Newbie
 
Join Date: Jan 2005
Posts: 29
Rep Power: 0 n3o_X is on a distinguished road
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;
		}
	}
}
n3o_X is offline   Reply With Quote
Old Aug 29th, 2007, 1:45 PM   #4
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,623
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
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.
big_k105 is offline   Reply With Quote
Old Aug 29th, 2007, 1:55 PM   #5
n3o_X
Newbie
 
Join Date: Jan 2005
Posts: 29
Rep Power: 0 n3o_X is on a distinguished road
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
n3o_X is offline   Reply With Quote
Old Aug 29th, 2007, 2:03 PM   #6
big_k105
PFO Founder

 
big_k105's Avatar
 
Join Date: Mar 2004
Location: Fargo, ND
Posts: 1,623
Rep Power: 10 big_k105 is on a distinguished road
Send a message via AIM to big_k105 Send a message via MSN to big_k105 Send a message via Yahoo to big_k105
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.
big_k105 is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:29 AM.

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