8
\$\begingroup\$

The Challenge

I was looking into small code examples for a specific language as a small tutorial and came accross a simplified Blackjack that should be implemented. In this Challenge you will have to implement a full Blackjack game, with 5 Decks.

The Game

You are playing against a Dealer and he has 5 Decks of Poker playing cards.

A pokerdeck contains the cards "2♠","3♠",...,"K♠","A♠","2♥",...,"A♥",...,"A♣", which may be represented by their values for 2-10, Jack-King is all 10, Ace is 11.

The gamecycle begins with the Dealear giving a card to you, and one to him self. Your goal now is to reach 21, by adding the values of the cards, that the dealer gives you.

After recieving a card, you have two options:

  1. Stay: You decide, to not be dealt additional cards, therefore determining your score for the round: the sum of cards. For example: you have a 2, a 10, and a 8, and you decide to stay, then your score for the round is 20.

  2. Hit: You decide to be dealt an additional card. This can lead to three cases:

    • The total sum of your cards is under 21: Nothing really happens. Here you could again decide wether to stay or to hit again.
    • The total sum of your cards is exactly 21: This is the best score you can get, and makes you automatically stay. If you get 21, with 2 cards (i.e.: 10 + Ace, this is called a "Blackjack") you automatically win. Else there is still possibilities for a draw.
    • The Total sum of your cards exceeds 21: You busted. this is automaticaly a loose.

This is done, until you decide to stay/bust/get blackjack. Then its the Dealers turn, if you havent busted. The dealer does exactly the same. He stays/hits normally, until his score is above 17 (This is his strategy for winning). If the Dealer busts, meaning his score is above 21, you automatically win, if you havent busted yourself.

After this both you and the Dealer should have a score (i.e.: Player 20, Dealer 18). In this case the person with the higher score wins. If you draw, you draw.

The Strategy

The best strategy for the game looks like this:

enter image description here

Our case is slightly easier, as you cant Double and Split.

Additional notes

You may decide how to handle user input (there should obviously be some, due to the way this game is reactive), and how to do output. The carddrawing should be random, and semi-nonreproducible (C example would be: srand(time(0))).

Rules

Standard Golf rules. Shortest byte amount wins. As this is random, there arent really any good testcases, but it should look something like this:

~$ ./blackjack
You got [10]
Dealer got [7]
Suggested: Hit
[s]tay
[h]it
h
You got [3]
Suggested: Hit
[s]tay
[h]it
h
You got [7]
Suggested: Stay
[s]tay
[h]it
s
Dealer got [9]
Dealer got [10]
Dealer busted. You won.
Another round? [y/n]: y
Next round:
You got[10]
...

Unexpected input may result in undefined behaviour.

Extra

Try and include Split/DoubleDown in both userinput and Suggested moves :) Good Luck.

Notes

If my explanation of the game was satisfying for you needs here is the Wikipedia page, to the game.

\$\endgroup\$
  • \$\begingroup\$ Welcome to the PPCG, Jacobus! This is a really nice first question. However, I am not sure if your question is different enough from this question. The input formats are very similar, and answers from this can be translated here with ease. Aside from that, you should clarify what the ending conditions are, i.e. be more firm: Games are played until the user decides to exit or 104 cards remain, for example. Our sandbox is a great way to get feedback :) \$\endgroup\$ – Kade Nov 11 '16 at 12:36
  • \$\begingroup\$ @Shebang thanks. Fixed it. Also very true. the basic idea is similar :/ \$\endgroup\$ – PlatinTato Nov 11 '16 at 12:40
  • \$\begingroup\$ Suggestion if the other one is too similar: Calculate basic strategy: for each possible situation (your score vs dealer's one visible card) output the move (hit or stay) that will maximize your expected value. Or have the situation (ex. 19, 10) as input and output a truthy value for hit or stay. \$\endgroup\$ – JollyJoker Nov 11 '16 at 13:11
  • \$\begingroup\$ @JollyJoker nice Idea, however i feel like that is close to this as well. If i were to ask for what you suggested, should i rather write a whole new challenge, or rewrite this? \$\endgroup\$ – PlatinTato Nov 11 '16 at 13:18
  • \$\begingroup\$ @JacobusConradi The constraint of having exactly one valid output for each input would make the entries very different. Modding this question should work; just remove Extras and Ending conditions and add an explanation about the "best move". Maybe calculate the full output for these simplified rules and show a chart like this \$\endgroup\$ – JollyJoker Nov 11 '16 at 14:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.