Quantcast
Channel: YGC » ComputerScience
Viewing all articles
Browse latest Browse all 27

project euler -- problem 54

$
0
0

In the card game poker, a hand consists of five cards and are ranked, from lowest to highest, in the following way:

  • High Card: Highest value card.
  • One Pair: Two cards of the same value.
  • Two Pairs: Two different pairs.
  • Three of a Kind: Three cards of the same value.
  • Straight: All cards are consecutive values.
  • Flush: All cards of the same suit.
  • Full House: Three of a kind and a pair.
  • Four of a Kind: Four cards of the same value.
  • Straight Flush: All cards are consecutive values of same suit.
  • Royal Flush: Ten, Jack, Queen, King, Ace, in same suit.

The cards are valued in the order:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace.

If two players have the same ranked hands then the rank made up of the highest value wins; for example, a pair of eights beats a pair of fives (see example 1 below). But if two ranks tie, for example, both players have a pair of queens, then highest cards in each hand are compared (see example 4 below); if the highest cards tie then the next highest cards are compared, and so on.

Consider the following five hands dealt to two players:

Hand Player 1 Player 2 Winner
1 5H 5C 6S 7S KD
Pair of Fives
2C 3S 8S 8D TD
Pair of Eights
Player 2
2 5D 8C 9S JS AC
Highest card Ace
2C 5C 7D 8S QH
Highest card Queen
Player 1
3 2D 9C AS AH AC
Three Aces
3D 6D 7D TD QD
Flush with Diamonds
Player 2
4 4D 6S 9H QH QC
Pair of Queens
Highest card Nine
3D 6D 7H QD QS
Pair of Queens
Highest card Seven
Player 1
5 2H 2D 4C 4D 4S
Full House
With Three Fours
3C 3D 3S 9S 9D
Full House
with Three Threes
Player 1

The file, poker.txt, contains one-thousand random hands dealt to two players. Each line of the file contains ten cards (separated by a single space): the first five are Player 1's cards and the last five are Player 2's cards. You can assume that all hands are valid (no invalid characters or repeated cards), each player's hand is in no specific order, and in each hand there is a clear winner.

How many hands does Player 1 win?

这道题真的是,非常多的判断,写起来相当繁琐。

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
#include<iostream>
#include<fstream>
#include<vector>
using namespace std;
 
vector<int> get_pairs(vector<int>);
 
class CARD {
private:
  const static int CARD_SIZE = 5;
  char card[CARD_SIZE][2];
  int get_card_rank();
  int get_high_card();
  bool is_royal_flush();
  bool is_straight_flush();
  bool is_four_of_a_kind();
  bool is_full_house();
  bool is_flush();
  bool is_straight();
  bool is_three_of_a_kind();
  bool is_two_pairs();
  bool is_one_pair();
  bool is_same_suit();
  vector<int> card_to_vector();
public:
  void show();
  friend int card_compare(CARD &x, CARD &y);
  friend ifstream &operator>>(ifstream &stream, CARD &obj);
};
 
int main() {
  ifstream in;
  in.open("poker.txt");
  if (!in.is_open()) {
    cout << "File is not open." << endl;
    return 1;
  }
 
  vector<int> res;
 
  while(!in.eof()) {
    CARD p1;
    CARD p2;
    in >> p1;
    in >> p2;
 
    int ans = card_compare(p1,p2);
    res.push_back(ans);
  }
  in.close();
 
  int cnt = 0;
 
  for (int i=0; i < res.size(); i++) {
    cnt += res[i];
  }
 
  cout << cnt << endl;
  return 0;
}
 
void CARD::show() {
  for (int i=0; i<CARD_SIZE; i++) {
    cout << (this->card)[i][0] << "\t" 
	 << (this->card)[i][1] << endl;
  }
}
 
