No, it initializes the array. It looks similar, but there is a formal difference. Some things can only be initialized but not assigned, like arrays and const variables. There's also a difference with classes concerning which functions are called.
No, it initializes the array. It looks similar, but there is a formal difference. Some things can only be initialized but not assigned, like arrays and const variables. There's also a difference with classes concerning which functions are called.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
Sorry if this is off-topic, but is there a way to efficiently check the sum of each value in an array without manually adding deck[1] + deck[2] + deck[3] etc., and a way to force a static variable to reset to its original value? I would like to add a line in the loop that chooses a card to check the sum, and if it's 0 it should reset the array. This would prevent infinite loops and reset the deck if it runs out of cards.
Like I said originally, you shuffle the deck before you start dealing.
Shuffle: to arrange in a random order; pick two random card positions, and swap them over; repeat a number of times.
Deal: take the next card from the deck; all you need to do here is keep a count of how many cards you've dealt.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Last edited by wipeout4wh; 06-07-2011 at 01:47 AM.
You can get a new shuffled deck by shuffling the deck again and reseting the next card pointer/index to the beginning.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
> I still don't see why shuffling is necessary, either.Code:if ( numCardsDealt == 52 ) { // reshuffle the deck numCardsDealt = 0; }
Have you ever played blackjack with a deck of real cards?
Try to model the actions you take there.
People do not pick random numbers up to 52, and then check to see if that card has already been drawn.
No, they randomise the order to begin with, and then sequentially draw cards from the top.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
Shuffeling is a more standard solution, and eliminates the need to repopulate the array. You can do it your way, but then you'll need to write a function to repopulate the deck. Also, as a lesser point if you use a shuffled deck, you automatically have the number of cards played.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
How would it eliminate the need to repopulate the array? I would have to send the cards that are no longer in play to a discard pile array, then bring them back to the deck when it's reshuffled.
Edit: Also, I could get the number of cards played by using a static int and adding 1 to it each time drawCard() is called.
You can just have an index to the current point in the deck. Every time you draw a card, you return the card at that index, and increment it by 1.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
The index is just the part of an array that's in square brackets, right?
If I do that, it would still reuse the cards that are still in play when I reshuffle the deck. If I have a 5 in my hand, there should only be 3 5's in the deck when it gets reshuffled. That means I would need to remove the card from the deck and add it to a discard pile when it's out of play.
Edit: I decided to have it shuffle the deck first like you guys said. Here's drawCard() so far:
I'll have to learn about passing variables between functions, and I haven't written shuffleDeck() yet, but does this look good so far?Code:int drawCard() { static int deck[52] = {1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,11,11,11,11,12,12,12,12,13,13,13,13}; static int cardToDraw = 0; int drawnCard; if (cardToDraw < 52) { drawnCard = deck[cardToDraw]; deck[cardToDraw] = 0; cardToDraw++; } else { shuffleDeck(); cardToDraw = 0; } cout<< "cardToDraw = "<< cardToDraw<< ".\n";//Debugging line. return drawnCard; }
Edit: Would it be better to just set deck[52] as a global variable? I don't really understand how to pass variables between functions. I wrote part of shuffleDeck(), but I can't continue until I figure this out.
Code:void shuffleDeck() { int firstCard; int secondCard; int i; for (i = 0; i < 100; i++){ firstCard = (rand() % 52); secondCard = (rand() % 52); } }
Last edited by wipeout4wh; 06-07-2011 at 06:07 AM.