-
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
}
-
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();
}
-
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;
}
}
-
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.
-
DECKS is a constant of 8 also its random so i get a shuffled deck of cards
-
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?
-
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
-
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?