-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFourCardEvaluator.java
194 lines (168 loc) · 6.48 KB
/
FourCardEvaluator.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
import java.util.Collections;
import java.util.Comparator;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
public class FourCardEvaluator implements Evaluator {
private final static int CARDS_PER_HAND = 4;
private static class Hand implements Comparable<Hand> {
public final int player;
public final List<Card> cards;
public final int value;
public Hand(int player, List<Card> cards, int value) {
this.player = player;
this.cards = cards;
this.value = value;
}
public int compareTo(Hand h) {
return value - h.value;
}
}
private enum HandType {
JUNK(0), PAIR(3), TWO_PAIR(4), THREE_OF_A_KINDS(5), FOUR_OF_A_KIND(6);
private final int ranking;
private HandType(int ranking) {
this.ranking = ranking;
}
public int getRankValue() {
return ranking;
}
}
private enum CardCount {
ONE_CARD(1), TWO_CARDS(2), THREE_CARDS(3), FOUR_CARDS(4);
private final int count;
private CardCount(int count) {
this.count = count;
}
public int getCount() {
return count;
}
public static CardCount ofCount(int count) {
for (CardCount c : CardCount.values()) {
if (c.getCount() == count) {
return c;
}
}
throw new RuntimeException("no CardCount of given count");
}
}
public double[] payoffs(List<List<Card>> holeCards, List<Card> sharedCards, double[] bets, boolean[] folds) {
int numPlayers = holeCards.size();
int numFolds = Arrays.sum(folds);
Set<Integer> winners = Sets.newHashSet();
if (numFolds + 1 < numPlayers) {
List<Hand> hands = buildHands(holeCards, sharedCards, folds);
Collections.sort(hands, Collections.reverseOrder());
for (Hand hand : hands) {
if (hand.value == hands.get(0).value) {
winners.add(hand.player);
} else {
break;
}
}
} else {
for (int i = 0; i < numPlayers; i++) {
if (!folds[i]) {
winners.add(i);
break;
}
}
}
double pot = Arrays.sum(bets);
double[] payoffs = new double[holeCards.size()];
for (int p = 0; p < numPlayers; p++) {
payoffs[p] = -bets[p];
if (winners.contains(p)) {
payoffs[p] += (1.0 / winners.size()) * pot;
}
}
return payoffs;
}
private static List<Hand> buildHands(List<List<Card>> holeCards, List<Card> sharedCards, boolean[] folds) {
List<Hand> hands = Lists.newArrayList();
for (int player = 0; player < holeCards.size(); player++) {
if (folds[player]) {
continue;
}
List<Card> cards = Lists.newArrayList(holeCards.get(player));
for (Card c : sharedCards) {
cards.add(c);
}
List<ArrayList<Card>> playerHands = buildPossibleHands(cards, CARDS_PER_HAND);
for (ArrayList<Card> hand : playerHands) {
hands.add(new Hand(player, hand, evaluate(hand)));
}
}
return hands;
}
private static List<ArrayList<Card>> buildPossibleHands(List<Card> cards, int size) {
if (size == 0) {
return Collections.singletonList(new ArrayList<Card>());
}
Preconditions.checkArgument(cards.size() != 0, "not enough cards to make hand of desired size");
List<ArrayList<Card>> hands = Lists.newArrayList();
for (int i = cards.size() - 1; i >= 0; i--) {
Card c = cards.get(i);
cards.remove(c);
for (ArrayList<Card> hand : buildPossibleHands(cards, size - 1)) {
hand.add(c);
hands.add(hand);
}
cards.add(c);
}
return hands;
}
private static int evaluate(List<Card> hand) {
Map<CardCount, List<Integer>> vtr = buildCountMap(hand);
List<Integer> kickers = vtr.keySet().contains(CardCount.ONE_CARD)
? vtr.get(CardCount.ONE_CARD) : new ArrayList<Integer>();
if (vtr.keySet().contains(CardCount.FOUR_CARDS)) {
return value(HandType.FOUR_OF_A_KIND, vtr.get(CardCount.FOUR_CARDS).get(0), 0, 0, 0);
} else if (vtr.keySet().contains(CardCount.THREE_CARDS)) {
return value(HandType.THREE_OF_A_KINDS, vtr.get(CardCount.THREE_CARDS).get(0), kickers.get(0), 0, 0);
} else if (vtr.keySet().contains(CardCount.TWO_CARDS)) {
List<Integer> pairs = vtr.get(CardCount.TWO_CARDS);
if (pairs.size() == 2) {
return value(HandType.TWO_PAIR, pairs.get(0), pairs.get(1), 0, 0);
} else {
return value(HandType.PAIR, pairs.get(0), kickers.get(0), kickers.get(1), 0);
}
} else {
return value(HandType.JUNK, kickers.get(0), kickers.get(1), kickers.get(2), kickers.get(3));
}
}
public static int value(HandType e, int d, int c, int b, int a) {
return 10000 * e.getRankValue() + 1000 * d + 100 * c + 10 * b + a;
}
private static Map<CardCount, List<Integer>> buildCountMap(List<Card> hand) {
Map<CardCount, List<Integer>> countMap = Maps.newHashMap();
Collections.sort(hand, Collections.reverseOrder());
int rank = hand.get(0).rank();
int count = 1;
for (int i = 1; i < hand.size(); i++) {
Card c = hand.get(i);
if (c.rank() == rank) {
count++;
} else {
addCountToMap(countMap, count, rank);
rank = c.rank();
count = 1;
}
}
addCountToMap(countMap, count, rank);
return countMap;
}
private static void addCountToMap(Map<CardCount, List<Integer>> countMap, int count, int rank) {
CardCount cc = CardCount.ofCount(count);
if (!countMap.keySet().contains(cc)) {
countMap.put(cc, new ArrayList<Integer>());
}
countMap.get(cc).add(rank);
}
}