-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokerHands.java
175 lines (148 loc) · 4.9 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
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
package xjtu.zyh.task3;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//终稿,为第三次提交
public class PokerHands {
/**
* 主函入口
* @param args
*/
public static void main(String[] args) {
char[][] white = new char[5][3];
char[][] black = new char[5][3];
//String s;
System.out.println("请输入黑白两方牌:");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
String s1 = in.readLine();
String s2 = s1.replaceAll(" ", "");
char[] chars = s2.toCharArray();
for (int i = 0; i < chars.length; i++) {
System.out.println(chars[i]);
}
System.out.println("填充后结果");
int count = 0;
for (int j = 0; j < 5; j++) {
for (int k = 0; k < 2; k++) {
white[j][k] = chars[count];
black[j][k] = chars[count + 10];
count++;
}
}
//此处为调试信息
//System.out.println();
System.out.println("white:");
showArrays(white);
System.out.println();
System.out.println("black:");
showArrays(black);
System.out.println();
//结果输出
int value = tests(black) - tests(white);
if (value < 0)
System.out.println("Black wins.");
else if (value > 0)
System.out.println("White wins.");
else
System.out.println("Tie.");
} catch (Exception e) {
e.printStackTrace();
}
}
public static int value(char ch) {
if (ch == 'T') return 8;
if (ch == 'J') return 9;
if (ch == 'Q') return 10;
if (ch == 'K') return 11;
if (ch == 'A') return 12;
return ch - '2';
}
public static int color(char ch) {
/*
方片 D
黑桃 S
红桃 H
梅花 C
*/
if (ch == 'S') return 0;
if (ch == 'H') return 1;
if (ch == 'D') return 2;
if (ch == 'C') return 3;
return 0;
}
public static void showArrays(char[][] myArray) {
for (char[] cells : myArray) {
for (char cell : cells) {
System.out.print(cell + "");
}
//System.out.println();
}
}
//核心算法
public static int tests(char[][] cards) {
char temp;
int[][] maps = new int[5][13];
//Arrays.fill(maps,0);
//showArrays(maps);
for (int i = 5; i > 1; --i) {
for (int j = 1; j < i; ++j) {
if (cards[j - 1][0] > cards[j][0]) {
temp = cards[j - 1][0];
cards[j - 1][0] = cards[j][0];
cards[j][0] = temp;
temp = cards[j - 1][1];
cards[j - 1][1] = cards[j][1];
cards[j][1] = temp;
}
}
}
for (int i = 0; i < 5; i++) {
maps[color(cards[i][1])][value(cards[i][0])]++;
maps[4][value(cards[i][0])]++;
}
//royal-flush | straight-flush
for (int i = 0; i < 4; ++i)
for (int j = 0; j < 9; ++j)
if ((maps[i][j] & maps[i][j + 1] & maps[i][j + 2] & maps[i][j + 3] & maps[i][j + 4]) > 0)
return 13 * 13 * 13 * 13 * 13 + 40 + j;
//four-of-a-kind
for (int i = 0; i < 13; ++i)
if (maps[4][i] >= 4) return 13 * 13 * 13 * 13 * 13 + 20 + i;
//full-house
int three = 0, two = 0;
for (int i = 12; i >= 0; --i) {
if (maps[4][i] == 2)
two = two * 15 + i + 1;
if (maps[4][i] == 3)
three = i + 1;
}
if (two > 0 || three > 0) return 13 * 13 * 13 * 13 * 13 + three + 1;
//flush
for (int i = 0; i < 4; ++i) {
int count = 0, number = 0;
for (int j = 12; j >= 0; --j)
for (int k = 0; k < maps[i][j]; ++k) {
++count;
number = number * 13 + j;
}
if (count == 5) return number;
}
//straight
for (int i = 0; i < 9; ++i)
if ((maps[4][i] & maps[4][i + 1] & maps[4][i + 2] & maps[4][i + 3] & maps[4][i + 4]) > 0)
return i - 20;
//three-of-a-kind
if (three > 0) return three - 40;
int ans = 0;
for (int i = 12; i >= 0; --i)
if (maps[4][i] == 1)
ans = ans * 13 + i;
//two-pairs
if (two >= 15)
return two * 15 + ans - 8000;
//one-pair
if (two > 0) return two * 13 * 13 * 13 + ans - 13 * 13 * 13 * 13 * 30;
//high-card
return ans - 13 * 13 * 13 * 13 * 50;
}
}