Trying to make a BlackJack game

Hello! I am trying to make a BlackJack game. When I type "Stand" it calculates the dealer hand, but when I type "Hit"and then "Stand" it shows that the dealer hand is 0.

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
  #include <iostream>
#include <cstdlib>
#include <ctime>
#include <string>

using namespace std;

int main()
{
    srand(time(0));

    int player[10];
    int dealer[10];
    int PlayerHand = 0, DealerHand = 0, z = 3;
    string Hit = "Hit", hit = "hit", Stand = "Stand", stand = "stand";
    string userValue;

    for(int s = 0; s <= 1; s++)
    {
        player[s] = rand() % 11;
        if(player[s] == 0 || player[s] == 1)
        {
            player[s] = player[s] + 2;
        }
        cout << s + 1 << ": card is " << player[s] << endl;
        PlayerHand = PlayerHand + player[s];
        if(PlayerHand > 21 && player[s] == 11)
        {
            PlayerHand = 12;
        }
    }


    cout << "Your total score is: " << PlayerHand << endl << "Hit or Stand? \n";

    cin >> userValue;

    for(int m = 2; m < z; m++)
    {
        if(userValue == Hit)
        {
            for(int s = 2; s < z; s++)
            {
                player[s] = rand() % 11;
                if(player[s] == 0 || player[s] == 1)
                    {
                        player[s] = player[s] + rand() + 2;
                    }
                cout << s + 1 << ": card is " << player[s] << endl;
                PlayerHand = PlayerHand + player[s];
                if(PlayerHand > 21)
                {
                    if(player[s] == 11)
                    {
                        player[s] = 1;
                        cout << "Your total score is: " << PlayerHand << endl << "Hit or Stand? \n";
                        cin >> userValue;
                        if(userValue == Hit)
                        {
                            z++;
                        }
                        if(userValue == Stand)
                        {
                            z = m;
                        }
                    }
                    else
                    {
                        cout << "You are bust! \n";
                        return 0;
                    }
                }

                cout << "Your total score is: " << PlayerHand << endl << "Hit or Stand? \n";
                cin >> userValue;
                if(userValue == Hit)
                {
                    z++;
                }
                else if(userValue == Stand)
                {
                    z = m;
                }
            }
        }
        else if(userValue == Stand)
        {
            cout << " \n" << " \n" << "Dealer's Hand: " << endl;

            for(int k = 0; k <= 3; k++)
            {
                dealer[k] = rand() % 11;
                if(dealer[k] == 0 || dealer[k] == 1)
                {
                    dealer[k] = dealer[k] + 2;
                }
                cout << k + 1 << ": card is " << dealer[k] << endl;
                DealerHand = DealerHand + dealer[k];
                if(DealerHand > 21 && dealer[k] == 11)
                {
                    DealerHand = 12;
                }
                if(DealerHand >= 17)
                {
                    k = 3;
                }
                if(DealerHand > 21)
                {
                    cout << "Dealer's hand total is: " << DealerHand << endl;
                    cout << "Player wins the hand!" << endl;
                    return 0;
                }

            }
        }

        cout << "Dealer's hand total is: " << DealerHand << endl;

        if(DealerHand > PlayerHand)
        {
            cout << "Dealer wins the hand!" << endl;
        }
        else
        {
            cout << "Player wins the hand!" << endl;
        }
    }
}
I fixed it by myself.
The way you are generating the cards makes no sense.
1
2
3
        player[s] = rand() % 11;
        if(player[s] == 0 || player[s] == 1)
            player[s] = player[s] + 2;

You change a 0 to a 2 and a 1 to a 3. Why? And it will never be 11, which you seem to think is possible. And remember that in blackjack, tens, Jacks, Queens and Kings are all worth 10. So getting a 10 is 4 times more likely than any other value. Also, in a normal deck of cards, once you've dealt out a 7, for example, then there are only 3 left so it should be less likely to get another 7 after that.
closed account (E0p9LyTq)
Generating and displaying a deck of cards is not that hard, with some help from C++11.

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
#include <array>    // for std::array<>
#include <iostream>
#include <numeric>  // for std::iota

int main()
{
   // manufacture a deck of cards:
   using card = unsigned short;

   std::array<card, 52> deck { };

   std::iota(deck.begin(), deck.end(), 0);

   /* shuffling routine(s) go here... */

   // display each card in the shuffled deck:
   auto rank = [ ](card c)
   {
      return "AKQJT98765432"[c % 13];
   };
   auto suit = [ ](card c)
   {
      return "SHDC"[c / 13];
   };

   for (card c : deck)
   {
      std::cout << rank(c) << suit(c) << ' ';

      if (12 == (c % 13))
      {
         std::cout << '\n';
      }
   }
   std::cout << '\n';
}

AS KS QS JS TS 9S 8S 7S 6S 5S 4S 3S 2S
AH KH QH JH TH 9H 8H 7H 6H 5H 4H 3H 2H
AD KD QD JD TD 9D 8D 7D 6D 5D 4D 3D 2D
AC KC QC JC TC 9C 8C 7C 6C 5C 4C 3C 2C

Shuffle the deck of cards with std::shuffle and a random engine from <random> and you have the basics you need for playing a card game.
Last edited on
Topic archived. No new replies allowed.