forked from prmr/DesignBook
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPokerHand.java
More file actions
54 lines (50 loc) · 1.5 KB
/
Copy pathPokerHand.java
File metadata and controls
54 lines (50 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*******************************************************************************
* Companion code for the book "Introduction to Software Design with Java"
* by Martin P. Robillard.
*
* Copyright (C) 2019 by Martin P. Robillard
*
* This code is licensed under a Creative Commons
* Attribution-NonCommercial-NoDerivatives 4.0 International License.
*
* See http://creativecommons.org/licenses/by-nc-nd/4.0/
*******************************************************************************/
package chapter5;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
// Let's assume we only consider ace-low as valid for straights, for simplicity.
public class PokerHand
{
private final List<Card> aCards;
public PokerHand( Card... pCards)
{
assert pCards.length == 5;
aCards = Arrays.asList(pCards);
aCards.sort(new Comparator<Card>() {
@Override
public int compare(Card pCard1, Card pCard2)
{
return pCard1.getRank().compareTo(pCard2.getRank());
}});
}
@SuppressWarnings("unused")
private boolean isStraightFlush()
{
Suit suit = aCards.get(0).getSuit();
Rank rank = aCards.get(0).getRank();
Iterator<Card> iterator = aCards.iterator();
iterator.next();
while( iterator.hasNext() )
{
Card card = iterator.next();
if( card.getSuit() != suit || card.getRank().ordinal() - rank.ordinal() != 1)
{
return false;
}
rank = card.getRank();
}
return true;
}
}