0

i am trying to detect a jokerStraightFlush when analysing a poker hand. I need to add a feature where this hand works 8S JK 6S JK 4S. The JK are jokers. I am using the exact same code logic as https://www.codeproject.com/Articles/38821/Make-a-poker-hand-evalutator-in-Java.

cardsTable represents the distribution of the Card ranks present in the hand. Each element of this array represents the amount of card of that rank(from 1 to 13, 1 being ace) present in the hand. So for 8S JK 6S JK 4S, the distribution would be

0 0 0 0 1 0 1 0 1 0 0 0 0 0

note that the position 1 is for ace (because it's simpler)

I need to find a way to detect if cardsTable[i] == 1 failed and decrement the amount of jokers used (numberOfJokers) to detect a jokerStraightFlush because in this incomplete piece of code, numberOfJokers dont decrement and i dont know how to write it in a nice way. What i do here is i check if the card at this rank exists cardsTable[i] == 1 or if its a joker... but i dont know how to check for a joker in the others consecutive rankings

I dont know if i'm clear, it's a twisted situation.. if i'm not let me know.

straight = false; // assume no straight
int numberOfJokers = 2; //(the maximum number of jokers in a hand is 2)
for (int i = 1; i <= 9; i++) { // can't have straight with lowest value of more than 10
numberOfJokers = 2 //reset the number of jokers available after each loop
    if ((cardsTable[i] == 1 || numberOfJokers > 0) &&
        (cardsTable[i + 1] == 1 || numberOfJokers > 0) &&
        (cardsTable[i + 2] == 1 || numberOfJokers > 0) &&
        (cardsTable[i + 3] == 1 || numberOfJokers > 0) &&
        (cardsTable[i + 4] == 1 || numberOfJokers > 0)) {
        straight = true;
        break;
    }
}

I also have this code but i don't know how to detect if the first condition failed so i can decrement the number of jokers remaining.

for (int i = 1; i <= 9; i++) {
numberOfJokers = 2
if (cardsTable[i] == 1 || numberOfJokers>0) {
    if (cardsTable[i + 1] == 1 || numberOfJokers>0) {
        if (cardsTable[i + 2] == 1 || numberOfJokers > 0) {
            if (cardsTable[i + 3] == 1 || numberOfJokers > 0) {
                if (cardsTable[i + 4] == 1 || numberOfJokers > 0) {
                    straight = true;
                    break;
                }
            }
        }
    }
}
}
Lynn
  • 121
  • 8
  • 25

3 Answers3

1

Conceptually, you just need 3 of the 5 slots to be equal to 1, and the other 2 equal to 0. You could do something like this:

for (int i = 1; i <= 9; i++) { 
    boolean straight = true;
    int numberOfJokers = 2;
    for (int j = i; j <= i + 5; j++) {  // I used a for loop instead of writing the statement 5 times
        if (cardsTable[j] > 1) {  // Can't have straight if more than one of a kind
           straight = false;
        }
        if (cardsTable[j] == 0) {
            numberOfJokers--;
        }
    }
    if (numberOfJokers >= 0 && straight == true) {
        break;
    }
}

Alternatively, maybe a simpler way would be to add a position in the cardsTable array indicating the number of jokers.

Diana Shao
  • 61
  • 1
  • 8
1

Due to "short-circuiting" behaviour, you don't need to detect the left operand of a || resulted in true: the right operand gets evaluated if the left one was false, only.
(cardsTable[i + c] == 1 || numberOfJokers-- > 0) would work; note that (numberOfJokers-- > 0 || cardsTable[i + c] == 1) would not.

Whether or not such code is maintainable or readable is an independent consideration, as is the quality of the overall approach to problem and solution.

greybeard
  • 2,249
  • 8
  • 30
  • 66
  • That said, advancing a control variable's value in the condition of a loop is common enough to render it *readable*. – greybeard May 15 '20 at 07:39
0

nvm i used a function inside my conditions.. i don't know if it's the right way but it's logic to me x)

straight = false; // assume no straight
for (int i = 1; i <= 9; i++) // can't have straight with lowest value of more than 10
{
            remainingJokers=jokers;
            System.out.println("resetjokers : "+jokers);
            if (cardsTable[i] == 1 || useJoker()) {
                if (cardsTable[i + 1] == 1 || useJoker()) {
                    if (cardsTable[i + 2] == 1 || useJoker()) {
                        if (cardsTable[i + 3] == 1 || useJoker()) {
                            if (cardsTable[i + 4] == 1 || useJoker()) {
                                System.out.println("straight");
                                straight = true;
                                topStraightValue = i + 4; // 4 above bottom value
                                break;

                            }

                        }

                    }
                }

            }
        }
}
    private boolean useJoker() {
        int remaining = remainingJokers;//remainingJokers is a global variable
        remainingJokers--;
        return remaining>0;
    }
Lynn
  • 121
  • 8
  • 25