I have written this program to deal 7 cards to one player.
#include <iostream>
#include <string>
#include <cstdlib> //for rand and srand
#include <cstdio>
#include <vector>
using namespace std;
string suit[] = {"Diamonds", "Hearts", "Spades", "Clubs"};
string facevalue[] = {"Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King", "Ace"};
int numberOfCardsDrawn = 0;
string drawnCards[52];
string drawCard () {
string card;
int cardvalue = rand()%13;
int cardsuit = rand()%4;
card += facevalue[cardvalue];
card += " of ";
card += suit[cardsuit];
return card;
}
bool isMyCardAlreadyDrawn (string card) {
for(int i = 0; i < 52; i++) {
if(card.compare(drawnCards[i]) == 0) {
// if this is true, then both strings are the same
return true;
}
}
return false;
// if the code reaches this point, then the card has not been drawn yet
}
string getCard () {
srand(time(0));
string card = drawCard();
while (isMyCardAlreadyDrawn(card) == true) {
// keep drawing until an undrawn card is found
card = drawCard(); // draw a new card
}
drawnCards[numberOfCardsDrawn] = card;
numberOfCardsDrawn++;
return card;
}
int main () {
cout << "Your Cards:\n";
vector<string> p0; //player 0's card
const int DEAL_CARDS = 7; //number of cards we can deal to each player
string choices[] = { "a", "b","c","d","e","f","g" };
for (int i = 0; i < DEAL_CARDS; i++){
string p0_getCard = getCard();
cout <<" " << choices[i] << ")"<< p0_getCard << " ";
p0.push_back(p0_getCard);
}
}
However, I will eventually want to have a five-player game (dealing cards to four more vectors), and I feel that extending this code to accomplish that would result in excessive code duplication. How do I go about improving the code to support that?
std::dequefor his purposes? \$\endgroup\$