能否在一个条件下减少计数器?

问题描述 投票:0回答:1

我试图在分析一手扑克牌时检测到一个jokerStraightFlush.我需要添加一个功能,使这手牌能发挥作用。8S JK 6S JK 4S. 该 JK 是开玩笑的。我使用的代码逻辑与 https:/www.codeproject.comArticles38821Make-a-poker-hand-evalutator-in-Java.

cardsTable 表示牌局中的牌的等级分布,这个数组的每一个元素都代表该等级的牌的数量(从1到13,1为A)。这个数组的每一个元素都代表该等级牌的数量(从1到13,1为A)。所以对于 8S JK 6S JK 4S,其分布情况将是

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

注:位置1为王牌 (因为它更简单)

我需要找到一种方法来检测 cardsTable[i] == 1 失败,并减少jokers的使用量(numberOfJokers)来检测jokerStraightFlush,因为在这段不完整的代码中。numberOfJokers 不递减,我不知道怎么写得好看。我所做的是检查这个等级的牌是否存在。cardsTable[i] == 1 或是否是小丑......但我不知道如何检查其他连续排名中的小丑。

我不知道我是否清楚,这是一个扭曲的情况......如果我没有,请告诉我。

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

我也有这样的代码,但我不知道如何检测第一个条件是否失败,以便我可以减少剩余的joker数量。

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;
                }
            }
        }
    }
}
}
java algorithm if-statement conditional-statements poker
1个回答
1
投票

由于 "短路 "行为,你不需要 detect 左操作数 || 导致 true:如果左边的操作数是 false,只有。(cardsTable[i + c] == 1 || numberOfJokers-- > 0) 才行;注意(numberOfJokers-- > 0 || cardsTable[i + c] == 1) 不会。

无论这种守则是否 可维护可读 是一个独立的考虑因素,正如问题和解决方案的整体方法的质量一样。


1
投票

从概念上讲,你只需要5个槽中有3个等于1,另外2个等于0,你可以这样做。

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

或者,也许一个更简单的方法是在卡牌表数组中增加一个位置 来表示jokers的数量.


0
投票

Nvm 我在我的条件里用了一个函数......我不知道这是否是正确的方法,但对我来说是符合逻辑的 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;
    }
© www.soinside.com 2019 - 2024. All rights reserved.