Need of Assistance :)
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: Need of Assistance :)

  1. #1
    Join Date
    Feb 2014
    Posts
    14

    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

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Posts
    12,057

    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();

  3. #3
    Join Date
    Feb 2014
    Posts
    14

    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;

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,277

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Feb 2014
    Posts
    14

    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 09:53 AM.

  6. #6
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,277

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,277

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Feb 2014
    Posts
    14

    Re: Need of Assistance :)

    Quote Originally Posted by 2kaud View Post
    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 View Post
    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 10:09 AM.

  9. #9
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,277

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  10. #10
    Join Date
    Feb 2014
    Posts
    14

    Re: Need of Assistance :)

    Quote Originally Posted by 2kaud View Post
    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.

  11. #11
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,277

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  12. #12
    Join Date
    Feb 2014
    Posts
    14

    Re: Need of Assistance :)

    Quote Originally Posted by 2kaud View Post
    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

  13. #13
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,277

    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. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  14. #14
    Join Date
    Feb 2014
    Posts
    14

    Re: Need of Assistance :)

    Quote Originally Posted by 2kaud View Post
    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
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center