This is a playing card deck creator and shuffler i made yesterday. It is very simple now, but i believe that it could easily be implemented into a card game program of some sort. Any tips/criticisms regarding the code or ways to improve it would be appreciated :)
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>
#define STANDARD_DECK 52
using std::cout;
using std::endl;
using std::cin;
class playingCard //When created, an instance of this class is equal to one playing card
{
public:
std::string suit; //The suit of the card
char card; //The number/ letter of the card
int value; //Value of card
int index; //Index number, used for shuffling
};
void init_deck(playingCard fresh_deck[STANDARD_DECK])
{
int currentCard = 0;
for(int suit = 0; suit<4; suit++) //Does one loop for each suit
{
for(int card = 0; card<13; card++) //Does one loop for each card
{
if(suit == 0)
{
fresh_deck[currentCard].suit = "Spades";
}
elseif(suit == 1)
{
fresh_deck[currentCard].suit = "Hearts";
}
elseif(suit == 2)
{
fresh_deck[currentCard].suit = "Clubs";
}
else
{
fresh_deck[currentCard].suit = "Diamonds";
}
fresh_deck[currentCard].value = card; //Gives the card a set value
fresh_deck[currentCard].index = currentCard; //Gives each card an index number for easy reference
currentCard++;
}
}
currentCard = 0;
while(currentCard<STANDARD_DECK) //This loop sets the cards symbol in relation to
// the value it recieved in the previous loop
{
if(fresh_deck[currentCard].value == 0)
fresh_deck[currentCard].card = '2';
elseif(fresh_deck[currentCard].value == 1)
fresh_deck[currentCard].card = '3';
elseif(fresh_deck[currentCard].value == 2)
fresh_deck[currentCard].card = '4';
elseif(fresh_deck[currentCard].value == 3)
fresh_deck[currentCard].card = '5';
elseif(fresh_deck[currentCard].value == 4)
fresh_deck[currentCard].card = '6';
elseif(fresh_deck[currentCard].value == 5)
fresh_deck[currentCard].card = '7';
elseif(fresh_deck[currentCard].value == 6)
fresh_deck[currentCard].card = '8';
elseif(fresh_deck[currentCard].value == 7)
fresh_deck[currentCard].card = '9';
elseif(fresh_deck[currentCard].value == 8)
fresh_deck[currentCard].card = 'T';
elseif(fresh_deck[currentCard].value == 9)
fresh_deck[currentCard].card = 'J';
elseif(fresh_deck[currentCard].value == 10)
fresh_deck[currentCard].card = 'Q';
elseif(fresh_deck[currentCard].value == 11)
fresh_deck[currentCard].card = 'K';
elseif(fresh_deck[currentCard].value == 12)
fresh_deck[currentCard].card = 'A';
currentCard++;
}
}
void print_deck(playingCard deck[STANDARD_DECK])
{
for(int cc = 0; cc< STANDARD_DECK; cc++) //This loop will print 52 individual cards
{
cout << deck[cc].card << " of " << deck[cc].suit<< " and the index # is : " << deck[cc].index << endl;
if((cc + 1)% 13 == 0) //This if statement will create a line
// break after every 13 cards / every suit in a fresh deck
cout << endl;
}
}
void shuffle_deck( playingCard real_deck[STANDARD_DECK])
{
playingCard temp_deck[STANDARD_DECK]; //This creates a fresh, new temporary deck of cards
init_deck(temp_deck);
for(int i = 0; i < STANDARD_DECK; i++)
{
real_deck[i] = temp_deck[rand()%STANDARD_DECK]; //Select a random
//card from temp_deck and place it in the real_deck
for(int j = 0; j <= i; j++)
{
if(real_deck[i].index == real_deck[j].index && j!=i )//If the card
//selected has already been placed before the current one
--i; //it insert another random card for the spot in
//the array until a new one is chosen
}
}
}
int main()
{
srand(time(0));
playingCard my_deck[STANDARD_DECK]; //This creates an array of 52 cards
init_deck(my_deck); //This fills the array with all the standard playing cards
print_deck(my_deck); //This function prints the deck of cards from top to bottom
cin.get();
shuffle_deck(my_deck); //This function shuffles the array in a random order
print_deck(my_deck);
cin.get();
shuffle_deck(my_deck); //An example of re-shuffling
print_deck(my_deck);
}