blackjack program.

[edit] I was pulling out a random card from an already randomized deck. Like playing blackjack and pulling cards from the middle. Sorry guys!
Can this thread remain open? I will need more help and would love the input of the people on this site.



Hey I've only just started the basics of this program and I'm wondering if anyone can shed some light on an issue I'm having.

My instructor uses hypergrade so my program has to match his results to the T.

Problem:
My cards dont match his for user and dealer.

Example:
Using rand() with a seed of (1) his user cards are 4,10 and dealer 10,10.
Mine are 11,9 and 10, 10.

Heres the 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
#include <iostream>
#include <cstdlib>
#include <string>
#include <cstring>
#include <iostream>
#include <cctype>
#include <fstream>
#include <locale>



using namespace std;

typedef enum
{
    VALUE,
    JACK,
    QUEEN,
    KING,
    ACE
} FACE;

typedef struct
{
    int value;
    bool inplay;
    FACE f;
} CARD;


float cash;

CARD userhand[2];
CARD dealerhand[2];




void dealerdeck(CARD deck[])
{
int card2=0;

for(int o=0; o<=1; o++)
{
do
{
card2 = rand() % 52;
cout << " dealers cards are: " << deck[card2].value<< endl;
}while(deck[card2].inplay = false );
deck[card2].inplay = false;
dealerhand[o].value = deck[card2].value;


}

}



void userdeck(CARD deck[])
{
int card1=0;

for(int x=0; x<=1; x++)
{
do
{
card1 = rand() % 52;
cout << "players cards are: " << deck[card1].value<< endl;
}while(deck[card1].inplay = false );
deck[card1].inplay = false;
userhand[x].value=deck[card1].value;

}

}



void seeder ()
{
    int seed;
   cout << "Seed the random number generator" << endl;
   cin >> seed;
    srand(seed);
}

void bankroll()
{
   cout << "Enter your bankroll"  << endl;
   cin >> cash;


}

bool checkbankroll()
{
    if (cash <= 0)
    {
        cout << "Out of money" << endl;
        return true;
    }
    return false;
}

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; // newindex = remainder of random divided by 52
        CARD temp = deck[x]; // temp is a card in deck[x]
        deck[x] = deck[new_index]; // card in deck[x] = random number newindex
        deck[new_index] = temp; // random card in deck is now temp
    }






}

void setupdeck()
{

    seeder();
    CARD deck[52];
    init_deck(deck);
    shuffle_deck(deck);
    print_deck(deck);
    userdeck(deck);
    dealerdeck(deck);

}







int main()
{

  setupdeck();
  //bankroll();



}




Last edited on
In a card game, it's typical that you deal off the top of the deck not the middle of the deck, or the bottom of the deck. We normally call that cheating.

I haven't found the problem, but I suspect it is due to your dealer deck and user deck functions. It looks like your pulling cards from 26-29. I figure if I tell you that, you might can fix it quicker than I could.

26 ACE (11)
27 NINE (9)
28 JACK (10)
29 KING (10)




Seed the random number generator
1
1 FOUR (4)
2 TEN (10)
3 JACK (10)
4 JACK (10)
5 SIX (6)
6 NINE (9)
7 ACE (11)
8 SIX (6)
9 TWO (2)
10 SIX (6)
11 EIGHT (8)
12 TWO (2)
13 QUEEN (10)
14 SEVEN (7)
15 ACE (11)
16 KING (10)
17 KING (10)
18 JACK (10)
19 THREE (3)
20 FOUR (4)
21 TEN (10)
22 SEVEN (7)
23 FIVE (5)
24 QUEEN (10)
25 FIVE (5)
26 ACE (11)
27 NINE (9)
28 JACK (10)
29 KING (10)
30 FIVE (5)
31 TWO (2)
32 EIGHT (8)
33 THREE (3)
34 NINE (9)
35 FOUR (4)
36 THREE (3)
37 TEN (10)
38 THREE (3)
39 QUEEN (10)
40 FIVE (5)
41 ACE (11)
42 SEVEN (7)
43 SIX (6)
44 TEN (10)
45 EIGHT (8)
46 FOUR (4)
47 KING (10)
48 QUEEN (10)
49 SEVEN (7)
50 NINE (9)
51 TWO (2)
52 EIGHT (8)
players cards are: 11
players cards are: 9
dealers cards are: 10
dealers cards are: 10
Last edited on
I also see in userdeck, if you comment out
// card1 = rand() % 52;

The first card shows up correct.
Topic archived. No new replies allowed.