its alot of code but this is my program so far.
i got everything to work except the aces for when the user goes over 21.
i was thinking that if the user inputs an ace it would count how many were put in total, and at the end if the user was over 21 then subtract 10 for each ace,but im not sure on how to do that.
do you think that this would work?
Code:
//after the user inputs the cards
if(card1char=='a' || cardchar2=='a'){
aces=aces+1;
value=0;
}
//if the user enters more aces later
if(newcardchar=='a' || newcardchar=='A')
ace++
//to calculate the total
if(//newcardchar or card1char or card2char=='a' or'A')
total+=newcard-(aces*10)
my entire program so far
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
10/6/10
*/
int main(){
int number,repeatcard,newcard;
int card1,card2,total;
char repeat,anothercard,card1char,card2char,newcardchar;
for(int x=10;x>0;x++) //generator for first 2 cards
{
srand(time(0));
cout<<"cards:";
for(int nextcard=2;nextcard>0;nextcard--){
number = 1 + (rand() % 13) ; //generates nums 1-13
if(number==1) //facecards generator
cout<<" A ";
else if(number>=2 && number<=9)
cout <<" "<<number<<" ";
else{
if(number==10)
cout<<" 10 ";
else if(number== 11)
cout<<" J ";
else if(number==12)
cout<<" Q ";
else
cout<<" K ";
} //endelse
} //endloop
cout<<"\n how many cards do you have? \n";
cin>>repeatcard;
cout<<"enter the cards <A,2-9,T,J,Q,K> \n";
cin>>card1char>>card2char;
if(card1char=='a'||card1char=='A')
card1=11;
if(card2char=='a'||card2char=='A')
card2=11;
if(card1char=='2')
card1=2;
if(card2char=='2')
card2=2;
if(card1char=='3')
card1=3;
if(card2char=='3')
card2=3;
if(card1char=='4')
card1=4;
if(card2char=='4')
card2=4;
if(card1char=='5')
card1=5;
if(card2char=='5')
card2=5;
if(card1char=='6')
card1=6;
if(card2char=='6')
card2=6;
if(card1char=='7')
card1=7;
if(card2char=='7')
card2=7;
if(card1char=='8')
card1=8;
if(card2char=='8')
card2=8;
if(card1char=='9')
card1=9;
if(card2char=='9')
card2=9;
if((card1char=='j'||card1char=='J') || (card1char=='q'||card1char=='Q') || (card1char=='k'||card1char=='K')|| (card1char=='t'||card1char=='T'))
card1=10;
if((card2char=='j'||card2char=='J') || (card2char=='q'||card2char=='Q')|| (card2char=='k'||card2char=='K')|| (card2char=='t'||card2char=='T'))
card2=10;
total=card1+card2;
cout<<"total "<<total<<endl;
if(total>21 && (card1char=='a' || card2char=='a')){ //if total is more than 21 and you have an ace, subtract 10 for each ace
total=total-10;
cout<<"new total: "<<total<<endl;}
if(total<21)
{
do
{
cout<<"do you want another card? <Y or N>? \n";
cin>>anothercard;
cout<<"how many cards do you have? \n";
cin>>repeatcard;
if(repeatcard==5){
cout<<"you cant have that many cards";
break;}
number = 1 + (rand() % 13);
cout<<"card: "<<endl;
if(number==1){
cout<<" A ";
}
else if(number>=2 && number<=9)
cout <<" "<<number<<" ";
else{
if(number==10)
cout<<" 10 ";
else if(number== 11)
cout<<" J ";
else if(number==12)
cout<<" Q ";
else
cout<<" K ";
} //endelse
cout<<"enter the card<A,2-9,T,J,Q,K>: ";
cin>>newcardchar;
if(newcardchar=='a')
newcard=11;
if(newcardchar=='2')
newcard=2;
if(newcardchar=='3')
newcard=3;
if(newcardchar=='4')
newcard=4;
if(newcardchar=='5')
newcard=5;
if(newcardchar=='6')
newcard=6;
if(newcardchar=='7')
newcard=7;
if(newcardchar=='8')
newcard=8;
if(newcardchar=='9')
newcard=9;
if(newcardchar=='j' || newcardchar=='q' || newcardchar=='k' || newcardchar=='t')
newcard=10;
total+=newcard;
cout<<"total: "<<total<<endl;
if(total<21){
cout<<"do you want another card? <Y or N>: ";
cin>>anothercard;
cout<<"how many cards do you have: \n";
cin>>repeatcard;
number = 1 + (rand() % 13);
cout<<"card: "<<endl;
if(number==1){
cout<<" A ";}
else if(number>=2 && number<=9)
cout <<" "<<number<<" ";
else{
if(number==10)
cout<<" 10 ";
else if(number== 11)
cout<<" J ";
else if(number==12)
cout<<" Q ";
else
cout<<" K ";
} //endelse
cout<<"enter the card<A,2-9,T,J,Q,K>: ";
cin>>newcard;
if(newcardchar=='a'|| newcardchar=='A')
newcard=11;
if(newcardchar=='2')
newcard=2;
if(newcardchar=='3')
newcard=3;
if(newcardchar=='4')
newcard=4;
if(newcardchar=='5')
newcard=5;
if(newcardchar=='6')
newcard=6;
if(newcardchar=='7')
newcard=7;
if(newcardchar=='8')
newcard=8;
if(newcardchar=='9')
newcard=9;
if((newcardchar=='j'|| newcardchar=='J')||(newcardchar=='q' || newcardchar=='Q')||(newcardchar=='k' ||newcardchar=='K')||(newcardchar=='t'||newcardchar=='T'))
newcard=10;
total+=newcard;
cout<<"total: "<<total<<endl;
} //end if
if(total>21 && (newcardchar=='a' || card1char=='a' || card2char=='a')){ //if total is more than 21 and you have an ace, subtract 10 for each ace
total=total-10;
cout<<"new total: "<<total<<endl;
} //end first if
}while( total<21 && (anothercard=='y' || anothercard=='Y') && repeatcard<=4); //end do loop
}
if(total==21) //if21 you win
cout<<"you win \n";
if(total>21) //if higherthan 21 lose
cout<<"busted \n";
if (anothercard=='n' || anothercard=='N') //if you dont want another card
break;
cout<<"\n Do you want to repeat? <Y or N> \n";
cin>>repeat;
if(repeat=='y' || repeat=='Y')
{
system("cls");
}
if(repeat=='n' || repeat=='N')
{
system("cls");
break;
}
}
system("pause");
return 0;
}