boolean[][] 数组中 Java 中的扑克牌五
Poker five in Java in boolean[][] array
我无法识别同花和顺子。我在 2D boolean array
操作,它必须是 boolean array
没有别的。有人可以帮我写这个方法吗?
我已经有逻辑上不正确的方法,但我不知道如何解决:
public static Boolean isFlush(boolean[][] hand) { // 5 cards same color
boolean found = false;
for (int i = 0; i < 4; i++) {
for(int j = 0; j < 9; j++){
if (hand[i][j] == true && hand[i][j+1] == true && hand[i][j+2] == true && hand[i][j+3] == true && hand[i][j+4] == true) {
found = true;
}
}}
System.out.println("Found from flush: " + found);
return found;
}
public static Boolean isStraight(boolean[][] hand) { // straight patter example 4,5,6,7,8
boolean found = false;
int pom = 0;
for(int i = 0; i<4; i++) {
for (int j = 0; j < 9; j++) {
if (hand[i][j] == true || hand[i][j+1] == true || hand[i][j+2] == true || hand[i][j+3] == true || hand[i][j+4])
pom++;
// System.out.println("straight value: "+i);
}
}
return pom==5;
}
Work Straight 方法但一步一步写
public static Boolean isStraight(boolean[][] hand) { // straight patter example 4,5,6,7,8
boolean found = false;
for (int j = 0; j < 9; j++) {
if ((hand[0][j] == true || hand[1][j] == true || hand[2][j] == true || hand[3][j] == true)
&& (hand[0][j+1] == true || hand[1][j+1] == true || hand[2][j+1] == true || hand[3][j+1] == true)
&& (hand[0][j+2] == true || hand[1][j+2] == true || hand[2][j+2] == true || hand[3][j+2] == true)
&& (hand[0][j+3] == true || hand[1][j+3] == true || hand[2][j+3] == true || hand[3][j+3] == true)
&& (hand[0][j+4] == true || hand[1][j+4] == true || hand[2][j+4] == true || hand[3][j+4] == true ))
found = true;
}
return found;
}
类似下面的内容
public static Boolean isFlush(boolean[][] hand) { // 5 cards same color
for (int i = 0; i < 4; i++) {
int count = 0;
for(int j = 0; j < 13; j++) {
if(hand[i][j]) {
count++;
}
}
if(count == 5) {
return true;
}
}
return false;
}
public static Boolean isStraight(boolean[][] hand) { // straight patter
for (int i = 0; i < 9; i++) {
int count = 0;
for(int j = 0; j < 5; j++) {
for(int k = 0; k < 4; k++) {
if (hand[k][i + j]) {
count++;
break;
}
}
}
if(count == 5) {
return true;
}
}
return false;
}
对于它的价值,这里是使用 DP
解决直线的最有效方法
public static boolean isStraight(boolean[][] hand) {
int[] straightCounter = new int[13];
for (int j=0; j<13; j++) {
boolean isCard = hand[0][j] || hand[1][j] || hand[2][j] || hand[3][j];
if (isCard) {
if (j==0)
straightCounter[j]=1;
else
straightCounter[j]=straightCounter[j-1]+1;
if (straightCounter[j] == 5)
return true;
if (j==12 && straightCounter[j] == 4 && straightCounter[0] == 1)
return true; // the 10/J/Q/K/A scenario
}
}
return false;
}
我无法识别同花和顺子。我在 2D boolean array
操作,它必须是 boolean array
没有别的。有人可以帮我写这个方法吗?
我已经有逻辑上不正确的方法,但我不知道如何解决:
public static Boolean isFlush(boolean[][] hand) { // 5 cards same color
boolean found = false;
for (int i = 0; i < 4; i++) {
for(int j = 0; j < 9; j++){
if (hand[i][j] == true && hand[i][j+1] == true && hand[i][j+2] == true && hand[i][j+3] == true && hand[i][j+4] == true) {
found = true;
}
}}
System.out.println("Found from flush: " + found);
return found;
}
public static Boolean isStraight(boolean[][] hand) { // straight patter example 4,5,6,7,8
boolean found = false;
int pom = 0;
for(int i = 0; i<4; i++) {
for (int j = 0; j < 9; j++) {
if (hand[i][j] == true || hand[i][j+1] == true || hand[i][j+2] == true || hand[i][j+3] == true || hand[i][j+4])
pom++;
// System.out.println("straight value: "+i);
}
}
return pom==5;
}
Work Straight 方法但一步一步写
public static Boolean isStraight(boolean[][] hand) { // straight patter example 4,5,6,7,8
boolean found = false;
for (int j = 0; j < 9; j++) {
if ((hand[0][j] == true || hand[1][j] == true || hand[2][j] == true || hand[3][j] == true)
&& (hand[0][j+1] == true || hand[1][j+1] == true || hand[2][j+1] == true || hand[3][j+1] == true)
&& (hand[0][j+2] == true || hand[1][j+2] == true || hand[2][j+2] == true || hand[3][j+2] == true)
&& (hand[0][j+3] == true || hand[1][j+3] == true || hand[2][j+3] == true || hand[3][j+3] == true)
&& (hand[0][j+4] == true || hand[1][j+4] == true || hand[2][j+4] == true || hand[3][j+4] == true ))
found = true;
}
return found;
}
类似下面的内容
public static Boolean isFlush(boolean[][] hand) { // 5 cards same color
for (int i = 0; i < 4; i++) {
int count = 0;
for(int j = 0; j < 13; j++) {
if(hand[i][j]) {
count++;
}
}
if(count == 5) {
return true;
}
}
return false;
}
public static Boolean isStraight(boolean[][] hand) { // straight patter
for (int i = 0; i < 9; i++) {
int count = 0;
for(int j = 0; j < 5; j++) {
for(int k = 0; k < 4; k++) {
if (hand[k][i + j]) {
count++;
break;
}
}
}
if(count == 5) {
return true;
}
}
return false;
}
对于它的价值,这里是使用 DP
解决直线的最有效方法public static boolean isStraight(boolean[][] hand) {
int[] straightCounter = new int[13];
for (int j=0; j<13; j++) {
boolean isCard = hand[0][j] || hand[1][j] || hand[2][j] || hand[3][j];
if (isCard) {
if (j==0)
straightCounter[j]=1;
else
straightCounter[j]=straightCounter[j-1]+1;
if (straightCounter[j] == 5)
return true;
if (j==12 && straightCounter[j] == 4 && straightCounter[0] == 1)
return true; // the 10/J/Q/K/A scenario
}
}
return false;
}