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.
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
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.
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)
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.
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... ?!)