4
\$\begingroup\$

This is a very stripped down version of A BlackJack KOTH contest...

This is the goal:

Upon the execution: There are two inputs, "H" to hit (have the code deal a card), "S" to stand (Stop getting cards, and let dealer try to win).

Coding Rules:

  1. There is no betting or splitting of hands, for simplicity of the challenge.
  2. Suits are HDCS; standing for Hearts, Diamonds, Clubs and Spades respectively.
  3. Trumps must be formatted as A23456789TJQK, where T is the 10; therefore TH is the 10 of Hearts, etc.
  4. A is worth 1 or 11. Face cards are worth 10 points. A perfect game is 21 points.
  5. If you go over 21 points, the output along with the cards must show "BUST".
  6. If the dealer goes over 21 points and you're standing, output with cards must show "WIN"
  7. If the dealer beats you even though you're standing, output with cards must show "LOSE"
  8. If the dealer matches your score on standing, output with cards must show "PUSH"
  9. Dealer must stand on 17 through to 21.
  10. If you Stand, it calculates your score and displays it (see below), and then goes into dealer play mode. If the dealer stands or busts, it calculates its score and displays it.
  11. Standard 52 cards in a deck, no duplicates can be generated.
  12. Output must resemble the following:

    H
    Your hand: 3H
    H
    Your hand: 3H 8S
    H
    Your hand: 3H 8S 9D
    S
    Standing... Your hand: 3H 8S 9D = 20 points
    My hand: 2C 5H 8H 9S = 24 points
    WIN
    
  13. Shortest code wins

\$\endgroup\$
  • \$\begingroup\$ Well, I was going to do this in PowerShell but I'm getting hung up on the damn aces. Handling one is probably simple enough, but more than that seems tricky (and not really golf-friendly). \$\endgroup\$ – Iszi Dec 12 '13 at 2:04
  • \$\begingroup\$ The game play exemplified in your output is not really accurate for Blackjack. I don't mind that it requires an initial request to deal the hand, but the first deal should be two cards. Also, should the dealer still take their turn (normally doesn't) if the player hits 21 or Blackjack (A and T/J/K/Q on initial dealing)? Any special announcement required for 21 or Blackjack? \$\endgroup\$ – Iszi Dec 12 '13 at 2:08
  • \$\begingroup\$ @Iszi I know, but like I said, it's very simplified.. I'm not asking for the Bellagio, I'm just asking for a simple game - nothing more ;) \$\endgroup\$ – WallyWest Dec 12 '13 at 23:49
0
\$\begingroup\$

Golf-Basic 84, 852 characters (ALL rules observed)

:"YOUR HAND:"_Str1:-1_X:0_O:0_A:0_P:2_H:3_2l`Ii`I@I=2o`H#o`Sl`H:Str1+" "_Str1:round(rand*13,0)_R@R=0:Str1+"A"_Str1@R=1:Str1+"2"_Str1@R=2:Str1+"3"_Str1@R=3:Str1+"4"_Str1@R4:Str1+"5"_Str1@R=5:Str1+"6"_Str1@R=6:Str1+"7"_Str1@R=7:Str1+"8"_Str1@R=8:Str1+"9"_Str1@R=9:Str1+"10"_Str1@R=10:Str1+"J"_Str1@R=11:Str1+"Q"_Str1@R=12:Str1+"K"_Str1@R=1:A+10_A@R<9:P+R+1_P#P+10_P:round(rand*4,0)_R@R=0:Str1+"H"_Str1@R=1:Str1+"D"_Str1@R=2:Str1+"C"_Str1@R=3:Str1+"S"_Str1d`Str1@P>20:o`So`Il`St`STANDING... "+Str1+", POINTS:"d`P@P=21 or P+A=21:"WIN"_Str2@P>21:"BUST"_Str2:round(rand*8,0)_R:"MY HAND: "_Str3l`X:X+1_X:round(rand*8,0)@R=0:Str3+"9H"_Str3@R=1:Str3+"9D"_Str3@R=2:Str3+"9C"_Str3@R=3:Str3+"9S"_Str3@R4:Str3+"JH"_Str3@R=5:Str3+"QD"_Str3@R=6:Str3+"KC"_Str3@R=7:Str3+"KS"_Str3@R<4:O+9_O#:O+10_O@Xo`E#o`Xl'Et`Str3+"= POINTS:"d`O@P=O:"PUSH"_Str2@P<O:"LOSE"_Str2d`Str2

