-2

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;

}
Jedyn
  • 1
  • 5
  • What are the exact errors that you encounter when you run the code? I am guessing that you are encountering an `ArrayIndexOutOfBounds` error in your if statements but I just want to be sure with it. – Whooper Feb 01 '18 at 19:31
  • I don't have errors, it's logical problem because we can have cards like : allcards[1][1] / [1][3] / [1][5] / [1][6] / [1][9] = true; and both methods return false – Jedyn Feb 01 '18 at 19:33
  • Also, what does it mean if `hand[i][j] == true`? What makes the value of a particular element of the array `true` or `false`? – Whooper Feb 01 '18 at 19:33
  • hand[i][j] == true <- there is problem we can have hand[i][j] and next card in flush can be hand[i][j+3] and my if return false, simple example allcards[1][1] / [1][3] / [1][5] / [1][6] / [1][9] = true; – Jedyn Feb 01 '18 at 19:35
  • I'm guessing that ``hand[i][j] == true`` means the card is in the player hand. – Guillaume Poussel Feb 01 '18 at 19:47
  • right, and i - color j - value, we have 4 colors and 13 values – Jedyn Feb 01 '18 at 19:52
  • Straight: Brute force way is to step from j=0 to 8 and always check consecutive 5 = 9*5 = 45 if statements. There is a dynamic programming solution which only steps j=0 to 12 and no extra check of 5 consecutive. This is a small problem so no need for DP? I can post if interested. – Ian Mc Feb 01 '18 at 20:02
  • Solved by Abdul Ahad, thanks! :) – Jedyn Feb 01 '18 at 20:09

2 Answers2

1

For what it is worth, here is the most efficient way to solve a straight, using 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;
}
Ian Mc
  • 5,656
  • 4
  • 18
  • 25
0

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;
}
Abdul Ahad
  • 826
  • 8
  • 16