0

There is a 2d grid for example 7(column)x16(row). A total of 111 cells are obtained with an index from 0 to 111. Along the perimeter of this grid are 7 numbers and 16 letters. I want to refer to the index of each individual cell not by their number, but for example using the text field to send commands 1a, 2b, 3c, etc.

I introduced it so that the string element of the array - "numbers[0]" and "letters[0]" will be equal to the original cell of the grid with the number 0. Then it turns out to associate the elements of the arrays with the grid index it will be necessary to add +1 to the string element "letters[0] -> letters[1]" in the case when the value of the grid row does not change, but the column changes (for example, 1a -> 1b). And in this case, add +16 to the value of the previous row of the grid (1a -> 2a). I tried to create two one-dimensional arrays "numbers[]" and "letters[]" with a cycle for, but I had a problem, and how to actually associate the data of this array with the cell index if I know the number of columns and rows of the grid.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GridsNL : MonoBehaviour {


public int indexCellStart;
private int countRow;
private int countColumn;



// Use this for initialization
void Start () {

    Grid grid = Grid.instance;
    countRow = grid.rowCount;
    countColumn = grid.columnCount;
    CreateArray();   
}


// Update is called once per frame
void Update () {

}



public void CreateArray()
{

    int[] numbers = new int[countRow];

    for (int countRow = 0; countRow < numbers.Length; countRow++)
    {

        //Debug.Log(countRow);

    }

    string[] letters = new string[countColumn]

    for (int countColumn = 0; countColumn < letters.Length; 
    countColumn++)
    {

        //Debug.Log(countColumn);

    }



    }

I understand that maybe I need to use some kind of cyclic construction inside an array.

1
  • A good option, as I understand it, you can still use this way string[,], but my problem is how to bind the condition of the cell index to number[] and letters[] inside the created array. Commented Apr 26, 2019 at 16:17

1 Answer 1

1

I would either use a multidimensional array like e.g.

string[,] stringArray = new string[rowCount, columnCount];

which you later access e.g.

for(int r = 0; r < rowCount; r++)
{
    for(int c = 0; c < columnCount; c++)
    {
        var certainValue = stringArray[r, c];

        ...
    }
}

Or alternatively if you want a one dimensional array

string[] stringArray = new string[rowCount * columnCount];

And iterate over it like e.g.

for(int r = 0; r < rowCount; r++)
{
    for(int c = 0; c < columnCount; c++)
    {
        var certainValue = stringArray[r * columnCount + c];

        ...
    }
 }

Those would not be accessed by something like 2c but it makes little sense in the end and you should stick to those int indexes instead ...

If you really want letters you might want to try e.g.

char someExampleChar = 'e'; // what you get from the InputField
int indexInt = (int) someExampleChar - (int) 'a';

which converts the char value to a 0 based index.

As a simple Extension method like

public static class CharExtensions
{
    public static int ToIndex(this char character)
    {
        return (int) character - (int) 'a';
    }
}

Then you can simply use

char someExampleChar = 'f';
int index = someExampleChar.ToIndex();

Typed on smartphone so no warranty but I hope the idea gets clear

Sign up to request clarification or add additional context in comments.

1 Comment

Agreed. I would toss my hat in with the 1-dimensional array idea. You can display your items however you want, but the backing data will be much easier to manipulate in 1D space. If OP is really wanting to refer to the cells by something other than index, I would suggest a helper class that can parse that information and convert to an index.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.