From 60fe77f0440811b4a752531c90c4f9f3f1c438ed Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Sat, 14 Jan 2017 20:05:38 +0000 Subject: [PATCH] Added one pair loss condition. --- src/main/java/uk/co/neviyn/pokergame/App.java | 5 +---- .../java/uk/co/neviyn/pokergame/game/Hand.java | 18 +++++++++++++++--- .../uk/co/neviyn/pokergame/model/Result.java | 5 +++-- src/test/java/game/HandTest.java | 15 +++++++++++++-- 4 files changed, 32 insertions(+), 11 deletions(-) diff --git a/src/main/java/uk/co/neviyn/pokergame/App.java b/src/main/java/uk/co/neviyn/pokergame/App.java index 6dbf2b7..18042c5 100644 --- a/src/main/java/uk/co/neviyn/pokergame/App.java +++ b/src/main/java/uk/co/neviyn/pokergame/App.java @@ -43,10 +43,7 @@ public class App newCards.forEach(x -> System.out.print(x.getShortName() + " ")); System.out.println(); Result result = hand.checkForWin(); - if (result.isWin()) - System.out.println("Win! " + result.toString()); - else - System.out.println("Lose!"); + System.out.println((result.isWin() ? "Win! " : "Lose! ") + result.toString()); if(reader.readLine().equals("q")){ running = false; } diff --git a/src/main/java/uk/co/neviyn/pokergame/game/Hand.java b/src/main/java/uk/co/neviyn/pokergame/game/Hand.java index 551ccdc..08a4ca5 100644 --- a/src/main/java/uk/co/neviyn/pokergame/game/Hand.java +++ b/src/main/java/uk/co/neviyn/pokergame/game/Hand.java @@ -127,7 +127,7 @@ public class Hand { if (royalStraightFlush(valueFrequency.keySet(), suitFrequency.values(), hasJoker)) { return Result.ROYAL_STRAIGHT_FLUSH; } - if (fiveOfAKind(valueFrequency, hasJoker)) { + else if (fiveOfAKind(valueFrequency, hasJoker)) { return Result.FIVE_OF_A_KIND; } if (straightFlush(valueFrequency.keySet(), suitFrequency.values(), hasJoker)) { @@ -149,9 +149,12 @@ public class Hand { return Result.THREE_OF_A_KIND; } if (twoPair(valueFrequency.values())) { - return Result.TWO_OF_A_KIND; + return Result.TWO_PAIR; } - return Result.LOSS; + if (onePair(valueFrequency.values(), hasJoker)){ + return Result.ONE_PAIR; + } + return Result.NO_MATCH; } // Win Conditions @@ -253,6 +256,15 @@ public class Hand { return Collections.frequency(valuesCount, 2) == 2; } + /** + * Checks for one pairs of cards i.e. (J,J) + * @param valuesCount Counts of value frequencies. + * @return Whether one of the frequency counts equal two. + */ + private boolean onePair(final Collection valuesCount, boolean hasJoker) { + return hasJoker || Collections.frequency(valuesCount, 2) == 1; + } + /** * Checks whether the values in the given iterator are sequential, it assumes they are sorted, * diff --git a/src/main/java/uk/co/neviyn/pokergame/model/Result.java b/src/main/java/uk/co/neviyn/pokergame/model/Result.java index f265609..9f253db 100644 --- a/src/main/java/uk/co/neviyn/pokergame/model/Result.java +++ b/src/main/java/uk/co/neviyn/pokergame/model/Result.java @@ -9,8 +9,9 @@ public enum Result { FLUSH(true), STRAIGHT(true), THREE_OF_A_KIND(true), - TWO_OF_A_KIND(true), - LOSS(false) + TWO_PAIR(true), + ONE_PAIR(false), + NO_MATCH(false) ; private final boolean win; diff --git a/src/test/java/game/HandTest.java b/src/test/java/game/HandTest.java index 695f4da..75a7528 100644 --- a/src/test/java/game/HandTest.java +++ b/src/test/java/game/HandTest.java @@ -167,7 +167,7 @@ public class HandTest { deck.addCard(new Card(Suit.SPADES, Value.FIVE)); deck.addCard(new Card(Suit.CLUBS, Value.SIX)); Hand hand = new Hand(deck); - assertEquals(Result.LOSS, hand.checkForWin()); + assertEquals(Result.ONE_PAIR, hand.checkForWin()); } @Test @@ -225,7 +225,18 @@ public class HandTest { deck.addCard(new Card(Suit.CLUBS, Value.ACE)); deck.addCard(new Card(Suit.SPADES, Value.ACE)); Hand hand = new Hand(deck); - assertEquals(Result.TWO_OF_A_KIND, hand.checkForWin()); + assertEquals(Result.TWO_PAIR, hand.checkForWin()); + } + + @Test + public void onePair() { + deck.addCard(new Card(Suit.SPADES, Value.FOUR)); + deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); + deck.addCard(new Card(Suit.DIAMONDS, Value.TEN)); + deck.addCard(new Card(Suit.CLUBS, Value.SIX)); + deck.addCard(new Card(Suit.SPADES, Value.ACE)); + Hand hand = new Hand(deck); + assertEquals(Result.ONE_PAIR, hand.checkForWin()); } private class FakeDeck implements IDeck {