blackjack program face cards problem

This is a discussion on blackjack program face cards problem within the C++ Programming forums, part of the General Programming Boards category; hello alright, i made a blackjack program, it works. i don't know what i would do for the face cards, ...

  1. #1
    new to c++
    Join Date
    Feb 2009
    Posts
    53

    blackjack program face cards problem

    hello
    alright, i made a blackjack program, it works. i don't know what i would do for the face cards, like jack,king,queen,and ace, how would i put them into a random generator. the jack,king,queen count as 10. also how would i get the ace to count as either 11 or 1, when needed.

    i know the indentation is off, but its better than other programs that i have made.
    also any hints if i could put more into a loop or something.

    thanks

    Code:
    #include<iostream>
    #include <cstdlib>  //to use rand function
    #include <ctime>  // to use time as the seed for rand
    using namespace std;
    
    /*
    	Randall Foor
    	Blackjack program
    	9/30/10
    */
    
    int main(){
    
    	for(int x=10;x>0;x--)
    {
    
    int guess, number,numcards, cardnum;
    int cardnumber;
    int n1,n2,n3,n4,n5;
    int numberGuesses;
    int total;
    char anothercard,repeat;
    
    
    	srand(time(0));   
    		
    	/*	while (numberGuesses < 10)
    		{
    			number = 1 + rand() % 10;
    			cout << number << endl;
    			numberGuesses++;
    		}
    	*/
    
    
    	for (int n=2; n>0; n--)				//outputs random numbers for cards
    	{
    	
    
    		numberGuesses = 1;
    		number = 1 + rand() % 10;
    		cout <<"card with the value of: "<< number<<","<<endl;
    
    	}
    
    	cout<<"how many cards do you have? \n";		//asks how many cards you got
    	cin>>cardnum;
    
    
    	cout<<"what is the card value? \n";		//asks what the number of the card was
    	cin>>n1>>n2;								//stores card value
    
    	
    	total=n1+n2;		//computes total
    
    	if(total==21)
    		{
    			cout<<"you win!! \n";
    		}
    	if(total>=22)
    		{
    			cout<<"busted \n";
    		}
    
    	if(total<=20)
    		{
    			cout<<"your total is "<<total<<" \n";
    			cout<<"would you like another card? <Y or N>:";
    			cin>>anothercard;
    
    				if(anothercard=='y' || anothercard=='Y')		//if you want another card
    				{
    					for(int nextcard=3; nextcard>0;nextcard++)		//enters loop to get card number
    						{
    							numberGuesses = 1;
    							number = 1 + rand() % 10;			//makes random number for card
    							cout <<nextcard<<" cards \n";
    							cout<<" card value of : "<< number<<endl;
    							cout<<"what was the card value? ";
    									if(nextcard==3)
    										{
    											cin>>n3;
    
    											total=n1+n2+n3;
    											cout<<"total value of cards "<<total<<endl;
    
    											if(total==21)
    												{
    													cout<<"you win!!! \n";
    												}
    											if(total>=22)
    											{
    												cout<<"busted \n";
    											}
    										}
    									if(nextcard==4)
    										{
    											cin>>n4;
    
    											total=n1+n2+n3+n4;
    											cout<<"total value of cards "<<total<<endl;
    
    											if(total==21)
    												{
    													cout<<"you win!!! \n";
    												}
    											if(total>=22)
    											{
    												cout<<"busted \n";
    											}
    										}
    									if(nextcard==5)
    										{
    											cin>>n5;
    
    											total=n1+n2+n3+n4+n5;
    											cout<<"total value of cards "<<total<<endl;
    
    											if(total==21)
    												{
    													cout<<"you win!!! \n";
    												}
    											if(total>=22)
    											{
    												cout<<"busted \n";
    											}
    										}
    
    							if(nextcard==5 || total>=21)
    									{
    										break;		//breaks loop after
    										cout<<"you lose \n";
    									}
    							
    						}
    					
    				}
    		}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	
    
    			
    
    cout<<"Do you want to repeat? <Y or N> \n";
    	cin>>repeat;
    
    	if(repeat=='y' || repeat=='Y')
    	{				//breaks loop 
    		
    		system("cls");
    		
    	
    	}
    
    	if(repeat=='n' || repeat=='N'){				//breaks loop 
    		
    		system("cls");
    		
    		break;
    	}
    }
    
    
    
    
    	system("pause");
    		return 0;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    There is a function called random_shuffle that I hear works well. Look it up.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,499
    SourceForge.net: Indentation - cpwiki

    Also, break up your excessively long main into several functions, like
    - initialiseDeck
    - shuffle
    - DrawCard
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    329
    Regarding card values, have a look at enums, it would probably be better for you to store the cards as enums....setting the values yourself. You can then set the face value cards to all equal ten. Maybe set the value of an ace at 1 initially and then if the need arises, you can always add another 10 to the final hand score?

  5. #5
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Maybe set the value of an ace at 1 initially and then if the need arises, you can always add another 10 to the final hand score?
    Or the other way around. If the hand score goes above 21, make the ace 1 (if possible).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    the only time an ace would be worth 1 is if the hand would be over 21 if it was worth 11, you would also need to keep track of how many aces are in the hand
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    I am actually writing my own blackjack program right now! I handled the issue by having a "caseOfAce" variable, which increases by one every time an ace is drawn. I used the following code to generate cards.

    After every drawn card, a checkBust() function is called, which checks if the hand's new total value is over 21. If the value is over 21 and caseOfAce is greater than 0, subtract one from caseOfAce and subtract 10 from the total value, then rerun the check.

    Code:
        //Determine value and printable "worth"
        newCard = ( rand() % 13 ) + 1;
    
        if (newCard == 1) { //Determines Ace
            cWorth = "A";
            cValue += 11;
            caseOfAce++;
        } else if (newCard > 1 && newCard < 10) { //Determines Numeral
            cWorth = ( newCard + 48 );
            cValue += newCard;
        } else { //Determines Face
            if (newCard == 10)
                cWorth += "10";
            else if (newCard == 11)
                cWorth = "J";
            else if (newCard == 12)
                cWorth = "Q";
            else
                cWorth = "K";
    
            cValue += 10;
        }

  8. #8
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    ok
    i got the program to output face cards, and gave them values for when i put them in.
    my program crashes if i put in letters(face cards) when it asks what cards i have, it also doesnt continue in the beginning if it outputs a facecard. i still dont get how i would change the ace from 1 to 11 if needed. i was thinking

    Code:
    if(total>=22 && number=1)
    {
       total=n1+n2+n3+n4+n5 -10             //i can change totals as i go, but subtract 10
    }
    Code:
    #include<iostream>
    #include <cstdlib>  //to use rand function
    #include <ctime>  // to use time as the seed for rand
    using namespace std;
    
    /*
    	Randall Foor
    	Blackjack program
    	9/30/10
    */
    
    int main(){
    
    	for(int x=10;x>0;x--)
    {
    
    int number, cardnum;
    int k,q,j,a;
    int n1,n2,n3,n4,n5;
    int total;
    char anothercard,repeat,face;
    
    
    
    
    	srand(time(0));   
    
    	for (int n=2; n>0; n--)				//outputs random numbers for cards
    	{
    	
    		number = 1 + rand() % 13;
    
    		if(number=1)
    		{
    			a=11;
    			cout<<" A "<<endl;
    		}
    
    		if(number== 11)
    		{
    			j=10;
    			cout<<" J "<<endl;
    			cin>>face;
    		}
    		if(number==12)
    		{
    			q=10;
    			cout<<" Q "<<endl;
    			cin>>face;
    		}
    		if(number==13)
    		{
    			k=10;
    			cout<<" K "<<endl;
    			cin>>face;
    		}
    		if(number>=2 && number<=9)
    		{
    		cout <<number<<endl;
    		}
    		
    	}
    
    	cout<<"how many cards do you have? \n";		//asks how many cards you got
    	cin>>cardnum;
    
    
    	cout<<"what is the card value? \n";		//asks what the number of the card was
    	cin>>n1>>n2;								//stores card value
    
    	
    	total=n1+n2;		//computes total
    
    	if(total==21)
    		{
    			cout<<"you win!! \n";
    		}
    	if(total>=22)
    		{
    			cout<<"busted \n";
    		}
    
    	if(total<=20)
    		{
    			cout<<"your total is "<<total<<" \n";
    			cout<<"would you like another card? <Y or N>:";
    			cin>>anothercard;
    
    		if(anothercard=='y' || anothercard=='Y')		//if you want another card
    		{
    			for(int nextcard=3; nextcard>0;nextcard++)		//enters loop to get card number
    				{
    					number = 1 + rand() % 13;			//makes random number for card
    					cout <<nextcard<<" cards \n";
    					cout<<" next card value: ";
    
    					if(number== 11)
    					{
    						j=10;
    						cout<<" J \n";
    					}
    					if(number==12)
    					{
    						q=10;
    						cout<<"Q \n";
    					}
    					if(number==13)
    					{
    						k=10;
    						cout<<"K \n";
    					}
    					if(number>=2 && number<=9)
    					{
    					cout <<number<<endl;
    					}
    					cout<<"what was the card value? ";
    						if(nextcard==3)
    						{
    							cin>>n3;
    							total=n1+n2+n3;
    							cout<<"total value of cards "<<total<<endl;
    
    							if(total==21)
    							{
    								cout<<"you win!!! \n";
    							}
    							if(total>=22)
    							{
    								cout<<"busted \n";
    							}
    						}
    						if(nextcard==4)
    						{
    							cin>>n4;
    							total=n1+n2+n3+n4;
    							cout<<"total value of cards "<<total<<endl;
    
    							if(total==21)
    								{
    									cout<<"you win!!! \n";
    								}
    							if(total>=22)
    							{
    								cout<<"busted \n";
    							}
    						}
    						if(nextcard==5)
    						{
    							cin>>n5;
    							total=n1+n2+n3+n4+n5;
    							cout<<"total value of cards "<<total<<endl;
    
    							if(total==21)
    								{
    									cout<<"you win!!! \n";
    								}
    							if(total>=22)
    							{
    								cout<<"busted \n";
    							}
    						}
    
    						if(nextcard==5 || total>=21)
    						{
    							break;		//breaks loop after
    							cout<<"you lose \n";
    						}
    					
    				}
    					
    			}
    		}
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    	
    
    			
    
    cout<<"Do you want to repeat? <Y or N> \n";
    	cin>>repeat;
    
    	if(repeat=='y' || repeat=='Y')
    	{				//breaks loop 
    		
    		system("cls");
    		
    	
    	}
    
    	if(repeat=='n' || repeat=='N'){				//breaks loop 
    		
    		system("cls");
    		
    		break;
    	}
    }
    
    
    
    
    	system("pause");
    		return 0;
    }
    Last edited by rfoor; 10-01-2010 at 07:08 PM.

  9. #9
    Registered User
    Join Date
    Sep 2010
    Posts
    31
    There are a number of issues with this code. Before I get to them, I'd like to suggest to you this: if ever, when programming, you find yourself manually typing out sections of repeated code, ask yourself if it can't be done automatically by the computer itself. You could probably cut this code down 80% by using loops or reorganizing; and that will be a major help with bug-fixing and generally enhancing code-readability!

    To be honest, I simply cannot understand why you took the approach to this problem you did. Why do you have variables n1,...,n5 if their values are only used once? You could replace them all with a single variable "n". Moreover, the variables 'j,q,k,a' are completely unused and do nothing in your program after assignment. Keep in mind that the use of a variable comes from its ability to vary. Every unneeded extra variable means increased code complexity and increased memory usage.

    Why do you ask the player for input on how many cards they have, or what values the cards were? Doing that is completely redundant, since the computer has already generated a card for you, and can easily keep track of how many it has generated. This is where you could reduce your code significantly--a single "while" loop would be able to handle all of the card-generation, counting, and bust-checking.

    The overall loop is also awkward. Why does it count down from 10? This limits the number of times a player can play--sometimes this is useful, but here I don't think that's the point. Any infinite loop would be appropriate here, because as you have a "break" command when the user types that they don't want to repeat, there is a way to escape the loop.

    I think that if you rewrite the card-generation etc within a loop, the rest of your problems will disappear--and if not, they will be a LOT easier to solve. Give it a shot, and be sure to post your solution!

  10. #10
    new to c++
    Join Date
    Feb 2009
    Posts
    53
    i dont understand how i would calculate the total if i used a loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program problem
    By Birdhaus in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2005, 09:37 PM
  2. Replies: 2
    Last Post: 04-25-2005, 11:59 AM
  3. Some Problem With My Program
    By Americano in forum C Programming
    Replies: 5
    Last Post: 10-18-2003, 01:58 AM
  4. BlackJack Program...
    By 67stangman in forum C++ Programming
    Replies: 3
    Last Post: 05-06-2002, 10:44 PM
  5. Console Program Problem
    By Breach23 in forum C++ Programming
    Replies: 3
    Last Post: 10-19-2001, 12:35 AM

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