Tutorial: how to map a 2-dimensional grid structure/matrix to a 1-dimensional array and back again
-
@mc-games (pretty clever, but multidimensional arrays work)
-
@Jack8680 I must be missing something. As far as I understand, the Array behavior in Hyperpad generates a collection of ordered values, hence a 1-dimensional array: Array(i).
How does one create a 2-dimensional array: Array(i,j)? -
@mc-games you can put arrays in arrays. For example in your 5*5 grid, you can have 5 arrays inside an array. It's a bit hard to explain, but here is a 2d array of your grid:
[
"["0,0","0,1","0,2","0,3","0,4"]",
"["1,0","1,1","1,2","1,3","1,4"]",
"["2,0","2,1","2,2","2,3","2,4"]",
"["3,0","3,1","3,2","3,3","3,4"]",
"["4,0","4,1","4,2","4,3","4,4"]"
]The index of the main array is the X value, then the index of the inside arrays are the y value. For example, if you want the object at 2,4, you would get the array at index 2, which will output the array ["2,0","2,1","2,2","2,3","2,4"]. Then you can get index 4 of that output, which will give the value 2,4. Note that the hyperPad forums are hiding the backslashes in the array, they are automatically created when you use modify array with characters that it might interpret as closing the main array/values:
I've also formatted the array, it would actually like like this:
["["0,0","0,1","0,2","0,3","0,4"]","["1,0","1,1","1,2","1,3","1,4"]","["2,0","2,1","2,2","2,3","2,4"]","["3,0","3,1","3,2","3,3","3,4"]","["4,0","4,1","4,2","4,3","4,4"]"] -
@Jack8680 Ahh, arrays within arrays, thanks. This opens up all kinds of possibilities.
-
Indeed. I created a project on game press for a chess game. This would be much more realistic now. Thought that project was lost forever, but just found it in my email!
-
@TutorialDoctor speaking of chess games, I have one I should really finish 😛
http://bit.ly/2oIDvna -
Thanks! for the tutorial and sharing. Right on time for my project.
-
@Jack8680 much better than what I had. Please do finish!
-
@TutorialDoctor I might eventually, but detecting check/mate would be a bit of effort
-
@Jack8680 I guess the theory is to check if all squares around the king are attacked and also to check if any of the pieces are attacking the square the king is on.
-
@TutorialDoctor But you would also have to make sure no pieces can defend and no pieces can take the attacking piece for checkmates...