Not sure if this is the best place to post this. Maybe we need a Tutorial section on the forum.
Hyperpad now has array behaviors (yay!), but we’re limited to 1-dimensional arrays. One can do quite a bit with 1-dimensional arrays, but for some things, like grid-based games, 2-dimensional arrays would be ideal. For example, in a game like Battleship, 2-dimensional arrays would be ideal for keeping track of ship locations and the coordinates of hits and misses and then mapping the array information onto a graphic display.
The good news is, we can do the same thing with 1-dimensional arrays, it just takes a few extra steps. So as an example, let’s take a 5x5 grid layout with a ship at coordinates (1,2)(2,2)(3,2) as shown in the left figure.
We can assign each square in the grid to an index in a 1-dimensional array with 25 elements as shown in the right figure. Remember, arrays are indexed beginning at 0. So our ship is sitting at array indices 11, 12, & 13.
A 1-dimensional array representation of our ship, where we assign cells with a ship on them a “1” and other cells a “0”, would look like this:
Array = (0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0)
That is a visual representation of the problem, now how do we actually calculate the array index number for a given grid coordinate? We need one key number – the column dimension or grid width. In this case, we have 5 columns, so the column dimension is 5.
To determine the array index for a given (x,y) coordinate we use the formula:
Array index = (y*column dimension) + x
For the ship at (1,2)(2,2)(3,2), and a column dimension of 5, we calculate:
(1,2) --> (2*5)+1 = 11
(2,2) --> (2*5)+2 = 12
(3,2) --> (2*5)+3 = 13
Which gives us our array indices of 11, 12, and 13.
Pretty cool, huh?
Now how about going the other way. We have our 1-dimensional array representation of the ship, how do we translate that to coordinates so we can then display a graphic representation of the ship in the grid?
This is a little more involved, but not too bad. If we divide an array index by the column dimension, the remainder is our x coordinate and the whole number is our y coordinate.
Let’s take the array index 13 from our previous example. The column dimension is 5. So divide 13 by 5 and get 2 with a remainder of 3, which maps to x=3, y=2.
In Hyperpad we’ll need three behaviors to do this:
Divide Values
Round Number
Modulus
Given an array index “N” and a column dimension “D”
To get x, we use the Modulus behavior which divides two numbers and returns the remainder
x = N mod D
To get y, first divide N by D with a Divide Values behavior,
then input this result into a Round Number behavior, and set the Rounding Type to “Truncate”
y=truncate[N/D]
Hope this makes sense and is helpful.
Note: this could possibly be done with multiple arrays,
but where’s the fun in that! :smiley: