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
|
#include <iostream>
#include <ctime>
#include <string>
static int i = -1;
using namespace std;
string name();
int getRand();
int firstTurn(int first);
void dealer(int& dNew, int& dOld);
void player(int& pNew, int& pOld);
bool hs();
bool botHs(int dealer);
void card(int card);
int specCard(int num);
bool instawinner(int check);
bool botInstawinner(int check);
void winner (int dHand, int pHand);
int ace (int card, int hand);
int ace1Counter(int card, int counter);
void aceTo1(int card, int& counter, int& hand);
int main()
{
int dealerN = 0, dealerO = 0, playerN = 0, playerO = 0;
string playerName = name();
dealerO = firstTurn(dealerO);
player(playerN, playerO);
if (playerO <= 21)
dealer(dealerN, dealerO);
if(playerO < 21 && dealerO < 21)
winner (dealerO, playerO);
}
string name() //this is a pointless function needed for the assignment
{
string name;
cout << "Welcome to the Trump Casino!\nPlease enter your name: ";
cin >> name;
return name;
}
int getRand() //this is the random function (i do not know how this works. This is what the teacher provided.
{
if(i == 100) i = 0;
int rands[100];
srand((unsigned)time(0));
for(int index=0; index<100; index++)
{
rands[index] = (rand()%13)+1;
}
i++;
int n = rands[i];
return n;
}
int firstTurn(int first)//this represents the first turn in the game
{
int temp, reg;
temp = getRand();
reg = temp;
cout << "\nThe dealer:\n? + " << endl;
card(temp);
temp = specCard(temp);
temp += getRand();
return temp;
}
void dealer(int& dNew, int& dOld) //this represents the bot dealers turn
{
while(botInstawinner(dOld)&&botHs(dOld))
{
int aceCounter = 0;
dNew = getRand();
cout << "\nDealer: \n";
cout << dOld << endl;
cout <<"+ \n";
card(dNew);
dNew = specCard(dNew);
dNew = ace(dNew, dOld);
dOld = dNew + dOld;
aceTo1(dNew, aceCounter, dOld);
}
}
void player(int& pNew, int& pOld) //this represents the players turn
{
int temp, temp1, aceCounter = 0;
if(pOld == 0)
{
temp = getRand();
pNew = getRand();
cout << "\nPlayer: \n";
card(temp);
cout <<"+ \n";
card(pNew);
pNew = specCard(pNew);
temp = specCard(temp);
temp1 = temp + pNew;
aceCounter = ace1Counter(pNew, aceCounter);
pNew = ace(pNew, temp1);
temp = ace(temp, temp1);
pOld = pNew + temp;
aceTo1(pNew, aceCounter, pOld);
}
while (instawinner(pOld)&&hs())
{
pNew = getRand();
cout << "\nPlayer: \n";
cout << pOld << endl;
cout <<"+ \n";
card(pNew);
pNew = specCard(pNew);
pNew = ace(pNew, pOld);
pOld = pNew + pOld;
aceTo1(pNew, aceCounter, pOld);
}
}
bool hs()//this will ask the user to hit or stand and return true or false
{
char response;
do
{
cout << "Hit(h) or Stand(s)? : ";
cin >> response;
}
while (response != 'h' && response != 's' && response != 'H' && response != 'S');
if (response == 'h' || response == 'H')
return 1;
else
return 0;
}
bool botHs(int card) //this will check whether the bot will keep going or not
{
if (card > 17)
return 0;
else if (card <= 17)
return 1;
else if (card == 21)
return 0;
}
void card(int card) //this will display the correct symbol for the card
{
switch(card)
{
case 1:
cout << "A" << endl;
break;
case 2:
cout << card << endl;
break;
case 3:
cout << card << endl;
break;
case 4:
cout << card << endl;
break;
case 5:
cout << card << endl;
break;
case 6:
cout << card << endl;
break;
case 7:
cout << card << endl;
break;
case 8:
cout << card << endl;
break;
case 9:
cout << card << endl;
break;
case 10:
cout << card << endl;
break;
case 11:
cout << "J" << endl;
break;
case 12:
cout << "Q" << endl;
break;
case 13:
cout << "K" << endl;
break;
default:
cout << card << endl;
}
}
int specCard(int num)//changes 11-13 into 10 since they represent J, Q, K
{
if (num > 10 && num <= 13)
{
num = 10;
}
return num;
}
bool instawinner(int check)//this checks whether the user won or not
{
if (check < 21)
return true;
else if (check == 21)
{
cout << "You win! Here's NOTHING" << endl;
return false;
}
else if(check > 21)
{
cout << "You busted with a "<< check<< " mate." << endl;
return false;
}
}
bool botInstawinner(int check) //this checks whether the bot won or not
{
if (check <= 17)
return true;
else if (check == 21)
{
cout << "Dealer got blackjack. He wins! He's too pro for you." << endl;
return false;
}
else if(check > 21)
{
cout << "Dealer busted with a "<< check<< " mate. You win!" << endl;
return false;
}
}
void winner (int dHand, int pHand)
{
if (dHand>pHand)
cout << "You lost to the dealer since he had " << dHand <<" while you had " << pHand <<endl;
else if (dHand<pHand)
cout << "You won against the dealer since he had " << dHand <<" while you had " << pHand <<endl;
else
cout << "Tie at " << dHand <<endl;
}
int ace (int card, int hand)
{
if (card == 1)
return 11;
else
return card;
}
int ace1Counter(int card, int counter)
{
if (card == 1)
{
counter++;
return counter;
}
else
return counter;
}
void aceTo1(int card, int& counter, int& hand)
{
if(counter >= 1 && (hand + card) > 21)
{
counter--;
hand = hand - 10;
}
}
|