-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathHandParser.java
More file actions
63 lines (49 loc) · 2.22 KB
/
HandParser.java
File metadata and controls
63 lines (49 loc) · 2.22 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
package problem54;
/*
Poker hands
Problem 54
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.
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?
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class HandParser {
private static String file = "src/problem54/poker.txt";
public static void main(String[] args) throws FileNotFoundException {
Scanner scan = new Scanner(new File(file));
int playerOneWins = 0;
while (scan.hasNextLine()) {
String line = scan.nextLine();
Hand first = new Hand(line.substring(0, 14).split(" "));
Hand second = new Hand(line.substring(15).split(" "));
if (first.compareTo(second) < 0) {
playerOneWins++;
}
}
System.out.println(playerOneWins);
}
}