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
|
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
using namespace std;
void randomizer(); //Function that allows a randomly picked card
void cardPicker(); //Function that assigns a card based upon number
void computerCardPicker();
void cardSuiteIdentifier();
void cardNumberIdentifier();
void ace();
bool totalChecker(int total);
int card; //The current card being dealt
int stand = 0, suite;
int randomNum;
int score; //Score of current card
int total = 0, computerTotal = 0;
int deck[52] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, //Clubs
13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, //Diamonds
26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, //Hearts
39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50 ,51, }; //Spades
int main ()
{
cout << "Welcome to Blackjack" << endl;
while (stand == 0)
{
randomizer();
cardPicker(); //Picks a card for player
total += score;
cout << "Your total is: " << total << endl; //Shows converted number
randomizer();
computerCardPicker();
computerTotal += score;
cout << "My total is: " << computerTotal << endl;
if (totalChecker(total)) //Figures out if the game can continue
{
cout << "Dealing..." << endl;
}
else
{
cout << "Goodbye" << endl;
return 0;
}
}
while ((stand == 1) && (computerTotal <= 21))
{
computerCardPicker();
cardNumberIdentifier(); //Figures out and displays number of card
cardSuiteIdentifier(); //Figures out and displays suite of card
computerTotal += score;
cout << "My total is: " << computerTotal << endl;
}
return 0;
}
void randomizer()
{
srand ( time (NULL) ); //Initialize random seed
randomNum = rand() % 51 + 0; //Generate random number
if (deck[randomNum] == 100)
{
randomizer();
}
else
{
card = deck[randomNum]; //Picks card out of deck corresponding to number
deck[randomNum] = 100;
}
}
void cardPicker()
{
cout << "You have been dealt the ";
cardNumberIdentifier(); //Figures out and displays number of card
}
void computerCardPicker()
{
cout << "I have been dealt the ";
cardNumberIdentifier(); //Figures out and displays number of card
}
void ace() //Figures out if the player wants a 1 or 11 Ace.
{
int ace;
cardSuiteIdentifier(); //Assigns suite
cout << "Do you want the Ace to be a 1 or 11?" << endl << "1) 1" << endl << "2) 11" << endl;
cin >> ace;
if (ace == 1)
{
score = 1;
}
else
{
score = 11;
}
}
void cardNumberIdentifier() //Figures out number of card
{
if ((card == 0) || (card == 13) || (card == 26) || (card == 39))
{
cout << "Ace of ";
ace();
return; //Stops it outputting the suite, which is handled in the ace function
}
else if ((card == 1) || (card == 14) || (card == 27) || (card == 40))
{
cout << "Two of ";
score = 2;
}
else if ((card == 2) || (card == 15) || (card == 28) || (card == 41))
{
cout << "Three of ";
score = 3;
}
else if ((card == 3) || (card == 16) || (card == 29) || (card == 42))
{
cout << "Four of ";
score = 4;
}
else if ((card == 4) || (card == 17) || (card == 30) || (card == 43))
{
cout << "Five of ";
score = 5;
}
else if ((card == 5) || (card == 18) || (card == 31) || (card == 44))
{
cout << "Six of ";
score = 6;
}
else if ((card == 6) || (card == 19) || (card == 32) || (card == 45))
{
cout << "Seven of ";
score = 7;
}
else if ((card == 7) || (card == 20) || (card == 33) || (card == 46))
{
cout << "Eight of ";
score = 8;
}
else if ((card == 8) || (card == 21) || (card == 34) || (card == 47))
{
cout << "Nine of ";
score = 9;
}
else if ((card == 9) || (card == 22) || (card == 35) || (card == 48))
{
cout << "Ten of ";
score = 10;
}
else if ((card == 10) || (card == 23) || (card == 36) || (card == 49))
{
cout << "Jack of ";
score = 10;
}
else if ((card == 11) || (card == 24) || (card == 37) || (card == 50))
{
cout << "Queen of ";
score = 10;
}
else //(card == 12 || 25 || 38 || 51)
{
cout << "King of ";
score = 10;
}
cardSuiteIdentifier(); //Figures out and displays suite of card
}
void cardSuiteIdentifier() //Figures out suite of card
{
if (card <= 12)
{
cout << "Clubs" << endl;
}
else if (card >= 13 && card < 26)
{
cout << "Diamonds" << endl;
}
else if (card >= 26 && card < 39)
{
cout << "Hearts" << endl;
}
else
{
cout << "Spades" << endl;
}
}
bool totalChecker(int total)
{
int hitStand;
if ((total <= 21) && (computerTotal > 21))
{
cout << "You win!" << endl;
return false;
}
else if ((total <= 20) && (computerTotal == 21))
{
cout << "You lose!" << endl;
return false;
}
else if ((total < 21) && (computerTotal < 21))
{
cout << "What would you like to do?" << endl << "1) Hit" << endl << "2) Stand" << endl;
cin >> hitStand;
if (hitStand == 1)
{
return true;
}
else if (hitStand == 2)
{
stand = 1;
return true;
}
else
{
totalChecker(total);
}
}
else
{
cout << "You lose!" << endl;
return false;
}
}
|