9 Replies - 672 Views - Last Post: 21 December 2011 - 07:40 AM Rate Topic: ***-- 2 Votes

#1 kov628  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-December 11

Simple question regarding blackjack code

Posted 20 December 2011 - 06:13 PM

I have a complete blackjack game program done. And I have an array that makes sure the cards do not come up more than once. I just want to add a simple function that will alert me when all 52 cards in the deck run out. I am unsure on how to successfully do that. I also would like to make a seperate test driver in the playerloop (and maybe also dealerloop?) so that the game will play continuously. If anyone could show me hows it's done that would be great! Here is was I have so far. The error I believe that shows up is from the viod drawCard function..but I'm not sure?
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;


void drawCard (int &face, int &suit, int drawn[])
{
	int counter = 0;
	counter ++;
	cin >> drawn [counter];

	if (drawn [counter] <= drawn [52])
	{
		do
		{
			face = (rand()%13)+1;
			suit = (rand()%4)+1;
			
		} while
			(drawn[(face+(13*(suit-1)))-1]==1);

		drawn[(face+(13*(suit-1)))-1]=1;	
	}
	else (drawn [counter] > drawn [52]);
		cout << "Error, The deck is out of cards!!!" << endl;
}



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 getCardPoints (int value, int & aceValue)
{
	if ((value >=2) && (value<=10))
	{
		return (value);
	}
	else if (value==1)
	{
		aceValue = 10; //11-1
		return 1;
	}		
	else if (value ==11)
		{
			return 10;
		}
	else if (value ==12) 
	{
		return 10;
	}
	else if (value== 13)
	{
		return 10;
	}
	else
	{
		cout << "Error! Invalid card face." << endl;
		exit (0);
	}
	
}
int playerLoop (int playerTotal, int & aceValue, int drawn[])
{
	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;
	int nextSuit;
	drawCard (nextValue, nextSuit, drawn);
	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 drawn[])
{
	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;
		if (aceValue >0)
			cout << " or " <<dealerTotal+aceValue;
		cout << endl;
		int nextValue;
		int nextSuit;
		
		drawCard (nextValue, nextSuit, drawn);
		cout << "Dealer is dealt ";
		displayCard (nextValue, nextSuit);
		cout << endl;
		dealerTotal = dealerTotal +getCardPoints (nextValue, aceValue);
		if (dealerTotal +aceValue <22)
		{
			bestHand = dealerTotal + aceValue;
		}
		else
			bestHand=dealerTotal;		
	}

	return dealerTotal;
}


int main()
{
	int drawn [52] = {0};
	int face=0, suit=0 , index;
	
	index= (face + (13*(suit-1))) -1;

	srand ((unsigned)time (0));

	int playerC1_value=0;
	int playerC1_suit=0;
	int dealerC1_value=0;
	int dealerC1_suit=0;
	int playerC2_value=0;
	int playerC2_suit=0;
	int dealerC2_value=0;
	int dealerC2_suit=0;

	drawCard (playerC1_value, playerC1_suit, drawn);
	drawCard (dealerC1_value, dealerC1_suit, drawn);
	drawCard (playerC2_value, playerC2_suit, drawn);
	drawCard (dealerC2_value, dealerC2_suit, drawn);

	cout << "Player has ";
	displayCard(playerC1_value, playerC1_suit);
	cout<< " and ";
	displayCard(playerC2_value,playerC2_suit);
	cout <<endl;
	cout << "Dealer has ";
	displayCard (dealerC1_value, dealerC2_suit);
	cout << " showing "<<endl;
	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);
	
	playerTotal = playerLoop(playerTotal, playerAceValue, drawn);
	dealerTotal = dealerLoop(dealerTotal, dealerAceValue, drawn);
	

}








Is This A Good Question/Topic? 0
  • +

Replies To: Simple question regarding blackjack code

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Simple question regarding blackjack code

Posted 20 December 2011 - 07:23 PM

What are you doing here:

void drawCard (int &face, int &suit, int drawn[])
{
	int counter = 0;
	counter ++;
	cin >> drawn [counter];

	if (drawn [counter] <= drawn [52])


Every time you call this function, you initialize counter to 0, then increment it to 1.

And then what, the program waits for some number to appear in stdin, and when it does it is stored in drawn[1]. And then, if that supplied value is less than or equal to the value in drawn[52] (which is the memory space immediately following the end of the array) you enter a do-while loop that generates random cards until it produces one that hasn't been picked before.

But what is the meaning of the number that's being input from stdin, and why are you comparing it to drawn[52]? What were you trying to accomplish there?

If you're just trying to see when 52 cards have been dealt, you can just make counter a static variable, initialize it to 0, test it to see if it's still less than 52 before dealing, and increment it by 1 each time a card is successfully dealt.
Was This Post Helpful? 1
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Simple question regarding blackjack code

Posted 20 December 2011 - 07:37 PM

I should have asked: do you know what a static variable (declared in a function) is?

This post has been edited by r.stiltskin: 20 December 2011 - 07:39 PM

Was This Post Helpful? 0
  • +
  • -

#4 kov628  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-December 11

Re: Simple question regarding blackjack code

Posted 20 December 2011 - 07:43 PM

View Postr.stiltskin, on 20 December 2011 - 07:23 PM, said:

If you're just trying to see when 52 cards have been dealt, you can just make counter a static variable, initialize it to 0, test it to see if it's still less than 52 before dealing, and increment it by 1 each time a card is successfully dealt.


Thanks for the reply. Yes I am trying to see when the 52 cards ar dealt. I also would like to put and a message when the cards are out. Such as Error, deck is out of cards.

My finished inital program had the void drawCard function as follows:
void drawCard (int &face, int &suit, int drawn[])
	{
		do
		{
			face = (rand()%13)+1;
			suit = (rand()%4)+1;
		} while 
			(drawn[(face+(13*(suit-1)))-1]==1);

		drawn[(face+(13*(suit-1)))-1]=1;
	}




No I am trying to implement a counter that stops after 52 cards like you said. I motifyed it abit from before:
void drawCard (int &face, int &suit, int drawn[])
{
	int counter = 0;

	if (drawn [counter] <= drawn[52])
	{
		do
		{
			face = (rand()%13)+1;
			suit = (rand()%4)+1;
			
		} while
			(drawn[(face+(13*(suit-1)))-1]==1);

		drawn[(face+(13*(suit-1)))-1]=1;	
	}
	else 
		cout << "Error, The deck is out of cards!!!" << endl;
}





Would you be able to let me know how i can get this to work? I believe my IF statement is wrong for sure.

Thanks so much
Was This Post Helpful? 0
  • +
  • -

#5 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Simple question regarding blackjack code

Posted 20 December 2011 - 07:54 PM

A static variable is a variable that is declared using the static keyword, for example:
int my_func( /*arguments*/ ) {
    static int counter = 0;
// other code ...
    counter++;
    return counter;
}


If a static variable is declared in a function, it retains its value from one call to the next. In other words, when my main function calls my_func, you can see that before my_func returns, it increments counter to 1. Later, if my_func is called again, it is NOT re-initialized to 0. It retains the value 1 that it had at the end of the previous call. So at the end of the 2nd call counter ends up with 2 as its value. At the end of the 3rd call its value is 3, and so on.

By now I'm sure you can see how you can use that in your program.
Was This Post Helpful? 0
  • +
  • -

#6 kov628  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-December 11

Re: Simple question regarding blackjack code

Posted 20 December 2011 - 08:25 PM

Thank you so much it makes alot of sense. I tried playing around with it and I still can't actually get it to go through. I think it might just be some small formatting errors. Im unsure whether to make a seperate function which models the one you showed and call it into the current drawCard function i have or to implement it directly into drawCard function
Was This Post Helpful? 0
  • +
  • -

#7 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Simple question regarding blackjack code

Posted 20 December 2011 - 08:31 PM

I don't see any reason not to do it directly in drawCard.
Was This Post Helpful? 0
  • +
  • -

#8 kov628  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 15-December 11

Re: Simple question regarding blackjack code

Posted 20 December 2011 - 08:43 PM

Ok thanks for helping me. Its making a lot more sense to me now. The problem i have now id the I have to run my drawCard as a void. This in not allowing me to reconize the counter on the return. Do you have any suggeations?
void drawCard (int &face, int &suit, int drawn[])
	{
		static int counter = 0;
		{
		do
		{
			face = (rand()%13)+1;
			suit = (rand()%4)+1;

		} while 
			(drawn[(face+(13*(suit-1)))-1]==1);

		drawn[(face+(13*(suit-1)))-1]=1;
		
		}
		counter++;
		return counter;

	}




Also here is the entire program i am trying to implement it in to. Thanks so much!!!



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




void drawCard (int &face, int &suit, int drawn[])
	{
		do
		{
			face = (rand()%13)+1;
			suit = (rand()%4)+1;

		} while
			(drawn[(face+(13*(suit-1)))-1]==1);

		drawn[(face+(13*(suit-1)))-1]=1;	
	}

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 getCardPoints (int value, int & aceValue)
{
	if ((value >=2) && (value<=10))
	{
		return (value);
	}
	else if (value==1)
	{
		aceValue = 10; //11-1
		return 1;
	}		
	else if (value ==11)
		{
			return 10;
		}
	else if (value ==12) 
	{
		return 10;
	}
	else if (value== 13)
	{
		return 10;
	}
	else
	{
		cout << "Error! Invalid card face." << endl;
		exit (0);
	}
	
}
int playerLoop (int playerTotal, int & aceValue, int drawn[])
{
	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;
	int nextSuit;
	drawCard (nextValue, nextSuit, drawn);
	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 drawn[])
{
	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;
		if (aceValue >0)
			cout << " or " <<dealerTotal+aceValue;
		cout << endl;
		int nextValue;
		int nextSuit;
		
		drawCard (nextValue, nextSuit, drawn);
		cout << "Dealer is dealt ";
		displayCard (nextValue, nextSuit);
		cout << endl;
		dealerTotal = dealerTotal +getCardPoints (nextValue, aceValue);
		if (dealerTotal +aceValue <22)
		{
			bestHand = dealerTotal + aceValue;
		}
		else
			bestHand=dealerTotal;		
	}

	return dealerTotal;
}


