繁体   English   中英

如何获取一列的内容以向下滑动到7x7 2D数组的底部行?

[英]How to get the contents of a column to slide down to the bottom row of my 7x7 2D array?

我一直在潜伏,在这里找到了大量有用的信息,但是最近几天我一直受困,无法为我的问题寻求帮助,所以我想到了身份证件。

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

任何帮助是极大的赞赏。

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

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
}

}`

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

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

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/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM