Blackjack game help URGENT help please

i believe i have a functioning shuffle code for my deck only problem is i cannot get my dealt cards to display. here is my code:

#include <iostream>
#include <ctime>

using namespace std;

#define MENU 1

int bank;
int bet;

typedef struct
{
int number;
int suitvalue;
int value;
char face;
char suit;
}
s_cards;
s_cards card_set[53];

int current_card = 0;

void shuffle(void);

int main_menu(void);
void game_setup_m(void);
int exit(void);
void rules(void);
void play(void);
void deal_m(void);
void bet_m(void);
void decision_m(void);
void decision_hit(void);
void decision_stick(void);
void outcome_dealer(void);
void outcome_player(void);
void outcome_tie(void);
s_cards player_card [11];
s_cards dealer_card [11];


int main ()
{
int loop=0;
int bet;
int mode = MENU;
int cards[53];
int i;
int suitvalue = 1;
int value = 0;
time_t t;
time(&t);
srand(t);
int cardnum = 1;
int player_card [11];
int dealer_card [11];



for(suitvalue=1;suitvalue<5;suitvalue++)
{
value=1;

for(i=1; i<14; i++,value++)
{
card_set[i].number = i;
card_set[i].suitvalue = suitvalue;

if(value <10)
{
card_set[i].value = value;
}
else
{
card_set[i].value = 10;
}
}
}

while (loop!=1)
{
mode=main_menu();

switch(mode) // main menu selection switch
{
case 1: game_setup_m();
break;

case 2: rules();
break;

case 3: loop=exit();
if(loop==0)
mode=main_menu();
break;
}
}

}

int main_menu(void) // mode for the main menu
{
int exit1 = 0;
int choice;
do
{
system("CLS"); // clears the screen
cout << "\n \3 \4 BLACKJACK \5 \6 \n"; //main menu layout
cout << " _________________ \n\n";
cout << " [1] - Play\n";
cout << " [2] - Rules\n";
cout << " [3] - Quit\n\n Option: "<<endl;
cin >> choice;
cin.clear();
cin.ignore(INT_MAX,'\n');

if( (choice >0) && (choice<4)) // if choice is 1,2 or 3 go to selection switch
{
exit1=1;
}
else // if not then stay in the loop
{
exit1=0;
}
}
while(0 == exit1);
return choice;
}

void game_setup_m(void) // initialises the game
{

shuffle(); //shuffles deck
bank = 1000; //bank default
current_card = 0; //reset current card
play();
}

void play(void)
{
system ("CLS");
cout << "\n \3 \4 BLACKJACK \5 \6 \n _________________ \n\nYou Have a Balance of ";
cout << bank << endl;
cout << "\nHow much would you like to bet?\n\nBet : ";
cin >> bet;
cout << endl;
bet_m();
}

void bet_m(void)
{

if (bank < 1)
{
cout << "No money left, better luck next time";
system("PAUSE");
main_menu();
}

if (bet>bank)
{
play();
}
else
{
deal_m();
}
}

void deal_m(void)
{
cout << "\ntest\n";
system("PAUSE");
player_card [0] = card_set [current_card];
current_card++;
player_card [1] = card_set [current_card];
current_card++;

//etc..

cout << "Your Cards = " ;
switch(player_card[0].number)
{
case 1:
cout << "A";
break;
case 11:
cout << "J";
break;
case 12:
cout << "Q";
break;
case 13:
cout << "K";
break;
default:
cout << player_card [0].number;
}

switch(player_card[0].suit)
{
case 1:
cout << "\6";
break;
case 2:
cout << "\5";
break;
case 3:
cout << "\4";
break;
case 4:
cout << "\3";
break;
default:
break;
}

//players shown with total
//one of dealers shown
//hit or stick
decision_m();
}

