- Home /
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.
Answer by MartinCA · Sep 06, 2013 at 12:55 PM
I'll break down your problem into two discrete bits for you:
How to visualize data stored in an array
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.
1. 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[i][j], positionx = i, positiony = j )
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,
Answer by robhuhn · Sep 06, 2013 at 01:05 PM
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
Ins$$anonymous$$d of ints, I would create a struct for each state: EmptyCell, ShipPartCell, HitShipCell. Each would have a reference to a Ship class so each knows which ship they belong to.
The individual cells, would extend an abstract Cell which contains all common functionality and methods + abstract methods for state-specific behavior.
One abstract method could be public abstract void BombCell();
to throw a bomb into this cell. EmptyCell and HitShipCell have an empty method, ShipPartCell transforms that particular cell into a HitShipCell.
Using these constructs I can create code in which I don't really need to use if clauses (which can be hard to follow at times), because a call to GetCell(2,3).BombCell()
will perform the right action.
I find this is more readable than using ints.
Your answer
Follow this Question
Related Questions
I'm having trouble with grids and instantiating arrays of prefabs. 1 Answer
creating a 2d array for grid (Not been asked i swear) 1 Answer
Tracking ojects grid positon 1 Answer
Randomly Choosing two objects from an array and switching their positions 3 Answers
Moving a character into a 2d int array 0 Answers