Hi fellow coders,
I am having a bit of trouble simulating a deal between two card game players. What my program is supposed to do is deal 5 cards to each player one card at a time. I'm storing this dealt card string in a temp var called 'cardDealt' before copying it into a 5 element 'char* player' array. But what my program does is overwrite all elements of both arrays regardless of whether I am writing only to player1 array or player2 array. I've spent ages trying to sift this bug but I guess I just can't figure out what I'm doing wrong. Here is the code, please can someone enlighten me?
I know some of the coding is really bad practice, like my shuffling method had 'indefinite postponement' and also im initialising my player arrays with space chars, but i was just trying to get the code working!
I am having a bit of trouble simulating a deal between two card game players. What my program is supposed to do is deal 5 cards to each player one card at a time. I'm storing this dealt card string in a temp var called 'cardDealt' before copying it into a 5 element 'char* player' array. But what my program does is overwrite all elements of both arrays regardless of whether I am writing only to player1 array or player2 array. I've spent ages trying to sift this bug but I guess I just can't figure out what I'm doing wrong. Here is the code, please can someone enlighten me?
Code:
#include <iostream>
using std::cout;
using std::endl;
#include <iomanip>
#include <cstdlib>
#include <ctime>
#include <cstring>
const int FACE = 13;
const int SUIT = 4;
void shuffle( int wDeck[][FACE] );
void deal( const int wDeck[][FACE], const char* wFace[], const char* wSuit[], char cDealt[] );
int main()
{
// Local variables
char cardDealt[18] = { 0 };
int whichCard = 0;
int whichPlayer = 1;
int deck[SUIT][FACE] = { 0 };
char* player1[5] =
{
" ",
" ",
" ",
" ",
" "
};
char* player2[5] =
{
" ",
" ",
" ",
" ",
" "
};
const char* suit[SUIT] = { "Hearts", "Diamonds", "Clubs", "Spades" };
const char* face[FACE] =
{
"Ace", "Two", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King"
};
// Seed random function
srand( time(0) );
// Shuffle deck
shuffle( deck );
int i;
// Deal 5 cards to both players
while( whichCard < 5 )
{
switch( whichPlayer )
{
case 1:
deal( deck, face, suit, cardDealt );
cout << "\nPlayer1: " << cardDealt << endl;
cout << "WhichCard = " << whichCard << endl;
cout << "Whichplayer = " << whichPlayer << endl;
cout << strlen(player1[whichCard]) << ", " << strlen(cardDealt) << endl;
//strcpy( player1[ whichCard ], cardDealt );
player1[ whichCard ] = cardDealt;
whichPlayer = 2;
for( i=0; i<5; ++i)
cout << "Player1[" << i << "]: " << player1[i] << endl;
break;
case 2:
deal( deck, face, suit, cardDealt );
cout << "\nPlayer2 cardDealt: " << cardDealt << endl;
cout << "WhichCard = " << whichCard << endl;
cout << "Whichplayer = " << whichPlayer << endl;
cout << strlen(player1[whichCard]) << ", " << strlen(cardDealt) << endl;
//strcpy( player2[ whichCard ], cardDealt );
player2[ whichCard ] = cardDealt;
whichPlayer = 1;
++whichCard;
for( i=0; i<5; ++i)
cout << "Player2[" << i << "]: " << player2[i] << endl;
break;
default:
// It should never come to this!!!!
cout << "Invalid player!" << endl;
break;
}
}
cout << "\n\nFinal:" << endl;
for( i=0; i<5; ++i )
cout << "Player1[" << i << "]: " << player1[i] << "Player2[" << i << "]: " << player2[i] << endl;
return 0;
}
void shuffle( int wDeck[][FACE] )
{
int row, column;
for(int card=1; card <= 52; ++card )
{
do
{
row = rand() % 4;
column = rand() % 13;
}
while( wDeck[row][column] != 0 );
wDeck[row][column] = card;
}
}
void deal( const int wDeck[][FACE], const char* wFace[], const char* wSuit[], char cDealt[] )
{
static int topCard = 1;
for(int row=0; row<=3; ++row )
{
for( int column=0; column<=12; ++column )
{
if( wDeck[row][column] == topCard )
{
strcpy( cDealt, wFace[column] );
strcat( cDealt, " of " );
strcat( cDealt, wSuit[row] );
}
}
}
++topCard;
}
I know some of the coding is really bad practice, like my shuffling method had 'indefinite postponement' and also im initialising my player arrays with space chars, but i was just trying to get the code working!