void decision_m (void)
{
char decision;
cout << "\n\nWould you like to [H]it or [S]tick?\n\n";
cin >> decision;

switch (decision)
{
case 'h':
case 'H':
decision_hit();
case 's':
case 'S':
decision_stick();
//default:
// cout << "\nInvalid Choice\n";
// decision_m();
}
}
void decision_hit(void)
{
cout << "hit";
system ("PAUSE");
//deal new card
//new total
// if new total > 21 outcome (dealer)
//decision_m();
}

void decision_stick(void)
{
cout << "stick";
system ("PAUSE");
//if dealer < 17 deal new card to him
// show totals
//if dealer > player, dealer wins : goto dealer wins
//else player > dealer player wins : goto player wins
//else player = dealer tie: goto tie
}

void outcome_dealer(void)
{
int input;
(bank = bank - bet);
cout << "Dealer Wins";
if (bank == 0)
{
cout << "\nYou are out of Money/nGame Over\nWould you like to play again\n[y] [n]?";
cin >> input;
switch (input)
{
case 'y':
case 'Y':
game_setup_m();
case 'n':
case 'N':
exit();
//default:
// if any other key is selected (not n or y) please select again
}

}
else if (bank != 0)
{
cout << "\nDo you want to deal the next hand?";
cin >> input;
switch (input)
{
case 'y':
case 'Y':
play ();
case 'n':
case 'N':
main_menu();
//default:
// if any other key is selected (not n or y) please select again
}
}
}

void outcome_player(void)
{
int input;
//(bank = bank + bet)
cout << "Player Wins";
cout << "\nDo you want to deal the next hand?";
cin >> input;
switch (input)
{
case 'y':
case 'Y':
play ();
case 'n':
case 'N':
main_menu();
//default:
// if any other key is selected (not n or y) please select again
}
}

void outcome_tie(void)
{
int input;
cout << "Tie";
cout << "\nDo you want to deal the next hand?";
cin >> input;
switch (input)
{
case 'y':
case 'Y':
play ();
case 'n':
case 'N':
main_menu();
//default:
// if any other key is selected (not n or y) please select again
}


}

void rules(void)
{
system ("CLS");
cout << "\n \3 \4 BLACKJACK \5 \6 \n _________________ \n\ndisplay rules here\n\n";
cout<<"Press Enter to Return to Main Menu\n"<<endl;
while(cin.get() != '\n');
}

int exit(void)
{
char quit;
int exit2=0;
system ("CLS");
cout << "Are You Sure?\n\n[Y]es [N]o\n\n Option: "<<endl;
cin >> quit;
cin.clear();
cin.ignore(INT_MAX,'\n');
if (quit == 'y' || quit == 'Y')
{
exit2=1;
}
else
{
exit2=0;
//main_menu();
}

return exit2;
}


void shuffle(void)
{
int ii = 0;
int card_dealt=0;
int card_ok= 0;
s_cards swap_card;

srand(time(NULL));

for(ii=0; ii<52; ii++)
{
do
{
card_dealt = rand() % 52 + 0;
}
while(card_dealt == ii); //52)

//dealt_stack[card_dealt]=1;
//deal_stack[ii]=card_dealt;
swap_card = card_set[ii];
card_set[ii] = card_set[card_dealt];
card_set[card_dealt] = swap_card;
}
}


where am i going wrong, also is it possible to display the suits and face cards in a 1 off statement, as opposed to for every card dealt individually
Last edited on
You had an issue with not setting the card_set correctly and I changed the deal to work, I think.
I couldn't get the suits to work, so I just used H (heart), S (spade), D (diamond), C (clover).
I see that you are still working on this, so I hope this helps you get further.
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396

#include <iostream>
#include <ctime>

using namespace std;

#define MENU 1

int bank;
int bet;

typedef struct
{
   int number;
   int suitvalue;
   int value;
   char face;
   char suit;
} 
s_cards;
s_cards card_set[53];

int current_card = 0;

void shuffle(void);

