Tilted Forum Project Discussion Community  

Go Back   Tilted Forum Project Discussion Community > Interests > Tilted Technology


 
 
LinkBack Thread Tools
Old 03-19-2006, 10:51 AM   #1 (permalink)
Free Mars!
 
feelgood's Avatar
 
Location: I dunno, there's white people around me saying "eh" all the time
[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";
					}
				}
			}
	    }
	}
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war
feelgood is offline  
Old 03-19-2006, 02:26 PM   #2 (permalink)
Sky Piercer
 
CSflim's Avatar
 
Location: Ireland
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.
__________________
CSflim is offline  
Old 03-19-2006, 04:46 PM   #3 (permalink)
Free Mars!
 
feelgood's Avatar
 
Location: I dunno, there's white people around me saying "eh" all the time
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.
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war
feelgood is offline  
Old 03-20-2006, 05:34 AM   #4 (permalink)
Muffled
 
Kadath's Avatar
 
Location: Camazotz
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?
__________________
it's quiet in here
Kadath is offline  
Old 03-20-2006, 05:58 AM   #5 (permalink)
Insane
 
Location: Vermont
Yeah since he's couting down, his last trip trough the for loop has i = 1 and j =1.
RAGEAngel9 is offline  
Old 03-20-2006, 11:19 AM   #6 (permalink)
Free Mars!
 
feelgood's Avatar
 
Location: I dunno, there's white people around me saying "eh" all the time
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;
					}
				}
			}
		}
	}
}
__________________
Looking out the window, that's an act of war. Staring at my shoes, that's an act of war. Committing an act of war? Oh you better believe that's an act of war
feelgood is offline  
Old 04-05-2006, 01:03 PM   #7 (permalink)
Devils Cabana Boy
 
Dilbert1234567's Avatar
 
Location: Central Coast CA
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.
__________________
Donate Blood!

"Love is not finding the perfect person, but learning to see an imperfect person perfectly." -Sam Keen
Dilbert1234567 is offline  
 

Tags
array, javamoving, values


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 03:16 AM.

Tilted Forum Project

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, 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 74 75 76