I have attempted to incorperate all the suggestions you guys have given and will continue on with a 1 dimensional array deviating from the book a bit. Here is my code unfortunately I can
't spot the error(s).
Code:
#include <iostream>
using std::cout;
using std::left;
using std::right;
using std::endl;
#include <stdlib.h>
#include <iomanip>
using std::setw;
#include <cstdlib>
#include <ctime>
const int PLAYERS=2;
const int CARDS=5;
void shuffle( char *,int,int);
void deal(char [52],int [PLAYERS][CARDS]);
void showhand(int [PLAYERS][CARDS],char [52],const char *[],const char *[]);
int main(int argc, char *argv[])
{
//initialize suit array
const char *suit[4]={"Hearts","Diamonds","Clubs","Spades"};
//initialize face array
const char *face[13]={"Ace","Deuce","Three","Four","Five","Six",
"Seven","Eight","Nine","Ten","Jack","Queen","King"};
//initialize deck array
char deck[52];
for (int i=0;i<52;i++)
deck[i]=i;
int hands[PLAYERS][CARDS]={0};
srand((unsigned)time(NULL));
shuffle(deck,52,1);
deal(deck,hands);
showhand(hands,deck,face,suit);
system("PAUSE");
return 0;
}
//shuffle cards in deck
void shuffle(char deckPtr[52], int deck_size, int shuffle_count)
{
char cTemp;
int random_pos_0, random_pos_1;
srand( (unsigned)time( NULL ) );
for (int i=0; i<shuffle_count; ++i)
{
random_pos_0 = rand() * deck_size / RAND_MAX;
random_pos_1 = rand() * deck_size / RAND_MAX;
cTemp = deckPtr[random_pos_0];
deckPtr[random_pos_0] = deckPtr[random_pos_1];
deckPtr[random_pos_1] = cTemp;
}
}
//deal cards in deck
void showhand(int handsPtr[PLAYERS][CARDS],char * deckPtr, const char *wFace[],const char *wSuit[])
{
for (int player=0;player<=PLAYERS-1;player++){
cout << "\nPlayer number "<<player<<endl;
for (int c=0;c<=CARDS-1;c++)
cout << setw(5)<<right<<wFace[handsPtr[player][c]%13]
<< " of " << setw(8)<< left
<< wSuit[handsPtr[player][c]/4]<<endl;
}
}
void deal(char deckPtr[52],int handsPtr[PLAYERS][CARDS])
{
if ((PLAYERS*CARDS)>52){
cout << "There are only 52 cards in the deck!"<<endl;
return;
}
for (int i,j=0; i<((PLAYERS*CARDS));i++,j++)
{
handsPtr[i % PLAYERS][i % CARDS]=deckPtr[j];
}
}
Here is the output:
Player number 0
Ace of Hearts
Ace of Hearts
Ace of Hearts
Ace of Hearts
Ace of Hearts
Player number 1
Ace of Hearts
Ace of Hearts
Ace of Hearts
Ace of Hearts
Ace of Hearts
Press any key to continue . . .