cards.cpp

 // cards.cpp : Defines the entry point for the console application.
//
/*
This is the begining of a card game which creates a deck, stores
the cards values, suits and stores which face cards they are or
are not and then shuffles the deck. Incorperates array's, random
numbers, and for loops. All the code is commented so it should be
easy to figure out. This is one of my first attempts at programing
so some things might have been worked around the hard way so I would
welcome comments and suggestions from anyone.

john_69_11@hotmail.com

Thanks in advance for the help and feel free to use any of the code
in this program.

*/
#include "stdafx.h" //Contains all other includes

//**Initiate Variables
int TempPosition; //Creates a temperary variable to hold the card position when they are being shuffled
int TempSuit; //Creates a temperary variable to hold the suit number when shuffling (1 = spade, 2 = diamond, 3 = club, 4 = heart)
int TempValue; //Creates a temperary variable to hold the card value while shuffling (2,3,4 ..)
int TempFace; //Creates a temperary variable to hold the card type while shuffling (1 = jack, 2 = queen, 3 = king, 4 = ace)
char TempName[25]; //Creates a temp variable to hold the card name while shuffling

int Random; //Creates a variable to hold the randomly generated number
int i=0;  //Creates a variable to use as the index for the deck in for loops
int j=0;  //Creates a variable to use for counting for loops




int Deck[52][4]; //Creates an array to hold the card position, suit number and value.  Also tells if a card is Jack, Queen.. (Deck[i][3])
//**Stuff to make things easier to understand
//These 4 are used with the array, gave them names to make things easier
int Position = 0; //Section of the array that holds the cards position, used to shuffle the deck (0..51)
int Suit = 1; //Section of the array that holds which holds the suit of that card (1..4)
int Value = 2; //Section of the array that holds the value of the card (2..11)
int Face = 3; //Section of the array that tells which face card it is (0..4)

//Used instead of the numbers so it is easier to understand what is being changed
int Spade = 1;
int Diamond = 2;
int Club = 3;
int Heart = 4;

int VAce = 11; //Default value of the ace
//**Stuff to make things easier to understand


char Name[52][25] = {     //array filled with card names. Used for dealing

"Two of Spades", "Three of Spades", "Four of Spades",
"Five of Spades", "Six of Spades", "Seven of Spades", "Eight of Spades",
"Nine of Spades", "Ten of Spades", "Jack of Spades", "Queen of Spades",
"King of Spades", "Ace of Spades",

"Two of Diamonds", "Three of Diamonds", "Four of Diamonds",
"Five of Diamonds", "Six of Diamonds", "Seven of Diamonds", "Eight of Diamonds",
"Nine of Diamonds", "Ten of Diamonds", "Jack of Diamonds", "Queen of Diamonds",
"King of Diamonds", "Ace of Diamonds",

"Two of Clubs", "Three of Clubs", "Four of Clubs",
"Five of Clubs", "Six of Clubs", "Seven of Clubs", "Eight of Clubs",
"Nine of Clubs", "Ten of Clubs", "Jack of Clubs", "Queen of Clubs",
"King of Clubs", "Ace of Clubs",

"Two of Hearts", "Three of Hearts", "Four of Hearts",
"Five of Hearts", "Six of Hearts", "Seven of Hearts", "Eight of Hearts",
"Nine of Hearts", "Ten of Hearts", "Jack of Hearts", "Queen of Hearts",
"King of Hearts", "Ace of Hearts",

  };

//**Initiate Variables

