summaryrefslogtreecommitdiff
path: root/Game.java
blob: 903aa4233be34dc4d22415f639fa4c7f17833aac (plain)
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
import java.util.*;
import card.*;

class Game {
	private List<Card> deck = new ArrayList<>();
	private List<List<Card>> hands = new ArrayList<>();
	private List<Integer> ranks = new ArrayList<>();

	public Game() {
		setDeck();
	}

	public Game(int handsNum) {
		setDeck();
		// preflop
		for (int i = 0; i < handsNum; i++) {
			hands.add(setHand(2));
		}
		for (List<Card> hand : hands) {
			getHand(hand);
			System.out.println();
		}
		// flop, turn, and river
		hands.add(setHand(5));
		List<Card> street = hands.get(hands.size() - 1);
		getHand(hands.get(hands.size() - 1));
		System.out.println();
		for (int i = 0; i < handsNum; i++) {
			for (int j = 0; j < 5; j++) {
				hands.get(i).add(street.get(j));
			}
			hands.set(i, sort(hands.get(i)));
		}
		// TODO: find a better way to calculate combinations for each hand
		for (int i = 0; i < handsNum; i++) {
			List<List<Card>> combinations;
			combinations = generate(hands.get(i));
			List<Integer> combinationRanks = new ArrayList<>();
			for (List<Card> combination : combinations) {
				combinationRanks.add(getRank(combination));
			}
			int winner = getWinner(combinations, combinationRanks);
			hands.set(i, combinations.get(winner));
		}
		for (int i = 0; i < handsNum; i++) {
			ranks.add(getRank(hands.get(i)));
		}
	}

	public void setHands(List<List<Card>> hands) {
		this.hands = hands;
	}

	public void setRanks(List<Integer> ranks) {
		this.ranks = ranks;
	}

	public void setDeck() {
		Suit s = Suit.CLUBS;
		for (int i = 0; i < 4; i++) {
			switch (i) {
				case 0:
					s = Suit.CLUBS;
					break;
				case 1:
					s = Suit.DIAMONDS;
					break;
				case 2:
					s = Suit.HEARTS;
					break;
				case 3:
					s = Suit.SPADES;
					break;
			}
			for (int j = 1; j <= 13; j++) {
				Card card = new Card(j, s);
				deck.add(card);
			}
		}
	}

	public List<Card> sort(List<Card> hand) {
		if (hand.size() <= 1)
			return hand;
		int mid = hand.size() / 2;
		List<Card> left = sort(hand.subList(0, mid));
		List<Card> right = sort(hand.subList(mid, hand.size()));
		return merge(left, right);
	}

	public List<Card> merge(List<Card> left, List<Card> right) {
		List<Card> result = new ArrayList<>();
		int l = 0, r = 0;
		while (l < left.size() || r < right.size()) {
			if (l < left.size() && (r == right.size()
				|| left.get(l).getValue() < right.get(r).getValue() )) {
				result.add(left.get(l));
				l++;
			} else {
				result.add(right.get(r));
				r++;
			}
		}
		return result;
	}

	public List<Card> setHand(int size) {
		List<Card> hand = new ArrayList<>();
		Random rand = new Random();
		for (int i = 0; i < size; i++) {
			int randInt = rand.nextInt(deck.size());
			hand.add(deck.get(randInt));
			deck.remove(randInt);
		}
		return sort(hand);
	}

	public void getHand(List<Card> hand) {
		for (Card c : hand) {
			System.out.println(c.displayCard());
		}
	}

	public List<List<Card>> generate(List<Card> hand) {
		List<List<Card>> combinations = new ArrayList<>();
		helper(hand, new ArrayList<Card>(), combinations);
		return combinations;
	}

