Re: Need of Assistance :)
I don't see anything here that stops dealing at 52
Code:
for (i = 1; i <= n; i++)
draw_a_card();
Re: Need of Assistance :)
What would the code be to stop the program from dealing more than 52?
Code:
if (cards_remaining.empty())
break;
or
Code:
if (cards_remaining == 0)
return 0;
Re: Need of Assistance :)
One way would be like this
Code:
for (i = 1; i <= n && cards_remaining > 0; i++)
draw_a_card();
Re: Need of Assistance :)
Alright, I'll try that out.
Thank you.
_____EDIT_____
So, that code did the trick at stopping the never ending card drawing. But the code that I added to my draw_a_card function isn't doing what it's supposed to do.
It doesn't send out the cout "Reshuffling" to the screen, which it's supposed to do......... Am I just confusing myself?
Code:
if (cards_remaining == 0) {
cout << "Reshuffing." << endl;
cards_remaining = 52;
for (int i = 0; i < 52; i++)
card_drawn[i] = false;
This is the whole function
Code:
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;
if (cards_remaining == 0) {
cout << "Reshuffing." << endl;
cards_remaining = 52;
for (int i = 0; i < 52; i++)
card_drawn[i] = false;
}
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 = -1;
n++;
// At begininning 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.
Re: Need of Assistance :)
Quote:
But the code that I added to my draw_a_card function isn't doing what it's supposed to do.
Which is? Have you used the debugger to see where the code is deviating from what you expected? When code isn't doing what it should then tracing the code through with the debugger helps to track down the issue.
Re: Need of Assistance :)
Code:
int i = -1;
n++;
// At begininning of deck, skip cards already drawn.
while (card_drawn[i])
i++;
Indexes of arrays start at 0.
Re: Need of Assistance :)
Quote:
Originally Posted by
2kaud
Which is? Have you used the debugger to see where the code is deviating from what you expected? When code isn't doing what it should then tracing the code through with the debugger helps to track down the issue.
Code:
if (cards_remaining == 0) {
cout << "Reshuffing." << endl;
cards_remaining = 52;
for (int i = 0; i < 52; i++)
card_drawn[i] = false;
My instructor never went into detail about how to use the debugger. I only know how to write very simple programs and how to fix simple mistakes to a degree.
Quote:
Originally Posted by
2kaud
Code:
int i = -1;
n++;
// At begininning of deck, skip cards already drawn.
while (card_drawn[i])
i++;
Indexes of arrays start at 0.
Okay, never mind. I have figured it out. I was just too blind to see it. I need to fix the spacing so that "Reshuffling" is more noticable.
Re: Need of Assistance :)
Quote:
I' required to make a Texas Hold Em' program and my instructor explained that it's similar to the code that I will provide to you, but I honestly don't know where to begin.
I would suggest that the first thing you want is for draw_a_card() to actually return the card drawn.
For the Texas Hold Em' program, first you need to know the rules. Then you need to design an algorithm for the rules. Then you produce a program design, then you code the program from the design and then test/debug the code.
You have a function that gets a card. So from the rules, in English, produce the design so that someone who doesn't know anything about cards can follow these (like a computer would with a program) using this.
Re: Need of Assistance :)
Quote:
Originally Posted by
2kaud
I would suggest that the first thing you want is for draw_a_card() to actually return the card drawn.
For the Texas Hold Em' program, first you need to know the rules. Then you need to design an algorithm for the rules. Then you produce a program design, then you code the program from the design and then test/debug the code.
You have a function that gets a card. So from the rules, in English, produce the design so that someone who doesn't know anything about cards can follow these (like a computer would with a program) using this.
Okay, thank you.
Re: Need of Assistance :)
Quote:
My instructor never went into detail about how to use the debugger. I only know how to write very simple programs and how to fix simple mistakes to a degree.
Then now's a good time to learn to use the debugger. You need to become totally familiar with it. What os/compiler are you using?
Take your current card program and use the debugger to single step through the code, examine variables, set breakpoints etc and become familiar with it using a program you know. Then when you are trying to find a problem in another program that doesn't work as expected you will be familiar with the debugger and can then just use it to help find problems in the code. That's what we do. A program is seldom 'correct' the first time it is coded. So we use the debugger to determine what's happening and where what the program is doing is at variance with the design from which it was coded.
Re: Need of Assistance :)
Quote:
Originally Posted by
2kaud
Then now's a good time to learn to use the debugger. You need to become totally familiar with it. What os/compiler are you using?
Take your current card program and use the debugger to single step through the code, examine variables, set breakpoints etc and become familiar with it using a program you know. Then when you are trying to find a problem in another program that doesn't work as expected you will be familiar with the debugger and can then just use it to help find problems in the code. That's what we do. A program is seldom 'correct' the first time it is coded. So we use the debugger to determine what's happening and where what the program is doing is at variance with the design from which it was coded.
I'm running Windows 8 with Visual Studio Ultimate 2013
Re: Need of Assistance :)
Then you have one of the best debuggers available. A bit of time taken now to get used to it will pay dividends later.
Re: Need of Assistance :)
Quote:
Originally Posted by
2kaud
Then you have one of the best debuggers available. A bit of time taken now to get used to it will pay dividends later.
Alright. Thank you for your advice and help. Since I have nothing to do until 5pm tonight, I'll spend some time on the debugger.
Thanks again,
Cori