-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathPokerHand.java
74 lines (68 loc) · 2.65 KB
/
PokerHand.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
import java.util.Arrays;
import java.util.Scanner;
import java.util.regex.Pattern;
public class PokerHand
{
final static String FLUSH = "1?.(.)( 1?.\\1){4}";
final static String FOUR = ".?(.)\\1\\1\\1.?";
final static String FULL1 = "(.)\\1\\1(.)\\2";
final static String FULL2 = "(.)\\1(.)\\2\\2";
final static String THREE = ".*(.)\\1\\1.*";
final static String TWO_PAIRS = ".?(.)\\1.?(.)\\2.?";
final static String ONE_PAIR = ".*(.)\\1.*";
public static void main(String[] args) {
final var input = new Scanner(System.in);
final var pokerHand = input.nextLine();
input.close();
char[] cards = Arrays.stream(pokerHand
.replaceAll("[SCDH]", "")
.split(" "))
.mapToInt(PokerHand::getCardValue)
.sorted()
.collect(StringBuilder::new,
StringBuilder::appendCodePoint,
StringBuilder::append)
.toString().toCharArray();
boolean isColor = Pattern.matches(FLUSH, pokerHand);
boolean isOrder = cards[0] + 1 == cards[1]
&& cards[1] + 1 == cards[2]
&& cards[2] + 1 == cards[3]
&& cards[3] + 1 == cards[4]
|| cards[4] == '0' + 14 // Special «low» stright: A-2-3-4-5
&& cards[0] == '2' && cards[1] == '3' && cards[2] == '4' && cards[3] == '5';
String hand = new String(cards);
String rank;
if (isColor && isOrder && cards[0] == '0' + 10) {
rank = "Royal Flush";
} else if (isColor && isOrder) {
rank = "Straight Flush";
} else if (isColor) {
rank = "Flush";
} else if (isOrder) {
rank = "Straight";
} else if (Pattern.matches(FOUR, hand)) {
rank = "Four of a Kind";
} else if (Pattern.matches(FULL1, hand) || Pattern.matches(FULL2, hand)) {
rank = "Full House";
} else if (Pattern.matches(THREE, hand)) {
rank = "Three of a Kind";
} else if (Pattern.matches(TWO_PAIRS, hand)) {
rank = "Two Pairs";
} else if (Pattern.matches(ONE_PAIR, hand)) {
rank = "One Pair";
} else {
rank = "High Card";
}
System.out.println(rank);
}
static char getCardValue (String card) {
switch (card) {
case "10": return '0' + 10;
case "J": return '0' + 11;
case "Q": return '0' + 12;
case "K": return '0' + 13;
case "A": return '0' + 14;
default: return card.charAt(0);
}
}
}