| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
|
|
Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!
|
|
#1
|
|||
|
|||
|
Help with black jack program C++
This is what I have so far.........
//Matt Maurer //Black Jack #include<iostream.h> #include<string.h> #include<stdlib.h> #include<iomanip.h> #include<conio.h> void main() { int option2 = 0; char rule; char player1 ; char player1name [15]; char player2; char player2name [15]; int playernum = 0; int option = 0; { cout<<"Welcome To..."<<endl; cout<<"\nBBBBBBBBBB LL AAAAAAAAA CCCCCCCCCCC K K "; cout<<"\nB B LL A A C K K "; cout<<"\nB B LL A A C K K "; cout<<"\nB B LL A A C K K "; cout<<"\nBBBBBBBBBB LL A A C KKK "; cout<<"\nB B LL AAAAAAAAA C K K "; cout<<"\nB B LL A A C K K "; cout<<"\nB B LL A A C K K "; cout<<"\nBBBBBBBBBB LLLLLLLLLLLL A A CCCCCCCCCCC K K "; cout<<"\n"; cout<<"\n JJJJJJJJJJJJ AAAAAAAAAA CCCCCCCCCCC K K "; cout<<"\n JJ A A C K K "; cout<<"\n JJ A A C K K "; cout<<"\n JJ A A C K K "; cout<<"\n JJ AAAAAAAAAA C KKK "; cout<<"\n JJ A A C K K "; cout<<"\nJJ JJ A A C K K "; cout<<"\nJJ JJ A A C K K "; cout<<"\nJJJJJJJJ A A CCCCCCCCCCC K K "; cout<<endl; } cout << "Hello and Welcome to Matt's Black Jack Table, best of luck to ya! \n";endl; cout << "One or Two players? " ; cin >> playernum; if (playernum !=1 || playernum !=2) { cout << "I'm sorry, there is not enough room at this table for more than 2 players.\n"; endl; cout << "What'll it be pal, 1 or 2 players? "; endl; cin >> playernum; } else { switch (playernum); } switch (playernum) { case 1: cout << "Player One, what is your name? \n"; endl; cin.ignore(80, '\n'); cin.get(player1name, 15); cin.ignore(80, '\n'); clrscr(); cout << "Welcome, " << player1name <<endl; break; case 2: cout << "Player One, what is your name? ";endl; cin.ignore(80, '\n'); cin.get(player1name, 15); cin.ignore(80, '\n'); clrscr(); cout << "Welcome to my table, " << player1name << ". \n"; endl; cout << "Player Two, what is your name? ";endl; cin.get(player2name, 15); cout << "So nice of you to join us, " << player2name << ". \n"; endl; clrscr(); break; } cout << "Before we begin, do you wish to see the rules? \n"; endl; cout << "Enter 1 for yes or 2 for no: \n";endl; cin >> option; switch (option) { case 1: cout<<"Here are the rules:"<<endl; cout<<"A natural 21 means an automatic win."<<endl; cout<<"If both players hand's are equal, then it's a tie."<<endl; cout<<"The game is played with a standard 52 card deck. "<<endl; cout<<"The deck re-shuffles after 52 cards have been dealt."<<endl; cout<<"There is no doubling down or other complications."<<endl; cout<<"J,Q,K all count as 10. An Ace can count as an 11 or a 1."<<endl; cout<<endl; cout << "Press 1 to begin the game. \n" << endl; cin >> option2; switch (option2) { case 1: cout << "Ok, let's begin the game. " <<endl; } break; case 2: cout << "Ok, let's begin this game." <<endl; break; } typedef int bool; const int false=0; const int true=1; const int decksize=52; const int maxhand=5; struct card { char value; char suit; bool used; }; card dealerhand[maxhand]; card userhand[maxhand]; int totalu=0, totald=0; bool bu=false, bd=false, gameover=false; void dealerplay(card deckd[]); void userplay(card decku[]); void handhit(card hand[], int &cd, int &total); int game () { srand(time(0)); char choice=' '; do { totalu=0; totald=0; bu=false; bd=false; gameover=false; card deck[decksize]; for (int i=0; i<decksize; i++) { deck[i].used=false; if ((i>=0) && (i<=12)) deck[i].suit='s'; else if ((i>=13) && (i<=25)) deck[i].suit='d'; else if ((i>=26) && (i<=38)) deck[i].suit='c'; else if ((i>=39) && (i<=51)) deck[i].suit='h'; if (i==0) { for (int a=0; a<=7; a++) deck[a].value=(a+48)+2; deck[i+8].value='t'; deck[i+9].value='j'; deck[i+10].value='q'; deck[i+11].value='k'; deck[i+12].value='a'; } if (i==13) { for (int x=13; x<=20; x++) deck[x].value=(x+48)-11; deck[i+8].value='t'; deck[i+9].value='j'; deck[i+10].value='q'; deck[i+11].value='k'; deck[i+12].value='a'; } if (i==26) { for (int z=26; z<=33; z++) deck[z].value=(z+48)-24; deck[i+8].value='t'; deck[i+9].value='j'; deck[i+10].value='q'; deck[i+11].value='k'; deck[i+12].value='a'; } if (i==39) { for (int c=39; c<=51; c++) deck[c].value=(c+48)-37; deck[i+8].value='t'; deck[i+9].value='j'; deck[i+10].value='q'; deck[i+11].value='k'; deck[i+12].value='a'; } } //Print out the deck //for (int p=0; p<decksize; p++) //cout<<"\""<<deck[p].value<<"\""<<deck[p].suit<<endl; dealerplay(deck); userplay(deck); if(bu==false && gameover==false) { cout<<"The dealer totaled "<<totald<<" and "; if(bd==false && totald>=totalu) cout<<"won!"<<endl; else if(bd==true) cout<<"busted! YOU WIN!!!"<<endl; else if(bd==false && totald<=totalu) cout<<"lost! YOU WIN!!!"<<endl; } else if(bu==true && gameover==false) cout<<"The dealer won."<<endl; cout<<endl<<endl; cout<<"Would you like to play again? Type y for yes or anything else for no."<<endl; cin>>choice; cout<<endl<<endl<<"_-=+++=-_ _-=+++=-_ _-=+++=-_ _-=+++=-_"<<endl<<endl; }while(choice=='y'); return 0; } //-------------------------------------------------------------------------------------------- void dealerplay(card deckd[]) { int rcard2=0, cdd=0; //Fill the dealer's hand (5 cards) for(int v=0; v<=4; v++) { do { rcard2=rand()%52+1; }while(deckd[rcard2].used==1); deckd[rcard2].used=1; dealerhand[v].value=deckd[rcard2].value; dealerhand[v].suit=deckd[rcard2].suit; } for(int haha=0; haha<=1; haha++) { switch(dealerhand[haha].value) { case '2':totald+=2;break; case '3':totald+=3;break; case '4':totald+=4;break; case '5':totald+=5;break; case '6':totald+=6;break; case '7':totald+=7;break; case '8':totald+=8;break; case '9':totald+=9;break; case 't':totald+=10;break; case 'j':totald+=10;break; case 'q':totald+=10;break; case 'k':totald+=10;break; case 'a':totald+=11;break; default:cout<<"Not a real card.\n";break; } } cdd+=2; cout<<"The dealer is showing a "<<dealerhand[0].value<<" of "<<dealerhand[0].suit<<"."<<endl; while(totald<17) handhit(dealerhand, cdd, totald); if(totald>21) bd=true; //Print out all parameters of one random card /*cout<<"\n\n-------------------------------\n\n"; cout<<deckd[rcard2].value<<" "<<deckd[rcard2].suit<<" "<<deckd[rcard2].used; cout<<"\n\n-------------------------------\n\n";*/ return; } //---------------------------------------------------------------------------------------------- void userplay(card decku[]) { int rcard3=0, cdu=0; char hsu=' '; //Fill the user's hand (5 cards) for(int o=0; o<=4; o++) { do { rcard3=rand()%52+1; }while(decku[rcard3].used==1); decku[rcard3].used=1; userhand[o].value=decku[rcard3].value; userhand[o].suit=decku[rcard3].suit; } cdu+=2; for(int haLA=0; haLA<=1; haLA++) { switch(userhand[haLA].value) { case '2':totalu+=2;break; case '3':totalu+=3;break; case '4':totalu+=4;break; case '5':totalu+=5;break; case '6':totalu+=6;break; case '7':totalu+=7;break; case '8':totalu+=8;break; case '9':totalu+=9;break; case 't':totalu+=10;break; case 'j':totalu+=10;break; case 'q':totalu+=10;break; case 'k':totalu+=10;break; case 'a':totalu+=11;break; default:cout<<"Not a real card.\n";break; } } cout<<endl; for(int lad=0; lad<=1; lad++) cout<<"Your "<<(lad+1)<<" card is a "<<userhand[lad].value<<" of "<<userhand[lad].suit<<"."<<endl; if((userhand[0].value=='a' && userhand[1].value=='j') || (userhand[0].value=='j' && userhand[1].value=='a')) { gameover=true; cout<<"You got Blackjack! Congratulations, you win!"<<endl; cout<<"Total: "<<totalu<<endl; return; } cout<<"Total: "<<totalu<<endl; do { cout<<"Would you like to hit (h) or stay (s)?"<<endl; cin>>hsu; if(hsu=='h') { handhit(userhand, cdu, totalu); cout<<"Your "<<cdu<<" card is a "<<userhand[cdu].value<<" of "<<userhand[cdu].suit<<"."<<endl <<"Total: "<<totalu<<endl; if(totalu>21) { bu=true; cout<<"You busted."<<endl; } } else if(hsu=='s') break; else { cout<<"Invalid input. Please enter an h or an s."<<endl; continue; } }while(cdu<=3 && bu==false); if(cdu==5 && bu==false) { gameover=true; cout<<"Son of a gun! You got a five card charlie! You win!"<<endl; } cout<<"Total: "<<totalu<<endl; return; } //---------------------------------------------------------------------------------------------- void handhit(card hand[], int &cd, int &total) { switch(hand[(cd+1)].value) { case '2':total+=2;break; case '3':total+=3;break; case '4':total+=4;break; case '5':total+=5;break; case '6':total+=6;break; case '7':total+=7;break; case '8':total+=8;break; case '9':total+=9;break; case 't':total+=10;break; case 'j':total+=10;break; case 'q':total+=10;break; case 'k':total+=10;break; case 'a':total+=11;break; default:cout<<"Not a real card.\n";break; } cd++; return; }; } When I run it....i get a decleration syntax error.....and thats it...any help would be appreciated. |
|
#2
|
|||
|
|||
|
Well, first off, please put your code in a box.
Alright, well here are a few small things. you forgot to include using namespace std; I'm not sure if it's universal or not (only used one compiler) but I never put iomanip.h or iostream.h just #include <iostream> #include <iomanip> hmm.... just at a glance it doesn't look like any of your functions would call correctly. typedef int bool probably doesn't work, but I haven't compiled it. initialize what you are referencing before you do it, which may also be giving you the problem. i.e. Code:
void game()
void function1()
void function2()
int main()
{
game()
function1()
function2()
}
I'm only a second year CS major student. Lots more than that, but I think I touched over the major stuff. |
|
#3
|
|||
|
|||
|
i study c++for one year!
|
|
#4
|
|||
|
|||
|
iostream.h never was a standard header; meaning it will not be portable. And I'm quite sure the closely related iomanip.h is the same.
However, where they work, they do not place their contents in std. ----- If you have a syntax error it usually gives a line number - include that in some fashion, e.g. by // Syntax error here in your code, and letting us know we can just search for it. Tip: If you use the [ highlight=cpp ] [ /highlight ] tags, without spaces, the code will be line numbered and will also be color highlighted. And most importantly, indentation will be preserved. |
|
#5
|
|||
|
|||
|
Ah. Didn't know that. I probably should though.
|
|
#6
|
|||
|
|||
come to have a look |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with black jack program C++ |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|