Coded all by myself!

Explanation

  • Initialize variables; put 0 in P and "YOUR HAND:" in Str1
  • Take input, either H or S (no quotes needed)
  • Based on input, goto Lbl H or Lbl S
  • If input is H, generate a random card, add a space, a number/letter, and a suit to Str1
  • Based on the card generated, add the value of the card to P and additionally add 1 to A (ace count)
  • Get more input
  • If input is S, calculate the points and the dealer's points
  • Put WIN or BUST in Str2 depending on the points generated
  • Display the dealer's hand
  • Calculate your points and the dealer's points, conditionally put LOSE or PUSH in Str2
  • Display WIN, PUSH, LOSE, or BUST

Sample output

?H
YOUR HAND: 9C
?H
YOUR HAND: 9C 7S
?H
YOUR HAND: 9C 7S 4S
?S
STANDING... YOUR HAND: 3H 8S 9D, POINTS:
                                      20
MY HAND: 9H KS = POINTS:
                                      19
WIN
\$\endgroup\$
  • \$\begingroup\$ When did you create that language? - I should really ask "what is a language?" on Code Golf Meta \$\endgroup\$ – Johannes Kuhn Dec 11 '13 at 20:31
  • \$\begingroup\$ @JohannesKuhn A couple weeks ago \$\endgroup\$ – Timtech Dec 11 '13 at 21:36
  • \$\begingroup\$ Tim, great work, though I reckon you could whittle it down more with shorter variable names...? :) \$\endgroup\$ – WallyWest Dec 11 '13 at 21:52
  • \$\begingroup\$ @Eliseod'Annunzio Strings must have names of Str1, Str2, Str3, etc. \$\endgroup\$ – Timtech Dec 11 '13 at 22:10
  • 1
    \$\begingroup\$ @Eliseod'Annunzio Documentation + compiler - timtechsoftware.com/?page_id=1261 \$\endgroup\$ – Timtech Dec 13 '13 at 0:26
0
\$\begingroup\$

Javascript 838

The very first version, not optimized ! All rules are not fully observed, :-o

!function(){var f=require("readline").createInterface(process.stdin,process.stdout),g=[],h=function(){for(var b;!b||-1<g.indexOf(b);)b="A23456789TJQK".charAt(13*Math.random()|0)+"HDCS".charAt(4*Math.random()|0);g.push(b);return b},k=function(b){var a=0,d=0;for(z=b.length;z--;){var c=b[z].charAt(0);/[TJQK]/.test(c)?a+=10:/[2-9]/.test(c)?a+=+c:"A"==c&&(d++,a++)}for(;d--;)21>=a+10&&(a+=10);return a};(function(){var b=[],a=[],d=0,c=0;f.prompt();f.on("line",function(e){e=e.charAt(0);"H"==e&&(b.push(h()),
c=k(b),console.log("Your hand: "+b+" = "+c+" points"),21<c&&(console.log("LOSE"),process.exit(0)));if("S"==e){for(;17>(d=k(a));)a.push(h());console.log("My hand: "+a+" = "+d+" points");21<d||d<c?console.log("WIN"):d>c?console.log("LOSE"):console.log("DRAW");process.exit(0)}f.prompt()}).on("close",function(){process.exit(0)})})()}();

Execute it with nodeJS

\$\endgroup\$
  • \$\begingroup\$ "All rules are not fully observed" Lol, I did it right ;) \$\endgroup\$ – Timtech Dec 11 '13 at 16:46
  • \$\begingroup\$ Lol, just missed rules 5 and 8. My code not display BUST or PUSH but the game is fully playable. \$\endgroup\$ – guy777 Dec 11 '13 at 16:54

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.