	public void helper(List<Card> hand, List<Card> combination, List<List<Card>> combinations) {
		if (combination.size() == 5) {
			combinations.add(new ArrayList<>(sort(combination)));
		} else {
			for (int i = 0; i < 5; i++) {
				if (combination.contains(hand.get(i)))
					continue;
				combination.add(hand.get(i));
				helper(hand, combination, combinations);
				combination.remove(combination.size() - 1);
			}
		}
	}

	public int getRank(List<Card> hand) {
		if (isRoyalFlush(hand)) {
			return 9;
		} else if (isStraightFlush(hand)) {
			return 8;
		} else if (isFourOfKind(hand)) {
			return 7;
		} else if (isFullHouse(hand)) {
			return 6;
		} else if (isFlush(hand)) {
			return 5;
		} else if (isStraight(hand)) {
			return 4;
		} else if (isThreeOfKind(hand)) {
			return 3;
		} else if (isTwoPair(hand)) {
			return 2;
		} else if (isPair(hand)) {
			return 1;
		} else {
			return 0;
		}
	}

	public void displayRank(List<Card> hand, int rank) {
		switch (rank) {
			case 8:
				System.out.println("Straight flush");
				break;
			case 7:
				System.out.println("Four of a kind");
				break;
			case 6:
				System.out.println("Full house");
				break;
			case 5:
				System.out.println("Flush");
				break;
			case 4:
				System.out.println("Straight");
				break;
			case 3:
				System.out.println("Three of a kind");
				break;
			case 2:
				System.out.println("Two pair");
				break;
			case 1:
				System.out.println("Pair");
				break;
			default:
				System.out.println("High card " + highCard(hand));
		}
	}

	public String highCard(List<Card> hand) {
		if (hand.get(0).getValue() == 1)
			return hand.get(0).displayCard();
		return hand.get(hand.size() - 1).displayCard();
	}

	public boolean isPair(List<Card> hand) {
		boolean pair = false;
		for (int i = 0; i < hand.size() - 1; i++)
			pair = pair || (hand.get(i).getValue() == hand.get(i + 1).getValue());
		return pair;
	}

	public boolean isTwoPair(List<Card> hand) {
		int numberOfPairs = 0;
		int i = 0;
		while (i < hand.size() - 1) {
			if (isPair(hand.subList(i, i + 2))) {
				numberOfPairs++;
				i += 2;
			} else {
				i++;
			}
		}
		return numberOfPairs == 2;
	}

	public boolean isThreeOfKind(List<Card> hand) {
		boolean threeOfKind = false;
		for (int i = 0; i < hand.size() - 2; i++)
			threeOfKind = threeOfKind
				|| (hand.get(i).getValue() == hand.get(i + 2).getValue());
		return threeOfKind;
	}

	// TODO: ace high straight
	public boolean isStraight(List<Card> hand) {
		int prev = hand.get(0).getValue();
		int count = 0;
		for (int i = 1; i < hand.size() && count < 4; i++) {
			if (hand.get(i).getValue() - prev == 1) {
				count++;
			} else {
				count = 0;
			}
			prev = hand.get(i).getValue();
		}
		return count == 4;
	}

	public boolean isFlush(List<Card> hand) {
		boolean flush = true;
		Suit suit = hand.get(0).getSuit();
		for (int i = 1; i < hand.size(); i++)
			flush = flush && (hand.get(i).getSuit() == suit);
		return flush;
	}

	public boolean isFullHouse(List<Card> hand) {
		boolean pairThreeOfKind = false;
		boolean threeOfKindPair = false;

		for (int i = 0; i < hand.size() - 4; i++)
			pairThreeOfKind = (hand.get(i).getValue() == hand.get(i + 1).getValue())
				&& (hand.get(i + 2).getValue() == hand.get(i + 4).getValue());

		for (int i = 0; i < hand.size() - 4; i++)
			threeOfKindPair = (hand.get(i).getValue() == hand.get(i + 2).getValue())
				&& (hand.get(i + 3).getValue() == hand.get(i + 4).getValue());

		return pairThreeOfKind || threeOfKindPair;
	}

