r/dailyprogrammer • u/Coder_d00d 1 3 • May 05 '14
[5/5/2014] #161 [Easy] Blackjack!
Description:
So went to a Casino recently. I noticed at the Blackjack tables the house tends to use several decks and not 1. My mind began to wonder about how likely natural blackjacks (getting an ace and a card worth 10 points on the deal) can occur.
So for this monday challenge lets look into this. We need to be able to shuffle deck of playing cards. (52 cards) and be able to deal out virtual 2 card hands and see if it totals 21 or not.
- Develop a way to shuffle 1 to 10 decks of 52 playing cards.
- Using this shuffle deck(s) deal out hands of 2s
- count how many hands you deal out and how many total 21 and output the percentage.
Input:
n: being 1 to 10 which represents how many deck of playing cards to shuffle together.
Output:
After x hands there was y blackjacks at z%.
Example Output:
After 26 hands there was 2 blackjacks at %7.
Optional Output:
Show the hands of 2 cards. So the card must have suit and the card.
- D for diamonds, C for clubs, H for hearts, S for spades or use unicode characters.
- Card from Ace, 2, 3, 4, 5, 6, 8, 9, 10, J for jack, Q for Queen, K for king
Make Challenge Easier:
Just shuffle 1 deck of 52 cards and output how many natural 21s (blackjack) hands if any you get when dealing 2 card hands.
Make Challenge Harder:
When people hit in blackjack it can effect the game. If your 2 card hand is 11 or less always get a hit on it. See if this improves or decays your rate of blackjacks with cards being used for hits.
Card Values:
Face value should match up. 2 for 2, 3 for 3, etc. Jacks, Queens and Kings are 10. Aces are 11 unless you get 2 Aces then 1 will have to count as 1.
Source:
Wikipedia article on blackjack/21 [Link to article on wikipedia] (http://en.wikipedia.org/wiki/Blackjack)
6
u/KillerCodeMonky May 05 '14 edited May 05 '14
EDIT: Redid solution to better encapsulate cards.
PowerShell:
Execution:
Example Output:
1
u/Coder_d00d 1 3 May 05 '14
C
I wanted to practice structs/link lists so I more or less create a monster link list of card structs and then I randomly build a list of cards randomly from the in order list of cards.
My output for 3 decks:
4
u/gimpycpu May 06 '14
First ruby post
1
u/that_how_it_be May 05 '14
PHP goodness:
3
u/qwesx May 05 '14
Racket:
Execution:
Result:
3
u/skeeto -9 8 May 05 '14
Emacs Lisp. Each card is represented by an integer 0-51. The suit is that number integer-divided by 13 and the value is the number modulo 13.
Example output:
3
u/hokiecs16 May 05 '14
Ruby First post on here, so I decided to go the easier route.
Also, in case anybody wants it very short...
3
u/bretticus_rex May 05 '14 edited May 05 '14
Python 2.7
I am a beginner and this is my first submission. All critique is welcome.
Example output:
1
u/BluntnHonest May 05 '14 edited May 05 '14
First post here. Criticisms welcome and wanted!
Java (Only deals two cards. Does not account for low values or double Aces. Stops dealing 80% into the deck):
Input:
Output:
2
u/pbeard_t 0 1 May 05 '14 edited May 05 '14
C. Perhaps my lazyest post yet, written during classes.
Edit: Output for one and ten decs.
2
u/jbjose May 05 '14 edited May 05 '14
First time posting my work (have kept it to myself previously :)
Python 3.4
Input and output:
Code:
2
u/RabbiDan May 06 '14
Python 2.7, my second Python script ever! Feedback is welcome.
2
u/ignurant May 06 '14
Hello, I've been studying generic programming things for many years, but haven't really done a lot of actual "writing code." Recently I saw Notch using Google Dart for something on twitch.tv, and wanted to check out the language, since it was very easy for me to understand, having knowledge of oop, c# and some .js.
So, as my first post here, I present to you, my solution that runs in the Dart VM. It's an awful lot bigger than anyone else's solutions. Some of it is because a chunk was taken up exploring Dart's html selectors, which are kind of cool. I also tried describing various elements as classes for re-usability.
My code is too long to paste here apparently, so here's a gist link.
https://gist.github.com/anonymous/baa1925a4b1da8b4bc6e
2
u/woppr May 06 '14 edited May 06 '14
Hi! First time posting, and pretty new to programming (studying). Critique/comments are very welcome.
My C# Solution:
Input:
Output:
2
u/tenerhades May 07 '14
play_blackjack 1000 decks
2
u/mongreldog May 10 '14 edited May 10 '14
F#:
Execution:
Last portion of results:
1
u/dadosky2010 May 05 '14 edited May 05 '14
Python 3
EDIT: Fixed a few bugs, thanks /u/iobender!
1
u/kooschlig May 05 '14
Javascript: Decomment blackjackgame.displayHands(); to show each played hand
1
u/eltonhnjr May 05 '14
My CoffeeScript solution. You can play it at CoffeeScript.com > Try CoffeeScript
1
u/TheSuperWig May 05 '14 edited May 06 '14
C++ too lazy for a fancy output:
1
u/Lurker378 May 05 '14 edited May 05 '14
Rust 0.11-#pre. A little verbose since I used enums for the suites/cards and wanted variable hand sizes/pretty output.
1
u/ooesili May 05 '14
Easy Haskell solution:
1
u/Godspiral 3 3 May 05 '14 edited May 05 '14
in J,
shuffleddeck =: 52&( ([: ?~ [*]) { ([: i.[) #~ ])
Acecardvals =: 5 > ]
Tencardvals =: 36 < ]
isBJ =: +./@:Tencardvals *. +./@:Acecardvals
(+/ % #) _2 isBJ\ shuffleddeck 10
0.0692308
(+/ % #) _2 isBJ\ shuffleddeck 4
0.0480769
(+/ % #) _2 isBJ\ shuffleddeck 6
0.0897436
for a subset of hands as opposed to full deck (requested spec):
26 spec 3
After 26 hands there was 3 blackjacks at %11.5385
40 spec 10
After 40 hands there was 0 blackjacks at %0
40 spec 10
After 40 hands there was 3 blackjacks at %7.5
1
u/[deleted] May 05 '14
My attempt in C++. Ace is assumed be always 11 for now
1
u/glaslong May 05 '14 edited May 06 '14
C#
I extended it a bit to allow for hitting until you bust, one or more aces (high and low), multiple players and multiple rounds. I'm going to use that as an excuse for why it ended to being too long for reddit :p
As always, comments and criticism are appreciated.
https://gist.github.com/minsley/ffb1316f0db7ab3019d4
1
u/Shizka May 05 '14 edited Aug 29 '25
melodic innate capable growth steer dependent unwritten market salt tender
This post was mass deleted and anonymized with Redact
1
u/ukju_ May 05 '14 edited May 05 '14
1
u/dohaqatar7 1 1 May 05 '14
java,
1
u/badgers_uk May 05 '14
Python 3. (comparatively) nice looking version:
Ugly short version:
1
u/Fruglemonkey 1 0 May 06 '14 edited May 07 '14
Decided to write my own shuffle code, instead of using random.shuffle. Written in python! Also my first submission here.
EDIT: Fixed the logic (It was faulty), added unicode characters to display the suit and value of each card.
Sample output:
🂣 🃕 🃓 11 🃗 🃅 12 🂤 🂮 14 🃎 🂾 20 🂵 🃇 12 🂮 🂭 20 🂷 🃖 13 🃞 🃃 13 🃞 🃛 20 🃝 🂾 20 🃓 🃋 13 🃂 🂽 12 🃊 🃖 16 🂭 🂸 18 🃑 🃊 21 🂻 🂪 20 🂴 🃒 🂦 12 🃍 🂫 20 🃆 🂱 17 🂳 🂪 13 🃙 🂵 14 🂤 🂭 14 🃄 🂶 🃉 19 🃓 🃃 🃊 16 🃅 🃔 🃍 19 🃕 🃁 16
After 234990 hands there were 11180 blackjacks at 4.8%
1
u/systwin May 06 '14
First submission, in Python 2.7 to ease myself back into programming. I wanted to be a cool kid and make it super compact, but then I would have needed to scrap the letters. I'm banking on the later challenges needing the face cards to be unique, which is why I included them. :)
1
u/spfy May 06 '14
I did a C solution, too. I looked up Fisher-Yates shuffle to get the deck shuffled; it works really well!
Here's a sample of what my output looks like (snipped out a lot of it):
1
u/doubleagent03 May 06 '14 edited May 06 '14
Quick Clojure solution: https://www.refheap.com/85176
Sample output:
1
u/ethnicallyambiguous May 07 '14
I need to clean up my variable names a little. That being said, results:
Python 3.4 code (comments welcome)
1
u/Hero_764 May 07 '14
Solution in C! :D
}
1
u/Godde May 07 '14
Sloppy ruby hacked together at 4am. It's shit, but I wanted to post something :)
1
u/XenophonOfAthens 2 1 May 07 '14
Python 2.7, fairly straightforward. Uses Fischer-Yates shuffle:
1
u/mtbottens May 07 '14 edited May 07 '14
Ruby Blackjack Class
Basic usage like this:
Advanced usage (hits on 11) like this
And to figure which method works better I wrote the following code
Very ugly, but it works :)
1
u/cooper6581 May 08 '14
Erlang. Warning: Super lazy code ahead (especially the shuffle)
1
u/VerifiedMyEmail May 08 '14
python
1
u/mentalorigami May 08 '14
python 2.7 with multiple card draws. The ace rule threw me for a minute, I did not realize that was a Black Jack rule, but it does make sense now.
1
u/mordo May 08 '14
Java Solution with hardcoded inputs. Any feed back would be very welcome
(/s
)
output looks like: BlackJack: ☘11, ♠1 = 21
1
u/LostPrometheus May 08 '14 edited May 08 '14
Hi Reddit. I haven't been coding in awhile and I need to brush up on my skills so I thought a place like this would be helpful. I appreciate any and all feedback, particularly tips on how to write clearer simpler code. For example, I struggled to develop a simple regex to find blackjack hands using my largely unnecessary data structures and ended up with overly verbose code. Sorry about formatting mistakes - first time poster and there's no preview button but I'll figure it out in time.
Java:
}
1
u/[deleted] May 10 '14
I'm kinda late to the party but I wanted to post mine, i would like to get help with my "switch case" solution. Is there any possible way to handle that with enums or something more fancy?
Java solution:
1
u/oreo_fanboy May 11 '14
R: Simulates 10,000 hands to see the difference between your odds on 1 deck vs multiple decks. Spoiler alert: there is a difference but it is very small.
1
u/RednBIack May 11 '14 edited May 11 '14
In Racket. I have been going over SICP, but not getting enough practice. I think I am going to make a card-counting program using this now :)
Execution
Output
1
u/[deleted] May 12 '14
Java with an Object Oriented approach. CardPile object
import java.util.ArrayList; import java.util.Random;
public class CardPile {
}
import java.util.Scanner;
public class Main{
}
1
u/internet_badass_here May 13 '14
In J:
Example output:
1
u/[deleted] May 16 '14
My first submission, Python 2.7
1
u/[deleted] May 18 '14
Python. Feedback please; I am a noob ready to be upbraided for using global vars
1
u/kevn57 May 27 '14
[5/5/2014] #161 [Easy] Blackjack!
I changed the program, add a for loop and printed out only the totals at the end. From the multiple runs I ran, I found that the % of blackjacks either went down or stayed the same using upwards of 100 decks.
1
u/awkwardferny Jun 02 '14 edited Jun 02 '14
Python with commented steps:
1
u/the_dinks 0 1 Jul 21 '14
Terrible Python 2.7
Run data_miner(n) and see what happens.
Output:
1
u/isSoCool May 05 '14 edited May 05 '14
C#
Program will loop the game 100 times, for statistical purposes.
class Deck { public int index; public Dictionary<int, int> DeckOfCards = new Dictionary<int, int>(); public Deck(int TotalDecks) { for (int i = 0; i < TotalDecks; i++) // 1-10 decks. for (int j = 0; j < 4; j++) //Diamonds,Clubs,Hearts,Spades CreateCards(); } private void CreateCards() { for (int i = 2; i < 11; i++) //2 to 10 { index++; DeckOfCards.Add(index, i); } DeckOfCards.Add(index += 1, 11); //Ace DeckOfCards.Add(index += 1, 12); //Jack DeckOfCards.Add(index += 1, 13); //Queen DeckOfCards.Add(index += 1, 14); //King } }
class Program { //Declare variables public static bool ThreeCards; public static double AvgWinRate; public static int InitialAmountOfCardsInDecks; public static int TotalDecks; public static int TotalTopScore;
}
EDIT: Not sure how to handle '11'(Ace), when should/could it be a '1'? I'm not good with blackjack :'(
EDIT #2: Conclusion. If my math is correct, always ask for a third card if you have 11 or less points. Also, The more decks involved the higher the chance to win.
1
u/[deleted] May 05 '14 edited May 05 '14
blackjack.py (simple problem version)
example output
0
u/[deleted] May 06 '14
[deleted] — view deleted comment