Added one pair loss condition.
This commit is contained in:
parent
0592fbab86
commit
60fe77f044
@ -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;
|
||||
}
|
||||
|
@ -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<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,
|
||||
*
|
||||
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
Loading…
Reference in New Issue
Block a user