help with loop

This is a discussion on help with loop within the C++ Programming forums, part of the General Programming Boards category; im trying to write a loop so that 8 of each card in a blackjack game is made and i ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    44

    help with loop

    im trying to write a loop so that 8 of each card in a blackjack game is made and i cant get it to work right. it should print out 8 cards for each card made and delete the card if there are already enough of that type of card and loop through again untill a good card is found but its not doin that any suggestions??

    where i need help
    Code:
    int Card::counters[52] = {0};
    
    for(int i = 0; i < DECKS*52; i++) 
    		{
    			if(Card::counters[i] < DECKS)
    			{
    			// loop untill find good card
    				card = new Card;
    				card->print();
    				cout << endl;
    				// push only if < max num of each card
    				if(Card::counters[i] < DECKS)
    				{
    					deck.push(*card);
    				}
    				else 
    				{
    					delete card;
    				}
    			}
    				//if not delete card and loop again
    		}

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    any suggestions
    Code:
     
    //pseudocode----to give an idea
    createDeck()
    {
      for(i = 0; i < 4; ++i)  //control suit
    	for(j = 0; j < 13; ++j) //contro value
    	  //declare memory for new card here
    	  card.suit = i;
    	  card.num = j;
    }
     
    createDecks()
    {
      for(i = 0; i < MAXDECKS; ++i) //control how many	 decks
    	 createDeck();
    }
    Last edited by elad; 05-05-2006 at 03:27 PM.
    You're only born perfect.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    that helped a little but my card class creates the card so i cant really go card.suit orcard.num. i randomise the cards face and suit i think its more of an if statement what im having trouble with but im not sure

    Code:
    struct Card
    {
    	static int counters[52];
    	
    	int val; //0-51
    	int suit,
    		face;
    		
    	
    	Card();
    	~Card() {counters[val]--;}
    	Card (const Card& card);
    	Card& operator = (Card& card);
    	int getSuit() {return suit;}
    	int getFace() {return face;}
    	int getVal() {return val;}
    	void print();
    	void printCount() {for(int i = 0; i < 52; i++) cout << i << ": " << counters[i] << endl;}; // delete
    	int points();
    };
    
    Card::Card()
    {
    	val = rand()%52;
    	suit = val / 13;
    	face = val % 13;
    	points();
    	counters[val]++;
    }
    
    Card::Card(const Card& card)
    {
    	val = card.val;
    	suit = card.suit;
    	face = card.face;
    	points();
    }
    Card& Card::operator = (Card& card)
    {
    	val = card.val;
    	suit = card.suit;
    	face = card.face;
    	points();
    	return *this;
    }
    
    int Card::points()
    {
    	if(face == 0)
    	{
    		return 11;
    	}
    	else if(face >=10)
    	{
    		return 10;
    	}
    	else
    	{
    		return face+1;
    	}
    }
    
    void Card::print()
    {
    	switch(face) 
    	{
    	case 0:
    
    		cout << "Ace";
    		break;
    	case 10:
    
    		cout << "Jack";
    		break;
    	case 11:
    
    		cout << "Queen";
    		break;
    	case 12:
    
    		cout << "King";
    		break;
    	default:
    
    		cout << face+1;
    		break;
    	}
    
    	cout << " of ";
    
    	switch(suit)
    	{
    	case 0:
    
    		cout << H;
    		break;
    	case 1:
    
    		cout << D;
    		break;
    	case 2:
    
    		cout << C;
    		break;
    	case 3:
    
    		cout << S;
    		break;
    	default:
    		break;
    	}
    
    }

  4. #4
    Dae
    Dae is offline
    Deprecated Dae's Avatar
    Join Date
    Oct 2004
    Location
    Canada
    Posts
    1,034
    I wouldn't go with random. You could still do what elad said. You can do card.suit = suit, you just need to make card be the new card (pointer you're pushing). I vouched for using the constructor to.. construct the card. Something like this:

    Code:
    for(int i = 0; i < DECKS; ++i) 
    {
    	//declare "deck" here
    	for(int j = 0; j < 4; ++j)  //control suit
    	{
    		for(int k = 0; k < 13; ++k) //control value
    		{
    			//j is the suit, k is the val, k % 13 is the face
    			deck.push(new Card(k, j, k % 13));
    		}
    	}
    	//push "deck" to a list of decks here
    }
    Code:
    Card::Card(int p_val, int p_suit, int p_face)
    {
    	val = p_val;
    	suit = p_suit;
    	face = p_face;
    	points();
    	counters[val]++;
    }
    Don't know what deck is, so I had to put comments on what it means to do.
    Warning: Have doubt in anything I post.

    GCC 4.5, Boost 1.40, Code::Blocks 8.02, Ubuntu 9.10 010001000110000101100101

  5. #5
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    DECKS is a constant of 8 also its random so i get a shuffled deck of cards
    Last edited by tunerfreak; 05-06-2006 at 02:51 PM.

  6. #6
    Registered User
    Join Date
    Mar 2006
    Posts
    44

    breaking out of a loop

    forget about my other question now.

    ok simple question i would think but i cant get it to work right. i have this loop

    Code:
    while(1)
    			{
    			do
    			{
    				player = p.getPlayer(k);
    				cout << "Player " << player->getPlayer() << "'s hand\n" << endl; 
    				player->printHand();
    				p.getPlayer(k)->calcHand();
    				if(player->getHand() == 21)
    				{
    					cout << "You have 21" << endl;
    					return;
    				
    				}
    				else
    				{
    						cout << "would you like to hit? y or n" << endl;
    						cin >> choice1;
    						if (choice1 == 'y')
    						{
    							card = new Card;
    							player->recieveCard(*card);
    							if(player->getHand() > 21)
    							{
    								cout << "Bust" << endl;
    								return;
    								
    
    							}
    							else if(player->getHand() == 21)
    							{
    								cout << "You have 21" << endl;
    								
    							}
    						}
    						else
    						{
    							k = ++k % p.getSize();
    							break;
    						}
    					
    				} // end else*/	
    					
    					/*if(k == p.getSize()-1)
    					{
    						break;
    					}*/
    			}while (p.getPlayer(k)->getHand() < 21);
    						k = ++k % p.getSize();
    			}
    			cout << "Would you like to play again?" << endl;
    			cin >> choice;
    
    	}while (choice == 'y'); // end do/while
    and it is suppose to loop the player untill player either busts, gets 21, or chooses not to hit. but if player busts it breaks out of the whole thing which i dont want and if u choose not to hit it just loops again which i dont want it should go to next player in both cases. what am i doing wrong i just want to break out of one do/while loop not all of them?
    Last edited by tunerfreak; 05-06-2006 at 04:47 PM.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Posts
    44
    ok just an update now

    i got it to loop through the players but now i cant get it out of the first while loop after all the players have gone so that i can print who won the game

    Code:
    int g = 0;
    		while(g < p.getSize())
    		{
    			cout << g;
    			do
    			{
    				
    				clear_screen();			
    				player = p.getPlayer(k);
    				cout << "Player " << player->getPlayer() << "'s hand\n" << endl; 
    				player->printHand();
    				p.getPlayer(k)->calcHand();
    				if(player->getHand() == 21)
    				{
    					
    					cout << "You have 21" << endl;
    					cout << "Press any key to continue" << endl;
    					getche();
    					break;
    				
    				}
    				else if(player->getHand() > 21)
    				{
    					
    					player->calcHand();
    					cout << "Bust" << endl;
    					cout << "Press any key to continue" << endl;
    					getche();
    					break;
    				}
    				else
    				{
    						cout << "would you like to hit? y or n" << endl;
    						cin >> choice1;
    						if (choice1 == 'y')
    						{
    							card = new Card;
    							player->recieveCard(*card);
    							if(player->getHand() > 21)
    							{
    								
    								player->calcHand();
    								cout << "Bust" << endl;
    								cout << "Press any key to continue" << endl;
    								getche();
    								break;
    								
    
    							}
    							else if(player->getHand() == 21)
    							{
    								
    								player->printHand();
    								cout << "You have 21" << endl;
    								cout << "Press any key to continue" << endl;
    								getche();
    								break;	
    							}
    						}
    						else
    						{
    							break;
    						}
    					
    				} // end else*/	
    				
    					if(g == p.getSize()-1)
    					{
    						break;
    					}			
    			} while(g < p.getSize()); //while (p.getPlayer(k)->getHand() < 21);
    						k = ++k % p.getSize();
    			g++;
    does not get here
    	}
    		for(int v = 0; v < p.getSize(); v++)
    			for(int n = 0; n < p.getSize(); n++)
    			{
    				if(p.getPlayer(v)->calcHand() > p.getPlayer(n)->calcHand())
    				{
    					cout << "Player " << p.getPlayer(v) << " Wins!!!" << endl;
    				}
    				else
    				{
    					return;
    				}
    			}
    			cout << "Would you like to play again?" << endl;
    			cin >> choice;
    
    		}while (choice == 'y'); // end do/while
    }// end main

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    g is initialized to zero outside of either loop. Once inside the first loop g is never changed inside the do/while loop so you will never get out ot the do/while loop.

    Do you even need the do/while loop?
    You're only born perfect.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  3. loop in a linked linked lists
    By kris.c in forum C Programming
    Replies: 6
    Last Post: 08-26-2006, 12:38 PM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-19-2005, 11:30 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 08:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21