Skip to content
Advertisement

Is it possible to decrement counter within a condition?

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

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement