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
|
// BlackJack.cpp : Defines the entry point for the console application.
//
#include <stdafx.h>
#include <cstdlib> //needed for rand() and srand()
#include <iostream>
#include <string.h>
#include <ctime>
#include <cmath>
using namespace std;
char cards[52]= { '2','2','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','T','T','T','T','J','J','J','J',
'Q','Q','Q','Q','K','K','K','K','A','A','A','A' };
char Dealer[11];
char Player1[11];
int Player1total=0;
int Dealertotal=0;
int addcards(char count[11], int length)
{
cout<< "count[0] is: " << count[0] <<endl;
cout<< "count[1] is: " << count[1] <<endl;
cout<< "passed length is: " << length <<endl;
int total=0;
for (int x=1; x<length; x++)
{
switch (count[x])
{
case '2':
total= total+2;
break;
case '3':
total=total+3;
break;
case '4':
total= total+4;
break;
case '5':
total= total+5;
break;
case '6':
total= total+6;
break;
case '7':
total= total+7;
break;
case '8':
total= total+8;
break;
case '9':
total= total+9;
break;
case 'T':
total= total+10;
break;
case 'J':
total= total+10;
break;
case 'Q':
total= total+10;
break;
case 'K':
total= total+10;
break;
case 'A':
total= total+11;
if (total>22) {
total=total-10;
} // end of if statement.
break;
default:
total = 999;
break;
} //end of switch statement
cout<< "Player hand total is: " << total <<endl;
} // end of for statement.
return (total);
} //end of function.
int main()
{
int cardsdelt=0;
int Dealerhandlength;
int Player1handlength;
int h;
int swap;
char temp;
char Hit;
char Stand;
char n;
int bust=0;
cout<<"Welcome to Justin's BlackJack table. Ill be your dealer!" <<endl;
cout<< "We will be playing 1 hand at a time with one deck of cards. I hope you will enjoy your stay. " <<endl;
srand (time(NULL));
for (int j=0; j<52;j++)
{
swap = rand()%52;
temp = cards[j];
cards[j] = cards[swap];
cards[swap] = temp;
}
for (int h=0; h<2; h++)
{
Player1[h] = cards[cardsdelt];
cardsdelt++;
Dealer[h] = cards[cardsdelt];
cardsdelt++;
}
Player1handlength =2;
Dealerhandlength =2;
Player1total = addcards((&Player1)[Player1handlength], Player1handlength);
Dealertotal = addcards(Dealer, Dealerhandlength);
cout<< "Players Hand is: " << Player1total << endl;
cout<< "Dealers Hand is: " << Dealer[1] << endl;
n='h';
while (n=='h' || n== 'H'){
cout << "Player 1 would you like to Hit, or Stand? ";
cin>>n;
switch(n)
{
case 'h':
case 'H':
Player1[Player1handlength++] = cards[cardsdelt++]; // puts next card from deck to hand
cout << "your new card is a: " << Player1[Player1handlength - 1]<<endl;
Player1total = addcards((&Player1)[Player1handlength], Player1handlength);
if (Player1total>21)
{
n='s';
cout<< "PLAYER BUST!" <<endl;
bust=1;
break;
case 'S':
case 's':
cout << "your final hand is: " << Player1total <<endl;
break;
}
} //end while
while ((Dealertotal>18) && (bust == 0) {
Dealer[Dealerhandlength++] = cards[cardsdelt++]; // puts a new card in dealers hand
cout<< "Dealers new car is a: " <<Dealer[Dealerhandlength -1]<<endl;
Dealertotal = addcards((&Dealer)[Dealerhandlength], Dealerhandlength);
}
Dealertotal = addcards((&Dealer)[Dealerhandlength], Dealerhandlength);
cout<< "Dealer's total is: " << Dealertotal << endl;
if (Dealertotal>21)
{
cout<< "The Dealer BUST. Player WINS!!!!!" <<endl;
}
if (Player1total>Dealertotal) {
cout<< "Player 1 WINS!!!!!!" << endl;
}
else {
cout<< "Sorry but the Dealer has won! "<< endl;
}
}
|