Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Axealot · Sep 06, 2013 at 12:39 PM · arraygridtetrisvisualize

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by MartinCA · Sep 06, 2013 at 12:55 PM

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.

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,

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
2

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
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Jamora · Sep 06, 2013 at 01:30 PM 1
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

18 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges