-
February 8th, 2014, 09:57 AM
#1
Need of Assistance :)
First things first. I go to New England Institute of Technology for Software Engineering and Cyber Security. I'm currently in my second quarter (15th week) and I'm starting to do a little more intermediate stuff. So, I made this program that deals random cards. The code is set to make it stop at 52 cards, but if you type anything over 52, it still deals the cards randomly. Firstly, I wanted to know if I had a fault in my code that should be stopping the cards but isn't or if it's not there at all........ I'll post the source for anyone to look at it.
Secondly, 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. My teacher isn't very good at explaining code than what he does when he's not at school teaching us kids. So I was hoping to come to a place where people are more serious to teach and explain things that might help me understand all this more.
Thank you,
Cori
Source.cpp
-
February 8th, 2014, 10:16 AM
#2
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();
-
February 8th, 2014, 10:33 AM
#3
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;
-
February 8th, 2014, 10:44 AM
#4
Re: Need of Assistance :)
One way would be like this
Code:
for (i = 1; i <= n && cards_remaining > 0; i++)
draw_a_card();
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 8th, 2014, 10:48 AM
#5
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.
Last edited by CoriNEIT; February 8th, 2014 at 10:53 AM.
-
February 8th, 2014, 10:55 AM
#6
Re: Need of Assistance :)
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 8th, 2014, 10:58 AM
#7
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 8th, 2014, 11:06 AM
#8
Re: Need of Assistance :)
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.
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.
Last edited by CoriNEIT; February 8th, 2014 at 11:09 AM.
-
February 8th, 2014, 11:09 AM
#9
Re: Need of Assistance :)
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 8th, 2014, 11:11 AM
#10
Re: Need of Assistance :)
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.
-
February 8th, 2014, 11:21 AM
#11
Re: Need of Assistance :)
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 8th, 2014, 11:39 AM
#12
Re: Need of Assistance :)
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
-
February 8th, 2014, 11:45 AM
#13
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.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++23 Compiler: Microsoft VS2022 (17.6.5)
-
February 8th, 2014, 12:12 PM
#14
Re: Need of Assistance :)
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|