void CreateDeck() //Function to create the deck, set names, values...would only need to be called once
{

for (i=0; i<52; i++) //Generates 51 positions for the cards, used as placholders for the card
{
Deck[i][Position] = i;
}

//**Defines the cards suit, value and if it is king, queen...
//**Cards 2..10
for (i=0; i<9; i++) //Cards in positions 0 - 8 are spades 2..10
{
Deck[i][Suit] = Spade; //Sets Suit to Spades
Deck[i][Value] = i+2; //Gives the card a value (Deck starts at 0 so that first card has a value of 2)
Deck[i][Face] = 0; //Not a face card
}

for (i=13; i< 22; i++) //Cards in positions 13..21 are diamonds 2..10
{
Deck[i][Suit] = Diamond; //Sets suit to diamonds
Deck[i][Value] = i-11; //Gives the card a value
Deck[i][Face] = 0; //Not face card
}

for (i=26; i< 35; i++) //Cards in positions 26..34 are clubs
{
Deck[i][Suit] = Club; //Sets suit to clubs
Deck[i][Value] = i-24; //Gives the card a value
Deck[i][Face] = 0; //Not a face card
}

for (i=39;i< 48; i++) // Cards in positions 39..47 are Hearts
{
Deck[i][Suit] = Heart; //Sets suit to Heart
Deck[i][Value] = i-37; //Gives the card a value
Deck[i][Face] = 0; //Not a face card
}
//**Cards 2..10

//**Face Cards
j=0; //j is set to zero so that when it goes through the loop it counts 1..4, reducing lines
for (i=9; i<12; i++) //Cards in position 9..11 are face cards in spades
{
j=j+1; //1 is added to j so that each time it goes through the loop it is one more, that way jack will be 1, queen will be 2...
Deck[i][Suit] = Spade; //suit is set to spades
Deck[i][Value] = 10; //Gives the card a value
Deck[i][Face] = j; //Tells which face card it is
}

j=0;//up some
for (i=22; i<25; i++) //Cards in positons 22.. 24 are face cards in diamonds
{
j=j+1;//up some
Deck[i][Suit] = Diamond; //Sets suit to diamonds
Deck[i][Value] = 10; //Gives card a value
Deck[i][Face] = j; //Tells which face card it is
}

j=0;// up some
for (i=35; i<38; i++) //Cards in positions 35..37 are face cards in clubs
{
j=j+1;
Deck[i][Suit] = Club; // Sets suit to clubs
Deck[i][Value] = 10; //Gives Card a value
Deck[i][Face] = j; //Tells what face card it is
}

j=0;// up some
for (i=48; i<51; i++) //Cards in positions 48..50 are face cards in hearts
{
j=j+1;//up some
Deck[i][Suit] = Heart; //Sets suit to hearts
Deck[i][Value] = 10;  //Gives card a value
Deck[i][Face] = j; //Tells what face card it is
}
//**Face Cards

//**Aces
j=0;//up some
for (i=12; i<52; i=i+13) // Gets each ace (12 - spade, 25 - diamond, 38 - Club, 51 - Heart)
{
j=j+1;//up some
Deck[i][Suit] = j; // sets the suit of each ace
Deck[i][Value] = VAce; //Gives the card a value
Deck[i][Face] = 4; //Tells what face card it is
}
//**Aces
//**Defines the cards suit, value and if it is king, queen...

}

void Shuffle() //This function shuffles the deck
{
srand(time(NULL)); // Seeds the random number generator

//**Randomly sorts the cards keeping suits and values with the correct card position
for (i=0; i<52; i++)
{
Random = rand()%52; //Randomly picks a number from 0..51

TempPosition = Deck[i][Position];  //Sets Temp to the number in this position
TempSuit = Deck[i][Suit]; //Keeps the suit of the card in the same position as that card
TempValue = Deck[i][Value]; //Keeps the value of the card in the same position as the card
TempFace = Deck[i][Face]; //Keeps the Type of card in the same position as the card
strcpy(TempName,Name[i]); //Keeps the name of the card in the correct position

Deck[i][Position] = Deck[Random][Position];  //Sets the number in position i to the number in the randomly picked position
Deck[i][Suit] = Deck[Random][Suit];  //Keeps the suit of the card in the same position as that card
Deck[i][Value] = Deck[Random][Value];  //Keeps the value of the card in the same position as the card
Deck[i][Face] = Deck[Random][Face];  //Keeps the type of card in the same position as the card
strcpy(Name[i], Name[Random]); //Keeps the name of the card in the correct position

Deck[Random][Position] = TempPosition;  //Sets the number in the randomly picked position to the number that was held in Temp
Deck[Random][Suit] = TempSuit; //Keeps the suit of the card in the same position as that card
Deck[Random][Value] = TempValue; //Keeps the value of the card in the same position as the card
Deck[Random][Face] = TempFace; //Keeps the type of card in the same position as the card
strcpy(Name[Random],TempName); //Keeps the name of the card in the correct position
//**Randomly sorts the cards keeping suits and values with the correct card position
}

}


void main()
{

CreateDeck();
Shuffle();
for (i=0; i<52; i++)
{
cout << Name[i] << ", " ; //Displays the sorted deck
}

}






Project Homepage: