For loop help
Aug 4, 2012 at 6:14pm UTC
I am trying to figure out how to make a for loop help for my blackjack game so that when the player hits it will automatically give them another card and same for the dealer. The dealer stands at 17 and above and hits and 16 and below. I had it working with a bunch of nested if statements but I realized there has to be a simpler way with a for loop or something. Any help would be appreciated. Here is the for loop I had but it didn't work
1 2 3 4 5 6 7 8 9
if (result1 == 0)
{
for (int c = 0; total_value < 21; c++)
cout << "New card: " ;
print_card(c);
cout << endl;
total_value = total_value + c5.value;
cout << "Player has: " << total_value << endl;
}
Here is the rest of the code without all the nested if statements.
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 <cstdlib>
#include <cstring>
using namespace std;
typedef enum
{
VALUE,
JACK,
QUEEN,
KING,
ACE
} FACE;
typedef struct
{
int value;
FACE f;
} CARD;
void print_card(CARD c)
{
if (c.f == ACE)
cout << "ACE (11)" ;
else if (c.f == KING)
cout << "KING (10)" ;
else if (c.f == QUEEN)
cout << "QUEEN (10)" ;
else if (c.f == JACK)
cout << "JACK (10)" ;
else if (c.f == VALUE)
{
if (c.value == 10)
cout << "TEN (10)" ;
else if (c.value == 9)
cout << "NINE (9)" ;
else if (c.value == 8)
cout << "EIGHT (8)" ;
else if (c.value == 7)
cout << "SEVEN (7)" ;
else if (c.value == 6)
cout << "SIX (6)" ;
else if (c.value == 5)
cout << "FIVE (5)" ;
else if (c.value == 4)
cout << "FOUR (4)" ;
else if (c.value == 3)
cout << "THREE (3)" ;
else if (c.value == 2)
cout << "TWO (2)" ;
else
cout << "Value not recognized" ;
}
else
cout << "Face not recognized" ;
}
void init_deck(CARD deck[])
{
for (int index = 0; index < 4; index++)
{
int seg = index * 13;
deck[seg + 0].f = VALUE;
deck[seg + 0].value = 2;
deck[seg + 1].f = VALUE;
deck[seg + 1].value = 3;
deck[seg + 2].f = VALUE;
deck[seg + 2].value = 4;
deck[seg + 3].f = VALUE;
deck[seg + 3].value = 5;
deck[seg + 4].f = VALUE;
deck[seg + 4].value = 6;
deck[seg + 5].f = VALUE;
deck[seg + 5].value = 7;
deck[seg + 6].f = VALUE;
deck[seg + 6].value = 8;
deck[seg + 7].f = VALUE;
deck[seg + 7].value = 9;
deck[seg + 8].f = VALUE;
deck[seg + 8].value = 10;
deck[seg + 9].f = JACK;
deck[seg + 9].value = 10;
deck[seg + 10].f = QUEEN;
deck[seg + 10].value = 10;
deck[seg + 11].f = KING;
deck[seg + 11].value = 10;
deck[seg + 12].f = ACE;
deck[seg + 12].value = 11;
}
}
void print_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
print_card(deck[x]);
cout << endl;
}
}
void shuffle_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
int new_index = rand() % 52;
CARD temp = deck[x];
deck[x] = deck[new_index];
deck[new_index] = temp;
}
}
int main()
{
CARD deck[52];
//print_deck(deck);
int bank;
unsigned int seed;
int bet;
char word1[20];
char word2[20];
static const char hit[] = "hit" ;
static const char stand[] = "stand" ;
int result1;
int result2;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
srand(seed);
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];
CARD c3 = deck[next_index++];
CARD c4 = deck[next_index++];
CARD c5 = deck[next_index++];
CARD c6 = deck[next_index++];
CARD c7 = deck[next_index++];
CARD c8 = deck[next_index++];
CARD c9 = deck[next_index++];
CARD c10 = deck[next_index++];
CARD c11 = deck[next_index++];
CARD c12 = deck[next_index++];
int total_value = c1.value + c2.value;
cout << "Current Hand: " ;
print_card(c1);
cout << " " ;
print_card(c2);
cout << ". " << "Player has: " << total_value << endl;
int dealer_value = c3.value + c4.value;
cout << "Dealer's hand: " ;
print_card(c3);
cout << " " ;
print_card(c4);
cout << ". " << "Dealer has: " << dealer_value << endl;
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
result2 = strcmp (word2, stand);
if (result1 == 0)
{
for (int c = 0; total_value < 21; c++)
cout << "New card: " ;
print_card(c);
cout << endl;
total_value = total_value + c5.value;
cout << "Player has: " << total_value << endl;
}
}
Aug 4, 2012 at 6:23pm UTC
It would be not bad if you undesrstand what you are doing.
In your loop variable c is defined as int and you pass it as the argument of function print_card that accepts structure CARD.
Aug 4, 2012 at 6:30pm UTC
I am trying to understand it. I have only been learning c++ for 3 months now. I changed the for loop to
1 2 3 4 5 6 7 8 9
if (result1 == 0)
{
for (CARD c1 = 0; total_value < 21; c1++)
cout << "New card: " ;
print_card(c1);
cout << endl;
total_value = total_value + c1.value;
cout << "Player has: " << total_value << endl;
}
It still doesn't work. I am not sure how to make a loop that will work with the CARD structure.
Aug 5, 2012 at 6:00am UTC
I am trying to figure out how to make a for loop help for my blackjack game so that when the player hits it will automatically give them another card and same for the dealer. I wasn't able to figure it out so I ended up with a bunch of if statements and my code is crazy. Here is the part of the code that I am trying to make a loop for
1 2 3 4 5
cout << "New card: " ;
print_card(c5);
cout << endl;
total_value = total_value + c5.value;
cout << "Player has: " << total_value << endl;
Any help would be apprecaited. Thank you.
Aug 5, 2012 at 6:09am UTC
I think it would help if you create a DrawCard function. The function should accept no parameters and return a CARD. The CARD should also be removed from either the top or the bottom of the deck. This will ensure that the same card doesn't get drawn more than once (since your cards don't have suits, you'll have at least 4 of each card).
Aug 5, 2012 at 3:29pm UTC
Thank you for your help. I have actually changed the code entirely to simplify it. I got a little help. I still have a couple errors. I get the error "error: no match for 'operator<<' in 'std::operator<< [with _Traits = std::char_traits<char>](((std::basic_ostream<char, std::char_traits<char> >&)(& std::cout)), ((const char*)"New Card: ")) << print_card(c5)'|" at line 203 and I also have to give the option to repeat the entire game if the bankroll is greater than 0 by typing yes or no. I thinking a while loop would work. I have to fix this error before I can try it. Any help on this error would be appreciated.
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
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef enum
{
VALUE,
JACK,
QUEEN,
KING,
ACE
} FACE;
typedef struct
{
int value;
FACE f;
} CARD;
void print_card(CARD c)
{
if (c.f == ACE)
cout << "ACE (11)" ;
else if (c.f == KING)
cout << "KING (10)" ;
else if (c.f == QUEEN)
cout << "QUEEN (10)" ;
else if (c.f == JACK)
cout << "JACK (10)" ;
else if (c.f == VALUE)
{
if (c.value == 10)
cout << "TEN (10)" ;
else if (c.value == 9)
cout << "NINE (9)" ;
else if (c.value == 8)
cout << "EIGHT (8)" ;
else if (c.value == 7)
cout << "SEVEN (7)" ;
else if (c.value == 6)
cout << "SIX (6)" ;
else if (c.value == 5)
cout << "FIVE (5)" ;
else if (c.value == 4)
cout << "FOUR (4)" ;
else if (c.value == 3)
cout << "THREE (3)" ;
else if (c.value == 2)
cout << "TWO (2)" ;
else
cout << "Value not recognized" ;
}
else
cout << "Face not recognized" ;
}
void init_deck(CARD deck[])
{
for (int index = 0; index < 4; index++)
{
int seg = index * 13;
deck[seg + 0].f = VALUE;
deck[seg + 0].value = 2;
deck[seg + 1].f = VALUE;
deck[seg + 1].value = 3;
deck[seg + 2].f = VALUE;
deck[seg + 2].value = 4;
deck[seg + 3].f = VALUE;
deck[seg + 3].value = 5;
deck[seg + 4].f = VALUE;
deck[seg + 4].value = 6;
deck[seg + 5].f = VALUE;
deck[seg + 5].value = 7;
deck[seg + 6].f = VALUE;
deck[seg + 6].value = 8;
deck[seg + 7].f = VALUE;
deck[seg + 7].value = 9;
deck[seg + 8].f = VALUE;
deck[seg + 8].value = 10;
deck[seg + 9].f = JACK;
deck[seg + 9].value = 10;
deck[seg + 10].f = QUEEN;
deck[seg + 10].value = 10;
deck[seg + 11].f = KING;
deck[seg + 11].value = 10;
deck[seg + 12].f = ACE;
deck[seg + 12].value = 11;
}
}
void print_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
print_card(deck[x]);
cout << endl;
}
}
void shuffle_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
int new_index = rand() % 52;
CARD temp = deck[x];
deck[x] = deck[new_index];
deck[new_index] = temp;
}
}
int main()
{
CARD deck[52];
//print_deck(deck);
int bank;
unsigned int seed;
int bet;
char word1[20];
char word2[20];
static const char hit[] = "hit" ;
static const char stand[] = "stand" ;
int result1;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
srand(seed);
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
do
{
cout << "Bet must be greater than zero and be at most " << bank << endl;
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
}while (bet < 0);
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];
CARD c3 = deck[next_index++];
CARD c4 = deck[next_index++];
CARD c5 = deck[next_index++];
CARD c6 = deck[next_index++];
int total_value = c1.value + c2.value;
cout << "Current Hand: " ;
print_card(c1);
cout << " " ;
print_card(c2);
cout << ". " << "Player has: " << total_value << endl;
int dealer_value = c3.value + c4.value;
cout << "Dealer's hand: " ;
print_card(c3);
cout << " " ;
print_card(c4);
cout << ". " << "Dealer has: " << dealer_value << endl;
int hit_count = 3;
while (hit_count > 0 && total_value < 21)
{
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, stand);
if (result1 == 0)
{
break ;
hit_count--;
CARD c5 = deck[next_index++];
total_value += c5.value;
cout << "New Card: " << print_card(c5) << endl;
cout << "Player has: " << total_value << endl;
}
}
if (total_value > 21)
{
cout << "Player bust! You lose." << endl;
bank = bank - bet;
if (bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
else
{
cout << "Current bankroll: " << bank << endl;
}
}
int hit_count = 3;
while (hit_count > 0 && dealer_value < 17)
{
hit_count--;
CARD c6 = deck[next_index++];
dealer_value += c6.value;
cout << "New Card: " << print_card(c6) << endl;
cout << "Dealer has: " << dealer_value << endl;
}
if (dealer_value > 21)
{
cout << "Dealer busts. You win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
if (total_value <= 21 && dealer_value <= 21)
{
if (total_value == dealer_value)
{
cout << "Push." << endl;
cout << "Current bankroll: " << bank << endl;
}
else
print total_value > dealer_value ? "You win!" : "Dealer wins." ;
}
}
Aug 5, 2012 at 3:40pm UTC
cout << "New Card: " << print_card(c5) << endl;
It's because print_card(c5) doesn't return a value for cout to display. You have two options, change print_card() to return a string (I believe that's too difficult for you), or simply change line 203 to this:
1 2 3
cout << "New Card: " ;
print_card(c5);
cout << endl;
Edit: As for the loop, just make sure the players "bank" doesn't reset. Aside from that, it looks like you're almost done.
Last edited on Aug 5, 2012 at 3:42pm UTC
Aug 5, 2012 at 4:00pm UTC
Thanks for the help. I changed that. I am having problems with my while loop line 154 in for the bet. It is only suppose to display if the bet is below zero but it displays no matter what. I cannot get the "Stand" function to work line 190. The hit works but not the stand.
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
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef enum
{
VALUE,
JACK,
QUEEN,
KING,
ACE
} FACE;
typedef struct
{
int value;
FACE f;
} CARD;
void print_card(CARD c)
{
if (c.f == ACE)
cout << "ACE (11)" ;
else if (c.f == KING)
cout << "KING (10)" ;
else if (c.f == QUEEN)
cout << "QUEEN (10)" ;
else if (c.f == JACK)
cout << "JACK (10)" ;
else if (c.f == VALUE)
{
if (c.value == 10)
cout << "TEN (10)" ;
else if (c.value == 9)
cout << "NINE (9)" ;
else if (c.value == 8)
cout << "EIGHT (8)" ;
else if (c.value == 7)
cout << "SEVEN (7)" ;
else if (c.value == 6)
cout << "SIX (6)" ;
else if (c.value == 5)
cout << "FIVE (5)" ;
else if (c.value == 4)
cout << "FOUR (4)" ;
else if (c.value == 3)
cout << "THREE (3)" ;
else if (c.value == 2)
cout << "TWO (2)" ;
else
cout << "Value not recognized" ;
}
else
cout << "Face not recognized" ;
}
void init_deck(CARD deck[])
{
for (int index = 0; index < 4; index++)
{
int seg = index * 13;
deck[seg + 0].f = VALUE;
deck[seg + 0].value = 2;
deck[seg + 1].f = VALUE;
deck[seg + 1].value = 3;
deck[seg + 2].f = VALUE;
deck[seg + 2].value = 4;
deck[seg + 3].f = VALUE;
deck[seg + 3].value = 5;
deck[seg + 4].f = VALUE;
deck[seg + 4].value = 6;
deck[seg + 5].f = VALUE;
deck[seg + 5].value = 7;
deck[seg + 6].f = VALUE;
deck[seg + 6].value = 8;
deck[seg + 7].f = VALUE;
deck[seg + 7].value = 9;
deck[seg + 8].f = VALUE;
deck[seg + 8].value = 10;
deck[seg + 9].f = JACK;
deck[seg + 9].value = 10;
deck[seg + 10].f = QUEEN;
deck[seg + 10].value = 10;
deck[seg + 11].f = KING;
deck[seg + 11].value = 10;
deck[seg + 12].f = ACE;
deck[seg + 12].value = 11;
}
}
void print_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
print_card(deck[x]);
cout << endl;
}
}
void shuffle_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
int new_index = rand() % 52;
CARD temp = deck[x];
deck[x] = deck[new_index];
deck[new_index] = temp;
}
}
int main()
{
CARD deck[52];
//print_deck(deck);
int bank;
unsigned int seed;
int bet;
char word1[20];
char word2[20];
static const char hit[] = "hit" ;
static const char stand[] = "stand" ;
int result1;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
srand(seed);
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
while (bet <= 0);
{
cout << "Bet must be greater than zero and be at most " << bank << endl;
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
}
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];
CARD c3 = deck[next_index++];
CARD c4 = deck[next_index++];
CARD c5 = deck[next_index++];
CARD c6 = deck[next_index++];
int total_value = c1.value + c2.value;
cout << "Current Hand: " ;
print_card(c1);
cout << " " ;
print_card(c2);
cout << ". " << "Player has: " << total_value << endl;
int dealer_value = c3.value + c4.value;
cout << "Dealer's hand: " ;
print_card(c3);
cout << " " ;
print_card(c4);
cout << ". " << "Dealer has: " << dealer_value << endl;
int hit_count = 3;
while (hit_count > 0 && total_value < 21)
{
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if (result1 == 0)
{
hit_count--;
CARD c5 = deck[next_index++];
total_value += c5.value;
cout << "New Card: " ;
print_card(c5);
cout << endl;
cout << "Player has: " << total_value << endl;
if (total_value > 21)
{
cout << "Player bust! You lose." << endl;
bank = bank - bet;
if (bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
}
break ;
}
else if (result1 == 1)
{
break ;
}
}
int hit_count1 = 3;
while (hit_count > 0 && dealer_value < 17)
{
hit_count1--;
CARD c6 = deck[next_index++];
dealer_value += c6.value;
cout << "New Card: " ;
print_card(c6);
cout << "Dealer has: " << dealer_value << endl;
}
if (dealer_value > 21)
{
cout << "Dealer busts. You win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
if (total_value <= 21 && dealer_value <= 21)
{
if (total_value == dealer_value)
{
cout << "Push." << endl;
cout << "Current bankroll: " << bank << endl;
}
else if (total_value > dealer_value)
{
cout << "You Win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
}
else if (dealer_value > total_value)
{
cout << "You lose!" << endl;
bank = bank - bet;
if (bank == 0)
{
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
else
{
cout << "Current bankroll: " << bank << endl;
}
}
}
}
Aug 5, 2012 at 4:08pm UTC
@154
while (bet <= 0);
Remove the ;
As for the hit/stand issue, instead of use the line else if (result1 == 1)
just change it to else
I believe that will fix your problem.
Aug 5, 2012 at 4:24pm UTC
Thanks for the quick response. The last thing that I need is to get this loop line 125 to work to repeat the game and I should be good. Here is what I have that isn't working.
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 306 307 308 309 310 311 312 313 314
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
typedef enum
{
VALUE,
JACK,
QUEEN,
KING,
ACE
} FACE;
typedef struct
{
int value;
FACE f;
} CARD;
void print_card(CARD c)
{
if (c.f == ACE)
cout << "ACE (11)" ;
else if (c.f == KING)
cout << "KING (10)" ;
else if (c.f == QUEEN)
cout << "QUEEN (10)" ;
else if (c.f == JACK)
cout << "JACK (10)" ;
else if (c.f == VALUE)
{
if (c.value == 10)
cout << "TEN (10)" ;
else if (c.value == 9)
cout << "NINE (9)" ;
else if (c.value == 8)
cout << "EIGHT (8)" ;
else if (c.value == 7)
cout << "SEVEN (7)" ;
else if (c.value == 6)
cout << "SIX (6)" ;
else if (c.value == 5)
cout << "FIVE (5)" ;
else if (c.value == 4)
cout << "FOUR (4)" ;
else if (c.value == 3)
cout << "THREE (3)" ;
else if (c.value == 2)
cout << "TWO (2)" ;
else
cout << "Value not recognized" ;
}
else
cout << "Face not recognized" ;
}
void init_deck(CARD deck[])
{
for (int index = 0; index < 4; index++)
{
int seg = index * 13;
deck[seg + 0].f = VALUE;
deck[seg + 0].value = 2;
deck[seg + 1].f = VALUE;
deck[seg + 1].value = 3;
deck[seg + 2].f = VALUE;
deck[seg + 2].value = 4;
deck[seg + 3].f = VALUE;
deck[seg + 3].value = 5;
deck[seg + 4].f = VALUE;
deck[seg + 4].value = 6;
deck[seg + 5].f = VALUE;
deck[seg + 5].value = 7;
deck[seg + 6].f = VALUE;
deck[seg + 6].value = 8;
deck[seg + 7].f = VALUE;
deck[seg + 7].value = 9;
deck[seg + 8].f = VALUE;
deck[seg + 8].value = 10;
deck[seg + 9].f = JACK;
deck[seg + 9].value = 10;
deck[seg + 10].f = QUEEN;
deck[seg + 10].value = 10;
deck[seg + 11].f = KING;
deck[seg + 11].value = 10;
deck[seg + 12].f = ACE;
deck[seg + 12].value = 11;
}
}
void print_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
print_card(deck[x]);
cout << endl;
}
}
void shuffle_deck(CARD deck[])
{
for (int x = 0; x < 52; x++)
{
int new_index = rand() % 52;
CARD temp = deck[x];
deck[x] = deck[new_index];
deck[new_index] = temp;
}
}
void repeat(int &result2)
{
static const char yes[] = "yes" ;
char word2[20];
cout << "Would you like to play again? [yes] [no]" << endl;
cin >> word2;
result2 = strcmp (word2, yes);
if (result2 == 1)
{
cout << "Goodbye!" << endl;
}
}
int main()
{
CARD deck[52];
//print_deck(deck);
int bank;
unsigned int seed;
int bet;
char word1[20];
char word2[20];
static const char hit[] = "hit" ;
static const char stand[] = "stand" ;
int result1;
int result2;
cout << "Enter the initial bankroll" << endl;
cin >> bank;
cout << "Enter seed" << endl;
cin >> seed;
cout << "Seeding random number generator..." << endl;
srand(seed);
cout << "== Blackjack v1.0 ==" << endl;
cout << "Initializing deck..." << endl;
while (result2 == 0)
{
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
while (bet <= 0)
{
cout << "Bet must be greater than zero and be at most " << bank << endl;
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
}
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];
CARD c3 = deck[next_index++];
CARD c4 = deck[next_index++];
CARD c5 = deck[next_index++];
int total_value = c1.value + c2.value;
cout << "Current Hand: " ;
print_card(c1);
cout << " " ;
print_card(c2);
cout << ". " << "Player has: " << total_value << endl;
int dealer_value = c3.value + c4.value;
cout << "Dealer's hand: " ;
print_card(c3);
cout << " " ;
print_card(c4);
cout << ". " << "Dealer has: " << dealer_value << endl;
int hit_count = 3;
while (hit_count > 0 && total_value < 21)
{
cout << "Player's turn:" << endl;
cout << "Options: [hit] [stand]" << endl;
cin >> word1;
result1 = strcmp (word1, hit);
if (result1 == 0)
{
hit_count--;
CARD c5 = deck[next_index++];
total_value += c5.value;
cout << "New Card: " ;
print_card(c5);
cout << endl;
cout << "Player has: " << total_value << endl;
if (total_value > 21)
{
cout << "Player bust! You lose." << endl;
bank = bank - bet;
if (bank == 0)
{
cout << "Current bankroll: " << bank << endl;
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
}
}
else
{
break ;
}
}
if (total_value > 21)
{
}
else
{
cout << "Dealer's turn" << endl;
if (dealer_value > 17)
{
cout << "Dealer stands" << endl;
}
else
{
int hit_count1 = 3;
while (hit_count > 0 && dealer_value < 17)
{
hit_count1--;
CARD c5 = deck[next_index++];
dealer_value += c5.value;
cout << "New Card: " ;
print_card(c5);
cout << "Dealer has: " << dealer_value << endl;
}
if (dealer_value > 21)
{
cout << "Dealer busts. You win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
repeat(result2);
}
}
}
if (total_value <= 21 && dealer_value <= 21)
{
if (total_value == dealer_value)
{
cout << "Push." << endl;
cout << "Current bankroll: " << bank << endl;
repeat(result2);
}
else if (total_value > dealer_value)
{
cout << "You Win!" << endl;
bank = bank + bet;
cout << "Current bankroll: " << bank << endl;
repeat(result2);
}
else if (dealer_value > total_value)
{
cout << "You lose!" << endl;
bank = bank - bet;
if (bank == 0)
{
cout << "Current bankroll: " << bank << endl;
cout << "You are broke, get out of the casino!" << endl;
cout << "Goodbye!" << endl;
}
else
{
cout << "Current bankroll: " << bank << endl;
repeat(result2);
}
}
}
}
}
Last edited on Aug 5, 2012 at 5:55pm UTC
Topic archived. No new replies allowed.