Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,15 @@

]
},
{
"slug": "poker",
"difficulty": 7,
"topics": [

Copy link
Copy Markdown
Contributor

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?

"Parsing",
"Sorting",
"Games"
]
},
{
"slug": "clock",
"difficulty": 7,
Expand Down
18 changes: 18 additions & 0 deletions exercises/poker/build.gradle
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"]
}
}
25 changes: 25 additions & 0 deletions exercises/poker/src/example/java/Card.java
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));
}
}
104 changes: 104 additions & 0 deletions exercises/poker/src/example/java/Hand.java
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);
}
}
27 changes: 27 additions & 0 deletions exercises/poker/src/example/java/Poker.java
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());
}
}
2 changes: 2 additions & 0 deletions exercises/poker/src/main/java/Poker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class Poker {
}
164 changes: 164 additions & 0 deletions exercises/poker/src/test/java/PokerTest.java
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());
}
}
1 change: 1 addition & 0 deletions exercises/settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ include 'twelve-days'
include 'word-count'
include 'word-search'
include 'wordy'
include 'poker'