2

I've been lurking and found heaps of great information form here, however the last few days I have been stuck and haven't been able to find help with my issue so I thought id post.

I have some homework and I have to make the contents of my array drop down to the bottom row. If i rotate the grid the items should still drop down to the bottom row and if i eat an object from the bottom row, everything above it in that column should drop down too.

Any help is greatly appreciated.

Here is a demo video of what should happen:

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

This is what i have so far:

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

}`
Enigma
  • 21
  • 3
  • 1
    possible duplicate of [java falling matrix code (like the movie)](http://stackoverflow.com/questions/4703574/java-falling-matrix-code-like-the-movie) – trashgod Oct 27 '12 at 02:03
  • I saw that last night, its a bit too complex for my issue. the objects in my array called and i only want it to drop down if there's an empty space below it. – Enigma Oct 27 '12 at 02:43
  • Maybe something like [this](http://stackoverflow.com/a/11928255/230513) or [this](http://stackoverflow.com/a/3078354/230513)? – trashgod Oct 27 '12 at 03:25
  • Im looking up tetris clones, the sliding games with swings is sort of on the right track, im trying to figure out how to just get the contents of the column to slide down to the bottom if there's an empty space underneath it. – Enigma Oct 27 '12 at 04:49

2 Answers2

0

I'd model a column as a Queue<Icon> col = new LinkedList<Icon>(); there's an outline here for Queue<Segment> and a complete example here for Queue<Bauble>. You can peek() at the head (bottom) of the queue; if it's empty, you remove() a block from the column and add() it to the tail (top).

Addendum: You might start with this example, drop the getGray(), change the layout to new GridLayout(0, 1). Then, instead of shuffle(list), you'd cycle the queue.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • You sir are a god among men! thank you so much for your help i really appreciate it. – Enigma Oct 27 '12 at 12:19
  • Ok i have tried this and no good because this method is called in another class. I know that someone that knows what they are doing will take one look and know what to do in a matter of minutes. Ill share the package if anyone could take a look and possibly see the structure of the classes. The best i could get it to do was eat the cherry but instead of the above elements in that column falling to the bottom, it would add new elements to the top of the column. http://www.FilesOverMiles.com/3d665f3a0b844a5f974b65177ed771fe – Enigma Oct 27 '12 at 12:40
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;
      }
   }
}

This was posted by a lovely redditor (RankWeis) from the /learnprogramming sub-reddit. http://www.reddit.com/r/learnprogramming/comments/126597/java_help_needed_on_adding_a_gravity_effect_to/

Enigma
  • 21
  • 3