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
|
#include <iostream>
#include <cmath>
using namespace std;
void shuffleDeck (bool cardsDealt[]);
int drawCard (bool cardsDealt[]);
void showCard (int printCard);
void showHand (int playerHand[], const int playerCardCount);
int handScore (int hand[], const int cardCount);
void showScoresAndHands (int houseHand[], const int houseCardCount, int playerHand[], const int playerCardCount);
int main()
{
bool cardsDealt[52];
int houseCardCount = 0;
int playerCardCount = 0;
int houseHand[12];
int playerHand[12];
{
shuffleDeck(cardsDealt);
houseHand[0] = drawCard(cardsDealt);
playerHand[0] = drawCard(cardsDealt);
houseHand[1] = drawCard(cardsDealt);
playerHand[1] = drawCard(cardsDealt);
houseCardCount = 2;
playerCardCount = 2;
cout << "-----------------------New Hand-------------------------" << endl;
bool playerHits = true;
char playerDecision;
int playerScore = handScore(playerHand,playerCardCount);
do{
cout << "House Hand:\n";
cout << "* * ";
showCard(houseHand[1]);
cout << endl;
cout << "Player Hand:" << endl;
showHand(playerHand, playerCardCount);
cout << "Score :";
handScore(playerHand, playerCardCount);
cout << "Hit(h) or Stay(s)";
cin >> playerDecision;
if (playerDecision == 'h'){
playerHand[playerCardCount] = drawCard(cardsDealt);
playerCardCount++;
}else if (playerDecision == 's'){
playerHits = false;
}else{
cout << "Error! Try again!\n";
}
cout << endl;
playerScore = handScore(playerHand,playerCardCount);
}while (playerHits && playerScore < 22);
if (playerScore > 21) {
cout << "**** The House Wins!!! ****" << endl;
showScoresAndHands(houseHand, houseCardCount, playerHand, playerCardCount);
} else {
int houseScore = handScore(houseHand, houseCardCount);
while (houseScore < 17) {
houseHand[houseCardCount] = drawCard(cardsDealt);
++houseCardCount;
houseScore = handScore(houseHand, houseCardCount);
}
bool houseBusts = (houseScore > 21);
if (houseBusts) {
cout << "**** The Player Wins!!! ****" << endl;
showScoresAndHands(houseHand, houseCardCount, playerHand, playerCardCount);
} else {
if (playerScore == houseScore) {
cout << "**** Tie!!! ****" << endl;
showScoresAndHands(houseHand, houseCardCount, playerHand, playerCardCount);
} else if (playerScore > houseScore) {
cout << "**** The Player Wins!!! ****" << endl;
showScoresAndHands(houseHand, houseCardCount, playerHand, playerCardCount);
} else {
cout << "**** The House Wins!!! ****" << endl;
showScoresAndHands(houseHand, houseCardCount, playerHand, playerCardCount);
}
}
}
}
system("pause");
return EXIT_SUCCESS;
}
void shuffleDeck (bool cardsDealt[])
{
for(int deck = 0;deck < 52;deck++){
cardsDealt[deck] = false;
}
}
int drawCard (bool cardsDealt[])
{
bool carddealt = true;
int newcard = -1;
do{
newcard = (rand() % 52);
if(!cardsDealt[!newcard]){
carddealt = false;
}
}while(carddealt);
return newcard;
}
void showCard (int printCard) {
const int cRank = (printCard % 13);
if (cRank == 0){
cout << "Ace";
} else if (cRank < 9){
cout << cRank + 1;
}else if (cRank == 9){
cout << "Ten";
}else if (cRank == 10){
cout << "Jack";
}else if (cRank == 11){
cout << "Queen";
}else{
cout << "King";
}
const int cSuit = (printCard/13);
if (cSuit == 0){
cout << "Clubs";
}else if (cSuit == 1){
cout << "Diamonds";
}else if (cSuit == 2){
cout << "Hearts";
}else{
cout << "Spades";
}
}
void showHand(int playerHand[], const int playerCardCount)
{
for (int cards = 0; cards < playerCardCount; cards++){
const int nextCard = playerHand[cards];
showCard(nextCard);
cout << " ";
}
cout << endl;
}
int handScore (int playerHand[], const int playerCardCount)
{
int acecount = 0;
int score = 0;
for (int cards = 0; cards < playerCardCount; cards++){
const int nextCard = playerHand[cards];
const int cRank = (nextCard % 13);
if (cRank == 0) {
acecount++;
score++;
} else if (cRank < 9) {
score = score + (cRank + 1);
} else {
score = score + 10;
}
}
while (acecount > 0 && score < 12) {
acecount--;
score = score + 10;
}
return score;
}
void showScoresAndHands(int houseHand[], const int houseCardCount, int playerHand[], const int playerCardCount)
{
cout << "House Hand:" << endl;
showHand(houseHand, houseCardCount);
cout << "Score :" << endl;
handScore(houseHand, houseCardCount);
cout << "Player Hand:" << endl;
showHand(playerHand, playerCardCount);
cout << "Score :";
handScore(playerHand, playerCardCount);
}
|