About Monkey 2 › Forums › Monkey 2 Programming Help › How declare an multidimensional array
This topic contains 5 replies, has 4 voices, and was last updated by zuibaf 1 year ago.
-
AuthorPosts
-
January 26, 2018 at 1:02 pm #13261
I am creating an game Tetris,
there will be an multidimensional array for each piece,
each an multidimensional array is store inside of an unidimensional
For example, I want to store three pieces ‘L’, ‘T’ e ‘I’ this way:
The piece ‘L’
[1][0]
[1][0]
[1][1]
The piece ‘T’
[1][1][1]
[0][1][0]
[0][1][0]
The piece ‘I’
[1][1][1]
[0][1][0]
[1][1][1]
In C#, this is easy:C#1234567891011121314151617Int[][,] pieces = new Int[3][,];pieces[0] = new Int[3,2]{{1,0},{1,0},{1,1}};pieces[1] = new Int[3,3]{{1,1,1},{0,1,0},{0,1,0}};pieces[1] = new Int[3,3]{{1,1,1},{0,1,0},{1,1,1}};In monkey2, on the documentation, there is nothing similar, I try this way:
Monkey1234Local pieces: Int[][,] = new Int[3][,]pieces[0] = new Int[3,2]((1,0),(1,0),(1,0))pieces[1] = new Int[3,3]((1,1,1),(0,1,0),(0,1,0))pieces[1] = new Int[3,3]((1,1,1),(0,1,0),(1,1,1))I know what for unidimensional arrays, this is possible:
Local objPiece: Int[] = New Int[6](1, 2, 3, 4, 5, 6)In the documentation, there is nothing described, perhaps not yet supported. If really, it is not supported will be that in a next version we will have something similar to C#.
In my case, my intention is to avoid the other way described below, as it is not so readable to someone reading the code, that the multidimensional array demonstrates a piece of a tetris game.
Monkey12345678910111213Local pieces: Int[][,] = New Int[3][,]pieces[0] = New Int[3,2]' {1,0},' {1,0},' {1,1}pieces[0][0,0] = 1pieces[0][0,1] = 0pieces[0][1,0] = 1pieces[0][1,1] = 0pieces[0][2,0] = 1pieces[0][2,1] = 1January 26, 2018 at 1:35 pm #13263I enter the values in an string variable, this way it becomes more visibile what pieces were set in the game.
Monkey1234567891011121314151617181920212223242526272829303132Local strPieces: String = ""'L'strPieces += "10"strPieces += "10"strPieces += "11"'L INVERTIDO'strPieces += "01"strPieces += "01"strPieces += "11"''strPieces += "10"strPieces += "11"strPieces += "01"pieces = New Int[3][,]Local indice_strPiece: Int = 0Local line: Int = 0Local column: Int = 0pieces[0] = New Int[3,2]indice_strPiece = 0For line = 0 Until 3For column = 0 Until 2pieces[0][line,column] = Cast<Int>(strPieces.Slice(indice_strPiece, indice_strPiece))indice_strPiece += 1NextNextJanuary 27, 2018 at 3:25 pm #13286I think something like this should work.
Monkey123pieces:Int[][] = new Int[][](1,0),new Int[](1,0),new Int[](1,1)January 27, 2018 at 11:48 pm #13295you can create a multidimensional array like this:
Monkey123[crayon-5c6bcd8959877815658114 inline="true" ]Lpiece:Int[][] = New Int[][](New Int[](1,0),New Int[](1,0),New Int[](1,1))ignore line 1
actually not exactly a two dimensional array but an array of arrays.it should serve your purpose as it’s what you ask for.
January 28, 2018 at 5:12 am #13304Multi-dimensional arrays aren’t too well supported yet, in particular there’s no way to intiailize them with values at creation time yet.
Creating them is easy enough, eg: Local myArray:=New Int[10,20], but you’ll have to get a bit tricky to initialize them, maybe something like:
Monkey123456789101112131415161718Function CreatePiece:Int[,]( args:Int[] )Local w:=args[0],h:=args[1]Local piece:=New Int[w,h]For Local y:=0 Until hFor Local x:=0 Until wpiece[x,y]=args[y*w+x+2]NextNextReturn pieceEndFunciton Main()Local piece:=CreatePiece( New Int[]( 2,3, '2 x 3 'T style piece1,0,1,11,0 ) )End(totally untested). Of course, string are also a cool way to do this…
Peronsally, I’d recommend creating a ‘Piece’ class or similar just to make all the arrays easier to deal with (although you seem to be quite comfortable with arrays!) as you’re also gonna wanna ‘Rotate’ them etc. Then you can just have a 1d array of pieces for the game, and give each piece a rotate method, maybe a clone method too for creating new pieces from template pieces etc. Just off the top of my head – I’ve never actually written a tetris game!
February 2, 2018 at 5:05 am #13418I am creating the game, it is working, but I still have not finished
This is my game first.
All that remains is display of the next piece, punctuation, and phase.
The hard part has already been made.
The comment is an portuguese.
Sorry for English, but my native language is Portuguese from Brazil.Attachments:
-
AuthorPosts
You must be logged in to reply to this topic.