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
|
#include <iostream>
#include <fstream>
#include <ctime>
using namespace std;
//function to shuffle cards, lines 182-188
void Shuffle(bool CardsDealt[]);
//function print out the cards suit dealt, lines 190-238
void PrintCard(int Card);
//function to print out hands dealt, lines 240-251
void PrintHand(int Hand[], const int CardCount);
//function to deal cards, lines 253-269
int GetNextCard(bool CardsDealt[]);
//function to keep track of score, lines 271-301
int ScoreHand(int Hand[], const int CardCount);
//function to print out scores, lines 303-311
void PrintScoresAndHands(int HouseHand[], const int HouseCardCount, int PlayerHand[], const int PlayerCardCount);
int main()
{
// Seed the random number generator
time_t qTime;
time(&qTime);
srand(time(0));
// Declare the stream variables
ifstream infile;
//Declares and initialize variables
bool CardsDealt[52];
int HouseCardCount = 0;
int HouseHand[12];
int PlayerCardCount = 0;
int PlayerHand[12];
char ch;
// Loop once for each hand
while (true)
{
// Shuffles the cards
Shuffle(CardsDealt);
// Deal the hands. Get two cards for each one down one up for house
PlayerHand[0] = GetNextCard(CardsDealt);
HouseHand[0] = GetNextCard(CardsDealt);
PlayerHand[1] = GetNextCard(CardsDealt);
HouseHand[1] = GetNextCard(CardsDealt);
HouseCardCount = 2;
PlayerCardCount = 2;
// Open the files
infile.open("intro.txt");
infile.get(ch);
// prints out whats in intro.txt file
while (!infile.eof())
{
cout << ch ;
infile.get(ch);
}
cout <<endl;
// Signal a new hand.
cout << "--------------------------------------------------------" << endl;
cout << "-----------------------New Hand-------------------------" << endl;
cout << "--------------------------------------------------------" << endl;
// Get Player's hits. Calculate the score and redisplay after each hit
char PlayerChoice;
bool PlayerHits = true;
int PlayerScore = ScoreHand(PlayerHand, PlayerCardCount);
do
{
// Print the dealt cards, but only the House's second card
cout << "House's Hand" << endl;
cout <<endl;
cout << "** ";
PrintCard(HouseHand[1]);
cout << endl;
cout <<endl;
cout << "Player's Hand: Score = " << ScoreHand(PlayerHand, PlayerCardCount) << endl;
cout <<endl;
PrintHand(PlayerHand, PlayerCardCount);
cout <<endl;
// Ask the Player whether he wants a hit or to stay
cout << "Hit(h) or stay(s): ";
cout <<endl;
cin >> PlayerChoice;
if (PlayerChoice == 'h')
{
PlayerHand[PlayerCardCount] = GetNextCard(CardsDealt);
++PlayerCardCount;
}
else if (PlayerChoice == 's')
{
PlayerHits = false;
}
else
{
cout << "Error: Try Again!" << endl;
cout << endl;
}
// Get the Player's current score to update and check for bust
PlayerScore = ScoreHand(PlayerHand, PlayerCardCount);
}
while (PlayerHits && PlayerScore < 22);
// Once the player is done taking hits, check whether he busted
if (PlayerScore > 21)
{
// The Player busted. The House wins
cout << "**** The House Wins!!! ****" << endl;
cout <<endl;
PrintScoresAndHands(HouseHand, HouseCardCount, PlayerHand, PlayerCardCount);
}
else
{
// If the player didn't bust, then the house takes hits below 17
int HouseScore = ScoreHand(HouseHand, HouseCardCount);
while (HouseScore < 17)
{
HouseHand[HouseCardCount] = GetNextCard(CardsDealt);
++HouseCardCount;
HouseScore = ScoreHand(HouseHand, HouseCardCount);
}
bool HouseBusts = (HouseScore > 21);
if (HouseBusts)
{
// The House busted. Player wins
cout << "**** The Player Wins!!! ****" << endl;
cout <<endl;
PrintScoresAndHands(HouseHand, HouseCardCount, PlayerHand, PlayerCardCount);
}
else
{
// Compare scores and determine the winner
if (PlayerScore == HouseScore)
{
// Tie. This is called a "push"
cout << "**** Tie!!! ****" << endl;
cout <<endl;
PrintScoresAndHands(HouseHand, HouseCardCount, PlayerHand, PlayerCardCount);
}
else if (PlayerScore > HouseScore)
{
// The Player wins
cout << "**** The Player Wins!!! ****" << endl;
cout <<endl;
PrintScoresAndHands(HouseHand, HouseCardCount, PlayerHand, PlayerCardCount);
}
else
{
// The House wins
cout << "**** The House Wins!!! ****" << endl;
cout <<endl;
PrintScoresAndHands(HouseHand, HouseCardCount, PlayerHand, PlayerCardCount);
}
}
}
}
return 0;
}
void Shuffle(bool CardsDealt[])
{
for (int Index = 0; Index < 52; ++Index)
{
CardsDealt[Index] = false;
}
}
void PrintCard(int Card)
{
// Prints out Rank
const int Rank = (Card % 13);
if (Rank == 0)
{
cout << 'A';
}
else if (Rank < 9)
{
cout << (Rank + 1);
}
else if (Rank == 9)
{
cout << 'T';
}
else if (Rank == 10)
{
cout << 'J';
}
else if (Rank == 11)
{
cout << 'Q';
}
else
{
cout << 'K';
}
// Print out Suit
const int Suit = (Card/13);
if (Suit == 0)
{
cout << 'C'; //Clubs
}
else if (Suit == 1)
{
cout << 'D'; // Diamonds
}
else if (Suit == 2)
{
cout << 'H'; //Hearts
}
else
{
cout << 'S';//Spades
}
}
void PrintHand(int Hand[], const int CardCount)
{
for (int CardIndex = 0; CardIndex < CardCount; ++CardIndex)
{
const int NextCard = Hand[CardIndex];
PrintCard(NextCard);
cout << " ";
cout << endl;
}
}
int GetNextCard(bool CardsDealt[])
{
bool CardDealt = true;
int NewCard = -1;
do
{
NewCard = (rand() % 52);
if (!CardsDealt[NewCard])
{
CardDealt = false;
}
}
while (CardDealt);
return NewCard;
}
int ScoreHand(int Hand[], const int CardCount)
{
int AceCount = 0;
int Score = 0;
for (int CardIndex = 0; CardIndex < CardCount; ++CardIndex)
{
const int NextCard = Hand[CardIndex];
const int Rank = (NextCard % 13);
if (Rank == 0)
{
++AceCount;
++Score;
}
else if (Rank < 9)
{
Score = Score + (Rank + 1);
}
else
{
Score = Score + 10;
}
}
while (AceCount > 0 && Score < 12)
{
--AceCount;
Score = Score + 10;
}
return Score;
}
void PrintScoresAndHands(int HouseHand[], const int HouseCardCount, int PlayerHand[], const int PlayerCardCount)
{
cout << "House's Hand: Score = " << ScoreHand(HouseHand, HouseCardCount) << endl;
PrintHand(HouseHand, HouseCardCount);
cout <<endl;
cout << "Player's Hand: Score = " << ScoreHand(PlayerHand,PlayerCardCount) << endl;
PrintHand(PlayerHand, PlayerCardCount);
cout <<endl;
}
| |