public class BallMain {
public static void main(String[] args) {
int result = GroupComparer.CompareBallGroup(" AR CY CG AG 3Y ",
" 5W DG DY 5R 3W ");
System.out.println("result:" + result);
}
}
import java.util.Arrays;
import java.util.regex.Pattern;
public class BallGroup {
public enum BallGroupPriority {
LargestFlush("LargestFlush", 1), StraightFlush("StraightFlush", 2), FourofaKind(
"FourofaKind", 3), FullHouse("FullHouse", 4), Flush("Flush", 5), Straight(
"Straight", 6), ThreeofAKind("ThreeofAKind", 7), TwoPairs("TwoPairs", 8), OnePair(
"OnePair", 9), Highest("Highest", 10), Unknown("Unknown", 11);
private String name;
private int priority;
private BallGroupPriority(String name, int priority) {
this.name = name;
this.priority = priority;
}
public static String getName(int priority) {
for (BallGroupPriority ballGroupPriority : BallGroupPriority.values()) {
if (ballGroupPriority.getPriority() == priority) {
return ballGroupPriority.getName();
}
}
return Unknown.getName();
}
public static int getPriority(String name) {
if (name == null) {
return Unknown.getPriority();
}
for (BallGroupPriority ballGroupPriority : BallGroupPriority.values()) {
if (name.equals(ballGroupPriority.getName())) {
return ballGroupPriority.getPriority();
}
}
return Unknown.getPriority();
}
public String getName() {
return name;
}
public int getPriority() {
return priority;
}
}
private String balls[];
public static BallGroup getInstance(String originBalls) {
if (originBalls == null) {
return null;
}
String preBalls = originBalls.trim();
preBalls = preBalls.replaceAll(" {2,}", " ");
System.out.println("content:" + preBalls);
Pattern pattern = Pattern.compile("^[1-9 A-D][YRGW]( [1-9 A-D][YRGW]){4}$");
boolean match = pattern.matcher(preBalls).matches();
System.out.println("match:" + match);
BallGroup ballGroup = null;
if (match) {
ballGroup = new BallGroup(preBalls);
}
return ballGroup;
}
private BallGroup(String content) {
balls = content.split(" ");
Arrays.sort(balls);
}
public void print() {
for (int i = 0; i < 5; i++) {
System.out.print(balls[i] + " ");
}
System.out.println();
}
public int getValue(int index) {
char content = balls[index].charAt(0);
int value = 1;
if (content <= '9' && content >= '1') {
value = content - '0';
} else if (content >= 'A' && content <= 'D') {
value = content - 'A' + 10;
} else {
System.err.println("Unknonw value:" + content);
}
return value;
}
public char getColor(int index) {
char color = balls[index].charAt(1);
return color;
}
}
public class GroupComparer {
public static int CompareBallGroup(String ballgroup1, String ballgroup2) {
PatternFinder patternFinder = new PatternFinder();
String ballKind1 = patternFinder.FindPattern(ballgroup1);
String ballKind2 = patternFinder.FindPattern(ballgroup2);
System.out.println("ballKind1:" + ballKind1 + ", ballKind2:" + ballKind2);
int priority1 = BallGroup.BallGroupPriority.getPriority(ballKind1);
int priority2 = BallGroup.BallGroupPriority.getPriority(ballKind2);
System.out.println("priority1:" + priority1 + ", priority2:" + priority2);
if (priority1 < priority2) {
return 1;
} else if (priority1 == priority2) {
return CompareBallGroupOnPriority(ballgroup1, ballgroup2, priority1);
} else {
return -1;
}
}
public static int CompareBallGroupOnPriority(String ballgroup1, String ballgroup2,
int priority) {
BallGroup balls1 = BallGroup.getInstance(ballgroup1);
BallGroup balls2 = BallGroup.getInstance(ballgroup2);
int value1 = -1;
int value2 = -2;
switch (priority) {
case 1:
return 0;
case 2:
value1 = balls1.getValue(0);
value2 = balls2.getValue(0);
break;
case 4:
value1 = balls1.getValue(2);
value2 = balls2.getValue(2);
break;
case 5:
for (int i = 4; i >= 0; i--) {
value1 = balls1.getValue(i);
value2 = balls2.getValue(i);
if (value1 != value2) {
break;
}
}
break;
case 10:
value1 = balls1.getValue(4);
value2 = balls2.getValue(4);
break;
default:
return 0;
}
System.out.println("value1:" + value1 + ", value2:" + value2);
if (value1 > value2) {
return 1;
} else if (value1 == value2) {
return 0;
} else {
return -1;
}
}
}
public class PatternFinder {
private String content;
public String FindPattern(String ballGroup) {
this.content = ballGroup;
return judge();
}
private boolean checkValid() {
if (content == null) {
return false;
}
content = content.trim();
content = content.replaceAll(" {2,}", " ");
System.out.println("content:" + content);
Pattern pattern = Pattern.compile("^[1-9 A-D][YRGW]( [1-9 A-D][YRGW]){4}$");
boolean match = pattern.matcher(content).matches();
System.out.println("match:" + match);
return match;
}
private String judge() {
if (!checkValid()) {
return BallGroup.BallGroupPriority.Unknown.getName();
}
String pattern;
BallGroup ballGroup = BallGroup.getInstance(content);
if (judgeLargestFlush(ballGroup)) {
pattern = BallGroupPriority.LargestFlush.getName();
} else if (judgeStraightFlush(ballGroup)) {
pattern = BallGroupPriority.StraightFlush.getName();
} else if (judgeFourofaKind(ballGroup)) {
pattern = BallGroupPriority.FourofaKind.getName();
} else if (judgeFullHouse(ballGroup)) {
pattern = BallGroupPriority.FullHouse.getName();
} else if (judgeFlush(ballGroup)) {
pattern = BallGroupPriority.Flush.getName();
} else if (judgeStraight(ballGroup)) {
pattern = BallGroupPriority.Straight.getName();
} else if (judgeThreeofAKind(ballGroup)) {
pattern = BallGroupPriority.ThreeofAKind.getName();
} else if (judgeTwoPairs(ballGroup)) {
pattern = BallGroupPriority.TwoPairs.getName();
} else if (judgeOnePair(ballGroup)) {
pattern = BallGroupPriority.OnePair.getName();
} else {
pattern = BallGroupPriority.Highest.getName();
}
System.out.println("pattern:" + pattern);
return pattern;
}
private boolean judgeLargestFlush(final BallGroup ballGroup) {
// System.out.println("judgeLargestFlush()");
int lowValue = ballGroup.getValue(0);
System.out.println("lowValue:" + lowValue);
if (lowValue != 9) {
return false;
}
return judgeStraightFlush(ballGroup);
}
private boolean judgeStraightFlush(final BallGroup ballGroup) {
// System.out.println("judgeStraightFlush()");
char color = ballGroup.getColor(0);
if (color != ballGroup.getColor(1) || color != ballGroup.getColor(2)
|| color != ballGroup.getColor(3) || color != ballGroup.getColor(4)) {
return false;
}
int lowValue = ballGroup.getValue(0);
if (ballGroup.getValue(1) != lowValue + 1
|| ballGroup.getValue(2) != lowValue + 2
|| ballGroup.getValue(3) != lowValue + 3
|| ballGroup.getValue(4) != lowValue + 4) {
return false;
}
return true;
}
private boolean judgeFourofaKind(final BallGroup ballGroup) {
// System.out.println("judgeFourofaKind()");
int lowValue = ballGroup.getValue(2);
if (lowValue != ballGroup.getValue(1) || lowValue != ballGroup.getValue(3)) {
return false;
}
if (lowValue == ballGroup.getValue(0) || lowValue == ballGroup.getValue(4)) {
return true;
}
return false;
}
private boolean judgeFullHouse(final BallGroup ballGroup) {
// System.out.println("judgeFullHouse()");
int middleValue = ballGroup.getValue(2);
if (ballGroup.getValue(0) != ballGroup.getValue(1)
|| ballGroup.getValue(3) != ballGroup.getValue(4)) {
return false;
}
return middleValue == ballGroup.getValue(0)
|| middleValue == ballGroup.getValue(4);
}
private boolean judgeFlush(final BallGroup ballGroup) {
// System.out.println("judgeFlush()");
int color = ballGroup.getColor(0);
return color == ballGroup.getColor(1) && color == ballGroup.getColor(2)
&& color == ballGroup.getColor(3) && color == ballGroup.getColor(4);
}
private boolean judgeStraight(final BallGroup ballGroup) {
// System.out.println("judgeStraight()");
int lowValue = ballGroup.getValue(0);
return ballGroup.getValue(1) == lowValue + 1
&& ballGroup.getValue(2) == lowValue + 2
&& ballGroup.getValue(3) == lowValue + 3
&& ballGroup.getValue(4) == lowValue + 4;
}
private boolean judgeThreeofAKind(final BallGroup ballGroup) {
System.out.println("judgeThreeofAKind()");
ballGroup.print();
int middleValue = ballGroup.getValue(2);
return (ballGroup.getValue(0) == middleValue && ballGroup.getValue(1) == middleValue)
|| (ballGroup.getValue(3) == middleValue && ballGroup.getValue(4) == middleValue);
}
private boolean judgeTwoPairs(final BallGroup ballGroup) {
// System.out.println("judgeTwoPairs()");
boolean front = ballGroup.getValue(0) == ballGroup.getValue(1)
&& ballGroup.getValue(2) == ballGroup.getValue(3);
boolean back = ballGroup.getValue(1) == ballGroup.getValue(2)
&& ballGroup.getValue(3) == ballGroup.getValue(4);
return front || back;
}
private boolean judgeOnePair(final BallGroup ballGroup) {
// System.out.println("judgeOnePair()");
return ballGroup.getValue(0) == ballGroup.getValue(1)
|| ballGroup.getValue(1) == ballGroup.getValue(2)
|| ballGroup.getValue(2) == ballGroup.getValue(3)
|| ballGroup.getValue(3) == ballGroup.getValue(4);
}
}