I need help with my Blackjack game program C++

Hi. I'm trying to make a variation of a blackjack game for my C++ class but I'm having trouble with it.
One problem I'm having is that when I have each player (Let's say 3 players are playing) take a bet, it turns out that they are taking bets out of the same bank.
For example...

Let's say each player starts with 500 dollars. Each player takes 50 bets.
One player hits until they bust. The player busts, the dealer wins, and now the player has 400 dollars.

I'm trying to figure out how to make separate banks for my players but I haven't been able to do so.
I'm also trying to figure out how to restart the game if players don't decide to cashout at the end of the game.

I also never played Blackjack before so I'm not a 100% sure on how the game plays in case you see something wrong with how I see the gameplay to go in my code.

This is my code as of right 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
158
159
160
161
#include <iostream>
#include <cstdlib>

using namespace std;

int main() {
      int Numberofplayers = 0;
      int o;
      int k;
      int totalpoints = 0;
    
      srand(time(NULL));

      cout << "How many players do you have? (1-4 players)" << endl;
      cin >> Numberofplayers;
  
      while(Numberofplayers > 4 || Numberofplayers <= 0) {
      cout << "Enter a valid number of players" << endl;
      cin >> Numberofplayers;
      }

      float Money[Numberofplayers];
      for(int i = 0; i < Numberofplayers; i++) {
            Money[i] = 0;
      }

      float Sum = 0;
      for(o= 0; o < Numberofplayers; o++) {
            while(Money[o] <= 0) {
                  cout << "Player " << o + 1 << ", how much money would you like to start with?" << endl;
                  cin >> Money[o];
                  cout << "You have " << Money[o] << " dollars." << endl;
            }
            Sum += Money[o];
      }

      int Playerscore = 0;
      int Dealerscore = 0;
      int Player_Play_Again = 0;
      int Dealer_Play_Again = 0;
      int Bet = 0;
      int Cashout = 0;

      while(Sum != 0 && Cashout == 0) {
            Sum = 0;
            for(o = 0; o < Numberofplayers; o++) { 
                  Player_Play_Again = 1;
                  Dealer_Play_Again = 1;
                  Playerscore = 0;
                  Dealerscore = 0;

                  for(int j = 0; j < Numberofplayers; j++) {
                        if Money[j] != 0) {
                              cout << "Player " << j + 1 << ", how much money would you like to bet?" << endl;
                              cin >> Bet;
                              Money[j] -= Bet;
                        }
                  }

                  While(Player_Play_Again == 1) {
                        k = rand() % 11 + 1;
                        Playerscore += k;
                        totalpoints += k;

                        if(Playerscore <= 21 && Player_Play_Again == 1) {
                              cout << "Player " << o + 1 << ", you got " << k << Do you want to hit? (0 for stay, 1 for hit)" << endl;
                              cin >> Player_Play_Again;
                              while(Player_Play_Again > 1 || Player_Play_Again < 0) {
                                    cout << "Enter a valid option!" << endl;
                                    cin >> Player_Play_Again;
                              }
                  }

                        else {
                              cout << "Player " << o + 1 << ", you got " << k << endl;
                              cout << "Player " << o + 1 << "has << totalpoints << "points." << endl;
                              cout << "Player " << o + 1 << "busts! The dealer wins!" << endl;
                              Money[o] -= Bet;
                              cout << "The player has " << Money[o] << dollars remaining." << endl;
                              break;
                        }

                        if(Player_Play_Again == 0)
                              cout << "Your current score is " << Playerscore "." << endl;
                  }
      
                  while(Dealer_Play_Again == 1 && Player_Play_Again == 0) {
                        k = rand() % 11 + 1;
                        Dealerscore += k;
                        totalpoints += k;

                        if(Dealerscore <= 21) {
                              cout << "Dealer gets " << k << "." << endl;
                              if(Dealerscore > Playerscore) {
                              cout << "Dealer wins!" << endl;
                              cout << "Player " << o + 1 << " has " << Money[o] << " dollars remaining." << endl;
                              break;
                        }

                        cout << "Does the dealer hit? (0 for stay, 1 for hit)" << endl;
                        cin >> Dealer_Play_Again;

                        while(Dealer_Play_Again > 1 || Dealer_Play_Again < 0) {
                              cout << "Enter a valid option." << endl;
                              cin >> Dealer_Play_Again;
                        }
                  }

                  else {
                        cout << "Dealer gets " << k << "." << endl;
                        cout << "The dealer busts! You win!" << endl;
                        cout << "The dealer has " << totalpoints << " points!" << endl;
                        Money[o] -= Bet;
                        cout << "Player " << o + 1 << " has " << Money[o] << " dollars remaining." << endl;
                        break;
                  }
            }

            if(Dealer_Play_Again == 0) { 
                  cout << "The dealer has " << Dealerscore << " points." << endl;
            }

            if(Player_Play_Again == 0 && Dealer_Play_Again == 0) {
                  if(Dealerscore > Playerscore) {
                        cout << "Dealer wins!" << endl;
                        Money[o] -= Bet;
                        cout << "The player has " << Money[o] << " dollars remaining." << endl;
                  }
                  else if(Playerscore > Dealerscore) { 
                        cout << "You win!" << endl;
                        Money[o] += Bet;
                        cout << "Player " << o + 1 << " has " << Money[o] << " dollars remaining." << endl;
                  }
                  else {
                        cout << "It's a tie!" << endl;
                        cout << "Player " << o + 1 << " has " << Money[o] << " dollars remaining." << endl;
                  }
            }

            Sum += Money[o];

            if(Sum == 0) {
                  cout << "Game Over!" << endl;
                  break;     
            }

            cout << "Would all players like to cash out? (0 for no, 1 for yes)" << endl;
            cin >> Cashout;
            while(Cashout < 0 || Cashout > 1) {
                  cout << "Would all players like to cash out? (0 for no, 1 for yes)" << endl;
                  cin >> Cashout;
            }

            if(Cashout == 1) {
                  cout << "Game Over!" << endl;
                  break;
            }
      }
}
return 0;
} 


