r/dailyprogrammer • u/Elite6809 1 1 • Jul 09 '14
[7/9/2014] Challenge #170 [Intermediate] Rummy Checker
(Intermediate): Rummy Checker
Rummy is another very common card game. This time, the aim of the game is to match cards together into groups (melds) in your hand. You continually swap cards until you have such melds, at which point if you have a valid hand you have won. Your hand contains 7 cards, and your hand will contain 2 melds - one that is 3 long and one that is 4 long. A meld is either:
- 3 or 4 cards of the same rank and different suit (eg. 3 jacks or 4 nines) called a set
- 3 or 4 cards in the same suit but increasing rank - eg. Ace, Two, Three, Four of Hearts, called a run
Ace is played low - ie. before 2 rather than after king.
Your challenge today is as follows. You will be given a Rummy hand of 7 cards. You will then be given another card, that you have the choice to pick up. The challenge is to tell whether picking up the card will win you the game or not - ie. whether picking it up will give you a winning hand. You will also need to state which card it is being replaced with.
Input Description
First you will be given a comma separated list of 7 cards on one line, as so:
Two of Diamonds, Three of Diamonds, Four of Diamonds, Seven of Diamonds, Seven of Clubs, Seven of Hearts, Jack of HeartsNext, you will be given another (new) card on a new line, like so:
Five of DiamondsOutput Description
If replacing a card in your hand with the new card will give you a winning hand, print which card in your hand is being replaced to win, for example:
Swap the new card for the Jack of Hearts to win!Because in that case, that would give you a run (Two, Three, Four, Five of Diamonds) and a set (Seven of Diamonds, Clubs and Hearts). In the event that picking up the new card will do nothing, print:
No possible winning hand.Notes
You may want to re-use some code for your card and deck structure from your solution to this challenge where appropriate.
3
u/Elite6809 1 1 Jul 09 '14
Here's my solution, again in C#: https://github.com/DropTableSpoon/Challenge170Intermediate
This should work for all possible cases. Hopefully.
3
u/Godspiral 3 3 Jul 09 '14 edited Jul 09 '14
cards =: ;: 'Ace Two Three Four Five Six Seven Eight Nine Ten Jack Queen King'
suits =: ;: 'Hearts Diamonds Spades Clubs'
6 10 14 15 16 17 19 45
getset =: (#~ [: (2 < +/) [: =/~ 13&|)
getrun =: 3 : '~. ; a: -.~ (#~ 2= {: - {.) each 3 <\ y'
tocardname =: (cards {~ 13| ]) , 'of' ; suits {~13 <.@:%~ ]
Win! Drop Jack of Hearts
Needs harder test cases, such as those where you might have a card that is part of a set and a run, and need to choose where to classify it. Also, I'm not handling case where all cards fit into melds.
2
u/ryani Jul 09 '14
Haskell.
Brute force solution: get all permutations of the hand without one card, and if the permutation is a 'canonical winning permutation' (first 3 cards + rest of hand melded, runs in ascending order), it wins.
Could be golfed a lot smaller; I didn't use a ton of standard library utilities, instead showing simple implementations of the needed abstractions.
2
u/Reboare Jul 10 '14
written in rust for version
Any feedback is welcome :)
2
u/marchelzo Jul 10 '14 edited Jul 11 '14
Quite possibly the worst Haskell code I have ever written. I would really appreciate any feedback about how I could make this more elegant (if you can even read it).
1
u/ftrbot Jul 10 '14
Is King, Ace, Two a correct run?
1
u/mvolling Jul 10 '14
Can a card be used in both of the melds?
1
u/mvolling Jul 10 '14
C++
This program accepts input from a file but is easily modifiable to get it to read in from a command line. Simply change both lines that say "getline(in,handInfo);" to "getline(cin,handInfo);". Both of those lines can be found in the main function. Most of the input and ouput stuff came from my submission to Monday's Thread.
This program currently does make the cards stick to one meld. I may or may not fix this tomorrow depending on how busy I am.
As always, feedback is greatly appreciated.
Wow! This is my first comment that I tried to submit that went over reddit's comment character count! I posted it to pastebin instead.
Code:
http://pastebin.com/JGM7HVT5
Output:
1
u/poltergeistt Jul 11 '14
Solution in Haxe. Because I don't understand the rules clearly, I wrote it so that after a swap you'll have at least a single meld of three cards in your hand. For example, for the provided challenge input, my solution will let you know that swapping the seven of diamonds for the five of diamonds will earn you a suit (well, a "win").
Main.hx
Card.hx
INPUT / OUTPUT
1
u/Moonwalkings Jul 12 '14
Here's my C++ solution. Actually, I hope I'm not late. https://gist.github.com/fightyz/2d5f5d6e8d305a908e47
1
u/Coplate Jul 13 '14 edited Jul 13 '14
COBOL
1
u/Octopuscabbage Jul 15 '14 edited Jul 15 '14
Haskell, my god I hate IO in haskell... This example is pretty purely brute force. I'm considering adding some parrallelism here, especially in the main map. Update: I made it parallel but it goes so fast it's hard to time, I think it worked. I can't tell, it seems to easy.
Example:
1
u/healthycola Jul 15 '14
Here's my solution in C++: https://gist.github.com/healthycola/41786e7eb7be4257ac83
Feedback is much appreciated!
1
u/adrian17 1 4 Jul 15 '14 edited Jul 16 '14
C++. File reading in constructor, no input validation and a silly way of ignoring commas. I'm generating 3- and 4-card combinations by creating all permutations of a hand and splitting it into first three and last four cards - not optimal at all, but works and it's under 100 lines so it's good enough for me.
0
u/flugamababoo Jul 10 '14 edited Jul 10 '14
I think this works for every case, but I didn't check many. More Python 3, with the input in a file named game_definition.txt:
Edit: removed some unused variables I mistakenly left in, and test with the sample input.
output:
0
u/[deleted] Jul 10 '14 edited Jul 10 '14
I know this code's a bit on the longer side, but it seems to work perfectly, as far as I've tested.
Any advice on optimization (or anything else for that matter) is very welcome.
Code (JAVA):
Class "RummyChecker"
Class "Card"
Output:
0
u/viciu88 Jul 10 '14 edited Jul 10 '14
Java 1.8.
Should work with multiple decks (duplicate cards). Finds every possible meld. For every 4 card meld finds 3 card meld that doesn't contain the same card.
Edit: Reworked to accept some edge cases
0
u/ENoether Jul 10 '14
Python 3 (Could very easily be modified to support different hand sizes; as always, feedback and criticism welcome):
Output:
0
u/[deleted] Jul 11 '14
Java solution.
I wanted to create a solution that does not grow as the number of combinatorical possibilities grows. This solution uses a strategy pattern, and the number of passes over the data set grows based on how many possible melds it generated in the previous strategy. So, if only one run is possible in your current hand, it only makes one pass over the hand when looking for sets. Also, an advantage of this approach if a new strategy to the game was possible, such as a flush, it would be easily implemented by adding a new strategy class and adding it to the Rummy class. The guts of it happen in the canMeld method, where we return a List of melds that the current strategy found were possible, then we pass this list to the next strategy, which ignores any of the cards that have been previously melded.
I bet the Deck/Suits/Cards stuff could be more compact, but it's easy code to read.
Happy to hear some feedback.
JUnit test cases
0
u/[deleted] Jul 10 '14
[deleted] — view deleted comment
0
u/[deleted] Jul 09 '14
[deleted] — view deleted comment