Blackjack Game help

I need a lot of help on this one. This is my last assignment and I am having some trouble with it. I need to write a blackjack game that has this

•All cards are dealt face up.
•You do not need to support splitting or double downs.
•You do not need to pay extra if the player hits 21 on the first two cards.
•You need to verify the user has enough in their bankroll to place a bet. Constantly ask the user for a valid bet.
•If the player's bankroll hits zero, print a message and exit.
•A deck must be represented as an array of cards. (See next.)
•A card must be represented as a structure where at least one member is an enumerated data type.
•Before each round the cards must be shuffled.
•Aces will be counted as only 11.
•At the end of each round ask the user if they want to play again.
•Your function to initialize the deck must order the cards from low to high, the two card through Ace in sets of four.

I am having trouble with how to make the user input the random seed and how to output just 2 random cards and how to add them together. Any help would be appreciated. Here is what I have so far.

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
#include <iostream>
#include <cstdlib>
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()
{
    srand();
    CARD deck[52];
    init_deck(deck);
    shuffle_deck(deck);
    print_deck(deck);
    int bank;
    int seed;
    int bet;

    cout << "Enter the initial bankroll" << endl;
    cin >> bank;
    cout << "Enter seed" << endl;
    cin >> seed;
    cout << "Seeding random number generator..." << endl;
    cout << "== Blackjack v1.0 ==" << endl;
    cout << "Initializing deck..." << endl;
    init_deck(deck);
    cout << "Shuffling deck..." << endl;
    shuffle_deck(deck);
    cout << "Enter bet:" << endl;
    cin >> bet;
    cout << "Current hand: " << srand(seed) % 3 << ".";
    cout << "Player has: " << endl;
}
I am having trouble with how to make the user input the random seed

You don't need to ask the user for a seed. You just need to call srand() with some random value. See the following reference:
http://www.cplusplus.com/reference/clibrary/cstdlib/srand/

how to output just 2 random cards

You need a function to remove a card from the deck. Call that function once for each card the player or dealer draws.

how to add them together
1
2
3
4
5
6
7
8
CARD card1;
CARD card2;
int score = 0;
card1 = DrawOneCard();
print_card (card1);
card2 = DrawOneCard();
print_card (card2);
score = card1.value + card2.value;

Obviously, either the player or the dealer can might draw more than 2 cards, so you might want to implement an array or a loop.
Hi BroncoAG ,

Just some points to make your code better.

In the function init_deck, have another nested for loop that goes from 0 to 12, use that index variable in these lines

1
2
deck[seg + 0].f = VALUE;
        deck[seg + 0].value = 2;


That way you can have 2 lines of code in the for loop not 26.

In your CARD struct, have another variable that stores the "TEN" , "NINE" strings etc. Then in the print_card function you can just have 3 cout statements (rather than 33) that print the face, value and string variable.

You can use enum like this :

1
2
3
4
enum CardNum{
TWO=2, THREE, FOUR //etc
//THREE = 3, FOUR = 4 etc
}


If you find yourself writing the same code over and over, then you probably need a loop or a function (or both)

Hope this helps
Thanks for the replies. Most of the code was started by the teacher and so I am just suppose to finish the int main() part of it. On the function to remove the card from the deck I am a little confused on how to do that. I have tried a couple different ways but I am getting the error no match for operator (line 155) . Here is what I have now.


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
#include <iostream>
#include <cstdlib>
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];
    init_deck(deck);
    shuffle_deck(deck);
    print_deck(deck);
    int bank;
    unsigned int seed;
    int bet;

    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++];

    int total_value = c1.value + c2. value;

    cout << "Current Hand: " << c1 << c2 << ".";
    cout << "Player has: " << total_value << endl;
}



Just to give you an idea of the program when it is done here is what the output should look like minus the input that is what the computer will input once I submit it.

INPUT
Bankroll: 999
Seed: 66
Bet: 300
Hit
Stand
No


Enter the initial bankroll
Enter seed
Seeding random number generator...
== Blackjack v1.0 ==
Initializing deck...
Shuffling deck...
Current bankroll: 999
Enter bet:
Current hand: Six (6) Jack (10). Player has: 16
Dealer's hand: Five (5) Eight (8). Dealer has: 13
Player's turn:
Options: [hit] [stand]
New card: Four (4)
Player has: 20
Options: [hit] [stand]
Dealer's turn:
New card: Four (4)
Dealer has: 17
Dealer stands.
You win!
Current bankroll: 1299
Would you like to play again? [yes] [no]
Goodbye!
int total_value = c1.value + c2. value;

You have an extra space.

Have made any of the changes I suggested?

Edit:

Also, it is usual practice to declare your functions before main, and put the definition of them after main
Last edited on
Thank you for your help. I fixed the space but I still get the error "no match for operator<< in std::operator" at line 155 which is line 7 of this code.

1
2
3
4
5
6
7
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];

int total_value = c1.value + c2.value;

cout << "Current Hand: " << c1 << c2 << ".";


TheIdeasMan - I didn't make the changes you suggested because those parts of the code were written by the teacher and they already work so I don't want to try and change it and screw something up.
Last edited on
I didn't make the changes you suggested because those parts of the code were written by the teacher and they already work so I don't want to try and change it and screw something up.


Really? I couldn't imagine any half decent teacher giving students such repetitive code. The init_deck & print_card functions I mean

c1 & c2 are object of type CARD which is a struct, so to cout the CARD object makes no sense, - you need to refer to the variable inside the CARD struct. Either c1.value or c1.f
Thanks, everything was written by the teacher except for the main function. I changed the code

1
2
3
4
5
6
7
8
int next_index = 0;
CARD c1 = deck[next_index++];
CARD c2 = deck[next_index++];

int total_value = c1.value + c2.value;

cout << "Current Hand: " << c1.f << c2.f << ". ";
cout << "Player has: " << total_value << endl;


It compiles nows but it says that the Current hand is 00 and Player has: 14. It should say the Current Hand: Five (5) Jack (10). Player has: 15 or whatever cards it chooses.
The reason it is printing 00 is because you are printing the face value of each card which most of the time is going to be 0.

Change your code so it is something like this (You already have a nice function to print out your cards for you).

1
2
3
4
 
cout << "Current Hand: " << endl;
print_card(c1);
print_card(c2);


The function already prints the results to the screen so that should handle it all correctly.

(It is also odd that the srand is set by the player as it will always give the same cards if the number entered is the same, but if that's intended then ok).
Last edited on
Topic archived. No new replies allowed.