Repository navigation
poker : add to track #384
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
poker : add to track #384
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
7ddbc8a
poker : add to track
Zaldrick 1338bbb
add poker to config.json
Zaldrick 46dabc3
Change code according to FridaTveit's review
Zaldrick fe2139b
code improvements (adding Card & Hand classes)
Zaldrick eba0291
Merge branch 'master' into poker
Zaldrick 6b6e207
Use instance functions and improve code readability
Zaldrick 29a7738
fix according to POLICIES.doc
Zaldrick File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| apply plugin: "java" | ||
| apply plugin: "eclipse" | ||
| apply plugin: "idea" | ||
|
|
||
| repositories { | ||
| mavenCentral() | ||
| } | ||
|
|
||
| dependencies { | ||
| testCompile "junit:junit:4.12" | ||
| } | ||
|
|
||
| test { | ||
| testLogging { | ||
| exceptionFormat = 'full' | ||
| events = ["passed", "failed", "skipped"] | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| public class Card { | ||
| private int rank; | ||
| private int suit; | ||
|
|
||
| public Card(String card) { | ||
| rank = parseRank(card); | ||
| suit = parseSuit(card); | ||
| } | ||
|
|
||
| public int getRank() { | ||
| return rank; | ||
| } | ||
|
|
||
| public int getSuit() { | ||
| return suit; | ||
| } | ||
|
|
||
| private int parseRank(String card) { | ||
| return "..23456789TJQKA".indexOf(card.charAt(0)); | ||
| } | ||
|
|
||
| private int parseSuit(String card) { | ||
| return ".HSDC".indexOf(card.charAt(1)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.Collections; | ||
| import java.util.Comparator; | ||
| import java.util.HashMap; | ||
| import java.util.Iterator; | ||
| import java.util.LinkedHashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Hand { | ||
| private String input; | ||
| private int score; | ||
|
|
||
| public Hand(String hand) { | ||
| this.input = hand; | ||
| this.score = scoreHand(parseCards(hand)); | ||
| } | ||
|
|
||
| public int getScore() { | ||
| return score; | ||
| } | ||
|
|
||
| public String getInput() { | ||
| return input; | ||
| } | ||
|
|
||
| private List<Card> parseCards(String hand) { | ||
| ArrayList<Card> parsedCards = new ArrayList<Card>(); | ||
| String[] parseString = hand.split(" "); | ||
| for (int i = 0; i < parseString.length; i++) { | ||
| parsedCards.add(new Card(parseString[i])); | ||
| } | ||
| return parsedCards; | ||
| } | ||
|
|
||
| private Map<Integer, Integer> getfrequencyMap(List<Card> cards) { | ||
| Map<Integer, Integer> frequencyMap = new HashMap<Integer, Integer>(); | ||
| for (Card c : cards) { | ||
| if (frequencyMap.containsKey(c.getRank())) { | ||
| frequencyMap.put(c.getRank(), frequencyMap.get(c.getRank()) + 1); | ||
| } else { | ||
| frequencyMap.put(c.getRank(), 1); | ||
| } | ||
| } | ||
| return frequencyMap; | ||
| } | ||
|
|
||
| private int scoreHand(List<Card> cards) { | ||
| List<Card> cardsByRank = cards.stream().sorted(Comparator.comparing(Card::getRank)).unordered() | ||
| .collect(Collectors.toList()); | ||
|
|
||
| Map<Integer, Integer> frequencyMap = getfrequencyMap(cards); | ||
| List<Integer> ranks = frequencyMap.entrySet().stream().map(x -> x.getKey()).sorted(Comparator.reverseOrder()) | ||
| .collect(Collectors.toList()); | ||
| frequencyMap = frequencyMap.entrySet().stream().sorted(Map.Entry.comparingByValue(Collections.reverseOrder())) | ||
| .collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (e1, e2) -> e1, LinkedHashMap::new)); | ||
| List<Integer> rankCounts = frequencyMap.entrySet().stream().map(x -> x.getValue()).collect(Collectors.toList()); | ||
| List<Integer> suits = cards.stream().map(s -> s.getSuit()).collect(Collectors.toList()); | ||
|
|
||
| return calculatedScore(frequencyMap, cardsByRank, ranks, rankCounts, suits); | ||
| } | ||
|
|
||
| private int calculatedScore(Map<Integer, Integer> frequencyMap, List<Card> cardsByRank, List<Integer> ranks, | ||
| List<Integer> rankCounts, List<Integer> suits) { | ||
| if (ranks.equals(Arrays.asList(14, 5, 4, 3, 2))) { | ||
| ranks = Arrays.asList(5, 4, 3, 2, 1); | ||
| } | ||
|
|
||
| boolean flush = suits.stream().distinct().count() == 1; | ||
| boolean straight = ranks.stream().distinct().count() == 5 && ranks.get(0) - ranks.get(4) == 4; | ||
| Iterator<Integer> iteratorOverFrequencies = frequencyMap.keySet().iterator(); | ||
| int highestFrequency = iteratorOverFrequencies.next(); | ||
|
|
||
| if (straight && flush) { | ||
| return 800 + highestFrequency; | ||
| } | ||
| if (rankCounts.equals(Arrays.asList(4, 1))) { | ||
| return 700 + cardsByRank.get(0).getRank(); | ||
| } | ||
| if (rankCounts.equals(Arrays.asList(3, 2))) { | ||
| return 600 + cardsByRank.get(0).getRank(); | ||
| } | ||
| if (flush) { | ||
| return 500 + highestFrequency; | ||
| } | ||
| if (straight) { | ||
| return 400 + highestFrequency; | ||
| } | ||
| if (rankCounts.equals(Arrays.asList(3, 1, 1))) { | ||
| return 300 + cardsByRank.get(0).getRank(); | ||
| } | ||
| if (rankCounts.equals(Arrays.asList(2, 2, 1))) { | ||
| return 200 + Math.max(highestFrequency, iteratorOverFrequencies.next()); | ||
| } | ||
| if (rankCounts.equals(Arrays.asList(2, 1, 1, 1))) { | ||
| return 100 + highestFrequency; | ||
| } | ||
| Collections.sort(ranks, (a, b) -> b.compareTo(a)); | ||
| Collections.reverse(ranks); | ||
| return ranks.get(0); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.Comparator; | ||
| import java.util.List; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Poker { | ||
| private final List<String> bestHands; | ||
|
|
||
| public Poker(final List<String> hands) { | ||
| bestHands = bestHands(hands); | ||
| } | ||
|
|
||
| public List<String> getBestHands() { | ||
| return bestHands; | ||
| } | ||
|
|
||
| public List<String> bestHands(final List<String> hands) { | ||
| ArrayList<Hand> scoredHands = new ArrayList<Hand>(); | ||
|
|
||
| for (String s : hands) { | ||
| scoredHands.add(new Hand(s)); | ||
| } | ||
| int maxscore = scoredHands.stream().map(h -> h.getScore()).max(Comparator.naturalOrder()).get(); | ||
| return scoredHands.stream().filter(h -> h.getScore() == maxscore).map(h -> h.getInput()) | ||
| .collect(Collectors.toList()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| public class Poker { | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| import static org.junit.Assert.assertEquals; | ||
|
|
||
| import java.util.Arrays; | ||
|
|
||
| import org.junit.Test; | ||
|
|
||
| import org.junit.Ignore; | ||
|
|
||
| public class PokerTest { | ||
| @Test | ||
| public void oneHand() { | ||
| final String hand = "4S 5S 7H 8D JC"; | ||
| assertEquals(Arrays.asList(hand),new Poker(Arrays.asList(hand)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void nothingVsOnePair() { | ||
| final String nothing = "4S 5H 6S 8D JH"; | ||
| final String pairOf4 = "2S 4H 6S 4D JH"; | ||
| assertEquals(Arrays.asList(pairOf4),new Poker(Arrays.asList(nothing, pairOf4)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void twoPairs() { | ||
| final String pairOf2 = "4S 2H 6S 2D JH"; | ||
| final String pairOf4 = "2S 4H 6S 4D JH"; | ||
| assertEquals(Arrays.asList(pairOf4), new Poker(Arrays.asList(pairOf2,pairOf4)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void onePairVsDoublePair() { | ||
| final String pairOf8 = "2S 8H 6S 8D JH"; | ||
| final String doublePair = "4S 5H 4S 8D 5H"; | ||
| assertEquals(Arrays.asList(doublePair), new Poker(Arrays.asList(pairOf8, doublePair)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void twoDoublePairs() { | ||
| final String doublePair2And8 = "2S 8H 2S 8D JH"; | ||
| final String doublePair4And5 = "4S 5H 4S 8D 5H"; | ||
| assertEquals(Arrays.asList(doublePair2And8), new Poker(Arrays.asList(doublePair2And8, doublePair4And5)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void doublePairVsThree() { | ||
| final String doublePair2And8 = "2S 8H 2S 8D JH"; | ||
| final String threeOf4 = "4S 5H 4S 8D 4H"; | ||
| assertEquals(Arrays.asList(threeOf4 ), new Poker(Arrays.asList(doublePair2And8, threeOf4)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void twoThrees() { | ||
| final String threeOf2 = "2S 2H 2S 8D JH"; | ||
| final String threeOf1 = "4S AH AS 8D AH"; | ||
| assertEquals(Arrays.asList(threeOf1), new Poker(Arrays.asList(threeOf2, threeOf1)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void threeVsStraight() { | ||
| final String threeOf4 = "4S 5H 4S 8D 4H"; | ||
| final String straight = "3S 4H 2S 6D 5H"; | ||
| assertEquals(Arrays.asList(straight), new Poker(Arrays.asList(threeOf4, straight)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void twoStraights() { | ||
| final String straightTo8 = "4S 6H 7S 8D 5H"; | ||
| final String straightTo9 = "5S 7H 8S 9D 6H"; | ||
| assertEquals(Arrays.asList(straightTo9 ), new Poker(Arrays.asList(straightTo8, straightTo9)).getBestHands()); | ||
|
|
||
| final String straightTo1 = "AS QH KS TD JH"; | ||
| final String straightTo5 = "4S AH 3S 2D 5H"; | ||
| assertEquals(Arrays.asList(straightTo1), new Poker(Arrays.asList(straightTo1, straightTo5)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void straightVsFlush() { | ||
| final String straightTo8 = "4S 6H 7S 8D 5H"; | ||
| final String flushTo7 = "2S 4S 5S 6S 7S"; | ||
| assertEquals(Arrays.asList(flushTo7 ), new Poker(Arrays.asList(straightTo8, flushTo7)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void twoFlushes() { | ||
| final String flushTo8 = "3H 6H 7H 8H 5H"; | ||
| final String flushTo7 = "2S 4S 5S 6S 7S"; | ||
| assertEquals(Arrays.asList(flushTo8), new Poker(Arrays.asList(flushTo8, flushTo7)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void flushVsFull() { | ||
| final String flushTo8 = "3H 6H 7H 8H 5H"; | ||
| final String full = "4S 5H 4S 5D 4H"; | ||
| assertEquals(Arrays.asList(full ), new Poker(Arrays.asList(full, flushTo8)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void twoFulls() { | ||
| final String fullOf4By9 = "4H 4S 4D 9S 9D"; | ||
| final String fullOf5By8 = "5H 5S 5D 8S 8D"; | ||
| assertEquals(Arrays.asList(fullOf5By8 ), new Poker(Arrays.asList(fullOf4By9, fullOf5By8)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void fullVsSquare() { | ||
| final String full = "4S 5H 4S 5D 4H"; | ||
| final String squareOf3 = "3S 3H 2S 3D 3H"; | ||
| assertEquals(Arrays.asList(squareOf3 ), new Poker(Arrays.asList(full, squareOf3)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void twoSquares() { | ||
| final String squareOf2 = "2S 2H 2S 8D 2H"; | ||
| final String squareOf5 = "4S 5H 5S 5D 5H"; | ||
| assertEquals(Arrays.asList(squareOf5), new Poker(Arrays.asList(squareOf2, squareOf5)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void squareVsStraightFlush() { | ||
| final String squareOf5 = "4S 5H 5S 5D 5H"; | ||
| final String straightFlushTo9 = "5S 7S 8S 9S 6S"; | ||
| assertEquals(Arrays.asList(straightFlushTo9 ), new Poker(Arrays.asList(squareOf5, straightFlushTo9)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void twoStraightFlushes() { | ||
| final String straightFlushTo8 = "4H 6H 7H 8H 5H"; | ||
| final String straightFlushTo9 = "5S 7S 8S 9S 6S"; | ||
| assertEquals(Arrays.asList(straightFlushTo9 ), new Poker(Arrays.asList(straightFlushTo8, straightFlushTo9)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void threeHandWithTie() { | ||
| final String spadeStraightTo9 = "9S 8S 7S 6S 5S"; | ||
| final String diamondStraightTo9 = "9D 8D 7D 6D 5D"; | ||
| final String threeOf4 = "4D 4S 4H QS KS"; | ||
| assertEquals(Arrays.asList(spadeStraightTo9, diamondStraightTo9), new Poker(Arrays.asList(spadeStraightTo9, diamondStraightTo9, threeOf4)).getBestHands()); | ||
| } | ||
|
|
||
| @Test | ||
| @Ignore("Remove to run test") | ||
| public void straightTo5AgainstAPairOfJacks() { | ||
| final String straightTo5 = "2S 4D 5C 3S AS"; | ||
| final String twoJacks = "JD 8D 7D JC 5D"; | ||
| assertEquals(Arrays.asList(straightTo5), new Poker(Arrays.asList(straightTo5, twoJacks)).getBestHands()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -71,3 +71,4 @@ include 'twelve-days' | |
| include 'word-count' | ||
| include 'word-search' | ||
| include 'wordy' | ||
| include 'poker' | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At the moment, none of the other exercises have topics (see issue #523). But letting people add topics to new exercises if they want to might be a good idea. Thoughts @exercism/java?