【问题标题】:Is it possible to decrement counter within a condition?是否可以在条件内递减计数器?
【发布时间】:2020-05-15 04:05:19
【问题描述】:

我在分析一手牌时试图检测一个小丑同花顺。 我需要在此手工作的地方添加一个功能8S JK 6S JK 4SJK 是小丑。我使用与https://www.codeproject.com/Articles/38821/Make-a-poker-hand-evalutator-in-Java 完全相同的代码逻辑。

cardsTable 代表手牌中牌位的分布。该数组的每个元素代表手中的牌的数量(从 1 到 13,1 是 ace)。所以对于8S JK 6S JK 4S,分布是

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

注意位置 1 是 ace(因为它更简单)

我需要找到一种方法来检测 cardsTable[i] == 1 是否失败并减少使用的小丑数量 (numberOfJokers) 来检测小丑StraightFlush,因为在这段不完整的代码中,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;
    }
}

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

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】:

    由于"short-circuiting" behaviour,您不需要将|| 的左操作数detect 导致true:如果左操作数是false,则仅计算右操作数。
    (cardsTable[i + c] == 1 || numberOfJokers-- &gt; 0) 会工作;注意 (numberOfJokers-- &gt; 0 || cardsTable[i + c] == 1) 不会。

    此类代码是否可维护可读是一个独立的考虑因素,以及解决问题和解决方案的整体方法的质量.

    【讨论】:

    • 也就是说,在循环条件下推进控制变量的值很常见,足以使其可读
    【解决方案2】:

    从概念上讲,您只需要 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;
        }
    }
    

    或者,也许更简单的方法是在cardsTable 数组中添加一个位置,指示小丑的数量。

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-04-08
        • 1970-01-01
        • 1970-01-01
        • 2021-07-08
        • 1970-01-01
        • 1970-01-01
        • 2014-05-26
        相关资源
        最近更新 更多