int card_compare(CARD &x, CARD &y) {
  int high = 100;
  int xr = x.get_card_rank();
  int yr = y.get_card_rank();
 
  vector<int> xv = x.card_to_vector();
  vector<int> yv = y.card_to_vector();
 
  if (xr > yr) {
    return 1;
  } else if ( xr < yr ) {
    return 0;
  } else {
    if (x.is_full_house()) {
      vector<int> xp = get_pairs(xv);
      vector<int> xpp = get_pairs(xp);
      vector<int> yp = get_pairs(yv);
      vector<int> ypp = get_pairs(yp); 
      // xpp and ypp is the three of a kind.
      if (xpp[0] > ypp[0]) {
	return 1;
      } else if (xpp[0] < ypp[0]) {
	return 0;
      } else {
	if (xp[xp.size()-1] > yp[yp.size()-1]) { 
	  return 1;
	} else {
	  return 0;
	}
      }
    } else if (x.is_two_pairs()) {
      vector<int> xp = get_pairs(xv);
      vector<int> yp = get_pairs(yv);
      if (xp[1] > yp [1]) {
	return 1;
      } else if (xp[1] < yp[1]) {
	return 0;
      } else {
	if (xp[0] > yp[0]) {
	  return 1;
	} else if (xp[0] < yp[0]) {
	  return 0;
	} else {
	  int x_high, y_high;
	  for (int i=0; i < x.CARD_SIZE; i++) {
	    if( xv[i] != xp[0] && xv[i] != xp[1] ) {
	      x_high = xv[i];
	    } 
	    if( yv[i] != yp[0] && yv[i] != yp[1] ) {
	      y_high = yv[i];
	    }
	  }
	  if (x_high > y_high) {
	    return 1;
	  } else {
	    return 0;
	  }
	}
      }
    } else if (x.is_one_pair()) {
      vector<int> xp = get_pairs(xv);
      vector<int> yp = get_pairs(yv);
      if(xp[0] > yp[0]) {
	return 1;
      } else if (xp[0] < yp[0]) {
	return 0;
      } 
    } 
  }
  for (int i=(x.CARD_SIZE-1); i >= 0; i--) {
    if (xv[i] > yv[i]) {
      return 1;
    } else if (xv[i] == yv[i]) {
      continue;
    } else {
      return 0;
    }
  } 
}
 
ifstream &operator>>(ifstream &stream, CARD &obj) {
  for (int i=0; i < obj.CARD_SIZE; i++) {
    for (int j=0; j < 2; j++) {
      stream >> obj.card[i][j];
    }
  }
  return stream;
}
 
 
int CARD::get_card_rank() {
  int high = 100;
  if(this->is_royal_flush()) 
    return (high-1);
  if(this->is_straight_flush())
    return (high-2);
  if(this->is_four_of_a_kind())
    return (high-3);
  if(this->is_full_house()) 
    return (high-4);
  if(this->is_flush())
    return (high-5);
  if(this->is_straight())
    return (high-6);
  if(this->is_three_of_a_kind())
    return (high-7);
  if(this->is_two_pairs()) 
    return (high-8);
  if(this->is_one_pair())
    return (high-9);
 
  return (this->get_high_card());
}
 
bool CARD::is_royal_flush() {
  if (!this->is_same_suit())
    return 0;
  if (!this->is_straight())
    return 0;
  bool flag = 1;
  for (int i=0; i < this->CARD_SIZE; i++) {
    flag = ((this->card)[i][0] == 'T' ||
	    (this->card)[i][0] == 'J' ||
	    (this->card)[i][0] == 'Q' ||
	    (this->card)[i][0] == 'K' ||
	    (this->card)[i][0] == 'A');
    if (!flag) {
      break;
    }
  }
  return flag;
}
 