int main_menu(void);
void game_setup_m(void);
int exit(void);
void rules(void);
void play(void);
void deal_m(void);
void bet_m(void);
void decision_m(void);
void decision_hit(void);
void decision_stick(void);
void outcome_dealer(void);
void outcome_player(void);
void outcome_tie(void);
s_cards player_card [11];
s_cards dealer_card [11];


int main ()
{
   int loop=0;
   int bet;
   int mode = MENU;
   int cards[53];
   int i;
   int suitvalue = 1;
   int value = 0;
   time_t t;
   time(&t);
   srand(t);
   int cardnum = 1;
   int player_card [11];
   int dealer_card [11];
   


   for(suitvalue=1;suitvalue<5;suitvalue++)
   {
      value=1;
   
      for(i=0; i<13; i++,value++)
      {
         card_set[(suitvalue-1)*13+i].number = i+1;
         card_set[(suitvalue-1)*13+i].suitvalue = suitvalue;
      
         if(value <10)
         {
            card_set[(suitvalue-1)*13+i].value = value;
         }	
         else
         { 
            card_set[(suitvalue-1)*13+i].value = 10;	
         } 
      }
   }
   
   while (loop!=1)
   {
      mode=main_menu();
   
      switch(mode) // main menu selection switch
      {
         case 1: game_setup_m();
            break;
   
         case 2: rules();
            break;
   
         case 3:	 
            loop=exit();
            if(loop==0)
              mode=main_menu();
            break;
      }
   }	
   
}	
   
int main_menu(void) // mode for the main menu
{
   int exit1 = 0;
   int choice;	
   do
   {
      system("CLS"); // clears the screen
      cout << "\n \3 \4 BLACKJACK \5 \6 \n"; //main menu layout
      cout << " _________________ \n\n";
      cout << " [1] - Play\n";
      cout << " [2] - Rules\n";
      cout << " [3] - Quit\n\n Option: "<<endl;
      cin >> choice;
      cin.clear(); 
      cin.ignore(INT_MAX,'\n');
   
      if( (choice >0) && (choice<4))	// if choice is 1,2 or 3 go to selection switch
      {
         exit1=1;
      }
      else	 // if not then stay in the loop
      {
         exit1=0;
      }
   }
   while(0 == exit1);
   return choice;
}

void game_setup_m(void)	// initialises the game
{

   shuffle();	 //shuffles deck
   bank = 1000;	 //bank default
   current_card = 0;	//reset current card
   play();
}

void play(void)	
{
   system ("CLS");
   cout << "\n \3 \4 BLACKJACK \5 \6 \n _________________ \n\nYou Have a Balance of ";
   cout << bank << endl;
   cout << "\nHow much would you like to bet?\n\nBet : ";
   cin >> bet;
   cout << endl;
   bet_m();
}

void bet_m(void)
{	
   if (bank < 1)
   {
      cout << "No money left, better luck next time";
      system("PAUSE");
      main_menu();
   }

   if (bet>bank)
   {
      play();
   }
   else
   {
      deal_m();
   }
}

void deal_m(void)
{
   cout << "\ntest\n";
   system("PAUSE");
   player_card [0] = card_set [current_card];
   current_card++;
   player_card [1] = card_set [current_card];
   current_card++;
      
   //etc..
   cout << "Your Cards = " ;
   for ( int i(0); i < 2; i++) {
      switch(player_card[i].number)
      {
         case 1:
         cout << "A-";
         break;
         case 11:
         cout << "J-";
         break;
         case 12:
         cout << "Q-";
         break;
         case 13:
         cout << "K-";
         break;
         default:
           cout << static_cast<int>(player_card [i].number) << "-";
      }

      switch(player_card[i].suitvalue)
      {
         case 1:
         cout << "H ";
         break;
         case 2:
         cout << "S ";
         break;
         case 3:
         cout << "C ";
         break;
         case 4:
         cout << "D ";
         break;
         default:
         break;
      }
   }
   //players shown with total
   //one of dealers shown
   //hit or stick
   decision_m();
}

