Array help...

BlitzMax Forums/BlitzMax Programming/Array help...

I think I have spent too much time programming in the last week, because my brain has just died.

Basically, if I had a 2D array set up like so:-

00000
00000
00000
00200
32422

How could I shuffle the area so that it adds a row of 1's to the bottom, then upwards, e..g:-

00000
00000
00000
00200
32422

00000
00000
00200
32422
11111

00000
00200
32422
11111
11111

etc etc

cheers! :)

Dabz

One way to do it (although probably not the most efficient)
SuperStrict

Graphics 640 , 480 , 0 , 0

Global array:Int[5 , 5]

array[0 , 0] = 0
array[1 , 0] = 0
array[2 , 0] = 0
array[3 , 0] = 0
array[4 , 0] = 0

array[0 , 1] = 0
array[1 , 1] = 0
array[2 , 1] = 0
array[3 , 1] = 0
array[4 , 1] = 0

array[0 , 2] = 0
array[1 , 2] = 0
array[2 , 2] = 0
array[3 , 2] = 0
array[4 , 2] = 0

array[0 , 3] = 0
array[1 , 3] = 0
array[2 , 3] = 2
array[3 , 3] = 0
array[4 , 3] = 0

array[0 , 4] = 3
array[1 , 4] = 2
array[2 , 4] = 4
array[3 , 4] = 2
array[4 , 4] = 2


While Not KeyDown(KEY_ESCAPE)
	
	DrawArray()	
	DrawText "Press a key" , 0 , 200
	
	Flip
	Cls

	WaitKey()
	shift()

Wend
End

Function DrawArray()
	For Local y:Int = 0 To 4
		For Local x:Int = 0 To 4
			DrawText(array[x,y], x*10, y*10)
		Next
	Next
End Function

Function shift()
	For Local y:Int = 0 To 3
		For Local x:Int = 0 To 4
			array[x, y] = array[x, y+1]
		Next
	Next
	array[0 , 4] = 1
	array[1 , 4] = 1
	array[2 , 4] = 1
	array[3 , 4] = 1
	array[4 , 4] = 1
End Function


So simple, but I couldnt see it! :(

Thanks Perturbatio!

Dabz

Nice code. This will go in my working code folder for future reference. :)

with an array of arrays it's much smaller (and a little easier to maintain)

SuperStrict

Graphics 640 , 480 , 0 , 0

Global array:Int[][]
array = array[..5]
array[0] = [0,0,0,0,0]
array[1] = [0,0,0,0,0]
array[2] = [0,0,0,0,0]
array[3] = [0,0,2,0,0]
array[4] = [3,2,4,2,2]



While Not KeyDown(KEY_ESCAPE)
	
	DrawArray()	
	DrawText "Press a key" , 0 , 200
	
	Flip
	Cls

	WaitKey()
	shift()

Wend
End

Function DrawArray()
	For Local y:Int = 0 To 4
		For Local x:Int = 0 To 4
			DrawText(array[y][x], x*10, y*10)
		Next
	Next
End Function

Function shift()
	For Local y:Int = 0 To 3
		For Local x:Int = 0 To 4
			array[y][x] = array[y+1][x]
		Next
	Next
	array[4] = [1,1,1,1,1]
End Function


SuperStrict

Graphics 640 , 480 , 0 , 0

Global array:Int[][]
array = array[..5]
array[0] = [0,0,0,0,0]
array[1] = [0,0,0,0,0]
array[2] = [0,0,0,0,0]
array[3] = [0,0,2,0,0]
array[4] = [3,2,4,2,2]


While Not KeyDown(KEY_ESCAPE)
	
	DrawArray()	
	DrawText "Press a key" , 0 , 200
	
	Flip
	Cls

	WaitKey()
	shift()

Wend
End

Function DrawArray()
	For Local y:Int = 0 To 4
		For Local x:Int = 0 To 4
			DrawText(array[y][x], x*10, y*10)
		Next
	Next
End Function

Function shift()
	For Local y:Int = 0 To 3
		array[y] = array[y+1]
	Next
	array[4] = [1,1,1,1,1]
End Function


Cleaner still.

Just poping into say thanks for the help Perturbatio! :)

I had brain rot the other day and just couldnt think, I guess that happens to everyone once in a while.

Cheers

Dabz

no problem :)