Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   [java]Moving values in 2d array (https://thetfp.com/tfp/tilted-technology/102321-java-moving-values-2d-array.html)

feelgood 03-19-2006 10:51 AM

[java]Moving values in 2d array
 
Ok, I got a 2d array where they are filled with certain letter. So, I need to remove any location that has the letter "X" and replace it with whatever is above.
Here what the 2D array would start out as. Notice that there are Xs in Row 12-14 that needs to be removed with whatever is above it:
Code:

  A B C D E F G H I J K L M
1  X X X X X X X X X X X X X
2  X X X X X X X X X X X X X
3  X X X X X X X X X X X X X
4  X X X X X X X X X X X X X
5  X X X X X X X X X X X X X
6  X X X X X X X X X X X X X
7  X X X X X X X X X X X X X
8  X X X X X X X X X X X X X
9  X X X X X X X X X X X X X
10 Y Y G Y G Y R G G G R G G
11 R R Y R G G R R Y G Y R Y
12 X Y R R R Y R R G Y R G G
13 X X R R R R Y R G R Y Y R
14 X X Y G Y R G R R Y R Y Y
15 R Y Y R G G G Y R R R G Y

Here is what I need to end up with after running the method:
Code:

  A B C D E F G H I J K L M
1  X X X X X X X X X X X X X
2  X X X X X X X X X X X X X
3  X X X X X X X X X X X X X
4  X X X X X X X X X X X X X
5  X X X X X X X X X X X X X
6  X X X X X X X X X X X X X
7  X X X X X X X X X X X X X
8  X X X X X X X X X X X X X
9  X X X X X X X X X X X X X
10 X X G Y G Y R G G G R G G
11 X X Y R G G R R Y G Y R Y
12 X Y R R R Y R R G Y R G G
13 Y R R R R R Y R G R Y Y R
14 R Y Y G Y R G R R Y R Y Y
15 R Y Y R G G G Y R R R G Y

But here's what actually happens:
Code:

  A B C D E F G H I J K L M
1  X X X X X X X X X X X X X
2  X X X X X X X X X X X X X
3  X X X X X X X X X X X X X
4  X X X X X X X X X X X X X
5  X X X X X X X X X X X X X
6  X X X X X X X X X X X X X
7  X X X X X X X X X X X X X
8  X X X X X X X X X X X X X
9  X X X X X X X X X X X X X
10 X X G Y G Y R G G G R G G
11 Y Y Y R G G R R Y G Y R Y
12 R R R R R Y R R G Y R G G
13 X Y R R R R Y R G R Y Y R
14 X X Y G Y R G R R Y R Y Y
15 R Y Y R G G G Y R R R G Y

It seems to me that everytime the method is suppose to replace X in a specific row, it's actually doing the row above.


Code:

private void collapseRow() {
                for(int i = ROW_LIMITS; i > 0; i--) {
                        for(int j = COLUMN_LIMITS; j > 0; j--) {
                                if(grid[i][j] == "X") {
                                        if(i - 1 >= 1) {
                                                grid[i][j] = grid[i - 1][j];
                                                grid[i - 1][j] = "X";
                                        }
                                }
                        }
            }
        }


CSflim 03-19-2006 02:26 PM

Quote:

So, I need to remove any location that has the letter "X" and replace it with whatever is above.
What do you mean 'above'? I can't quite make out what your code is supposed to be doing.

In your example, row 13 goes from

[X, X, R, ...] -> [Y, R, R, ...]

Where are the Y and R coming from? The values literally above them (row 12) are X and Y, so that's obviously not what you meant.

feelgood 03-19-2006 04:46 PM

X is like a void, empty space if you will, and I need whatever is above the empty space to drop into the empty space. Kinda like the game Tetris (sp?) where you drop the blocks into the right place.

Kadath 03-20-2006 05:34 AM

This may be a stupid question, but if it's doing it one row off, are you counting the fact that array starts at 0 and not at 1?

RAGEAngel9 03-20-2006 05:58 AM

Yeah since he's couting down, his last trip trough the for loop has i = 1 and j =1.

feelgood 03-20-2006 11:19 AM

I and J is suppose to end at 1 since the number on the column and letter at the top row is also part of the array. Don't ask me why, I'm just doing what my instructor told me to do.

I fixed the issue, it was pretty stupid since I was only bringing down whatever was above the row and then move on without considering the fact that the above row was also X.

Code:

private void collapseRow() {
        for(int i = ROW_LIMITS; i > 0; i--) {
                for(int j = COLUMN_LIMITS; j > 0; j--) {
                        if(grid[i][j] == "X") {
                                for(int k = i; k > 0; k--) {
                                        if(grid[k][j] != "X") {
                                                grid[i][j] = grid[k][j];
                                                grid[k][j] = "X";
                                                break;
                                        }
                                }
                        }
                }
        }
}


Dilbert1234567 04-05-2006 01:03 PM

Another way to do this, better perdorming with out all the redundant writes. Travel down the array (vertically, along the i) and if the value is not x, you add it to a string (or chr array of size row_limit for speed) and delete them when you add them, then when you hit the bottom, you empty the array back up. So the first column would make an array

[y,r,r,NULL,NULL...]

Keep track of the last value (index 2 in this case) and replace it as you travel back up the row. Iterate across the columns and you’re done.


All times are GMT -8. The time now is 04:38 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73