| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Blackjack game help
me and my friends have been workin on this blackjack game but its not finished yet we still have to do hit stand and double down....some things i have been having trouble with are when you get an ace you have a choice of it being 11 or 1 and it messes up...also if your first card is = to ur second card it wont cout ur second card...same thing with dealer...but with the dealers second card i made it a graphic so you dont know wut the dealer has like in blackjack....here is my code....for some reason it didnt space it out correctly when i copied and pasted it... if you have any suggestions let me know thnx i couldnt fit the whole program on here so ill post the rest in a reply
#include <iostream.h> #include <iomanip.h> #include <string.h> #include <stdio.h> #include <stdlib.h> #include <time.h> int main() { cout << "\t\t\t_____INSTRUCTIONS_____\n\n\n² You need to get 21 to win.\n\n² An Ace can count as 1 or 11.\n\n² King, Queen, and Jack count as 10.\n\n² All of the others cards count for as much as the number on the card i.e.(2 of hearts is worth 2).\n\n² You can choose to hit(take another card) stand(keep the cards you have) double down(you get 1 more card).\n\n² Whoever is closest to 21 without going over wins.\n\n"; int card; int value; int value2; int value3; int value4; int acevalue; int acevalue2; int acevalue3; int acevalue4; int card2; int card3; int card4; int suite; char suite3[100]=""; char suite4[100]=""; char suite2[100]=""; char play; int ptotal=0; int dtotal=0; char ace; int hit; int doubledown; char choice; srand(time(NULL)); card=rand()%13+1; card2=rand()%13+1; card3=rand()%13+1; card4=rand()%13+1; suite=rand()%4+1; if (suite==1) { strcpy(suite2,"Spades"); } if (suite==2) { strcpy(suite2,"Hearts"); } if (suite==3) { strcpy(suite2,"Diamonds"); } if (suite==4) { strcpy(suite2,"Clubs"); } suite=rand()%4+1; if (suite==1) { strcpy(suite3,"Spades"); } if (suite==2) { strcpy(suite3,"Hearts"); } if (suite==3) { strcpy(suite3,"Diamonds"); } if (suite==4) { strcpy(suite3,"Clubs"); } suite=rand()%4+1; if (suite==1) { strcpy(suite4,"Spades"); } if (suite==2) { strcpy(suite4,"Hearts"); } if (suite==3) { strcpy(suite4,"Diamonds"); } if (suite==4) { strcpy(suite4,"Clubs"); } if (card==1) { cout << "You have the Ace of " << suite2 << ".\n"; } if (card==2) { acevalue=0; value=2; cout << "You have the Two of " << suite2 << ".\n"; } if (card==3) { acevalue=0; value=3; cout << "You have the Three of " << suite2 << ".\n"; } if (card==4) { acevalue=0; value=4; cout << "You have the Four of " << suite2 << ".\n"; } if (card==5) { acevalue=0; value=5; cout << "You have the Five of " << suite2 << ".\n"; } if (card==6) { acevalue=0; value=6; cout << "You have the Six of " << suite2 << ".\n"; } if (card==7) { acevalue=0; value=7; cout << "You have the Seven of " << suite2 << ".\n"; } if (card==8) { acevalue=0; value=8; cout << "You have the Eight of " << suite2 << ".\n"; } if (card==9) { acevalue=0; value=9; cout << "You have the Nine of " << suite2 << ".\n"; } if (card==10) { acevalue=0; value=10; cout << "You have the Ten of " << suite2 << ".\n"; } if (card==14) { acevalue=0; value=10; cout << "You have the Jack of " << suite2 << ".\n"; } if (card==12) { acevalue=0; value=10; cout << "You have the Queen of " << suite2 << ".\n"; } if (card==13) { acevalue=0; value=10; cout << "You have the King of " << suite2 << ".\n"; } if (card==11) { cout << "You have the Ace of " << suite2 << ".\n"; } while (card==card2) { card=rand()%13+1; } if (card2==1) { cout << "You also have the Ace of " << suite3 << ".\n"; } if (card2==2) { acevalue2=0; value2=2; cout << "You also have the Two of " << suite3 << ".\n"; } if (card2==3) { acevalue2=0; value2=3; cout << "You also have the Three of " << suite3 << ".\n"; } if (card2==4) { acevalue2=0; value2=4; cout << "You also have the Four of " << suite3 << ".\n"; } if (card2==5) { acevalue2=0; value2=5; cout << "You also have the Five of " << suite3 << ".\n"; } if (card2==6) { acevalue2=0; value2=6; cout << "You also have the Six of " << suite3 << ".\n"; } if (card2==7) { acevalue2=0; value2=7; cout << "You also have the Seven of " << suite3 << ".\n"; } if (card2==8) { acevalue2=0; value2=8; cout << "You also have the Eight of " << suite3 << ".\n"; } if (card2==9) { acevalue2=0; value2=9; cout << "You also have the Nine of " << suite3 << ".\n"; } if (card2==10) { acevalue2=0; value2=10; cout << "You also have the Ten of " << suite3 << ".\n"; } if (card2==14) { acevalue2=0; value2=10; cout << "You also have the Jack of " << suite3 << ".\n"; } if (card2==12) { acevalue2=0; value2=10; cout << "You also have the Queen of " << suite3 << ".\n";; } if (card2==13) { acevalue2=0; value2=10; cout << "You also have the King of " << suite3 << ".\n"; } if (card2==11) { cout << "You also have the Ace of " << suite3 << ".\n"; } while (card2==card) { card2=rand()%13+1; } if (card3==1) { acevalue3=1; value3=0; cout << "The dealer has the Ace of " << suite4 << ".\n"; } if (card3==2) { acevalue3=0; value3=2; cout << "The dealer has the Two of " << suite4 << ".\n";; } if (card3==3) { acevalue3=0; value3=4; cout << "The dealer has the Three of " << suite4 << ".\n"; } if (card3==4) { acevalue3=0; value3=4; cout << "The dealer has the Four of " << suite4 << ".\n"; } if (card3==5) { acevalue3=0; value3=5; cout << "The dealer has the Five of " << suite4 << ".\n";; } if (card3==6) { acevalue3=0; value3=6; cout << "The dealer has the Six of " << suite4 << ".\n"; } if (card3==7) { acevalue3=0; value3=7; cout << "The dealer has the Seven of " << suite4 << ".\n"; } if (card3==8) { acevalue3=0; value3=8; cout << "The dealer has the Eight of " << suite4 << ".\n"; } if (card3==9) { acevalue3=0; value3=9; cout << "The dealer has the Nine of " << suite4 << ".\n"; } if (card3==10) { acevalue3=0; value3=10; cout << "The dealer has the Ten of " << suite4 << ".\n"; } if (card3==14) { acevalue3=0; value3=10; cout << "The dealer has the Jack of " << suite4 << ".\n"; } if (card3==12) { acevalue3=0; value3=10; cout << "The dealer has the Queen of " << suite4 << ".\n"; } if (card3==13) { acevalue3=0; value3=10; cout << "The dealer has the King of " << suite4 << ".\n"; } if (card3==11) { acevalue3=11; value3=0; cout << "The dealer has the Ace of " << suite4<< ".\n"; } while ((card3==card2) || (card3==card)) { card3=rand()%13+1; } if (card4==1) { acevalue4=1; value4=0; cout << "The dealer also has ²?\n"; } if (card4==2) { acevalue4=0; value4=2; cout << "The dealer also has ²?\n"; } if (card4==3) { acevalue4=0; value4=4; cout << "The dealer also has ²?\n"; } if (card4==4) { acevalue4=0; value4=4; cout << "The dealer also has ²?\n"; } if (card4==5) { acevalue4=0; value4=5; cout << "The dealer also has ²?\n"; } if (card4==6) { acevalue4=0; value4=6; cout << "The dealer also has ²?\n"; } if (card4==7) { acevalue4=0; value4=7; cout << "The dealer also has ²?\n"; } if (card4==8) { acevalue4=0; value4=8; cout << "The dealer also has ²?\n"; } if (card4==9) { acevalue4=0; value4=9; cout << "The dealer also has ²?\n"; } if (card4==10) { acevalue4=0; value4=10; cout << "The dealer also has ²?\n"; } if (card4==14) { acevalue4=0; value4=10; cout << "The dealer also has ²?\n"; } if (card4==12) { acevalue4=0; value4=10; cout << "The dealer also has ²?\n"; } if (card4==13) { acevalue4=0; value4=10; cout << "The dealer also has ²?\n"; } if (card4==11) { acevalue4=11; cout << "The dealer also has ²?\n"; } while ((card4==card3) || (card4==card2) || (card4==card)) { card4=rand()%13+1; } if (card==1) { cout << "Do you want your Ace to equal 11(a) or 1(b)?"; cin >> ace; if (ace=='a') { acevalue=11; } if (ace=='b') { acevalue=1; } } if (card==11) { cout << "Do you want your Ace to equal 11(a) or 1(b)?"; cin >> ace; if (ace=='a') { acevalue=11; } if (ace=='b') { acevalue=1; } } if (card2==1) { cout << "Do you want your Ace to equal 11(a) or 1(b)?"; cin >> ace; if (ace=='a') { acevalue=11; } if (ace=='b') { acevalue=1; } } if (card2==11) { cout << "Do you want your Ace to equal 11(a) or 1(b)?"; cin >> ace; if (ace=='a') { acevalue=11; } if (ace=='b') { acevalue=1; } } ptotal=value+value2+acevalue+acevalue2; dtotal=value3+value4+acevalue3+acevalue4; cout << "You have " << ptotal << " Hit(h) Stand(s) or Double Down(d)\n"; if (choice=='h') { hit=rand()%13+1; cin >> hit; } if (choice=='s') { cout << "You have " << ptotal << ".\n"; } if (choice=='d') { doubledown=rand()13%1; cin >> doubledown; } if (dtotal>ptotal && dtotal<=21) { cout << "You lose.\n"; } if (ptotal>dtotal && ptotal<=21) { cout << "You win.\n"; } |
|
#2
|
|||
|
|||
|
here is the rest of the blackjack program
cout << "Would you like to play again? (y=yes n=no)"; cin >> play; if (play=='n') { return 0; } while (play=='y') { srand(time(NULL)); card=rand()%13; card2=rand()%13; while (card2==card) { card2=rand()%13; } card3=rand()%13; while ((card3==card2) || (card3==card)) { card3=rand()%13; } card4=rand()%13; while ((card4==card3) || (card4==card2) || (card4==card)) { card4=rand()%13; } suite=rand()%4; if (suite==1) { strcpy(suite2,"Spades"); } if (suite==2) { strcpy(suite2,"Hearts"); } if (suite==3) { strcpy(suite2,"Diamonds"); } if (suite=4) { strcpy(suite2,"Clubs"); } if (card==1) { cout << "You have the Ace of " << suite2 << ".\n"; } if (card==2) { acevalue=0; value=2; cout << "You have the Two of " << suite2 << ".\n"; } if (card==3) { acevalue=0; value=3; cout << "You have the Three of " << suite2 << ".\n"; } if (card==4) { acevalue=0; value=4; cout << "You have the Four of " << suite2 << ".\n"; } if (card==5) { acevalue=0; value=5; cout << "You have the Five of " << suite2 << ".\n"; } if (card==6) { acevalue=0; value=6; cout << "You have the Six of " << suite2 << ".\n"; } if (card==7) { acevalue=0; value=7; cout << "You have the Seven of " << suite2 << ".\n"; } if (card==8) { acevalue=0; value=8; cout << "You have the Eight of " << suite2 << ".\n"; } if (card==9) { acevalue=0; value=9; cout << "You have the Nine of " << suite2 << ".\n"; } if (card==10) { acevalue=0; value=10; cout << "You have the Ten of " << suite2 << ".\n"; } if (card==14) { acevalue=0; value=10; cout << "You have the Jack of " << suite2 << ".\n"; } if (card==12) { acevalue=0; value=10; cout << "You have the Queen of " << suite2 << ".\n"; } if (card==13) { acevalue=0; value=10; cout << "You have the King of " << suite2 << ".\n"; } if (card==11) { cout << "You have the Ace of " << suite2 << ".\n"; } while (card==card2) { card=rand()%13+1; } if (card2==1) { cout << "You also have the Ace of " << suite3 << ".\n"; } if (card2==2) { acevalue2=0; value2=2; cout << "You also have the Two of " << suite3 << ".\n"; } if (card2==3) { acevalue2=0; value2=3; cout << "You also have the Three of " << suite3 << ".\n"; } if (card2==4) { acevalue2=0; value2=4; cout << "You also have the Four of " << suite3 << ".\n"; } if (card2==5) { acevalue2=0; value2=5; cout << "You also have the Five of " << suite3 << ".\n"; } if (card2==6) { acevalue2=0; value2=6; cout << "You also have the Six of " << suite3 << ".\n"; } if (card2==7) { acevalue2=0; value2=7; cout << "You also have the Seven of " << suite3 << ".\n"; } if (card2==8) { acevalue2=0; value2=8; cout << "You also have the Eight of " << suite3 << ".\n"; } if (card2==9) { acevalue2=0; value2=9; cout << "You also have the Nine of " << suite3 << ".\n"; } if (card2==10) { acevalue2=0; value2=10; cout << "You also have the Ten of " << suite3 << ".\n"; } if (card2==14) { acevalue2=0; value2=10; cout << "You also have the Jack of " << suite3 << ".\n"; } if (card2==12) { acevalue2=0; value2=10; cout << "You also have the Queen of " << suite3 << ".\n";; } if (card2==13) { acevalue2=0; value2=10; cout << "You also have the King of " << suite3 << ".\n"; } if (card2==11) { cout << "You also have the Ace of " << suite3 << ".\n"; } while (card2==card) { card2=rand()%13+1; } if (card3==1) { acevalue3=1; value3=0; cout << "The dealer has the Ace of " << suite4 << ".\n"; } if (card3==2) { acevalue3=0; value3=2; cout << "The dealer has the Two of " << suite4 << ".\n";; } if (card3==3) { acevalue3=0; value3=4; cout << "The dealer has the Three of " << suite4 << ".\n"; } if (card3==4) { acevalue3=0; value3=4; cout << "The dealer has the Four of " << suite4 << ".\n"; } if (card3==5) { acevalue3=0; value3=5; cout << "The dealer has the Five of " << suite4 << ".\n";; } if (card3==6) { acevalue3=0; value3=6; cout << "The dealer has the Six of " << suite4 << ".\n"; } if (card3==7) { acevalue3=0; value3=7; cout << "The dealer has the Seven of " << suite4 << ".\n"; } if (card3==8) { acevalue3=0; value3=8; cout << "The dealer has the Eight of " << suite4 << ".\n"; } if (card3==9) { acevalue3=0; value3=9; cout << "The dealer has the Nine of " << suite4 << ".\n"; } if (card3==10) { acevalue3=0; value3=10; cout << "The dealer has the Ten of " << suite4 << ".\n"; } if (card3==14) { acevalue3=0; value3=10; cout << "The dealer has the Jack of " << suite4 << ".\n"; } if (card3==12) { acevalue3=0; value3=10; cout << "The dealer has the Queen of " << suite4 << ".\n"; } if (card3==13) { acevalue3=0; value3=10; cout << "The dealer has the King of " << suite4 << ".\n"; } if (card3==11) { acevalue3=11; value3=0; cout << "The dealer has the Ace of " << suite4<< ".\n"; } while ((card3==card2) || (card3==card)) { card3=rand()%13+1; } if (card4==1) { acevalue4=1; value4=0; cout << "The dealer also has ²?\n"; } if (card4==2) { acevalue4=0; value4=2; cout << "The dealer also has ²?\n"; } if (card4==3) { acevalue4=0; value4=4; cout << "The dealer also has ²?\n"; } if (card4==4) { acevalue4=0; value4=4; cout << "The dealer also has ²?\n"; } if (card4==5) { acevalue4=0; value4=5; cout << "The dealer also has ²?\n"; } if (card4==6) { acevalue4=0; value4=6; cout << "The dealer also has ²?\n"; } if (card4==7) { acevalue4=0; value4=7; cout << "The dealer also has ²?\n"; } if (card4==8) { acevalue4=0; value4=8; cout << "The dealer also has ²?\n"; } if (card4==9) { acevalue4=0; value4=9; cout << "The dealer also has ²?\n"; } if (card4==10) { acevalue4=0; value4=10; cout << "The dealer also has ²?\n"; } if (card4==14) { acevalue4=0; value4=10; cout << "The dealer also has ²?\n"; } if (card4==12) { acevalue4=0; value4=10; cout << "The dealer also has ²?\n"; } if (card4==13) { acevalue4=0; value4=10; cout << "The dealer also has ²?\n"; } if (card4==11) { acevalue4=11; cout << "The dealer also has ²?\n"; } while ((card4==card3) || (card4==card2) || (card4==card)) { card4=rand()%13+1; } if (card==1) { cout << "Do you want your Ace to equal 11(a) or 1(b)?"; cin >> ace; if (ace=='a') { acevalue=11; } if (ace=='b') { acevalue=1; } } if (card==11) { cout << "Do you want your Ace to equal 11(a) or 1(b)?"; cin >> ace; if (ace=='a') { acevalue=11; } if (ace=='b') { acevalue=1; } } if (card2==1) { cout << "Do you want your Ace to equal 11(a) or 1(b)?"; cin >> ace; if (ace=='a') { acevalue=11; } if (ace=='b') { acevalue=1; } } if (card2==11) { cout << "Do you want your Ace to equal 11(a) or 1(b)?"; cin >> ace; if (ace=='a') { acevalue=11; } if (ace=='b') { acevalue=1; } } ptotal=value+value2+acevalue+acevalue2; dtotal=value3+value4+acevalue3+acevalue4; cout << "You have " << ptotal << " Hit(h) Stand(s) or Double Down(d)\n"; if (choice=='h') { hit=rand()%13+1; cin >> hit; } if (choice=='s') { cout << "You have " << ptotal << ".\n"; } if (choice=='d') { doubledown=rand()13%1; cin >> doubledown; } if (dtotal>ptotal && dtotal<=21) { cout << "You lose.\n"; } if (ptotal>dtotal && ptotal<=21) { cout << "You win.\n"; } cout << "Would you like to play again? (y=yes n=no)"; cin >> play; } return 0; } |
|
#3
|
|||
|
|||
|
The first thing I would do if I were you is to break up that code into functions. Not only to make it more readable but also for code reuse. You have a lot of repeat code where everything is the same except the variable names. Once you have cleaned it up you will probably find the bug yourself, but if not post the new code.
|
|
#4
|
|||
|
|||
|
Quote:
i dont know how to do functions |
|
#5
|
|||
|
|||
|
Did a quick google search for you to see what I could find about writing functions in C/C++ and this one looks alright.
http://cplus.about.com/od/beginnerctutoria1/l/aa051002a.htm -KM- |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Blackjack game help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|