Last edited on
going forward use the <> (in the edit post panel) around large code blocks please. Welcome to the forum!

the dealer maybe should just be a player, and to identify him, make him player[0] is recommended. Its a little special, but you can add a tiny bit of special logic rather than track extra variables. You can make a constant value dealer = 0 and use [dealer] to make this very readable.

try very, very hard to avoid using o and l and stuff like that as loop variables and array indices etc. finding that array[o] that is typed array[0] is really hard in some fonts.

I am still looking for your bug.


one possible issue is this:
float Money[Numberofplayers];
c++ standard does not allow variables here; array sizes MUST be known at compile time. you can set it to 5 (to hold dealer+4) and use as much of it as you need, for this program. Some compilers DO support this, so it may not be a real problem, only a compliance one to be aware of (if its supported, use it, as long as you know better, soon you will move from arrays to vectors anyway). you can initialize it to zero with = {0} and avoid that loop. eg float m[5] = {0};

there are a fair number of blackjack problems with your code.
- aces can be 1 or 11 points, player's choice (so if you bust on 11, you pick 1, mostly)
- the distribution of cards isn't equal. there are 10, jack, king, queen all at 10 value... its playable like you have it, but your players are going to be rich due to the extra luck of not getting 10s all the time.

- there are a bunch of things you can do over and above the core game... if you have 2 of the same card, you can split into 2 hands and play both, each costing 1 bet amount (so double bet cost) and each paying back to the player if they win... you can double (hit once and stop, no matter what you get, doubles your bet cost and winnings if any) and I think a couple more things of this nature (I don't play it seriously either, but Ive played a few simulators along the way).


you have multiple syntax errors. fix them. most involve missing quotation marks in your outputs. while is upcased in one spot
Last edited on
Hello!

<> in the edit post panel around large code blocks?
Have you learned about structs yet?
I think you will find it easier to keep everything related to a player in a struct.
As you deal with each player, you iterate through instances of the struct.
1
2
3
4
5
struct 
{   int score;
    int money;
    int bet;
} Player[4];


PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Ohhh okay.
Sorry I hadn't been on this website until today so I wasn't sure how to space my code yet

I actually have never heard of a struct until now.
I'm still a bit new to coding myself, this is my first term of coding actually.
Last edited on
After fixing all that, my game went like this. The important take-away is that player 2 didn't get a turn. It should probably increment to player 2 when player 1 loses. It does increment to player 2 for the next round but it took bets again before that so its really weird logic there. The flow should possibly be take bet only for active player, play active player, move to next player after winner is declared, … for all players. currently its something like take bets from all, play 1 player, take bets again, play next player... this is not so good.

debugging:
where you have this line, write all the player's bank values for now.
once debugged, write the actual player's name on the line:
"The player has -50dollars remaining."



the dealer should play itself, not have the players do it, in the long run. Just have it hit below 17 or something and stop above 17, for a really simple starting point (this would work better if you had the correct card distribution I mentioned, 17 may be too low given your approach, or it may be too high given the 11 forced value?)

How many players do you have? (1-4 players)
2
Player 1, how much money would you like to start with?
100
You have 100 dollars.
Player 2, how much money would you like to start with?
200
You have 200 dollars.
Player 1, how much money would you like to bet?
50
Player 2, how much money would you like to bet?
100
Player 1, you got 11Do you want to hit? (0 for stay, 1 for hit)
1
Player 1, you got 2Do you want to hit? (0 for stay, 1 for hit)
1
Player 1, you got 9
Player 1has22points.
Player 1busts! The dealer wins!
The player has -50dollars remaining.
Would all players like to cash out? (0 for no, 1 for yes)
Last edited on
I actually have never heard of a struct until now.

http://www.cplusplus.com/doc/tutorial/structures/

Line 61: You're not accurately representing the distribution of face cards in the deck.

Line 70: If the player busts, your're subtracting the bet again. You already subtracted it at line 56.

Line 113: If the dealer busts, you're subtracting the bet from the player's money. You should be adding it back.

Line 126: You're subtracting the bet a second time.





Last edited on
I really, really dislike the C-style struct usage (it works, its legal, no issues there). I have my reasons, but I prefer the c++ type style.

1
2
3
4
5
6
7
8
9
10
11
12
13
struct 
{   int score;
    int money;
    int bet;
} Player[4]; //Player is of type... what?   Its a hidden, un-named type!

struct playerinfo
{   int score;
    int money;
    int bet;
} 

playerinfo Player[4]; //Player has a named type now 

I'm sorry, I should've elaborated more on this when I said variation of blackjack
When I said a variation of blackjack, basically it's supposed to be played just a tiny bit differently from the actual blackjack. From what I can tell so far, we aren't assigning face cards as 10 points or aces as 1 or 11. Basically, each player is supposed to randomize a number (card value) from 1 to 11 until they stay or bust. And I think that's the only variation in our assignment for blackjack.

But thank you so far for your responses!

I do have a lot of problems with my code but the one I've trying to fix for 2 days now is my banks problem. I've tried different things like having int bank1, int bank2, int bank3, and int bank4 for the four players, but my TA's have all told me my code looks too big and stuff like that...and that I should use an array

But I'm a bit too stumped on how I'm supposed to implement a bank array to work with each player's bank besides them all taking money out of the same bank over and over. Because as of right now, I have an array for my bank (Money[o]) but it's just one bank.
Last edited on
it isn't. Money hold 4 values, one for each player. Money[o] is unique to player o at any given time. The problem is logic / bugs not the array. The array is fine. You have a fair number of things to fix listed above that are contributing to problems with your bank.

if you need to see how money should work it may be worth making a tiny program and playing with just that piece to see how it works before moving on. I don't know that you need this (you seem to grasp the array idea in the code, but then your words indicate not getting it... ?!)
Last edited on
Topic archived. No new replies allowed.