Programming Forums
User Name Password Register
 

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

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
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
 

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 12:56 AM.

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