2

我一直在潜伏并在这里找到大量信息,但是最近几天我被困住了,无法找到解决我的问题的帮助,所以我认为是 id post。

我有一些作业,我必须让我的数组的内容下拉到最后一行。如果我旋转网格,项目仍应下降到底行,如果我从底行吃掉一个对象,则该列中它上面的所有内容也应该下降。

任何帮助是极大的赞赏。

这是应该发生的事情的演示视频:

http://youtu.be/CB07vN-C_-Y

这是我到目前为止所拥有的:

`public class Assignment
{
// This method should return a *new copy* of
// the 2D cell matrix, with entries rotated clockwise
// The original matrix should not be changed
public static int[][] rotateClockwise(int[][] cells)
{  
    int w = cells.length;
    int h = cells[0].length;   
    int[][] matrix = new int[h][w];
    for (int i = 0; i < h; ++i) 
    {
        for (int j = 0; j < w; ++j) 
        {
            matrix[i][j] = cells[j][h - i - 1];
        }
    }
    return matrix;
}

// This method should return a *new copy* of
// the 2D cell matrix, with entries rotated anti-clockwise
// The original matrix should not be changed
public static int[][] rotateAnticlockwise(int[][] cells)
{
    int w = cells.length;
    int h = cells[0].length;
    int[][] matrix = new int[h][w];
    for (int i = 0; i < h; ++i) 
    {
        for (int j = 0; j < w; ++j) 
        {
            matrix[i][j] = cells[w - j - 1][i];
        }
    }
    return matrix;
}

// This method should return a *new copy* of the array, except
// that if there is a 0 that has a non-zero in the preceding
// slot in the array, then those two entries should be swapped
// See ProgrammingProject.pdf for an example
// The original array should not be changed
public static int[] dropOne(int[] column)
{  
            return column; // this will compile but gives the wrong result
}

}`
4

2 回答 2

0
for(int i  = 0; i < arrayWidth; i++) {
   boolean reachedZero = false;
   for( int j = 0; j < arrayHeight; j++) {
      if(array[i][j] == 1 && reachedZero == true) {
         while( j >=0 && array[i][j - 1] == 0) {
            array[i][j-1] = array[i][j];
            array[i][j] = 0;
            j--;
            reachedZero = false;
         }
         j--; // Maybe an error here, it's late
      if( array[i][j] == 0) {
      reachedZero = true;
      }
   }
}

这是 /learnprogramming 子 reddit 的一位可爱的 redditor (RankWeis) 发布的。 http://www.reddit.com/r/learnprogramming/comments/126597/java_help_needed_on_adding_a_gravity_effect_to/

于 2012-10-27T15:18:11.580 回答
0

我将列建模为Queue<Icon> col = new LinkedList<Icon>(); 这里有一个大纲,这里Queue<Segment>一个完整的例子。您可以在队列的头部(底部);如果它是空的,则从列到尾部(顶部)有一个块。Queue<Bauble>peek()remove()add()

附录:您可以从这个示例开始,删除getGray(),将布局更改为new GridLayout(0, 1)。然后,shuffle(list)您将循环队列而不是 。

于 2012-10-27T12:17:50.203 回答