0

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??

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
		}

my card class

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()
{
	int num;

	if(face == 0)
	{
		cout << "You have an Ace would you like it to be a 1 or 11?" << endl;
		cin >> num;
		if(num  == 11)
		{
			return 11;
		}
		else if(num == 1)
		{
			return 1;
		}
		else
		{
			while (num != 1 || num != 11)
			{
				cout << "you must enter a 1 or 11" << endl;
				return false;
			}
		}
		return false;
	}
	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;
	}

}
2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by Lerner
0

One of the principles of coding is to break a big problem down into smaller problems. You apparently want to create a dealers boot with 8 decks. So figure out how to create a card and then do it eight times. Once all cards are created shuffle them. Don't try to randomly create a card to similute a shuffle while you are trying to create the boot, which is what it looks like your code is trying to do.

loop to control suit
  loop to control face value of card
    loop to control how many of each card you want
       allocate memory for a new card
       initialize suit and face value of each new card
       give the card a unique value from 0 to numberOfDecks * 52 - 1
shuffle all 52 * numberOfDecks cards
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.