Challenge: Functioning Blackjack Game

4

3

This is kept fairly original:

Write a program that creates a standard blackjack game:

  • input "h" means hit or draw another card
  • input "s" means stand, or take what you have already

    Rules:

  • cards values are as follows:

  • 2-9 = face value of card

  • 10, J, Q, K = 10

  • aces = 11 unless player busts, then it's only a 1

  • if 2 or more aces are drawn the first is 11, and each one after is a 1

  • the bank needs 18-21, and has to draw another card until they are in this range

  • this game runs forever

    Procedure:

    • The player gets 2 cards at the beginning, shown by "Player:" in the console (Example: Player gets 2 and 5 at the beginning. Console output: "Player: 2, 5").
    • The cards can be shown in the console by their values (For example: 2, 10, 1 | No need for: 2, K, A), but you can also use the card-names instead. Your choice
    • The input by the player is "h" for "hit" and "s" for "stand". Also allowed is 1 and 0
    • The bank gets one card at the beginning, shown by "Bank:"
    • At the end you get the output "Win", "Lose", "Draw", or "Blackjack", depending on the game

This game is a challenge.

DudeWhoWantsToLearn

Posted 2019-12-16T10:41:32.630

Reputation: 163

Question was closed 2019-12-16T17:13:26.473

7>

  • Do we show the bank's other cards and, if so, when? 2. Do we need to pick from some number of shuffled decks in a uniformly random manner, and if so do we need to implement any logic around when these cards get shuffled, like casinos do or may we shuffle every hand? 3. Some people might prefer accepting other input (e.g. 1 for hit 0 for stand) as it'll cut down on boiler-plate code, I'd suggest allowing for this lax input (but it's up to you, of course). [Note: do update the post with any clarifications regarding these queries rather than just answering here in comments :)]
  • < – Jonathan Allan – 2019-12-16T10:52:59.083

    ...in fact also, 2.a. May our program exit after one hand? – Jonathan Allan – 2019-12-16T10:54:21.470

    8

    Pretty complex challenge, it could benefit of some time in Sandbox for Proposed Challenges.

    – manatwork – 2019-12-16T10:55:04.187

    5

    I also recommend the sandbox. To get you started there, you need to explain what constitutes Win/Lose/Draw/Blackjack. and what exactly happens after the user answers.

    – Adám – 2019-12-16T10:56:41.667

    9Oh, and don't forget to state what hitting and standing means. Challenges should be self-contained, and not rely external resources. – Adám – 2019-12-16T10:58:01.290

    1

    I think this should be tagged as interactive.

    – Arnauld – 2019-12-16T11:13:00.957

    Answers

    3

    Python 3, 574 \$\cdots\$ 338 337 bytes

    from random import*
    e,L,B,P=exit,"Lose","Bank:","Player:"
    d=[*range(2,12)]*4+[10]*12
    c=d.pop
    def h(l,m):
     l+=[c()]
     if sum(l)>21and 11in l:l[l.index(11)]=1
     print(m,*l);return sum(l)
    shuffle(d)
    b=[]
    t=h(b,B)
    p=[c()]
    u=h(p,P)
    u>20<e("Blackjack")
    while'h'==input():u=h(p,P)
    u>21<e(L)
    while t<18:t=h(b,B)
    22>t>u<e(L)
    u==t<e("Draw")
    e("Win")
    

    Try it online!

    Saved 34 bytes thanks to @ReinstateMonica!!!

    Noodle9

    Posted 2019-12-16T10:41:32.630

    Reputation: 2 776

    list(...) can be [*...] – Reinstate Monica – 2019-12-17T13:39:48.520

    And correct me if I'm wrong, but since you don't ever change the list except its order, can't you take out *4 and change [10]*12 to [10]*3? – Reinstate Monica – 2019-12-17T13:44:55.703

    1@ReinstateMonica Thanks, that's a good one! :-) If we take out the *4 etc then we wouldn't be playing with a full deck! In fact only one suit. So, for instance, two aces could never be in play. – Noodle9 – 2019-12-17T13:55:29.377

    Ah right, right; my head was thinking with choice rather than shuffle. – Reinstate Monica – 2019-12-17T14:02:20.433