Search code examples
javamethodspoker

Poker five in Java in boolean[][] array


I have problem with recognizing flushes and straights. I operate in 2D boolean array and it must be a boolean array nothing else. Could somebody help me in writing this method?

I already have logically incorrect methods, but I don't know how to solve it:

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;

        }

Working Straight method but step by step writed

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;

}

Solution

  • something like the following

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