trueis defined as "not-zero". Therefore 1 is true. Likewise 2 is trueand 3 is true, etc.
Only 0 is false.
Why would you use while(1) when you can use while(true)? It doesn't make a difference, but in C there is no true or false so you'd have to do while(1) if you are writing C code. There may be a few of these left over if your code was ported from C or was written by someone who is more used to C.
int main() {
int n, i;
srand(time(NULL)); // Set seed for randomizing.
while (1) {
cout << "Enter no. of cards to draw ";
cout << "(0 to exit): ";
cin >> n;
if (n == 0)
break;
for (i = 1; i <= n; i++)
draw_a_card();
}
return 0;
}
// Draw-a-card function
// Perform a card draw by getting a random 0-4 and a
// random 0-12. Use these to index the strings
// arrays, ranks and suits.
//
void draw_a_card() {
int r; // Random index (0 thru 12) into
// ranks array
int s; // Random index (0 thru 3) into
// suits array
int n, card;
n = rand_0toN1(cards_remaining--);
card = select_next_available(n);
r = card % 13; // r = random 0 to 12
s = card / 13; // s = random 0 to 3
cout << ranks[r] << " of " << suits[s] << endl;
}
// Select-next-available-card function.
// Find the Nth element of card_drawn, skipping over
// those elements already set to true.
//
int select_next_available(int n) {
int i = 0;
// At beginning of deck, skip cards already drawn.
while (card_drawn[i])
i++;
while (n-- > 0) { // Do the following n times:
i++; // Advance to next card
while (card_drawn[i]) // Skip past cards
i++; // already drawn.
}
card_drawn[i] = true; // Note card to be drawn
return i; // Return this number.
}
// Random 0-to-N1 Function.
// Generate a random integer from 0 to N–1.
//
int rand_0toN1(int n) {
return rand() % n;
}
@Stewbond
I think in the book I read they said it's the same,but sometimes they use while(1) and sometimes while(true),
so I taught there must be some difference.
There is a better way to handle cards I have learned over the years. Cards represented by a number between 0 and 51. (I made it 1 - 52 so 0 could represent a card back and 53 - 54 could represent the jokers. Now how do you get the card number? I will use the original 0 - 51 example.
unsigned int card;
int face = (card / 13) + 1;
int suit = card % 4;
Now you have a number in suit from 0 to 3 while in suit you have a number between 1 and 13. Just assign values for suit like 0 is clubs, 1 is hearts, 2 is diamonds and 3 is clubs.
This way you only have one random number to worry about instead of two.
In the future, please post using "code tags" (they're by Format: when you post a new thread or a reply.) It makes code easier to read.
The comment is misleading. "s" is not set to a random number from 0 to 3, it is set to either 0, 1, 2, or 3 depending on which random card is selected.
The program uses a boolean array (bool card_drawn[52]; ) to keep track of which cards have already been drawn. In a sense, that means the cards are numbered from 0 to 51 (slot 0, slot 1, slot 2...slot 51).
The variable "card" is set equal to a number from 0 to 51, depending on what select_next_available returns (which is random, sorta).
So "card" is at least 0, and at most 51.
s = card / 13;
If we selected card 51, then 51 / 13 = 3.
If we selected card 30, then 30 / 13 = 2.
If we selected card 14, then 14 / 13 = 1.
Also, 0 / 13 = 0.
Since we set s = card / 13, s will be a number from 0 to 3. S isn't random, but "card" is, and the range of card gives a range of 0, 1, 2, and 3 for "s".
The logic for "r" is very similar.
r = card % 13;
51 % 13 = 12.
0 % 13 = 0
and we can have all the values in between. "r" isn't random, but "card" is.
In my opinion, that code isn't good...and the comments are outright misleading.