bool CARD::is_straight_flush() {
  if (!this->is_same_suit())
    return 0;
  return(this->is_straight());
}
 
 
bool CARD::is_four_of_a_kind() {
  vector<int> values = this->card_to_vector();
  if (values[0] != values[1]) {
    for (int i=2; i < this->CARD_SIZE; i++) {
      if (values[1] != values[i])
	return 0;
    }
  } else {
    for (int i=1; i < (this->CARD_SIZE-1); i++)
      if (values[0] != values[i])
	return 0;
  }
  return 1;
}
 
bool CARD::is_full_house() {
  vector<int> values = this->card_to_vector();
  if (values[0] == values[1] &&
      values[0] == values[2] &&
      values[3] == values[4])
    return 1;
  if (values[0] == values[1] &&
      values[2] == values[3] &&
      values[2] == values[4])
    return 1;
  return 0;
}
 
bool CARD::is_flush() {
  if(this->is_same_suit())
    return 1;
  return 0;
}
bool CARD::is_straight() {
  vector<int> values = this->card_to_vector();
  if ( (values[0] + 1) == values[1] &&
       (values[1] + 1) == values[2] &&
       (values[2] + 1) == values[3] &&
       (values[3] + 1) == values[4] )
    return 1;
  return 0;
}
 
bool CARD::is_three_of_a_kind() {
  if(this->is_four_of_a_kind())
    throw 1;
 
  vector<int> values = this->card_to_vector();
   if(values[0] == values[1] &&
     values[0] == values[2])
    return 1;
  if(values[1] == values[2] &&
     values[1] == values[3])
    return 1;
  if(values[2] == values[3] &&
     values[2] == values[4])
    return 1;
  return 0;
}
 
bool CARD::is_two_pairs() {
  if(this->is_three_of_a_kind())
    throw 1;
 
  if(this->is_one_pair()) {
    vector<int> values = this->card_to_vector();
    vector<int> p = get_pairs(values);
    if ( p.size() == 2 )
      return 1;
  }
  return 0;  
}
 
bool CARD::is_one_pair() {
  // this function test if it is has one pair.
  // not has and only has one pair.
  vector<int> values = this->card_to_vector();
  for (int i=0; i < this->CARD_SIZE; i++) {
    for (int j=i+1; j < this->CARD_SIZE; j++) {
      if (values[i] == values[j])
	return 1;
    }
  }
  return 0;
}
 
int CARD::get_high_card() {
  vector<int> values = this->card_to_vector();
  return(values[this->CARD_SIZE-1]);
}
 
bool CARD::is_same_suit() {
  char suit = (this->card)[0][1];
  for (int i=1; i < this->CARD_SIZE; i++) {
    if ( (this->card)[i][1] != suit) {
      return 0;
    }
  } 
  return 1;
}
 
vector<int> CARD::card_to_vector() {
  vector<int> values;
  for (int i=0; i < this->CARD_SIZE; i++) {
    switch((this->card)[i][0]) {
    case '2':
      values.push_back(2);
      break;
    case '3':
      values.push_back(3);
      break;
    case '4':
      values.push_back(4);
      break;
    case '5':
      values.push_back(5);
      break;
    case '6':
      values.push_back(6);
      break;
    case '7':
      values.push_back(7);
      break;
    case '8':
      values.push_back(8);
      break;
    case '9':
      values.push_back(9);
      break;
    case 'T':
      values.push_back(10);
      break;
    case 'J':
      values.push_back(11);
      break;
    case 'Q':
      values.push_back(12);
      break;
    case 'K':
      values.push_back(13);
      break;
    case 'A':
      values.push_back(14);
      break;
    }
  }
  sort(values.begin(), values.end());
  return values;
}
 
vector<int> get_pairs(vector<int> x) {
  vector<int> p;
  for (int i=0; i < x.size(); i++) {
    for (int j=i+1; j < x.size(); j++) {
      if(x[i] == x[j])
	p.push_back(x[i]);
    }
  }
  return p;
}

Related Posts


Viewing all articles
Browse latest Browse all 27

Trending Articles