forked from AllenDowney/ThinkJavaCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartPlayer.java
More file actions
35 lines (30 loc) · 931 Bytes
/
SmartPlayer.java
File metadata and controls
35 lines (30 loc) · 931 Bytes
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
public class SmartPlayer extends Player {
public SmartPlayer(String name) {
super(name);
}
public Card play(Eights eights, Card prev) {
Card card = searchForBetterMatch(prev);
if (card == null) {
card = drawForMatch(eights, prev);
}
return card;
}
public Card searchForBetterMatch(Card prev) {
int bestMatch = -1;
for (int i = 0; i < getHand().size(); i++) {
Card card = getHand().getCard(i);
if (card.getRank() == 8) {
return getHand().popCard(i);
}
if (cardMatches(card, prev)) {
if (bestMatch == -1 || card.getRank() > getHand().getCard(bestMatch).getRank()) {
bestMatch = i;
}
}
}
if (bestMatch > -1) {
return getHand().popCard(bestMatch);
}
return null;
}
}