I'm trying to implement multi-dimensional arrays in Scratch, for an implementation of maze generation.

To simplify, I'd like to implement a couple of custom blocks which return a value, e.g. get2dValue( x, y ). I see hints in the forum this might be possible, but I cannot find it documented in the wiki.

// Use Scratch's list variable as a 2d array, 30 elements maximum
get2dValue( X, Y )
  return item( ( ( Y - 1 ) * 30 ) + X ) of maze
  • I'm unsure as to what your asking. Are you wondering how to treat a 1-dimensional array (list) in Scratch as a 2-dimensional array? – user103853 Sep 14 '14 at 11:49
  • No. What I want is to create a custom 'reporter block'. This specific example is using a work-around to the lack of multidimensional arrays, but there are many other examples where one might want a custom reporter block since the available blocks are... limited. – Amgine Sep 15 '14 at 17:47
  • 1
    You can see a bidimensional array nxn as n arrays of size n one afther the other. This way you can access for example width and height using element = height * max_width + width – Cygni_61 Sep 27 '14 at 15:35
  • 1
    Almost correct. element = ( height - 1 ) * max_width + width. However, the question was how to return a value. I've discovered it's not actually possible at present, and the amount of bad hacks required to do something simple has pushed me to drop this class. I assume this is a deliberate plan of CS50 else this assignment would not have been required (witness how many people asking if they can skip just this assignment.) – Amgine Sep 28 '14 at 16:17

You could always use a variable or a list. For example, you could have a variable called "2dreturn" and then have the block set that variable to what you want the function to return. In pseudocode, with an integer value called "2dreturn"

block get2dvalue (x, y):
    set 2dreturn to (((y-1)*30)+x)
    set 2dreturn to 2dreturn of maze

If you wanted to take that value, and, for example, set a variable to the result, you could set putvariablehere to 2dreturn. Then, you can just ignore 2dreturn until you use the get2dvalue block again.

| improve this answer | |

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .