Blackjack

This is a discussion on Blackjack within the C++ Programming forums, part of the General Programming Boards category; I was just curios because I am making a console blackjack game as well. I used a shuffle functions which ...

  1. #16
    Registered User manofsteel972's Avatar
    Join Date
    Mar 2004
    Posts
    317
    I was just curios because I am making a console blackjack game as well. I used a shuffle functions which incrments through the array swaping with a random slot in array. I initialzed the array with a loop in the range 1-52 and then start swapping.

    Code:
    	for (int i=0;i<52;i++)
    		{
    			deck[i]=i;
    		}
    
    
    int* shuffle(int *deck)
    {
    	
    
    	for(int x=0;x<4;x++)	//SHUFFLES DECK 4 TIMES
    	{
    		for(int i=0;i<52;i++)
    		{
    			int random=rand()%52;
    			int temp=deck[i];
    			deck[i]=deck[random];
    			deck[random]=temp;
    		}
    	}
    	return deck;
    }
    "Knowledge is proud that she knows so much; Wisdom is humble that she knows no more."
    -- Cowper

    Operating Systems=Slackware Linux 9.1,Windows 98/Xp
    Compilers=gcc 3.2.3, Visual C++ 6.0, DevC++(Mingw)

    You may teach a person from now until doom's day, but that person will only know what he learns himself.

    Now I know what doesn't work.

    A problem is understood by solving it, not by pondering it.

    For a bit of humor check out xkcd web comic http://xkcd.com/235/

  2. #17
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    There are several versions of a function called random_shuffle() available in the algorithm file of the C++ Standard Template Library, when you get that far in your journey.

  3. #18
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Post

    Got my program to compile.. although it is still not perfect. If anyone would like to take a look at my code and offer some suggestions.. that would be wicked cool.



    Here is the code I have come up with so far.


    Code:
    #include<algorithm>	//Provides swap( ) 
    #include<iostream>	//Provides 'cin' and 'cout'
    #include<sstream>	//Provides 'string to int' conversion
    #include<cstdlib>	//Provides system( ) and rand( )
    #include<string>	//Provies 'string' type qualifier
    #include<cctype>	//Provides toupper( )
    #include<conio.h>	//Provides getch()
    
    using namespace std;
    
    
    
    class blackjack
    {
    
    public:
    
    	//CONSTRUCTOR
    	blackjack();
    
    	//MUTATORS
    	void shuffle_deck();
    	void the_dealer(char);
    	void winner_determination(char);
    
    	//ACCESSORS
    	void display_hand();	
    
    	//CONSTANTS	
    	bool is_bust(int) const;	
    
    	//FRIENDS
    	friend int card_value(string);
    
    private:
    
    	string deck[4][13];
    	string player_hand[10];
    	string dealer_hand[10];
    	int counter_1D;
    	int counter_2D;
    	int ph_counter;
    	int dh_counter;
    	int playerhand_value;
    	int dealerhand_value;
    };
    
    //NONMEMBER FUNCTION PROTOTYPES
    char get_user_command();
    void exit_message();
    
    
    int main()
    {
    
    	blackjack card;
    
    	char choice = 'x';
    
    	system("cls");
    
    
    	do
    	{
    		//card.shuffle_deck();  Need to review the shuffle algorithm
    			
    		card.display_hand();
    				
    		choice = toupper(get_user_command());
    				
    		card.the_dealer(choice);	
    
    		
    		if (choice != 'Q')
    		
    			card.winner_determination(choice);
    
    		cout << endl;
    		system("cls");
    
    	}while (choice != 'Q');
    
    
    exit_message();
    
    return 0;
    
    }//end MAIN()	
    
    
    
    
    
    ////////////////////////////////////////////
    /////////// FUNCTION DEFINITIONS ///////////
    ////////////////////////////////////////////
    
    blackjack::blackjack()
    {
    	
    	//Initialize card counter member variables.
    	counter_1D = 0;   
    	counter_2D = 0;
    	ph_counter = 0;
    	dh_counter = 0;
    
    
    	//Initialize player & dealer hand string arrays
    	for(int h = 0; h < 10; h++)
    	{
    		player_hand[h] = "Test";
    		dealer_hand[h] = "Test";
    	}
    
    	
    	//Initialize deck[][]
    	string deck[4][13]; //create a deck of cards, 4 suits, 13 cards per suit (2 thru Ace)
    	string suite, face_value, card;
    
    	for (int i = 0; i < 4; i++)  //Use the outter FOR loop to determine the suite of deck[suite][face_value]
    	{	
    		switch(i)
    		{
    
    			case 0: suite = "Hearts";
    					break;
    
    			case 1: suite = "Diamonds";
    					break;
    
    			case 2: suite = "Clubs";
    					break;
    
    			case 3: suite = "Spades";
    					break;
    			    
    			default:  cout << "\n\nsuite loop overflow\n";
    
    		}//end switch
    
    			for (int j = 0; j < 13; j++)    //The Inner FOR loop will assign card face values
    			{	
    					switch(j)
    					{
    						case 0:  face_value = "2";
    								 break;
    				             
    						case 1:  face_value = "3";
    								 break;
                                 
    						case 2:  face_value = "4";
    						                 break;
                                 
    						case 3:  face_value = "5";
    								 break;
                        
    						case 4:  face_value = "6";
                                                                     break;
                                 
    						case 5:  face_value = "7";
    								 break;
                        
    						case 6:  face_value = "8";
    								 break;
                                 
    						case 7:  face_value = "9";
    								 break;
                                 
    						case 8:  face_value = "10";
    								 break;                   
    
    						case 9:  face_value = "Jack";
    								 break;
    
    						case 10: face_value = "Queen";
    								 break;
    
    						case 11: face_value = "King";
    								 break;
    
    						case 12: face_value = "Ace";
    								 break;
    						     
    						default: cout << "\n\nface_value loop overflow\n";
    
    					}//end switch		              
    			
    
    			//This block will create a "card" to be assigned to the deck
    			card = face_value; 
    			card += " of ";
    			card += suite;
    		 
    			deck[i][j] = card; 
    
    		}//end INNER FOR
    			
    
    	}//end OUTTER FOR
    
    
    }
    
    
    /*
    void blackjack::shuffle_deck()
    {
    	for (int x = 0; x < 5; x++)	//shuffles the deck 4 times
    		
    		for (int y = 0; y < 4; y++)
    			for (int z = 0; z < 13; z++) 
        
    				swap(deck[y][z], deck[rand()%4][rand%13]);
    
    }
    */
    
    
    void blackjack::display_hand()
    {
    	cout << endl << endl << " Dealer's Hand:\n\n";
    
    	for (int i = 0; i < dh_counter; i++)
    
    		cout << "\t" << dealer_hand[i];
    
    	cout << endl << endl << "\n\n Your Hand:\n\n";
    
    		
    	for (int j = 0; j < ph_counter; j++)
    
    		cout << "\t" << player_hand[j];
    
    	cout << endl << endl << endl;
    
    }
    
    
    
    char get_user_command()
    {
    
    	char command = 'x';
    	
    	cout << "\n\n 'H' to HIT"
    		 << "\n 'S' to STAY"
    		 << "\n 'Q' to QUIT\n\n";
    	
    	command = getch();
    
    	return command;
    
    }
    
    
    
    void blackjack::the_dealer(char choice)
    {	
    	
    	if (counter_2D >= 13) //This will advance deck to the second 'row' if row[0] has been used
    	{
    		++counter_1D;
    		counter_2D = 0;
    	}
    
    
    	else if (choice == 'H')
    	{	
    	
    		string card = deck[counter_1D][counter_2D++];	//Pick a card starting from the top of the deck (deck[0][0])
    
    		player_hand[ph_counter++] = card;				//Add card 'string' to players hand
    
    		playerhand_value += card_value(card);			//Add the cards numerical value to the players hand
    
    	}
    	
    		
    	else if (choice == 'S')
    	{		
    		do
    		{
    
    			string card = deck[counter_1D][counter_2D++];
    
    			dealer_hand[dh_counter++] = card;
    	
    			dealerhand_value += card_value(card);
    
    		
    		}while (dealerhand_value < playerhand_value);
    	}
    		
    }
    	
    
    
    bool blackjack::is_bust(int test) const
    {
    	return (test > 21);
    
    }
    	 
    
    
    void blackjack::winner_determination(char choice)
    {
    
    	
    	if (is_bust(playerhand_value) || ((playerhand_value <= dealerhand_value) && (dealerhand_value <= 21)
    		&& (choice == 'S')) || (dealerhand_value == 21))
    	{
    
    		cout << "\n You Loooooose !!!!\n\n\n"
    			 << "Press [Enter] to continue ";
    
    		cin.get();
    
    		//reset everything in preperation for the next hand
    		playerhand_value = 0;
    		dealerhand_value = 0;
    		ph_counter = 0;
    		dh_counter = 0;
    
    	}
    
    			
    	else if (is_bust(dealerhand_value) || ((playerhand_value > dealerhand_value) && (playerhand_value < 21)
    			&& (choice == 'S')) || (playerhand_value == 21))
    	{
    				
    		cout << "\n You Weeeeeeen !!!!\n\n\n"
    			 << " Press [Enter] to continue ";
    		
    		cin.get();
    
    		//reset everything in preperation for the next hand
    		playerhand_value = 0;
    		dealerhand_value = 0;
    		ph_counter = 0;
    		dh_counter = 0;
    
    	}	
    	
    
    }
    
    
    
    //Converts the first character of a playing card from 'string' to 'int' so numeric comparisons can be made.
    //Example:  string '2 of Diamonds' will be converted to integer '2'
    int card_value(string card)  
    {
    	int result = 0;
    
    	istringstream myStream(card[0]); //create a 'myStream' object of class istringstream 
    
      
    	if (myStream >> result)  //String to Integer converstion (works only for numeric face values)
        	
    		return result;
        
    	else
    	{
        
    		char first_letter = card[0]; //string class variables can be treated like arrays  
    
    		if (first_letter != 'A')
    
    			return 10;	//"J", "Q", and "K" will return a face value of 10
    
    		else
    
    			return 11; //"A" will always return a value of 11
    
    	}
    
    }
    
    
    
    void exit_message()
    {
    	cout << "\nThank you for playing.\n\n";
    }

    If you have any q's.. please don't hesitate to ask..
    Attached Files Attached Files
    Last edited by The Brain; 08-24-2004 at 08:11 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  4. #19
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Instead of using strings why not create a Card struct, maybe something like this:

    Code:
    struct Card
    {
    int value;
    string name;
    };
    Then, in blackjack class, deck becomes:

    Card deck[52];

    You can initialize each card in the deck pretty much the way you do now, except you also assign a value to each Card at initialization. The advantage is that you don't need the card_value() function because you can access the value directly. It might also make the syntax of the shuffle() function a little less complex.

  5. #20
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Great job! Only a couple of small things I noticed when I quickly looked it over:

    It says you need to analyze the shuffle algorithm and if it is giving you errors its because you have "rand%13" instead of "rand()%13". And minor nitpick, but with that algorithm you don't need to shuffle 4 times, once is enough to get it perfectly random. Doesn't matter for a simple game but would save a LOT of calc time if you use this class for something like a simulation on probability.

    Also, I didn't see it anywhere, but don't forget to seed the random number generator, otherwise you'll get the same cards everytime you start the game.

    Last nitpick (I swear! ) You could save yourself a lot of code if you take out the big switch statements that assign the suit and face value of the card and replace it with something like this:
    Code:
    string suits[4]={"Hearts", "Diamonds", "Spades", "Clubs"};
    string values[13]={"2", "3", "4", etc etc "King", "Ace"};
    
    for (int i=0; i<4; i++)     
         for (int j=0; j<13; j++)
               deck[i][j]=values[j]+" of "+suits[i];

  6. #21
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Thanks for the tips guys.. (i can't give you guys good feedback because I have to, "spread some feedback around to others")

    Good call on that shuffle algorithm.. works great.

    Someday I'm gonna try this program again using a linked list of structs.

    Not sure how to 'seed' the random number generator... if you could provide a line or two of code that would be cool

    I did run into a roadblock on my program.. and as part of my troubleshooting technique.. I cout << parts of my program.. and when I cout'd the deck in my constructor.. all I got was garbage.... and I was wondering, "why did this algorithm work by itself as it's own program.. but doesn't work now in the constructor..??" and then it hit me.. I was creating new objects inside the constructor.. "string card;", "suite face_value", and "string face_value"..... And then I remembered that I cannot create new objects inside the constructor... you can only initialize existing private class variables...

    and although my program doesn't work as advertised yet (still need to clean up 'win/lose' logic in winner_determination( ) ) At least I got me' program working like after 4 days of troubleshooting.

    Hopefully others can learn from this.. and save themselves hours and hours of troubleshooting.
    Last edited by The Brain; 08-25-2004 at 08:02 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  7. #22
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    Quote Originally Posted by The Brain
    Not sure how to 'seed' the random number generator... if you could provide a line or two of code that would be cool
    time_t seed = SOME_SEED; // like time(NULL) for example
    srand(seed);


    edit: and make sure only do this once in your program, not everytime you generate a number

  8. #23
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Thumbs up :)

    gracias.. thank you very much
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  9. #24
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Yeah, just make sure that the seed is a different number each time the program runs otherwise you'll again get the same random numbers. Most people use time(null) which returns the number of milliseconds since midnight.

  10. #25
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Quote Originally Posted by PJYelton
    Yeah, just make sure that the seed is a different number each time the program runs otherwise you'll again get the same random numbers. Most people use time(null) which returns the number of milliseconds since midnight.
    1) Its time(NULL)
    2) time returns
    the time since the Epoch (00:00:00 UTC, January 1, 1970), measured in seconds.
    not midnight

  11. #26
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Oops, you're right

  12. #27
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    t'is done

    Here is it... the final program

    Thanks to everyone who helped out

    The biggest problems with writing this program were:

    1) declaring new objects inside the constructor (illegal in c++)

    2) istreamstring will only accept variable &addresses. It took me awhile to figure this out. I tried to stream using variable itself.. but it never worked.. I thought it looked like the example in the FAQ.. but as soon as I put in the variable address.. bingo! worked like a charm.

    so kids.. save yourself a lot of trouble.. and learn from my mistakes.

    Feel free to compile and play me' game also, please continue to critique my code.. because your feedback will help me and others become better programmers



    Code:
    #include<algorithm>	//Provides swap() 
    #include<iostream>	//Provides 'cin', 'cout', & 'NULL'
    #include<sstream>	//Provides 'string to int' conversion
    #include<cstdlib>	//Provides system(), rand() & srand()
    #include<string>	//Provies 'string' type qualifier
    #include<cctype>	//Provides toupper()
    #include<conio.h>	//Provides getch() & clrscr()
    
    using namespace std;
    
    
    
    class blackjack
    {
    
    public:
    
    	//CONSTRUCTOR
    	blackjack();
    
    	//MUTATORS
    	void shuffle_deck();
    	void the_dealer(char);
    	void winner_determination(char);
    
    	//ACCESSORS
    	void display_hand();	
    
    	//CONSTANTS	
    	bool is_bust(int) const;	
    
    	//FRIENDS
    	friend int card_value(string);
    
    private:
    
    	string deck[4][13];
    	string player_hand[10];
    	string dealer_hand[10];
    	string face_value;
    	string suite;
    	string card;
    	int counter_1D;
    	int counter_2D;
    	int ph_counter;
    	int dh_counter;
    	int playerhand_value;
    	int dealerhand_value;
    };
    
    //NONMEMBER FUNCTION PROTOTYPES
    void seed_random();
    char get_user_command();
    void exit_message();
    
    
    
    int main()
    {
    	seed_random();
    
    	blackjack card;
    
    	char choice = 'x';
    
    	clrscr();
    
    
    	do
    	{
    
    		card.shuffle_deck();
    		
    		card.display_hand();
    			
    		choice = toupper(get_user_command());
    
    		card.the_dealer(choice);
    
    		clrscr();
    
    		card.display_hand();		
    				
    		if (choice != 'Q')
    		
    			card.winner_determination(choice);
    
    		clrscr();
    
    	}while (choice != 'Q');
    
    
    exit_message();
    
    return 0;
    
    }//end MAIN()	
    
    
    
    
    
    ////////////////////////////////////////////
    /////////// FUNCTION DEFINITIONS ///////////
    ////////////////////////////////////////////
    
    blackjack::blackjack()
    {
    	
    	//Initialize card counter member variables.
    	counter_1D = 0;   
    	counter_2D = 0;
    	ph_counter = 0;
    	dh_counter = 0;
    	playerhand_value = 0;
    	dealerhand_value = 0;
    
    
    	//Initialize player & dealer hand string arrays
    	for(int h = 0; h < 10; h++)
    	{
    		player_hand[h] = "Test";
    		dealer_hand[h] = "Test";
    	}
    
    	
    	//Initialize deck[][]
    	//create a deck of cards, 4 suits, 13 cards per suit (2 thru Ace)
    	//string suite, face_value, card;
    
    	for (int i = 0; i < 4; i++)  //Use the outter FOR loop to determine the suite of deck[suite][face_value]
    	{	
    		switch(i)
    		{
    
    			case 0: suite = "Hearts";
    					break;
    
    			case 1: suite = "Diamonds";
    					break;
    
    			case 2: suite = "Clubs";
    					break;
    
    			case 3: suite = "Spades";
    					break;
    			    
    			default:  cout << "\n\nsuite loop overflow\n";
    
    		}//end switch
    
    			for (int j = 0; j < 13; j++)    //The Inner FOR loop will assign card face values
    			{	
    					switch(j)
    					{
    						case 0:  face_value = "2";
    								 break;
    				             
    						case 1:  face_value = "3";
    								 break;
                                 
    						case 2:  face_value = "4";
    						         break;
                                 
    						case 3:  face_value = "5";
    								 break;
                        
    						case 4:  face_value = "6";
                                                                      break;
                                 
    						case 5:  face_value = "7";
    								 break;
                        
    						case 6:  face_value = "8";
    								 break;
                                 
    						case 7:  face_value = "9";
    								 break;
                                 
    						case 8:  face_value = "10";
    								 break;                   
    
    						case 9:  face_value = "Jack";
    								 break;
    
    						case 10: face_value = "Queen";
    								 break;
    
    						case 11: face_value = "King";
    								 break;
    
    						case 12: face_value = "Ace";
    								 break;
    						     
    						default: cout << "\n\nface_value loop overflow\n";
    
    					}//end switch		              
    			
    
    			//This block will create a "card" to be assigned to the deck
    			card = face_value; 
    			card += " of ";
    			card += suite;
    		 
    			deck[ i][j] = card; 
    
    		}//end INNER FOR
    			
    
    	}//end OUTTER FOR
    
    
    }
    
    
    
    void blackjack::shuffle_deck()
    {		
    		
    	for( int y = 0; y < 4; y++)
    		for (int z = 0; z < 13; z++) 
        
    			swap(deck[y][z], deck[rand()%4][rand()%13]);
    
    }
    
    
    
    void blackjack::display_hand()
    {
    	cout << endl << endl << " Dealer's Hand: " << dealerhand_value << "\n\n ";
    
    	for (int i = 0; i < dh_counter; i++)
    
    		cout << dealer_hand[ i] << "\t";
    
    	cout << endl << endl << endl;
    
    	cout <<"\n\n Your Hand:  " << playerhand_value << "\n\n ";
    		
    	for (int j = 0; j < ph_counter; j++)
    
    		cout << player_hand[j] << "\t";
    
    	cout << endl << endl << endl;
    
    }
    
    
    
    char get_user_command()
    {
    
    	char command = 'x';
    	
    	cout << "\n\n 'H' to HIT"
    		 << "\n 'S' to STAY"
    		 << "\n 'Q' to QUIT\n\n";
    	
    	command = getch();
    
    	return command;
    
    }
    
    
    
    //seeds the random number generator to eliminate repitition
    void seed_random()
    {
    	time_t seed = time(NULL);
    
    	srand(seed);
    }
    
    
    
    void blackjack::the_dealer(char choice)
    {	
    	
    	if (counter_2D >= 13) //This will advance deck to the second 'row' if row[0] has been used
    	{
    		++counter_1D;
    		counter_2D = 0;
    	}
    
    
    	else if (choice == 'H')
    	{	
    	
    		string card = deck[counter_1D][counter_2D++];	//Pick a card starting from the top of the deck (deck[0][0])
    
    		player_hand[ph_counter++] = card;				//Add card 'string' to players hand
    
    		playerhand_value += card_value(card);			//Add the cards numerical value to the players hand
    
    	}
    	
    		
    	else if (choice == 'S')
    	{		
    		do
    		{
    
    			string card = deck[counter_1D][counter_2D++];
    
    			dealer_hand[dh_counter++] = card;
    	
    			dealerhand_value += card_value(card);
    
    		
    		}while (dealerhand_value < playerhand_value);
    	}
    		
    }
    	
    
    
    bool blackjack::is_bust(int test) const
    {
    	return (test > 21);
    
    }
    	 
    
    
    void blackjack::winner_determination(char choice)
    {
    	
    	//Lose Logic is based on 3 situations: 1) you bust  2)you 'stayed' and lost  3) dealer has 21
    	//This evaluation for 'Losing' is made first, due to  "dealerhand_value == 21"
    
    	if (is_bust(playerhand_value) || ((playerhand_value <= dealerhand_value) && (dealerhand_value <= 21)
    		&& (choice == 'S')) || (dealerhand_value == 21))
    	{
    
    		cout << "\n\n\n You Loooooose !!!!\n\n"
    			 << "Press [Enter] to continue ";
    
    		cin.get();
    
    		//reset everything in preperation for the next hand
    		playerhand_value = 0;
    		dealerhand_value = 0;
    		ph_counter = 0;
    		dh_counter = 0;
    
    	}
    
    			
    	else if (is_bust(dealerhand_value) || ((playerhand_value > dealerhand_value) && (playerhand_value < 21)
    			&& (choice == 'S') && (dealerhand_value != 0)) || (playerhand_value == 21))
    	{
    				
    		cout << "\n\n\n You Weeeeeeen !!!!\n\n"
    			 << "Press [Enter] to continue ";
    		
    		cin.get();
    
    		//reset everything in preperation for the next hand
    		playerhand_value = 0;
    		dealerhand_value = 0;
    		ph_counter = 0;
    		dh_counter = 0;
    
    	}	
    	
    
    }
    
    
    
    //Converts the first character of a playing card from 'string' to 'int' so numeric comparisons can be made.
    //Example:  string '2 of Diamonds' will be converted to integer '2'
    //istringstream can only accept the &address of the character to be streamed to integer
    int card_value(string card)  
    {
    	int result = 0;
    	
    	char *card_ptr;
    	
    	card_ptr = &card[0];     //string class variables can be treated like arrays	
    
    	istringstream myStream(card_ptr); //create a 'myStream' object of class istringstream 
      
    	if (myStream >> result)	//String to Integer converstion (works only for numeric face values)
        					 
    		return result;		//return an integer value 2 thru 10
    
    	    
    	else
    	{
        
    		if (*card_ptr != 'A')
    
    			return 10;	//"J", "Q", and "K" will return a face value of 10
    
    		else
    
    			return 11; //"A" will always return a value of 11
    
    	}
    
    
    }
    
    
    
    void exit_message()
    {
    	cout << "\nProgram written in C++ by David R. Weirich "
    		 << "\nThanks to everyone at www.cprogramming.com"
    		 << "\ndrgreenthumb76@hotmail.com"
    		 << "\n\nThank you for playing.\n\n";
    }

    Program Assumptions: Ace will always equal 11

    Program Difficiencies: Program does not allow the user to place bets, split, double down, or buy insurance.
    Also, card output is affected by 'tab' discontinuities.

    All system("pause" ) and system("cls") commands have been replaced by better alternatives per forum FAQ.
    Attached Files Attached Files
    Last edited by The Brain; 08-26-2004 at 10:07 AM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  13. #28
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Played it briefly before it ran into a problem. After so many hands it eventually starts printing "test" for a card name and then it crashes. Also, many times when I went over 21 instead of saying you lose it just quickly jumps to the next hand.

  14. #29
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903

    Question

    not sure why it displays "test".. i reset all the array counters after the win/loss decision..


    perhaps.. i am thinking i should use.. cin.ignore() when retrieving user inputs via getch()...??!?

    remember, below 16 HIT, 16 or above STAY..




    edit: Ok... i forgot to set counter_1D and counter_2D to zero after each hand
    Last edited by The Brain; 08-26-2004 at 03:17 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

  15. #30
    Teenage Mutant Ninja Nerd MMD_Lynx's Avatar
    Join Date
    Aug 2004
    Posts
    65

    Smile Hey, i didn't need a new thread

    I'm also doing a blackjack game. but i was doing an unneccesarily (spelling?) complicated shuffle func that wouldn't even work . it had pointers and bool flags and crud like that.

    this is proof that searching the forum DOES work.
    Stupid people are useful. You can make them do all the mindless tasks you are too lazy to do yourself.

    Sphynx cats are just bald and wrinkly, like old people, and we don't reject them.

Page 2 of 5 FirstFirst 12345 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple Blackjack Program
    By saber1357 in forum C Programming
    Replies: 1
    Last Post: 03-28-2009, 04:19 PM
  2. Help with Blackjack program
    By sugie in forum C++ Programming
    Replies: 1
    Last Post: 04-30-2005, 01:30 AM
  3. Blackjack!
    By Dr. Bebop in forum Game Programming
    Replies: 1
    Last Post: 10-03-2002, 09:58 PM
  4. Blackjack
    By the_head in forum C Programming
    Replies: 1
    Last Post: 08-03-2002, 09:57 AM
  5. BlackJack Program...
    By 67stangman in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 11:44 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21