Hello!
Im in the process of learning C++ and boy am I having fun! The logic is overwhelming. Im stuck at an example of a card dealing program where the program prompts the user asking how many cards she wants to draw from a card deck. Naturally the same card cant show up twice.
Now I understand must of the code but the process of choosing next available card is little confusing. The specific function for choosing next available card is:
int select_next_available(int n) {
int i = 0;
while (card_drawn[i])
i++;
while (n-- > 0) {
i++;
while (card_drawn[i])
i++;
}
card_drawn[i] = true;
return i;
}
The first while-loop finds the first available "false" card i.e card that is not drawn yet. But I cant understand the two other while loops. The author explains it like this: "It then counts n more avialable cards, each time skipping over items already drawn." But I dont really understand the need for doing this. The functions "job" is to find the first available card that has the value false in the array, make it true and return it to the calling function which adds a suit and rank to it. Now the program knows that that specific place in the array is true and cant draw it again? Im consufed a since I dont have any teacher I thought you guys could help. Let me know if you need the rest of the code also.
Btw this is my first post so hi!
5 replies to this topic
#1
Posted 16 November 2011 - 05:18 AM
|
|
|
#2
Posted 16 November 2011 - 06:25 AM
I don't see the need either. You return the position of the first available card, which as you state is the purpose of the function. The other information does not add to the problem and changes your i value, unless we are missing something. :confused: Put you code in code tags next time. Use the # sign.
code goes here
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#3
Posted 16 November 2011 - 07:38 AM
Okay. Maybe its better I post the whole program:
Most likely me not understanding some part of the code making use of those two last while-loops in the select_next_available function.
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;
int rand_0toN1(int n);
void draw_a_card();
int select_next_available(int n);
bool card_drawn[52];
int cards_remaining = 52;
char *suits[4] =
{"hearts", "diamonds", "spades", "clubs"};
char *ranks[13] =
{"ace", "two", "three", "four", "five",
"six", "seven", "eight", "nine",
"ten", "jack", "queen", "king" };
int main() {
int n, i;
srand(time(NULL));
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;
}
void draw_a_card() {
int r;
int s;
int n, card;
n = rand_0toN1(cards_remaining--);
card = select_next_available(n);
r = card % 13;
s = card / 13;
cout << ranks[r] << " of " << suits[s] << endl;
}
int select_next_available(int n) {
int i = 0;
while (card_drawn[i])
i++;
while (n-- > 0) {
i++;
while (card_drawn[i])
i++;
}
card_drawn[i] = true;
return i; // Return this number.
}
int rand_0toN1(int n) {
return rand() % n;
}
Most likely me not understanding some part of the code making use of those two last while-loops in the select_next_available function.
#4
Posted 16 November 2011 - 09:51 AM
Ok i see. Although the algorithm gets the next available card he does not want it to get them in a sequential manner. So he uses a random value n to offset the position that will be returned. The functions returns the nth available unused card.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
#5
Posted 16 November 2011 - 11:27 AM
Okay I understand. Correct me if I'm wrong, but that part of the function is not really necessary since the randomization of the argument passed when the call occurs is already done in a previous stage of the program? If so, does it add more "randomness" to the program?
+Thanks btw.
+Thanks btw.
#6
Posted 16 November 2011 - 05:38 PM
The call to the random function in the 'main' part of the code only generates a random number, with its max possible value being the amount of possible cards you can choose from. It then passes this value to the get next selection function to work as an offset for the card to be drawn. Without passing the random value to the get next selection function you will return the first card that has not been chosen. That makes the result predictable.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account

Back to top









