-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokerHands.java
142 lines (125 loc) · 4.21 KB
/
PokerHands.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
/* Ally Sillins
Poker Hands is a game that reads five cards from
the user, analyzes them, and prints out the
category of hand they represent: straight flush,
four of a king, full house, flush, straight, three
of a king, two pairs, pair, high card.
*/
import java.util.*;
public class PokerHands {
public static void main(String[] args) {
Scanner scnr = new Scanner(System.in);
int[] hand = new int[5];
// Get 5 cards from the user and put them in an array
System.out.println("Enter five numeric cards, no face values. Use 2 - 9.");
for (int i = 0; i < 5; ++i) {
System.out.print("Card " + (i + 1) + ": ");
hand[i] = scnr.nextInt();
}
// Sort cards in order of lowest to highest
Arrays.sort(hand);
// check what hand we have & print answer
if (containsFourOfaKind(hand)){ // Four matching cards
System.out.println("Four of a kind!");
}
else if (containsFullHouse(hand)){ // A pair, and a three of a kind
System.out.println("Full House!");
}
else if (containsStraight(hand)){ // Card values can be arranged in order
System.out.println("Straight!");
}
else if (containsThreeOfaKind(hand)){ // Three matching cards
System.out.println("Three of a kind!");
}
else if (containsTwoPair(hand)){ // Two different pairs
System.out.println("Two Pair!");
}
else if (containsPair(hand)) { // Two of the cards are identical
System.out.println("Pair!");
}
else { // There are no matching cards, and the hand is not a straight
System.out.println("High Card!");
}
}
public static int counter(int array[], int compareValue) {
int count = 0;
for (int i = 0; i < 5; i++){
if (array[i] == compareValue) {
++count;
}
}
return count;
}
public static boolean containsStraight(int[] hand) {
int count = 0;
for (int i = 0; i < 4; i++) {
if (hand[i+1] - hand[i] == 1) {
++count;
}
}
if (count == 4) {
return true;
}
return false;
}
public static boolean containsPair(int[] hand) {
List countPairs = new ArrayList();
for (int i = 0; i < 5; i++) {
countPairs.add(counter(hand, hand[i]));
}
if (countPairs.contains(2)) {
return true;
}
return false;
}
public static boolean containsTwoPair(int[] hand) {
int value = 0;
boolean pair = false;
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if ((hand[j] == hand[i]) && (i != j)) {
value = hand[i];
pair = true;
}
}
}
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 5; j++) {
if (pair && (hand[j] == hand[i]) && (i != j) && (hand[i] != value)) {
return true;
}
}
}
return false;
}
public static boolean containsThreeOfaKind(int[] hand) {
List countPairs = new ArrayList();
for (int i = 0; i < 5; i++){
countPairs.add(counter(hand, hand[i]));
}
if (countPairs.contains(3)) {
return true;
}
return false;
}
public static boolean containsFullHouse(int[] hand) {
List countPairs = new ArrayList();
for (int i = 0; i < 5; i++){
countPairs.add(counter(hand, hand[i]));
}
if (countPairs.contains(2) && countPairs.contains(3)) {
return true;
}
return false;
}
public static boolean containsFourOfaKind(int[] hand) {
List countPairs = new ArrayList();
for (int i = 0; i < 5; i++){
countPairs.add(counter(hand, hand[i]));
}
if (countPairs.contains(4)) {
return true;
}
return false;
}
}