# Visualize array in game

Hello, everyone.
I feel like I’ve been googling this for ages.

Question: How do I visualize game information from an array?

Example 1: Battleship. If I had a grid representing the play area where players placed their ships (this saved in array) and you click on a part of the grid, how would I check the clicked area against the array? How would I know if I hit the battleship or not?

Example 2: Tetris. To see if a line is full and is to be removed someone said it was best to represent the lines with an array, if the array line is full remove the bricks and the information from the array. How would I do this?

What if I had a grid and when I clicked a specific place a specific array entry got changed?

I might be thinking completely wrong and might be talking jibberish. But I have been searching for an answer to this question for what feels like forever and I I die a little inside whenever I fail horribly at achieving this.

I’ll break down your problem into two discrete bits for you:

1. How to visualize data stored in an array
2. How to interact with data stored in an array

Both of these problems are quite simple to solve, and are very functionality / design driven, So I’ll try to give a high level direction here you can take to which way you want.

As far as visualizing data, an array of objects is just that - a group of several objects. How do you visualize a single data point in your game? Be it a Battleships game, a single “board” piece would either be empty or a ship piece. (without going into complexities ). Let’s say we decide to represent each of our board pieces as a cube primitive, saying a red cube is a ship and a blue cube is water.

To visualize a single object, you would draw a cube of the appropritate color ( one way to do that would be to instantiate a prefab of the correct type in Unity-esque terms ).

Got that working? Great!

Now the next bit is trying to understand the context of our array, or rather what functional purpose does it server in order to visualize it. Were it a list of phones, you would simply draw them sequentially like a list most likely. But as you want to represent a game board inside this array, this means each of the indicies ( being a 2 dimensional array ) is a coordinate.

You can now map the indicies to 2 axes, let’s say the outer array index is `i` which we’ll map to `x`, and the inner array index is `j` which we’ll map to `y`.

Building on the “Draw A Single Data Point” solution mentioned earlier, drawing a board would simply look something along the lines of this ( this is some pseudo-code, gonna have to implement this yourself )

``````for each i in outer array:
for each j in inner array:
DrawSingleBlock( data=array*[j], positionx = i, positiony = j )*
``````

2.
As far as interaction goes. well, this is simply the `inverse` of visualizing an array.
We mapped our i,j indicies to x,y world space. You can get your input in either world space or screen space. Either way, with the input position’s x & y values you can translate back to indices i & j and point to the array cell the user interacted with.
Good luck,

If you have a grid you could represent the columns and rows by a multi-dimensional array, multiple arrays in one array or even in a one-dimensional array with some calculations but thats another story. When using multi-dimensional arrays or jagged arrays you would have representation like the following (all int):

``````0, 1, 0, 1, 1
0, 1, 0, 0, 1
0, 0, 0, 0, 0
0, 0, 0, 0, 1
0, 1, 0, 0, 0
``````

The number of columns and rows, as well as the values can vary - that depends on your use case.

The declaration of a multi-dimensional array defines the first dimension (rows) by the first bracket pair and the second dimension (columns) by the second pair:

``````my2DArray[][] =
{0, 1, 0, 1, 1},
{0, 1, 0, 0, 1},
{0, 0, 0, 0, 0},
{0, 0, 0, 0, 1},
{0, 1, 0, 0, 0}

//my2DArray[0] returns {0, 1, 0, 1, 1}
//my2DArray[1] returns {0, 1, 0, 0, 1}
// ...
``````

the value at rowIndex 4 and columnIndex 1 is

``````//my2DArray[4][1] returns 1
``````

All the values in your grid could represent the position of water ( value 0), ship (value 1) positions and hits (value 2). Here is an example of one 4-field-ship on the field with one hit and a sunken 3-field-ship:

``````1, 0, 0, 0, 0
1, 0, 0, 0, 0
2, 0, 0, 0, 0
1, 0, 2, 2, 2
0, 0, 0, 0, 0
``````