Cards Dealt in Wrong Order
Aug 5, 2012 at 9:20pm UTC
Everything is done one this except that when a player or dealer chooses to hit the cards are dealt in the wrong order for example they are dealt a 5 then a 2 instead of a 2 then a 5. It seems like they are just backwards or something. I have tried changing line 189 from
CARD c5 = deck[next_index++];
to
CARD c5 = deck[next_index--];
but that didn't work. And I changed line 222 from
CARD c5 = deck[next_index--];
to
CARD c5 = deck[next_index++];
but that didn't work either. Any help would be appreciated. Here is my entire code.
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 315 316 317 318 319 320 321 322 323 324
#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;
do
{
init_deck(deck);
cout << "Shuffling deck..." << endl;
shuffle_deck(deck);
cout << "Current bankroll: " << bank << endl;
cout << "Enter bet:" << endl;
cin >> bet;
while (bet <= 0 || bet >= bank + 1)
{
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;
cout << "Player's turn:" << endl;
int hit_count = 3;
while (hit_count > 0 && total_value < 21)
{
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;
}
else
{
cout << "Current bankroll: " << bank << endl;
repeat(result2);
}
}
}
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_count1 > 0 && dealer_value < 17)
{
hit_count1--;
CARD c5 = deck[next_index--];
dealer_value += c5.value;
cout << "New card: " ;
print_card(c5);
cout << endl;
cout << "Dealer has: " << dealer_value << endl;
if (dealer_value >= 17)
{
cout << "Dealer stands." << 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);
}
}
}
}while (result2 == 0);
}
Aug 5, 2012 at 11:19pm UTC
Did you ever solve this issue?
Topic archived. No new replies allowed.