I've been trying to write out a program to deal out a specified number of cards. Each card is represented by an index between 0 and 51, for a grand total of 52. Obviously the tricky part is making sure the computer doesn't deal out the same card twice. I wrote up code that I thought should work, but after staring at it for an hour, I can't find the logic error.
When the computer selects a random int from the deck of cards, that random int is checked to see if it's already been selected and added to a 'cardsDealt' vector via the push_back function. If the card hasn't been dealt previously, then it should be dealt. If it has been dealt, then obviously I want the computer to make a different selection and reevaluate that selection until it finds an index that hasn't been previously dealt. I'm sure there are more efficient ways to do this, however I'm still a beginner learning the fundamentals. Could I just get some input as to why my logic is wrong. My code is attached below. I have the program set to deal out 10 cards. It may take anywhere from 1 to 6-ish runs to get a duplicate.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <iostream>
#include <vector>
#include <cstdlib>
#include <string>
#include <ctime>
using namespace std;
vector<int> deck(52); // global vector deck
vector<int> _cardsDealt; //holds the cards dealt as vector
void genDeck(); // assigns a card to each index as an int
void dealCards();
void displayDealtCards();
int selectCard(vector<int> deck);
int main(){
int cardsToDeal = 10;
srand(time(NULL));
genDeck();
for(int i=0; i<cardsToDeal; i++){ //deals out 10 cards
dealCards();
}
displayDealtCards();
cout << "Number of cards to deal: " << cardsToDeal << endl;
cout << "Acutual cards dealt: " << _cardsDealt.size() << endl;
return 0;
}
void genDeck(){ // creates a vector of ints from 0 to 51, each card corresponds to an int
vector<int> _deck(52);
for(int i=0; i <deck.size(); i++){
_deck[i] = i;
deck[i] = _deck[i];
}
}
int selectCard(vector<int> deck){ // randomly selects one of the indexes (cards), from the deck
int card = rand() % 52;
return card;
}
void dealCards(){
int card = selectCard(deck); // calls selectCard function to select a random card
cout << "The card selected is: " << card << endl;
for(int n=0; n < _cardsDealt.size();n++){ //compares new selection to existing selections (indexes) in _cardsdealt vector
if(card == _cardsDealt[n]){ // has the selected card already been dealt?
cout << card << " HAS ALREADY BEEN SELECTED" << endl;
dealCards(); // since that card is in _cardsDealt vector, start over and select another random card
break;
cout << "IT SHOULD NEVER OUTPUT THIS" << endl;
}
}
cout << card << " has been added to the vector" << endl;
_cardsDealt.push_back(card);
}
void displayDealtCards(){
cout << "The cards: ";
for(int i=0; i <_cardsDealt.size(); i++){
cout << _cardsDealt[i] << " ";
}
cout << "have been dealt" << endl;
}
|
When reading the output, I'm stumped. It'll output something like this:
The card selected is: 43
43 HAS ALREADY BEEN SELECTED
The card selected is: 16
16 has been added to the vector
43 has been added to the vector
Can anyone take a look at my code and explain why two different values for 'card' are being added to the vector? Intuitively I would think that calling the function again, which sets a different value for 'card', would overwrite the original, duplicate value, but apparently not.