Filling a 2d array from initialization.

BlitzMax Forums/BlitzMax Beginners Area/Filling a 2d array from initialization.

I know that you can set a single dimensional array upon initialization but is it possible to set the values of each cell in a 2d array upon creation?

I'm working on a 2d array with 4 rows and 4 columns.

How can I initialize an array that might look like this

0|1|0|0
0|1|0|0
0|1|1|1
0|0|0|0

I know that I can do each element individually but it the way they are done in a single dimensional array are very simple. You don't need to go index by index.

If you don't mind array of arrays you could use
Local a:Int[][]=[ ..
	[0,1,0,0], ..
	[0,1,0,0], ..
	[0,1,1,1], ..
	[0,0,0,0]]


You cannot initialize values into a multi dimentional array, you would have to use array of arrays as Azathoth says.

Here is a post about it: http://www.blitzbasic.com/Community/posts.php?topic=47147&hl=multidimensional%20array

Hi, I'm trying to code a TETRIS clone and I thought I could use an array of arrays to handle the pieces.
I figured out something like this to initialize the arrays.
At the end there is a bit of code to check out the variables:

Global Piece:Int[][][][]=..
	[..						'start of "Piece" variable
		[..					'Pezzo[0] (O)
			[..				'ROT[0]
				[1,1,0,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[0]
			[..				'ROT[1]
				[1,1,0,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[1]
			[..				'ROT[2]
				[1,1,0,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[2]
			[..				'ROT[3]
				[1,1,0,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			]..				'\ROT[3]
		]..					'\Pezzo[0]
,..		
		[..					'Pezzo[1] (|)
			[..				'ROT[0]
				[1,1,1,1],..'X[],Y[0]
				[0,0,0,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[0]
			[..				'ROT[1]
				[1,0,0,0],..'X[],Y[0]
				[1,0,0,0],..'X[],Y[1]
				[1,0,0,0],..'X[],Y[2]
				[1,0,0,0].. 'X[],Y[3]
			],..			'\ROT[1]
			[..				'ROT[2]
				[1,1,1,1],..'X[],Y[0]
				[0,0,0,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[2]
			[..				'ROT[3]
				[1,0,0,0],..'X[],Y[0]
				[1,0,0,0],..'X[],Y[1]
				[1,0,0,0],..'X[],Y[2]
				[1,0,0,0].. 'X[],Y[3]
			]..				'\ROT[3]
		]..					'\Pezzo[1]
,..		
		[..					'Pezzo[2] (Z)
			[..				'ROT[0]
				[1,1,0,0],..'X[],Y[0]
				[0,1,1,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[0]
			[..				'ROT[1]
				[0,1,0,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[1,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[1]
			[..				'ROT[2]
				[1,1,0,0],..'X[],Y[0]
				[0,1,1,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[2]
			[..				'ROT[3]
				[0,1,0,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[1,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			]..				'\ROT[3]
		]..					'\Pezzo[2]
,..		
		[..					'Pezzo[3] (S)
			[..				'ROT[0]
				[0,1,1,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[0]
			[..				'ROT[1]
				[1,0,0,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[0,1,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[1]
			[..				'ROT[2]
				[0,1,1,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[2]
			[..				'ROT[3]
				[1,0,0,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[0,1,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			]..				'\ROT[3]
		]..					'\Pezzo[3]
,..		
		[..					'Pezzo[4] (L)
			[..				'ROT[0]
				[1,0,0,0],..'X[],Y[0]
				[1,0,0,0],..'X[],Y[1]
				[1,1,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[0]
			[..				'ROT[1]
				[1,1,1,0],..'X[],Y[0]
				[1,0,0,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[1]
			[..				'ROT[2]
				[1,1,0,0],..'X[],Y[0]
				[0,1,0,0],..'X[],Y[1]
				[0,1,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[2]
			[..				'ROT[3]
				[0,0,1,0],..'X[],Y[0]
				[1,1,1,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			]..				'\ROT[3]
		]..					'\Pezzo[4]
,..		
		[..					'Pezzo[5] (J)
			[..				'ROT[0]
				[0,1,0,0],..'X[],Y[0]
				[0,1,0,0],..'X[],Y[1]
				[1,1,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[0]
			[..				'ROT[1]
				[1,0,0,0],..'X[],Y[0]
				[1,1,1,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[1]
			[..				'ROT[2]
				[1,1,0,0],..'X[],Y[0]
				[1,0,0,0],..'X[],Y[1]
				[1,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[2]
			[..				'ROT[3]
				[1,1,1,0],..'X[],Y[0]
				[0,0,1,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			]..				'\ROT[3]
		]..					'\Pezzo[5]
,..		
		[..					'Pezzo[6] (T)
			[..				'ROT[0]
				[0,1,0,0],..'X[],Y[0]
				[1,1,1,0],..'X[],Y[1]
				[0,0,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[0]
			[..				'ROT[1]
				[0,1,0,0],..'X[],Y[0]
				[0,1,1,0],..'X[],Y[1]
				[0,1,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[1]
			[..				'ROT[2]
				[0,0,0,0],..'X[],Y[0]
				[1,1,1,0],..'X[],Y[1]
				[0,1,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			],..			'\ROT[2]
			[..				'ROT[3]
				[0,1,0,0],..'X[],Y[0]
				[1,1,0,0],..'X[],Y[1]
				[0,1,0,0],..'X[],Y[2]
				[0,0,0,0].. 'X[],Y[3]
			]..				'\ROT[3]
		]..					'\Pezzo[6]		
	]						'End of "Piece" Variable




Graphics 800,600
'DebugStop
For p=0 To 6
	For r=0 To 3
		For y=0 To 3
			For x=0 To 3
				If piece[p][r][y][x]=1 
					SetColor 255,255,255
				Else
					SetColor 255,0,0
				EndIf
				DrawRect(r*65+x*16,y*16+p*65,16,16)
			Next
		Next
	Next
Next
Flip
WaitKey


Hello.

Can't you just define your array structure and then read the values from DATA statements (or whatever the BMax equivalent is)?

Goodbye.

Yeah, as SoggyP states, use defdata statements to hold the block data and then use a for next loop to fill the array with the data.

SuperStrict

Graphics 400, 400

Global ArrWidth:Int = 4
Global ArrHeight:Int = 4

Global GameArrayWidth:Int = 200 / 20
Global GameArrayHeight:Int = 200 / 20

Global ShapeArrayStartX:Int = 180
Global ShapeArrayStartY:Int = 20

Global GameArrayStartX:Int = 100
Global GameArraySTartY:Int = 0

Global ShapeArray:Int[ArrWidth, ArrHeight] 
Global GameArray:Int[GameArrayWidth, GameArrayHeight] 

Global CurrentShape:String = "LFShape"

Global Rot:Int = 0

Select CurrentShape
	Case "LFShape"
		LoadLFShape(0) 
End Select

'For Local x:Int = 0 Until ArrWidth
'	For Local y:Int = 0 Until ArrHeight
'		Print ShapeArray[x, y] 
'	Next
'Next

While Not KeyHit(KEY_ESCAPE) 
	Cls
		
		If KeyHit(KEY_RIGHT) 
			Rot:+1
			If Rot > 3 Then Rot = 0
			LoadLFShape(Rot) 
		ElseIf KeyHit(KEY_LEFT) 
			Rot:-1
			If Rot < 0 Then Rot = 3
			LoadLFShape(Rot) 
		End If
		
		DrawShapeArray() 
		
	Flip
Wend

Function DrawShapeArray() 
	For Local x:Int = 0 Until ArrWidth
		For Local y:Int = 0 Until ArrHeight
			'Print "Pre SHape Draw OK"
			If ShapeArray[x, y] = 1 And CurrentShape = "LFShape"
				SetColor 255, 255, 255
				DrawRect ShapeArrayStartX + x * 20, ShapeArrayStartY + y * 20, 20, 20
			End If
		Next
	Next
End Function

Function LoadLFShape(Turn:Int) 
	Local CollectedData:Int
	Select Turn
		Case 0
			RestoreData LFTurn0
			For Local y:Int = 0 Until ArrHeight
				For Local x:Int = 0 Until ArrWidth
					ReadData CollectedData
					ShapeArray[x, y] = CollectedData
				Next
			Next
		Case 1
			RestoreData LFTurn1
			For Local y:Int = 0 Until ArrHeight
				For Local x:Int = 0 Until ArrWidth
					ReadData CollectedData
					ShapeArray[x, y] = CollectedData
				Next
			Next
		Case 2
			RestoreData LFTurn2
			For Local y:Int = 0 Until ArrHeight
				For Local x:Int = 0 Until ArrWidth
					ReadData CollectedData
					ShapeArray[x, y] = CollectedData
				Next
			Next
		Case 3
			RestoreData LFTurn3
			For Local y:Int = 0 Until ArrHeight
				For Local x:Int = 0 Until ArrWidth
					ReadData CollectedData
					ShapeArray[x, y] = CollectedData
				Next
			Next
	End Select
End Function













'LeftFacingLShape
#LFTurn0
DefData 0, 0, 1, 0
DefData 0, 0, 1, 0
DefData 0, 1, 1, 0
DefData 0, 0, 0, 0

#LFTurn1
DefData 0, 1, 0, 0
DefData 0, 1, 1, 1
DefData 0, 0, 0, 0
DefData 0, 0, 0, 0

#LFTurn2
DefData 0, 0, 1, 1
DefData 0, 0, 1, 0
DefData 0, 0, 1, 0
DefData 0, 0, 0, 0

#LFTurn3
DefData 0, 0, 0, 0
DefData 0, 1, 1, 1
DefData 0, 0, 0, 1
DefData 0, 0, 0, 0

'Right Facing L Shape
#LRTurn0
DefData 0, 1, 0, 0
DefData 0, 1, 0, 0
DefData 0, 1, 1, 0
DefData 0, 0, 0, 0

#LRTurn1
DefData 0, 0, 0, 0
DefData 1, 1, 1, 0
DefData 1, 0, 0, 0
DefData 0, 0, 0, 0

#LRTurn2
DefData 1, 1, 0, 0
DefData 0, 1, 0, 0
DefData 0, 1, 0, 0
DefData 0, 0, 0, 0

#LRTurn3
DefData 0, 0, 1, 0
DefData 1, 1, 1, 0
DefData 0, 0, 0, 0
DefData 0, 0, 0, 0


Sure, it's much more simple your way...
Thank you! ;)