Added one pair loss condition.

This commit is contained in:
neviyn 2017-01-14 20:05:38 +00:00
parent 0592fbab86
commit 60fe77f044
4 changed files with 32 additions and 11 deletions

View File

@ -43,10 +43,7 @@ public class App
newCards.forEach(x -> System.out.print(x.getShortName() + " ")); newCards.forEach(x -> System.out.print(x.getShortName() + " "));
System.out.println(); System.out.println();
Result result = hand.checkForWin(); Result result = hand.checkForWin();
if (result.isWin()) System.out.println((result.isWin() ? "Win! " : "Lose! ") + result.toString());
System.out.println("Win! " + result.toString());
else
System.out.println("Lose!");
if(reader.readLine().equals("q")){ if(reader.readLine().equals("q")){
running = false; running = false;
} }

View File

@ -127,7 +127,7 @@ public class Hand {
if (royalStraightFlush(valueFrequency.keySet(), suitFrequency.values(), hasJoker)) { if (royalStraightFlush(valueFrequency.keySet(), suitFrequency.values(), hasJoker)) {
return Result.ROYAL_STRAIGHT_FLUSH; return Result.ROYAL_STRAIGHT_FLUSH;
} }
if (fiveOfAKind(valueFrequency, hasJoker)) { else if (fiveOfAKind(valueFrequency, hasJoker)) {
return Result.FIVE_OF_A_KIND; return Result.FIVE_OF_A_KIND;
} }
if (straightFlush(valueFrequency.keySet(), suitFrequency.values(), hasJoker)) { if (straightFlush(valueFrequency.keySet(), suitFrequency.values(), hasJoker)) {
@ -149,9 +149,12 @@ public class Hand {
return Result.THREE_OF_A_KIND; return Result.THREE_OF_A_KIND;
} }
if (twoPair(valueFrequency.values())) { 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 // Win Conditions
@ -253,6 +256,15 @@ public class Hand {
return Collections.frequency(valuesCount, 2) == 2; 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<Integer> 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, * Checks whether the values in the given iterator are sequential, it assumes they are sorted,
* *

View File

@ -9,8 +9,9 @@ public enum Result {
FLUSH(true), FLUSH(true),
STRAIGHT(true), STRAIGHT(true),
THREE_OF_A_KIND(true), THREE_OF_A_KIND(true),
TWO_OF_A_KIND(true), TWO_PAIR(true),
LOSS(false) ONE_PAIR(false),
NO_MATCH(false)
; ;
private final boolean win; private final boolean win;

View File

@ -167,7 +167,7 @@ public class HandTest {
deck.addCard(new Card(Suit.SPADES, Value.FIVE)); deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.CLUBS, Value.SIX)); deck.addCard(new Card(Suit.CLUBS, Value.SIX));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.LOSS, hand.checkForWin()); assertEquals(Result.ONE_PAIR, hand.checkForWin());
} }
@Test @Test
@ -225,7 +225,18 @@ public class HandTest {
deck.addCard(new Card(Suit.CLUBS, Value.ACE)); deck.addCard(new Card(Suit.CLUBS, Value.ACE));
deck.addCard(new Card(Suit.SPADES, Value.ACE)); deck.addCard(new Card(Suit.SPADES, Value.ACE));
Hand hand = new Hand(deck); 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 { private class FakeDeck implements IDeck {