C++ without fear Card Dealer question

This is a discussion on C++ without fear Card Dealer question within the C++ Programming forums, part of the General Programming Boards category; I am just starting to learn C++ and took the advice of this site on which book to start with, ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    1

    C++ without fear Card Dealer question

    I am just starting to learn C++ and took the advice of this site on which book to start with, namely C++ without fear. So far I am doing well and understanding what the writer is teaching but then I ran into the card dealer. Nothing to hard to understand until he adds in the array to ensure that no card was picked twice. I know that it works but I am unclear on HOW and WHY it works. As far as I can understand, the code simply passes a location in the array and thats all. I cannot figure out where the program is told what to do at the location, that is, check for 1's and skip them. Can someone please help me understand how this works. I do not want to continue on with the book without understanding this. Thank you.

    Code:
    #include "stdafx.h"
    #include <iostream>
    #include <stdlib.h>
    #include <time.h>
    #include <math.h>
    using namespace std;
    
    int rand_0toN1(int n);
    void draw_a_card();
    int select_next_available(int n);
    
    char *suits[4] = {"Hearts", "Spades", "Clubs", "Diamonds"};
    char *ranks[13] = {"Ace", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
    	"Nine", "Ten", "Jack", "Queen", "King"};
    int card_drawn[52];
    int cards_remaining = 52;
    
    int main() {
    	int n, i;
    
    	srand(time(NULL));
    
    	while(1) {
    		cout << "Enter number of cards to draw (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, s, 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;
    }
    
    int rand_0toN1(int n) {
    	return rand() % n;
    }

  2. #2
    hqt
    hqt is offline
    Registered User
    Join Date
    Aug 2011
    Posts
    115
    A card have two part: number and symbol, put you put it into together, so you will have 52 card->52 number
    So, I will explain how to choose random number:
    1) random number from 1->card remain:
    2) after, you see select_next_available function ? It have two loop: first loop, it will find the first place that this card haven't choose.
    second loop, you will choose which card(i) haven't choose yet in RANDOM (I will explain later). The action, this card haven't choosen is:
    Code:
    while(card_draw[i]) i++;
    So, where random ?? It depend on n parameter. So, this function really mean: CHOOSE N_TH CARD IN UNCHOOSE CARD SET
    and, because that, noway that repeat any card, because: you choose n_th card from unchoose card set, not all card set

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Random card dealer function
    By Loic in forum C++ Programming
    Replies: 13
    Last Post: 04-23-2007, 07:19 PM
  2. Video Card Question
    By xddxogm3 in forum Tech Board
    Replies: 7
    Last Post: 10-03-2004, 04:07 PM
  3. Graphics Card Question
    By XSquared in forum Tech Board
    Replies: 9
    Last Post: 01-04-2004, 02:23 PM
  4. graphics card question
    By dP munky in forum Tech Board
    Replies: 4
    Last Post: 01-09-2003, 09:05 PM
  5. card shuffling dealing question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 04-03-2002, 08:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21