void decision_m (void)
{
   char decision;
   cout << "\n\nWould you like to [H]it or [S]tick?\n\n";
   cin >> decision;

   switch (decision)
   {
      case 'h':
      case 'H':
         decision_hit();
      case 's':
      case 'S':
         decision_stick();
      //default:
      //	 cout << "\nInvalid Choice\n";
      //	 decision_m();
   }
}
void decision_hit(void)
{
   cout << "hit";
   system ("PAUSE");
   //deal new card
   //new total
   // if new total > 21 outcome (dealer)
   //decision_m();
}

void decision_stick(void)
{
   cout << "stick";
   system ("PAUSE");
   //if dealer < 17 deal new card to him 
   // show totals
   //if dealer > player, dealer wins : goto dealer wins
   //else player > dealer player wins : goto player wins
   //else player = dealer tie: goto tie
}

void outcome_dealer(void)
{
   int input;
   (bank = bank - bet);
   cout << "Dealer Wins";
   if	(bank == 0) 
   {
      cout << "\nYou are out of Money/nGame Over\nWould you like to play again\n[y] [n]?";
      cin >> input;
      switch (input)
      {
         case 'y':
         case 'Y':
            game_setup_m();
         case 'n':
         case 'N':
            exit();
         //default:
         //	 if any other key is selected (not n or y) please select again
      }
   }
   else if	(bank != 0)
   {
      cout << "\nDo you want to deal the next hand?";
      cin >> input;
      switch (input)
      {
         case 'y':
         case 'Y':
            play ();
         case 'n':
         case 'N':
            main_menu();
         //default:
         //	 if any other key is selected (not n or y) please select again
      }
   }
}

void outcome_player(void)
{
   int input;
   //(bank = bank + bet)
   cout << "Player Wins";
   cout << "\nDo you want to deal the next hand?";
   cin >> input;
   switch (input)
   {
      case 'y':
      case 'Y':
         play ();
      case 'n':
      case 'N':
         main_menu();
      //default:
      //	 if any other key is selected (not n or y) please select again
   }
}

void outcome_tie(void)
{
   int input;
   cout << "Tie";
   cout << "\nDo you want to deal the next hand?";
   cin >> input;
   switch (input)
   {
      case 'y':
      case 'Y':
         play ();
      case 'n':
      case 'N':
         main_menu();
      //default:
      //	 if any other key is selected (not n or y) please select again
   }
}

void rules(void)
{
   system ("CLS");
   cout << "\n \3 \4 BLACKJACK \5 \6 \n _________________ \n\ndisplay rules here\n\n";
   cout<<"Press Enter to Return to Main Menu\n"<<endl;
   while(cin.get() != '\n');
}

int exit(void)
{
   char quit;	
   int exit2=0;	
   system ("CLS");
   cout << "Are You Sure?\n\n[Y]es	[N]o\n\n Option: "<<endl;
   cin >> quit;
   cin.clear(); 
   cin.ignore(INT_MAX,'\n'); 
   if (quit == 'y' || quit == 'Y')
   {
      exit2=1;	
   }
   else
   {
      exit2=0;
      //main_menu();
   }
   return exit2; 
}

void shuffle(void)
{
   int ii = 0;
   int card_dealt=0;
   int card_ok= 0;
   s_cards swap_card;

   srand(time(NULL));

   for(ii=0; ii<52; ii++)
   { 
      do
      {
         card_dealt = rand() % 52 + 0;
      }
      while(card_dealt == ii);	 //52)
   
      //dealt_stack[card_dealt]=1;
      //deal_stack[ii]=card_dealt;
      swap_card = card_set[ii];
      card_set[ii] = card_set[card_dealt];
      card_set[card_dealt] = swap_card;
   }
}
Last edited on
Topic archived. No new replies allowed.