У меня проблемы с получением правильного решения онлайн-проблемы, когда я получаю больше случаев победы игрока 1, чем игрока 2. Не могли бы вы взглянуть на мой код и найти проблему, спасибо.
Я знаю, что этот код завершен, sh, но, пожалуйста, будьте любезны.
Я думаю, что может быть проблема в методе «две пары», потому что он рассматривает три в своем роде как две пары, но я не думал, что это вызовет какие-либо проблемы
public static boolean firstHandWinsArray(Card[] h1, Card[] h2){
//Straight Flushes
int a = getStraightFlush(h1);
int b = getStraightFlush(h2);
if(a > b){
return true;
}
if(a < b){
return false;
}
//Four of a kind check
a = getFourOfAKind(h1);
b = getFourOfAKind(h2);
if(a > b){
return true;
}
if(a < b){
return false;
}
//Full house check
Point p1 = getFullHouse(h1);
Point p2 = getFullHouse(h2);
if(p1.x > p2.x){
return true;
}
if(p1.x < p2.x){
return false;
}
if(p1.x == p2.x){
if(p1.y > p2.y){
return true;
}
if(p1.y < p2.y){
return false;
}
}
//Flush check
a = getFlush(h1);
b = getFlush(h2);
if(a > b){
return true;
}
if(a < b){
return false;
}
//Straight Check
a = getStraight(h1);
b = getStraight(h2);
if(a > b){
return true;
}
if(a < b){
return false;
}
//Three of a kind check
a = threeOfAKind(h1);
b = threeOfAKind(h2);
if(a > b){
return true;
}
if(a < b){
return false;
}
//Two pair check
p1 = twoPair(h1);
p2 = twoPair(h2);
int pair1High = Math.max(p1.x, p1.y);
int pair1Low = Math.min(p1.x, p1.y);
int pair2High = Math.max(p2.x, p2.y);
int pair2Low = Math.min(p2.x, p2.y);
if(pair1High > pair2High){
return true;
}
if(pair1High < pair2High){
return false;
}
if(pair1High == pair2High){
if(pair1Low > pair2Low){
return true;
}
if(pair1Low < pair2Low){
return false;
}
}
//Pair compare
a = highestPair(h1);
b = highestPair(h2);
if(a > b){
return true;
}
if(a < b){
return false;
}
//High card
return firstHandHigher(h1, h2);
}
public static int getStraightFlush(Card[] c){
int flush = getFlush(c);
if(flush > -1 && getStraight(c) > -1){
return flush;
}
return -1;
}
public static int getFourOfAKind(Card[] c){
int[] temp = new int[c.length];
for(int i = 0; i < c.length; i++){
for(int a = 0; a < c.length; a++){
if(c[a].getValue() == c[i].getValue()){
temp[i] = temp[i] + 1;
}
}
}
ArrayList<Integer> check = new ArrayList<Integer>();
for(int a: temp){
check.add(a);
}
if(check.contains(4)){
return c[check.indexOf(4)].getValue();
}
return -1;
}
public static Point getFullHouse(Card[] c){
int[] temp = new int[c.length];
for(int i = 0; i < c.length; i++){
for(int a = 0; a < c.length; a++){
if(c[a].getValue() == c[i].getValue()){
temp[i] = temp[i] + 1;
}
}
}
ArrayList<Integer> check = new ArrayList<Integer>();
for(int a: temp){
check.add(a);
}
if(check.contains(3) && check.contains(2)) {
return new Point(c[check.indexOf(3)].getValue(), c[check.indexOf(2)].getValue());
}
return new Point(-1, -1);
}
public static int getFlush(Card[] cards){
int suit = cards[0].getSuit();
int min = cards[0].getValue();
for(int i = 1; i < cards.length; i++){
if(cards[i].getValue() < min){
min = cards[i].getValue();
}
if(cards[i].getSuit() != suit){
return -1;
}
}
return min;
}
//returns starting value of straight if it is one or it returns -1; Must have at least 2 items in the array
public static int getStraight(Card[] c) {
ArrayList<Integer> temp = new ArrayList<>();
for(int i = 0; i < c.length; i++){
temp.add(c[i].getValue());
}
int min = 99;
for(int a: temp){
if(a < min){
min = a;
}
}
for(int i = 0; i < temp.size(); i++){
if(!temp.contains(min + i)) {
return -1;
}
}
return min;
}
//Returns value of three pair in hand if there is none it returns -1
public static int threeOfAKind(Card[] cards){
for(int i = 0; i < cards.length; i++){
for(int n = i+1; n < cards.length; n++){
if(cards[i].getValue() == cards[n].getValue()){
for(int x = n+1; x < cards.length; x++){
if(cards[x].getValue() == cards[n].getValue()){
return cards[x].getValue();
}
}
}
}
}
return -1;
}
//Integer value of value of highest pair in the hand is returned
public static int highestPair(Card[] c1) {
int highest = 0;
for(int i = 0; i < c1.length; i++){
for(int n = i+1; n < c1.length; n++){
if(c1[i].getValue() == c1[n].getValue() && c1[i].getValue() > highest){
highest = c1[i].getValue();
}
}
}
return highest;
}
//A point containing one pair's value in Point.x and the other in Point.y ** as This method is somewhat faulty because
//Three of a kind is considered a two pair but this is irrelevant because of the ranking in the poker hands
public static Point twoPair(Card[] c1) {
Point a = new Point(-1, -1);
for(int i = 0; i < c1.length; i++){
for(int n = i+1; n < c1.length; n++){
if(c1[i].getValue() == c1[n].getValue()){
if(c1[n].getValue() > a.x || c1[n].getValue() > a.y){
if(a.x <= a.y){
a.x = c1[n].getValue();
}
else{
a.y = c1[n].getValue();
}
}
}
}
}
return a;
}
public static boolean firstHandHigher(Card[] h1, Card[] h2){
Card[] copy1 = h1.clone();
Card[] copy2 = h2.clone();
int highVal1 = -1;
int highIndex1 = 0;
int highVal2 = -1;
int highIndex2 = 0;
while(highVal1 == highVal2) {
for(int i = 0; i < copy1.length; i++){
if(copy1[i].getValue() > highVal1){
highVal1 = copy1[i].getValue();
highIndex1 = i;
}
if(copy2[i].getValue() > highVal2){
highVal2 = copy2[i].getValue();
highIndex2 = i;
}
}
if(highVal1 > highVal2){
return true;
}
if(highVal1 < highVal2){
return false;
}
else {
copy1[highIndex1] = new Card("1D");
copy2[highIndex2] = new Card("1D");
highVal1 = -1;
highVal2 = -1;
}
}
return highVal1 > highVal2;
}
}
class Card{
//2, 3, 4, 5, 6, 7, 8, 9, 10, Jack (11), Queen (12), King (13), Ace (14).
//Diamonds (0), Hearts (1), Spades (2), Clubs (3)
private int value;
private int suit;
public Card(String cardString) {
String suit = cardString.substring(cardString.length()-1);
switch(suit){
case "D":
this.suit = 0;
break;
case "H":
this.suit = 1;
break;
case "S":
this.suit = 2;
break;
case "C":
this.suit = 3;
break;
}
String value = cardString.substring(0, cardString.length()-1);
try {
this.value = Integer.parseInt(value);
}
catch(Exception e){
switch(value){
case "J":
this.value = 11;
break;
case "Q":
this.value = 12;
break;
case "K":
this.value = 13;
break;
case "A":
this.value = 14;
break;
}
}
}
public int getSuit(){
return suit;
}
public int getValue(){
return value;
}
}
***