	public boolean isFourOfKind(List<Card> hand) {
		boolean fourOfKind = false;
		for (int i = 0; i < hand.size() - 3; i++)
			fourOfKind = fourOfKind
				|| (hand.get(i).getValue() == hand.get(i + 3).getValue());
		return fourOfKind;
	}

	public boolean isStraightFlush(List<Card> hand) {
		return isStraight(hand) && isFlush(hand);
	}

	// TODO: check A-10
	public boolean isRoyalFlush(List<Card> hand) {
		return isFlush(hand) && hand.get(0).getValue() == 1
			&& hand.get(1).getValue() == 10 && hand.get(4).getValue() == 13;
	}

	public boolean compareHighCard(List<Card> firstHand, List<Card> secondHand) {
		if (firstHand.get(0).getValue() == 1) {
			return true;
		} else if (secondHand.get(0).getValue() == 1) {
			return false;
		}
		for (int j = firstHand.size() - 1; j > 0; j--) {
			if (firstHand.get(j).getValue()
				> secondHand.get(j).getValue()) {
				return true;
			} else if (secondHand.get(j).getValue()
				> firstHand.get(j).getValue()) {
				return false;
			}
		}
		return false;
	}

	public boolean comparePair(List<Card> firstHand, List<Card> secondHand) {
		Set<Integer> temp = new HashSet<>();
		int high = firstHand.get(0).getValue();
		for (int i = 1; i < firstHand.size(); i++) {
			Card c = firstHand.get(i);
			if (temp.contains(c.getValue())) {
				high = c.getValue();
			} else {
				temp.add(c.getValue());
			}
		}
		temp.clear();
		for (Card c : secondHand) {
			if (temp.contains(c.getValue())) {
				if (high > c.getValue())
					return true;
			} else {
				temp.add(c.getValue());
			}
		}
		return false;
	}

	// TODO: combine with comparePair and compareThreeOfKind
	public boolean compareTwoPair(List<Card> firstHand, List<Card> secondHand) {
		Set<Integer> temp = new HashSet<>();
		int high = firstHand.get(0).getValue();
		for (int i = 1; i < firstHand.size(); i++) {
			Card c = firstHand.get(i);
			if (temp.contains(c.getValue()) && c.getValue() > high) {
				high = c.getValue();
			} else {
				temp.add(c.getValue());
			}
		}
		temp.clear();
		int greater = 0;
		for (Card c : secondHand) {
			if (temp.contains(c.getValue()) && high > c.getValue()) {
				greater++;
			} else {
				temp.add(c.getValue());
			}
		}
		return greater >= 2;
	}

	public int getWinner(List<List<Card>> hands, List<Integer> ranks) {
		int bestHand = 0, bestRank = 0;
		for (int i = 0; i < ranks.size(); i++) {
			if (ranks.get(i) > bestRank) {
				bestHand = i;
				bestRank = ranks.get(i);
			} else if (ranks.get(i) == bestRank) {
				switch (bestRank) {
					case 7:
					case 6:
					case 3:
					case 2:
						if (compareTwoPair(hands.get(i), hands.get(bestHand))) {
							bestHand = i;
							bestRank = ranks.get(i);
						}
						break;
					case 1:
						if (comparePair(hands.get(i), hands.get(bestHand))) {
							bestHand = i;
							bestRank = ranks.get(i);
						}
						break;
					case 5:
					case 4:
					case 0:
						if (compareHighCard(hands.get(i), hands.get(bestHand))) {
							bestHand = i;
							bestRank = ranks.get(i);
						}
				}
			}
		}
		return bestHand;
	}

	public static void main(String[] args) {
		Game g = new Game(8);
		for (int i = 0; i < 8; i++) {
			g.getHand(g.hands.get(i));
			g.displayRank(g.hands.get(i), g.ranks.get(i));
			System.out.println();
		}
		System.out.println(g.getWinner(g.hands, g.ranks));
	}
}