0

我需要在 Yahtzee 中做一个计算 smallStraight 的方法(小直意味着你有 4 个骰子增加一……例如,1,2,3,4,6是一个小直)。现在我正试图克服当你对数组进行排序时可能会有重复数字的障碍。
例如,如果您滚动然后排序,您可能会得到一个1, 2, 2, 3, 4. 现在我基本上需要删除数组末尾的第二个 2。这是我的代码。请注意,这显然不适用于嵌套的四个循环。我只是想知道解决这个问题的最佳方法。

public int setSmallStraight(int[] die)
{
    if (!isSmallStraightUsed)
    {
      int counter = 0;
      boolean found = false;
      Arrays.sort(die);

      for (int i = 0; i < die.length - 1; i++)
      {
          if (counter == 3)
              found = true;

          if (die[i + 1] == die[i] + 1)
          {
              counter++;
          }
          else if (die[i + 1] == die[i])
          {
              continue;
          }
          else
          {
              counter = 0;
          }
      }

      if (found)
      {
         smallStraight = 30; 
      }
      else
      {
          smallStraight = 0;
      }
      return smallStraight;
    }
   else
        return 0;
   }
4

4 回答 4

1

让一个int counter计算数组中连续+1增加的次数怎么样?像这样的东西:

public boolean hasSmallStraight(int[] sortedValues) {
    int counter = 0;
    for (int i=0; i<sortedValues.length-1; i++) {
        if (counter == 3) return true;

        if (sortedValues[i+1] == sortedValues[i] + 1) {
            counter++;
        } else if (sortedValues[i+1] == sortedValues[i]) {
            continue;
        } else {
            counter = 0;
        }
    }

    return counter==3;
}

注意:这只适用于小直道

于 2013-03-26T01:42:08.473 回答
1

这将为您工作:

public static void main(String[] args) {
Integer[] items = {0, 4, 2, 2, 10, 5, 5, 5, 2};
System.out.println(customSort(Arrays.asList(items)));
}

public static Collection<Integer> customSort(List<Integer> die) {
Collections.sort(die);
Stack<Integer> numbas = new Stack<Integer>();
List<Integer> dupes = new ArrayList<Integer>();
numbas.push(die.get(0));
for (int i = 1; i < die.size(); i++) {
    if (!die.get(i).equals(numbas.peek())) {
    numbas.push(die.get(i));
    } else {
    dupes.add(die.get(i));
    }
}
numbas.addAll(dupes);
return numbas;
}

这产生了输出

[0, 2, 4, 5, 10, 2, 2, 5, 5]

添加您认为合适的错误检查和处理。

于 2013-03-26T01:49:01.217 回答
0

假设数组已排序,您可以使用如下算法。请阅读我发表的评论,希望它能清楚地解释它是如何工作的。同样作为预防措施 - 这绝不是最有效的实现,因此如果您的阵列很大,请考虑性能。

// Sequentially iterate array using 2 indices: i & j
// Initially i points to 1st element, j point to 2nd element
// The assumption is there's at least 2 element in the array.
// 'end' acts as a boundary limit to which element hasn't been checked
for(int i=0,j=1,end=array.length; j<end; ) {

    // If element pointed by i & j are equal, shift element pointed
    // by j to the end. Decrement the end index so we don't test element
    // that's already shifted to the back.
    // Also in this case we don't increment i & j because after shifting we
    // want to perform the check at the same location again (j would have pointed
    // to the next element)
    if(array[i] == array[j]) {

        // This for loop shifts element at j to the back of array
        for(int k=j; k<array.length-1; k++) {
            int tmp = array[k+1];
            array[k+1] = array[k];
            array[k] = tmp;
        }

        end--;

    // if element at i and j are not equal, check the next ones
    } else {
        i++;
        j++;
    }
}
于 2013-03-26T01:41:19.877 回答
0

尝试

    int[] a1 = { 1, 2, 2, 3, 4 };
    int[] a2 = new int[a1.length];
    int j = 0, k = 0;
    for (int i = 0; i < a1.length - 1; i++) {
        if (a1[i + 1] == a1[j]) {
            a2[k++] = a1[i + 1];
        } else {
            a1[++j] = a1[i + 1];
        }
    }
    System.arraycopy(a2, 0, a1, j + 1, k);
    System.out.println(Arrays.toString(a1));

输出

[1, 2, 3, 4, 2]
于 2013-03-26T04:01:43.570 回答