-
Notifications
You must be signed in to change notification settings - Fork 106
/
Hand.java
173 lines (153 loc) · 3.67 KB
/
Hand.java
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package comp303m02;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import comp303m02.Card.Rank;
/**
* A collection of cards in a player's hand.
*/
public class Hand implements Iterable<Card>, Comparable<Hand>
{
private final List<Card> aCards = new ArrayList<>();
private final int aMaxCards;
/**
* Creates a new, empty hand, which can hold
* a maximum of pMaxCards.
*
* @param pMaxCards The maximum number of cards allowed in this hand.
* @pre pMaxCards > 0;
*/
public Hand(int pMaxCards)
{
assert pMaxCards > 0;
aMaxCards = pMaxCards;
}
/**
* Add pCards to the hand.
* @param pCard The card to add.
* @pre !isFull()
* @pre pCard != null;
*/
public void add(Card pCard)
{
assert pCard != null;
assert !isFull();
aCards.add(pCard);
}
/**
* @return True if the number of cards in the hand
* is the maximum number of cards allowable, as specified
* in the constructor.
*/
public boolean isFull()
{
return aCards.size() == aMaxCards;
}
/**
* @return True if there are no cards in this hand.
*/
public boolean isEmpty()
{
return aCards.size() == 0;
}
/**
* Removes pCards if it is in the hand. If it is not in the
* hand, does nothing.
*
* @param pCard The card to remove.
* @pre pCards != null;
*/
public void remove(Card pCard)
{
assert pCard != null;
aCards.remove(pCard);
}
/**
* @param pCard A card to check for containment.
* @return True if pCard is a card in this hand.
* @pre pCard != null
*/
public boolean contains(Card pCard)
{
assert pCard != null;
return aCards.contains(pCard);
}
@Override
public Iterator<Card> iterator()
{
return aCards.iterator();
}
@Override
public int compareTo(Hand pHand)
{
return aCards.size() - pHand.aCards.size();
}
public static Comparator<Hand> createAscendingComparator()
{
return new Comparator<Hand>() {
@Override
public int compare(Hand pHand1, Hand pHand2)
{
return pHand1.aCards.size() - pHand2.aCards.size();
}};
}
public static Comparator<Hand> createDescendingComparator()
{
return new Comparator<Hand>() {
@Override
public int compare(Hand pHand1, Hand pHand2)
{
return pHand2.aCards.size() - pHand1.aCards.size();
}};
}
/**
* Creates a comparator that compares hands in terms of ascending number
* of cards of rank pRank in the hand.
*
* @param pRank The rank to test against.
* @return A new Comparator instance that can compare by number
* of cards of the specified rank.
*/
public static Comparator<Hand> createByRankComparator(Rank pRank)
{
return new Comparator<Hand>()
{
@Override
public int compare(Hand pHand1, Hand pHand2)
{
return countCards(pHand1, pRank) - countCards(pHand2, pRank);
}
private int countCards(Hand pHand, Rank pRank)
{
int total = 0;
for( Card card : pHand)
{
if( card.getRank() == pRank )
{
total++;
}
}
return total;
}
};
}
/**
* This is the driver program.
* @param args
*/
public static void main(String[] args)
{
Hand hand1 = new Hand(5);
Hand hand2 = new Hand(5);
Deck deck = new Deck();
deck.shuffle();
hand1.add(deck.draw());
hand2.add(deck.draw());
hand2.add(deck.draw());
System.out.println(hand1.compareTo(hand2));
System.out.println(hand2.compareTo(hand1));
System.out.println(Hand.createAscendingComparator().compare(hand1, hand2));
System.out.println(Hand.createDescendingComparator().compare(hand1, hand2));
}
}