在布尔[] []数组中的Java五中

时间:2018-02-01 19:25:17

标签: java methods poker

我有识别冲洗和直道的问题。我在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;

        }

工作直线方法,但一步一步写下

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;

}

2 个答案:

答案 0 :(得分:1)

对于它的价值,这是使用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;
}

答案 1 :(得分:0)

类似于以下内容

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;
}