int main()
{
	int drawn [52] = {0};
	int face=0, suit=0 , index;
	
	index= (face + (13*(suit-1))) -1;

	srand ((unsigned)time (0));

	int playerC1_value=0;
	int playerC1_suit=0;
	int dealerC1_value=0;
	int dealerC1_suit=0;
	int playerC2_value=0;
	int playerC2_suit=0;
	int dealerC2_value=0;
	int dealerC2_suit=0;

	drawCard (playerC1_value, playerC1_suit, drawn);
	drawCard (dealerC1_value, dealerC1_suit, drawn);
	drawCard (playerC2_value, playerC2_suit, drawn);
	drawCard (dealerC2_value, dealerC2_suit, drawn);

	cout << "Player has ";
	displayCard(playerC1_value, playerC1_suit);
	cout<< " and ";
	displayCard(playerC2_value,playerC2_suit);
	cout <<endl;
	cout << "Dealer has ";
	displayCard (dealerC1_value, dealerC2_suit);
	cout << " showing "<<endl;
	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);
	
	playerTotal = playerLoop(playerTotal, playerAceValue, drawn);
	dealerTotal = dealerLoop(dealerTotal, dealerAceValue, drawn);
	
	string answer;
 
	{
		cout << "At the end, player has ";
		cout << playerTotal;
		if (playerAceValue >0)
		cout << " or " <<playerTotal+playerAceValue;
		cout << " points ";
		cout << endl;
	cout << "At the end, dealer has ";
		cout << dealerTotal;
		if (dealerAceValue >0)
		cout << " or " <<dealerTotal+dealerAceValue;
		cout << " points ";
		cout << endl;
	}


	if (playerTotal >=22)
	{
		cout << "Player busted" << endl;
	}
	else 
	{
		if ((playerTotal+playerAceValue > dealerTotal+dealerAceValue)&& (playerTotal+playerAceValue<22))
		{
			cout << "Player wins!" << endl;
		}
	
		else
		{
		if (dealerTotal >=22){
		cout << "Dealer busted -- Player wins!" << endl;
		}
		else  
		if ((dealerTotal+dealerAceValue > playerTotal+playerAceValue) && (dealerTotal+dealerAceValue<22))
		{
		cout << "Dealer wins" << endl;
		}
		if (dealerTotal+dealerAceValue == playerTotal+playerAceValue) 
		{
		cout << "Draw" << endl;
		}
		}
	}

}



Was This Post Helpful? 0
  • +
  • -

#9 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1833
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Simple question regarding blackjack code

Posted 20 December 2011 - 08:50 PM

View Postkov628, on 20 December 2011 - 10:43 PM, said:

Ok thanks for helping me. Its making a lot more sense to me now. The problem i have now id the I have to run my drawCard as a void. This in not allowing me to reconize the counter on the return. Do you have any suggeations?

Don't return anything. Just eliminate that line (your line #17).

But you still have to add code to that function to use the counter -- to make sure counter is < 52 before you try to generate another card.
Was This Post Helpful? 0
  • +
  • -

#10 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 5780
  • View blog
  • Posts: 12,595
  • Joined: 16-October 07

Re: Simple question regarding blackjack code

Posted 21 December 2011 - 07:40 AM

So you're holding a boolean list ( why isn't it bool? ) of cards you've already done. You are constantly converting the face and suit into a single number to check that list. And, you never quite know when you're done. Also, you have to keep spinning until you find one you haven't done.

For simplicity, your card value can be expressed as a single number:
const int SUITS = 4;
const int FACES = 13;
const int DECK_SIZE = 52;

int getRandomCard() { return rand()%DECK_SIZE; }

int getFaceNum(int cardValue) { return cardValue%FACES; }
int getSuitNum(int cardValue) { return cardValue/FACES; }



This would make your lookup vastly easier.

However, what if you just stored all possable values in a list? Then randomly removed a value from that list on each deal, decrementing the list size, until the size is zero?

e.g.
struct Deck {
	int card[DECK_SIZE];
	int size;
	Deck() : size(DECK_SIZE) { for(int i=0; i<DECK_SIZE; i++) { card[i] = i; } }
	int deal() { // random pick
		if (size==0) { return -1; }
		int cardIndex = rand()%size;
		int cardValue = card[cardIndex];
		card[cardIndex] = card[--size];
		return cardValue;
	}
};



A card class would also be nice. Hope this helps.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1