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 02:41 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 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360