0
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <string>
using namespace std;

int getCardPoints(int value, int & aceValue)
{
	if ((value >= 2) && (value <= 10))
	{
		return (value);
	}
	else if (value == 1)
	{
		aceValue = 10; // 11 -1
		return 1; // Already contain 1 for the Ace value
	}
	else if (value == 11)
	{
		return 10;
	}
	else if (value == 12)
	{
		return 10;
	}
	else if (value == 13)
	{
		return 10;
	}
	else
	{
		cout << "Error! Invalid card value." << endl;
		exit (0);
	}
}
void displayCardFace(int face)
{
	if ((face >= 2) && (face <= 10))
	{
		cout << face;
	}
	else if (face == 1)
	{
		cout << "Ace";
	}
	else if (face == 11)
	{
		cout << "Jack";
	}
	else if (face == 12)
	{
		cout << "Queen";
	}
	else if (face == 13)
	{
		cout << "King";
	}
	else
	{
		cout << "Error! Invalid card face." << endl;
		exit(0);
	}
}
void displayCardSuit(int suit)
{
	if (suit == 1)
	{
		cout << " of spades";
	}
	else if (suit == 2)
	{
		cout << " of clubs";
	}
	else if (suit == 3)
	{
		cout << " of diamonds";
	}
	else if (suit == 4)
	{
		cout << " of hearts";
	}
	else
	{
		cout << "Error! Invalid card suit." << endl;
		exit(0);
	}
}
void displayCard (int face, int suit)
{
	displayCardFace(face);
	displayCardSuit(suit);
}
int playerLoop(int playerTotal, int & aceValue)
{
	string answer;
	do
	{
		cout << "Player's current hand point value is ";
		cout << playerTotal;
		if (aceValue >0)
			cout << " or " << playerTotal + aceValue;
		cout << endl;
		cout << "Do you want another card? (\"hit\" or \"stay\"): ";
		getline(cin, answer);
		if (answer == "hit")
		{
			int nextValue = (rand()%13)+1;
			int nextSuit = (rand()%4)+1;
			cout << "Player is dealt ";
			displayCard(nextValue, nextSuit);
			cout << endl;
			playerTotal = playerTotal + getCardPoints(nextValue, aceValue);
		}
	} while ((answer=="hit") && (playerTotal <= 21));
	return playerTotal;
}
int dealerLoop(int dealerTotal, int & aceValue)
{
	int bestHand;
	if (dealerTotal + aceValue < 22)
	{
		bestHand = dealerTotal + aceValue;
	}
	else
	{
		bestHand = dealerTotal;
	}
	while (bestHand < 17)
	{
		cout << "Dealer's current hand point value is ";
		cout << dealerTotal << endl;
		if (aceValue > 0)
			cout << " or " << dealerTotal + aceValue << endl;
		int nextValue = (rand()%13)+1;
		int nextSuit = (rand()%4)+1;
		cout << "Dealer dealt ";
		displayCard(nextValue, nextSuit);
		cout << endl;
		
		dealerTotal = dealerTotal + getCardPoints(nextValue, aceValue);
		
		if (dealerTotal + aceValue < 22)
		{
			bestHand = dealerTotal + aceValue;
		}
		else
			bestHand = dealerTotal;
		//cout << "Dealer's current hand point value is "; 
		//cout << dealerTotal << endl;
	}
	return dealerTotal;
}




int main()
{
	// reset the random number generator
	srand((unsigned)time(0));
	int playerC1_value = (rand()%13) +1;	
	int playerC1_suit = (rand()%4) + 1; 	
	int dealerC1_value = (rand()%13) +1;	
	int dealerC1_suit = (rand()%4) + 1;	
	int playerC2_value = (rand()%13) +1;	
	int playerC2_suit = (rand()%4) + 1; 	
	int dealerC2_value = (rand()%13) +1;	
	int dealerC2_suit = (rand()%4) + 1;
	/*=== Fix the data for testing
	int playerC1_value = 1;
	int playerC1_suit = 1;
	int dealerC1_value = 1;
	int dealerC1_suit = 2;
	int playerC2_value = 1;
	int playerC2_suit = 3;
	// === Fix the data for testing */

	cout << "Player has ";
	displayCard(playerC1_value, playerC1_suit);
	cout << " and ";
	displayCard(playerC2_value, playerC2_suit);
	cout << endl;
	cout << "Dealer has ";
	displayCard(dealerC1_value, dealerC1_suit);
	cout << " showing " << endl;
	//figure player total hand value
	int playerAceValue = 0;
	int dealerAceValue = 0;
	int playerTotal = getCardPoints(playerC1_value, playerAceValue) + getCardPoints(playerC2_value, playerAceValue);
	int dealerTotal = getCardPoints(dealerC1_value, dealerAceValue) + getCardPoints(dealerC2_value, dealerAceValue);
	
	cout << "So far, player has " << playerTotal << " or " << playerAceValue+playerTotal << " points" <<endl;
	cout << "So far, dealer has " << dealerTotal << " or " << dealerAceValue+dealerTotal << " points" <<endl;

	playerTotal = playerLoop(playerTotal, playerAceValue);	
	dealerTotal = dealerLoop(dealerTotal, dealerAceValue);

	

}

When I run this code, it doesn't show dealer's current point at the end after dealer gets another card. Please tell me what's wrong.

2
Contributors
1
Reply
6
Views
5 Years
Discussion Span
Last Post by daviddoria
-1

Have you tried stepping through the code with a debugger? Once you do that, if you are still stuck, I would try to narrow down the problem. It is unlikely anyone is going to look through your 200 lines of code. If you can hardcode values into the program to get it into the last state you think it is correct, then you can ask a much more direct question. However, if you prepare the question correctly it is likely you will find your own answer :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.