1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322
|
#include "stdafx.h"
#include <iostream>
#include <cstdlib> // needed for rand() and srand()
#include <ctime> // needed for time()
#include <iomanip>
using std::cout; using std::endl;
using std::cin; using namespace std;
int deck[52]={2,11,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7,8,8,8,8,9,9,9,9,10,10,10,10,10,10,10,10,10,10,10,10,
10,10,10,10,11,11,11,11};
int hand[12]={0,0,0,0,0,0,0,0,0,0,0,0};
int dhand[12]={0,0,0,0,0,0,0,0,0,0,0,0};
int getCardCalled=-1;
int getcard();
int searcharray (int value, int hand[], int arraylen);
int b=2, d=2;
int main()
{
int i,p,temp, newcard=1, dnewcard=1;
int a;
int arraylen=12, total=0, pdiff=0;
char go;
enum {INITIAL, HIT, STAND, BUST, RESET} state;
int dtotal = 0, ddiff=0;
srand(time(NULL)); // seed for random function
cout<<setw(5)<<"Welcome to Blackjack"<<endl;cout<<endl;
cout<<"The RULES: At the beginning of each round, dealer shuffles a deck of cards."<<endl;
cout<<"The player receives an initial hand of two cards. The object of the game is to"<<endl;
cout<<"get a higher card total than the dealer, but without going over 21 which is "<<endl;
cout<<"called BUSTING.The spot cards count 2 to 9; the 10, jack, queen, and king count"<<endl;
cout<<"as ten; an ace can be either 1 or 11 which is changed automaticaly if the"<<endl;
cout<<"next card delt causes the player to bust. The player goes first and plays his"<<endl;
cout<<"hand by taking additional cards, called HITTING, untill he busts or STANDS. If "<<endl;
cout<<"he busts, he loses. Then the dealer plays its hand. If the dealer busts, the"<<endl;
cout<<"dealer loses. If neither busts, the higher hand total wins. In case of a tie,"<<endl;
cout<<"no one wins.The player's two initial cards are face-down, while the dealer"<<endl;
cout<<"has one face-up card called the UPCARD and one face-down card called the"<<endl;
cout<<"HOLE CARD Player and dealer can look at his own face-down cards. If the"<<endl;
cout<<"dealer has less than 17, it must hit. If the dealer has 17 or more, it"<<endl;
cout<<"must stand (take no more cards), unless it is a SOFT 17, a hand that"<<endl;
cout<<"includes an ace valued as 11. The dealer hits on a SOFT 17."<<endl;
cout<<endl;cout<<endl;
cout<<"Are you ready to play? Enter Y for yes or N for no"<<endl;
cin>>go;
if (go=='n' || go=='N'){
cout<<"Bye,Bye!"<<endl;
exit(0);
}
if (go=='y' || go=='Y'){
state = INITIAL;
}
cout<<endl;cout<<endl;cout<<endl;
while (go!='y' && go!='n'&& go!='Y' && go!='N'){ //loop "ready to play"
cout<<"Are you ready to play? Enter Y for yes or N for no"<<endl;
cin>>go;
if (go=='n' || go=='N'){
cout<<"Bye,Bye!"<<endl;
exit(0);
}
if (go=='y' || go=='Y'){
state = INITIAL;
}
cout<<endl;cout<<endl;cout<<endl;
}
while (1){
switch (state){
case INITIAL:
for (i=0; i<52; i++){ //print out deck of cards
cout<<deck[i]<<" ";
}
cout<<endl;cout<<endl;
cout<<"Dealer will now Shuffle the cards and deal."<<endl;
cout<<endl;cout<<endl;
hand[0]=getcard(); //initial deal
dhand[0]=getcard(); //value 11 "ACE"
hand[1]=getcard();
dhand[1]=getcard();
cout<<hand[0]<<endl; //print players cards
cout<<hand[1]<<endl;
if (hand[0]==11 && hand[1]==11){ //change and change ace to 1 if player delt two aces
cout<<"You have drawn two aces. One has been changed to a 1 in order to avoid busting"<<endl;
hand[0]=1;
} //end if
for(i=0; i<arraylen; i++){ //total players hand
total+=hand[i];
} //end for
cout<<"Your total is "<<total<<endl;cout<<endl;
cout<<"The dealer has "<<dhand[0]<<" X"<<endl;
cout<<"Would you like to Hit or Stand? H or S "<<endl;
cin>>go;
if (go == 'H'|| go == 'h'){
state = HIT;
}
if (go == 's'|| go == 'S'){
state = STAND;
}
while (go!='h' && go!='H' && go!='s' && go!='S'){ //loop HIT or STAND
cout<<"Would you like to Hit or Stand? H or S "<<endl;
cin>>go;
if (go == 'H'|| go == 'h'){
state = HIT;
}//end if
if (go == 's'|| go == 'S'){
state = STAND;
} //end if
} //emd while
break;
case HIT:
b=b++;
total=0;
cout<<"HIT"<<endl;
cout<<endl;cout<<endl;cout<<endl;
newcard=newcard+1; // deal card to player
hand[newcard]=getcard();
cout<<hand[newcard]<<" new card"<<endl;cout<<endl;
for(int x=0; x<arraylen; x++){ //total player hand
total+=hand[x];
} //end for
cout<<"Your total is "<<total<<endl;cout<<endl;
cout<<"The dealer has "<<dhand[0]<<" X"<<endl;cout<<endl;
for (int i=0; i<12; i++){ //print out player hand
cout<<hand[i]<<" "<<endl;
} //end for
a =searcharray(11,hand,arraylen); //search player hand for ace
if (hand[a]==11){ //print found ace
cout<<"You have an ace in your hand "<<hand[a]<<endl;
} //end if
if (total > 21 && hand[a]== 11){ // change ace to 1 reset player total
hand[a]=1;
total=0;
for(i=0; i<arraylen; i++){ //recalculate player total
total+=hand[i];
} //end for
cout<<"your new total is "<<total<<endl;
} //end if
if (total >21){ //check to see if busted
cout<<"You have busted"<<endl;
state = RESET;
break;
} // end if
cout<<"Would you like to Hit or Stand?"<<endl;
cin>>go;
if (go == 'H'|| go == 'h'){
state = HIT;
}//end if
if (go == 's'|| go == 'S'){
state = STAND;
}//end if
while (go!='h' && go!='H' && go!='s' && go!='S'){ // loop HIT or STAND
cout<<"Would you like to Hit or Stand?"<<endl;
cin>>go;
if (go == 'H'|| go == 'h'){
state = HIT;
}//end if
if (go == 's'|| go == 'S'){
state = STAND;
} //end if
} //end while
break;
case STAND:
for(i=0; i<arraylen; i++){ // calculate dealer total
dtotal+=dhand[i];
} //end for
cout<<"The dealer is drawing cards"<<endl;
if (dtotal< 17){ // ckeck to see if dealer can draw cards
dtotal=0;
while (dtotal<=17){ //dealer draws till 17 is reached
d=d++;
dnewcard=dnewcard+1;
dhand[dnewcard]=getcard();
dtotal=0;
for(i=0; i<arraylen; i++){ //calculate dealer total
dtotal+=dhand[i];
} //end for
} //end while
} //end if
for (int i=0; i<12; i++){ //print dealer hand
cout<<dhand[i]<<" "<<endl;
} //end for
cout<<"The dealer total is "<<dtotal<<endl;cout<<endl;
if (dtotal>21){ // check if dealer busts
cout<<"The dealer has busted!! YOU WIN!!"<<endl;
}
pdiff=21-total; // claculate and determine winner
ddiff=21-dtotal;
cout<<"Pd "<<pdiff<<" Dd "<<ddiff<<endl;
if (pdiff<ddiff){
cout<<"YOU WIN!!"<<endl;
}
if(ddiff<pdiff && ddiff>0){
cout<<"YOU LOOSE"<<endl;
}
if(pdiff == ddiff){
cout<<"You have tied. No winner determined."<<endl;
}
state = RESET;
case RESET:
total =0; dtotal=0; //reset values
dnewcard=1; newcard=1;
ddiff=0; pdiff=0;
for (i=0; i<12; i++){ //reset player hand to 0
hand[i]=0;
} //end for
for (i=0; i<12; i++){ //reset player hand to 0
dhand[i]=0;
} //end for
cout<<"Would you like to play again? Type y for yes or n for no to exit."<<endl;
cin>>go;
cout<<endl;cout<<endl;
if (go=='n' || go=='N') {
cout<<"Bye,Bye!"<<endl;
exit(0);
}
if (go=='y' || go=='Y'){
state = INITIAL;
} //end if
break;
default: cout<<"default"<<endl;
break;
} //end switch
} //end while
} //end main
//deal function
int getcard() {
++getCardCalled;
return deck[getCardCalled];
}
int searcharray (int value, int hand[], int arraylen){
int index;
for(index=0; index<12; index++){
if (hand[index]==11){
return(index);
}
}
}
|