Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple card dealer

Status
Not open for further replies.

0x4B47

Programmer
Jun 22, 2003
60
US
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?
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! :)
 
I tried passing the whole player array itself but still no joy. My new function looks like this:
Code:
void deal( const int wDeck[][FACE],
		   const char* wFace[], 
		   const char* wSuit[], 
		   char* player[],
		   int cardNum
		 )
{
	static int topCard = 1;
	char card[18] = {0};

	cout << "Inside deal: cardNum = " << cardNum << endl;
	for(int row=0; row<=3; ++row )
	{
		for( int column=0; column<=12; ++column )
		{
			if( wDeck[row][column] == topCard )
			{
				strcpy( card, wFace[column] );
				strcat( card, " of " );
				strcat( card, wSuit[row] );
				strcpy( player[cardNum], card );
			}
		}
	}
	cout << card << endl;
	
	++topCard;
}

I've also tried doing this inside deal()
Code:
strcpy( card, wFace[column] );
				strcat( card, " of " );
				strcat( card, wSuit[row] );
				player[cardNum] = card;

that doesn't work either, as soon as I use the cstring library functions with the player arrays I get a run-time error. Someone out there please help, before I rip my hair out and become a monk lookalike!

Kunal
 
There are a couple of problems with version 1 of the software, which should move you forward when fixed.

1. char* player1[5]
Make this
char player1[5][18];

2. player1[ whichCard ] = cardDealt;
Make this
strcpy( player1[ whichCard ], cardDealt );

--
 
Salem,

Thanks for that, it worked, but its strange because I tried that way earlier and it didnt work. I got an error from strcpy when i passed player[], it said something like cannot convert player[][18] to player[]. Also another thing I didnt get is why I cant use 'char* player[18]' coz essentially the array name is a const pointer to the first element, therefore shouldn't doing strcpy(player, card); be ok?

Thanks again,
Kunal.
 
> it said something like cannot convert player[][18] to player[].
Sounds like you tried the assignment which was in your code.

> therefore shouldn't doing strcpy(player, card); be ok?
Not the way you had it declared - as char *player[5]
You initialised it with constant strings, so any attempt to modify them using strcpy() would result in a run-time fault.

Previously, you had
player1[ whichCard ] = cardDealt;
which is valid, but not what you want. cardDealt is an array, and the assignment just copies the pointer to that array. The result you end up seeing is that all 5 entries end up pointing to the SAME array, and thus when you print player, they all have the same result (because its all the same array).

My char player1[5][18]; actually allocates space you can write into with strcpy(), and thus ensures that all 5 answers can be stored separately.

> coz essentially the array name is a const pointer to the first element,
But that isn't the whole story.
Whilst they share much of the same syntax, and you do get a "pointer to the first element" WHEN you pass the array as a function parameter, there are some very real and very important differences as well.
Here's more

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top