r/dailyprogrammer • u/Elite6809 1 1 • Jul 06 '14
[7/7/2014] Challenge #170 [Easy] Blackjack Checker
(Easy): Blackjack Checker
Blackjack is a very common card game, where the primary aim is to pick up cards until your hand has a higher value than everyone else but is less than or equal to 21. This challenge will look at the outcome of the game, rather than playing the game itself.
The value of a hand is determined by the cards in it.
- Numbered cards are worth their number - eg. a 6 of Hearts is worth 6.
- Face cards (JQK) are worth 10.
- Ace can be worth 1 or 11.
The person with the highest valued hand wins, with one exception - if a person has 5 cards in their hand and it has any value 21 or less, then they win automatically. This is called a 5 card trick.
If the value of your hand is worth over 21, you are 'bust', and automatically lose.
Your challenge is, given a set of players and their hands, print who wins (or if it is a tie game.)
Input Description
First you will be given a number, N. This is the number of players in the game.
Next, you will be given a further N lines of input. Each line contains the name of the player and the cards in their hand, like so:
Bill: Ace of Diamonds, Four of Hearts, Six of ClubsWould have a value of 21 (or 11 if you wanted, as the Ace could be 1 or 11.)
Output Description
Print the winning player. If two or more players won, print "Tie".
Example Inputs and Outputs
Example Input 1
3
Alice: Ace of Diamonds, Ten of Clubs
Bob: Three of Hearts, Six of Spades, Seven of Spades
Chris: Ten of Hearts, Three of Diamonds, Jack of ClubsExample Output 1
Alice has won!Example Input 2
4
Alice: Ace of Diamonds, Ten of Clubs
Bob: Three of Hearts, Six of Spades, Seven of Spades
Chris: Ten of Hearts, Three of Diamonds, Jack of Clubs
David: Two of Hearts, Three of Clubs, Three of Hearts, Five of Hearts, Six of HeartsExample Output 2
David has won with a 5-card trick!Notes
Here's a tip to simplify things. If your programming language supports it, create enumerations (enum) for card ranks and card suits, and create structures/classes (struct/class) for the cards themselves - see this example C# code.
For resources on using structs and enums if you haven't used them before (in C#): structs, enums.
You may want to re-use some code from your solution to this challenge where appropriate.
2
u/Elite6809 1 1 Jul 06 '14
My solution in C#: https://github.com/DropTableSpoon/Challenge170Easy
C# is definitely a lot more verbose than Ruby, haha. I suppose it does force you to write cleaner code.
7
u/CMahaff Jul 07 '14 edited Jul 09 '14
Haskell
Getting the input was ugly :) Special thanks to this stackoverflow for a nice solution to sorting tuples.
EDIT: Output for /u/chunes tests.
EDIT 2: Made some small changes.
7
u/chunes 1 2 Jul 07 '14 edited Jul 07 '14
Here are some gotcha inputs to make sure your program is rock-solid.
Someone wins by using lots of aces as a combination of 11s and 1s:
Expected output:
Alice wins!
Everyone busts:
Expected output:
Everyone busts! (I guess a tie works here too.)
Two people get a five-card trick:
Expected output:
Tie.
More than two people tie:
Expected output:
Tie.
3
u/carbonetc Jul 07 '14 edited Jul 07 '14
This is my first entry in this sub so I don't know if JavaScript is laughed at here, but it's what you're getting.
http://jsfiddle.net/pendensproditor/cLqt2/
Since I take it these are pure logic exercises, I skipped the speed-optimization or input-sanitizing or error-checking that I might clutter a real app with.
EDIT: Updated it to handle these test cases.
3
u/Driphter Jul 10 '14
Clojure! Still a noob. Any tips are appreciated. Idiomatic tips especially!
2
u/Godspiral 3 3 Jul 07 '14
in J,
22
20
17
22
22
cards =: ;: 'Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King'
2
u/mbcook Jul 07 '14
I read the title as "Blackjack Cipher" and thought this would be pretty cool. Transform letters to numbers and then do some form of computation to generate hands that 'spell' words based on what various players totals in various rounds were.
Nope.
But now my idea sounds cool to me, and I'm thinking I should try to make it anyway.
2
u/[deleted] Jul 08 '14 edited Jul 08 '14
Here's mine in Ruby! Just getting started coding, apologies for the mess :)
2
u/killedbythegrue Jul 08 '14 edited Jul 08 '14
Erlang,
Handles ties, and no winners. The scoring was easy handling the output was kind of a pain.
Edit: I was thinking to procedurally with this. I added a reduce operation to the scores to get the winners and the code is a lot better I think.
New Code:
Removed Code:
Unchanged:
Output:
2
u/DasEwigeLicht Jul 08 '14
Trying out some Python. Critique is welcome.
2
u/Coplate Jul 09 '14 edited Jul 09 '14
COBOL
INPUT
OUTPUT
2
u/chunes 1 2 Jul 07 '14
This was a bit trickier than it sounded at first. Java:
1
u/jnazario 2 0 Jul 07 '14
F#, see the note where i was struggling with an idiomatic FP way.
once compiled emits:
1
u/gfixler Jul 07 '14
This is my first Haskell program, which made it anything but [Easy] :)
1
u/mortenaa Jul 07 '14 edited Jul 08 '14
Solved with Dart. Extended it to take several games as input, and print the winner of each.
EDIT: Fixed a bug, didn't handle aces properly. I didn't reduce the value of previous aces if a later card brought the total over 21. Fixed by counting aces, and reducing the total if necessary with this code:
Here is a test case (Alice should win)
The code:
1
u/BryghtShadow Jul 08 '14
SWI-Prolog v6.6.6
Output:
1
u/[deleted] Jul 08 '14
So this is my solution in golang. I'm fairly sure I have covered everything that needed checking. Both given examples work so pfffrt....
Here's the github repo for it with both examples.
1
u/poltergeistt Jul 09 '14
Solution in Haxe. I used Haxe's Map class to map String keys (cards) to Integer values. I split each user input String (the hand) into an array of substrings and store it into another array. It's easy to calculate the hand value when you're looking if a substring matches a key in the map.
Busts have a value of 0. Five-card tricks have a value of 100. Any hand with a value greater than 0 is stored into a new array (outcome) along with the name of the hand bearer. That array is sorted so that the greatest hand value is placed at the 0 index of the array.
The winner is determined based on the outcome array. If the array is empty, nobody wins. If the array has only one element, then only one person won. If the array has more than one element, it is necessary to check if the first and second element in the array have a hand of equal value. If they do, then it's a tie. If they don't, then the person on the 0 index of the array is the winner.
The code is a bit of a mess. I could easily split it up into several functions to make it more readable. For the sake of the challenge, I decided to leave the logic behind the solution intact inside the main function.
1
u/Reboare Jul 09 '14
solution in rust using version
The solution's incredibly messy and I'm pretty sure it could be made simpler in some parts as I'm not completely comfortable with iterators yet.
1
u/brian-d Jul 10 '14
I gave this a crack using Typescript with the intention of being run through nodejs. The solution came together fairly easily... I felt like a vast majority of the work here ended up being parsing the input, but that's probably just my lack of exposure to nodejs's fs package and javascript tricks.
All in all a good challenge and a nice way to get a little more Typescript/nodejs experience.
Github Link
Declarations
Parsing Logic
Game State Logic
Game running
1
u/fredlhsu Jul 10 '14
Go
1
u/joeyGibson Jul 11 '14
Here's my Clojure solution. Pretty-printed version available at https://github.com/joeygibson/dailyprogrammer
And here's the output. I ran both example datasets, and then the four that /u/chunes gave in a comment.
1
u/jkudria Jul 12 '14 edited Jul 12 '14
For some reason I've got a mind-block for this one. Could barely do it. For something that seems so simple this really took quite some time. As always, feedback is appreciated.
Github link
EDIT: Added Github link
1
u/theruchet Jul 13 '14 edited Jul 13 '14
Hi! I'm a little late to the party but I would love comments on my implementation. I'm trying to become a better programmer.
Python
Edit: Formatting
1
u/kuzux 0 0 Jul 13 '14
Here's my solution in Haskell, I kind of got bored and went overboard with it :)
1
u/checho4 Jul 17 '14
First time using Python 3:
1
u/NorrinxRadd Jul 18 '14
My first attempt at a daily programmer. I know its late but here is my Python 2.7
1
u/[deleted] Jul 21 '14
C++ version:
1
u/[deleted] Jul 28 '14
This is messier than I'd like, but then I guess it's a fairly messy problem:
1
u/YouAreNotASlave Jul 30 '14
In Python 2.7. A bit verbose :(
1
u/Optimesh Aug 23 '14
Python 2.7 and my first submission here! I know it's been a while, but basically I just wanted a challenge I could do for practice. I'm rather new to Python and haven't written code in ~2 months (too much work), so I needed to find a way back. Not the prettiest thing but it works!
Feedback is more than welcome. Be gentle - I'm just a newbie :)
1
u/VerifiedMyEmail Jul 07 '14
python 3.3 works but isn't pretty
1
u/ehcubed Jul 07 '14
Python 3. Fun challenge! I've really grown to love how Python can split strings and unpack them into lists or tuples.
Code:
1
u/flugamababoo Jul 07 '14
Python 3.4, using "playerlist.txt" as the input:
Example output for input 2:
0
u/Reverse_Skydiver 1 0 Jul 07 '14 edited Jul 07 '14
My solution in Java. Slightly messy, but it does the job.
Using the one-liner in the comments (//) gave me a lot of happiness as it was the first time I'd used the ternary operator (?). However, it didn't really solve this challenge.
0
u/ENoether Jul 07 '14
Python 3 (as always, feedback appreciated):
Output:
0
u/undergroundmonorail Jul 07 '14
Python 3
I usually spend my time programming with code golf and other fun challenges, so I figured I'd practice doing something that actually follows good practices.
I'm also new to the whole "python 3" thing, so I'd love some critique here.
0
u/viciu88 Jul 07 '14
Java 1.8. getting a hang of the new streams
0
u/-AMD- Jul 07 '14
My python 3 solution
0
u/Frigguggi 0 1 Jul 07 '14 edited Jul 07 '14
Java:
Output:
4
Alice: Ace of Diamonds, Ten of Clubs
Bob: Three of Hearts, Six of Spades, Seven of Spades
Chris: Ten of Hearts, Three of Diamonds, Jack of Clubs
David: Two of Hearts, Three of Clubs, Three of Hearts, Five of Hearts, Six of Hearts
David (19) has won with a 5-card trick!
3
Alice: Ace of Diamonds, Ten of Clubs
Bob: Three of Hearts, Six of Spades, Seven of Spades
Chris: Ten of Hearts, Three of Diamonds, Jack of Clubs
Alice (21) has won!
0
u/marchelzo Jul 07 '14
Here is my solution in Haskell. It's pretty long, but I'm happy with the way it outputs the result.
If Alice and Bob tie, it outputs: "Tie between Alice and Bob!"
If Alice, Bob and Chris tie, it outputs "Tie between Alice, Bob and Chris!"
I checked it with all of the gotcha inputs provided by /u/chunes and it seemed to handle them correctly. Any feedback is welcome as I still feel that it could shortened considerably.
0
u/j0z Jul 07 '14
My solution, in C#:
0
u/[deleted] Jul 07 '14 edited Jul 07 '14
Phew! this took longer than I was expecting! Here's my solution in Java, I'm using three classes: A main class, a Dealer (or table) class that keeps track of the players, and a player class. Comments/criticisms are very welcome!
edit: I realized that my program only checks to see if someone has a blackjack, it doesn't even consider high point wins. I'm sure this would be an easy fix, and I'll probably fix it soon. This is definitely broken though
Main.java
Dealer.java
Player.java
output:
0
u/dp_account Jul 08 '14
Python 3. It also says who a tie is between.
0
u/mvolling Jul 08 '14 edited Jul 08 '14
C++
This was my first time using multiple user generated classes in a single program. Any helpful feedback would be greatly appreciated. Whether it be general syntax, pre-existing functions for handwritten pieces of code, performance tips. Anything that makes my code better would help.
Everything seems to work right as long as the input is formatted correctly.
Code:
Output 1
Output 2
0
u/[deleted] Jul 08 '14 edited Jul 08 '14
Here is the sloppiest answer you'll ever find. :/
0
u/newbdev2910 Jul 10 '14 edited Jul 10 '14
Java
Criticism and tips welcome.
Main.java
Player.java
Card.java
0
u/[deleted] Jul 14 '14
[deleted] — view deleted comment
0
u/[deleted] Jul 08 '14
[deleted] — view deleted comment