-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpocker.java
More file actions
279 lines (239 loc) · 7.6 KB
/
pocker.java
File metadata and controls
279 lines (239 loc) · 7.6 KB
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
import java.util.*;
// This program aims to compare the poker, poker has a number and a color
//Poker has 13 numbers, ascending order : 2, 3, 4, 5, 6, 7, 8, 9, T, J, Q, K, A
// and four color : D Diamond , S Spade , H heart , C Club , color doesn't has order
public class pocker {
public static char numbers[] = {2,3,4,5,6,7,8,9,'T','J','Q','K','A'};
public static String Numbers = new String(numbers);
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
ArrayList<card> Black = new ArrayList<card>();
ArrayList<card> White = new ArrayList<card>();
String Blackstr = s.nextLine();
String Whitestr = s.nextLine();
String [] blackstr = Blackstr.split("\\s+");
String [] whitestr = Whitestr.split("\\s+");
for(int i=0;i<blackstr.length;i++){
Black.add(new card(blackstr[i].charAt(0),blackstr[i].charAt(1)));
}
for(int i=0;i<whitestr.length;i++) {
White.add( new card(whitestr[i].charAt(0),whitestr[i].charAt(1)));
}
card.cardCompare(Black,White);
}
}
class cardComparator implements Comparator<card>{
@Override
public int compare(card A, card B) {
// TODO Auto-generated method stub card comparator
return pocker.Numbers.indexOf(A.Num) - pocker.Numbers.indexOf(B.Num);
}
}
class card {
char Num, Color;
card(char num,char color){
Num = num; //constructor : a card with a number and a color
Color = color;
}
public static void cardCompare(ArrayList<card>black,ArrayList<card> white) {
if(SameColorAndConsistent(black)!=null||SameColorAndConsistent(white)!=null) {
if(SameColorAndConsistent(black)!=null && SameColorAndConsistent(white) == null) { //To compare the cards
System.out.println("Black wins");
return ;
}else if(SameColorAndConsistent(black) == null && SameColorAndConsistent(white) != null) {
System.out.println("White wins");
return ;
}else {
System.out.println(compare(black,white));
return ;
}
}else if(sameColor(black)||sameColor(white)) {
if(sameColor(black) && !sameColor(white)) {
System.out.println("Black wins");
return ;
}else if(!sameColor(black) && sameColor(white)) {
System.out.println("White wins");
return ;
}else {
System.out.println(compare(black,white));
return;
}
}else if(isThird(black)!=null||isThird(white)!=null) {
if(isThird(black)!=null && isThird(white)==null) {
System.out.println("Black wins");
return ;
}else if (isThird(black)==null && isThird(white)!=null) {
System.out.println("White wins");
return ;
}else if(Compare(isThird(black),isThird(white))>0) {
System.out.println("Black wins");
return ;
}else if(Compare(isThird(black),isThird(white))<0) {
System.out.println("white wins");
return ;
}else {
card temp = isThird(black);
for(card A:black) {
if(A.Num == temp.Num) {
black.remove(A);
}
}
for(card B:white) {
if(B.Num == temp.Num) {
white.remove(B);
}
}
System.out.println(compare(black,white));
return ;
}
}else if(hasTwoPair(black)!=null||hasTwoPair(white)!=null) {
if(hasTwoPair(black)!=null && hasTwoPair(white)==null) {
System.out.println("Black wins");
return ;
}else if(hasTwoPair(black)==null &&hasTwoPair(white)!=null) {
System.out.println("White wins");
return ;
}else if(Compare(hasTwoPair(black),hasTwoPair(white))>0) {
System.out.println("Black wins");
return ;
}else if(Compare(hasTwoPair(black),hasTwoPair(white))<0) {
System.out.println("White wins");
return ;
}else {
card temp = hasTwoPair(black);
for(card A:black) {
if(A.Num == temp.Num) {
black.remove(A);
}
}
for(card B:white) {
if(B.Num == temp.Num) {
white.remove(B);
}
}
card.cardCompare(black, white);
return ;
}
}else if(isPair(black)!=null||isPair(white)!=null) {
if(isPair(black)!=null && isPair(white)==null) {
System.out.println("Black wins");
return ;
}else if(isPair(black)==null && isPair(white)!=null) {
System.out.println("White wins");
return ;
}else {
card temp = isPair(black);
for(card A:black) {
if(A.Num == temp.Num) {
black.remove(A);
}
}
for(card B:white) {
if(B.Num == temp.Num) {
white.remove(B);
}
}
System.out.println(compare(black,white));
return ;
}
}else {
System.out.println(compare(black,white));
}
}
private static boolean equal(card A, card B) {
if(A.Color == B.Color && A.Num == B.Num ) {
return true;
} //to judge two cards are equal or not
return false;
}
private static card compare(card A,card B) {
return pocker.Numbers.indexOf(A.Num) >= pocker.Numbers.indexOf(B.Num) ? A : B; //compare by order
}
private static int Compare(card A,card B) {
return pocker.Numbers.indexOf(A.Num) - pocker.Numbers.indexOf(B.Num);
}
private static String compare(List<card> A,List<card> B) {
Collections.sort(A,new cardComparator());
Collections.sort(B,new cardComparator());
Collections.reverse(A);
Collections.reverse(B);
// @override
for(int i=0;i<A.size();i++) {
if(Compare(A.get(i),B.get(i))>0) {
return "Black wins";
}else if (Compare(A.get(i),B.get(i))<0) {
return "White wins";
}
}
return "Tie";
}
private static card isPair(ArrayList<card> A) {
for(int i = 0; i<A.size()-1; ++i) {
for(int j = i+1;j<A.size();++j) { // to find out whether cards have Pair
if(equal(A.get(i),A.get(j))) {
return A.get(i);
}
}
}
return null;
}
private static card isThird(ArrayList<card> A) {
if(isPair(A) == null) {
return null;
}
for(int i=0;i<A.size();i++) {
for(int j=0;j<A.size();j++) {
if(equal(A.get(i),A.get(j))) {
if(j==A.size()-1) { // to find out whether cards have three cards that are the same
return null;
}
for(int k=j+1;k<A.size();k++) {
if(equal(A.get(j),A.get(k))) {
return A.get(i);
}
}
}
}
}
return null;
}
private static card hasTwoPair(ArrayList<card> A) {
card temp = (isPair(A));
if(temp == null) {
return null;
}
for(int i=0;i<A.size();i++) {
for(int j=i+1;j<A.size();j++) {
if(equal(A.get(i),A.get(j))&&!equal(A.get(i),temp)) {
return compare(A.get(i),temp);
}
}
}
return null;
}
private static card consistent(ArrayList<card> A) {
Collections.sort(A,new cardComparator());
Collections.reverse(A);
for(int i=0;i<A.size();i++) {
if(A.get(i+1).Num-A.get(i).Num!=1) {
return null;
}
}
return A.get(A.size()-1);
}
private static boolean sameColor(ArrayList<card>A) {
for(int i=0;i<A.size()-1;i++) {
if(A.get(i).Color!=A.get(i+1).Color) {
return false;
}
}
return true;
}
private static card SameColorAndConsistent(ArrayList<card> A) {
card temp = consistent(A);
if(A!=null&&sameColor(A)) {
return temp;
}
return null;
}
}