0

I have an dimensional array which is hardcoded which i would like to make an for loop for so randomized characters get inserted.

And then show it in the console as 3 by 3 with an foreach (that is what i tried):

a b c
d b a
a b d

So i can count them since i need to make a pointing system as they have specific points. (this i can manage on my own) But i was stuck with the dimensional array. It worked with a single dimension array but i need a 3 by 3 pointing game system.

Random random = new Random();
String chars = "ABCD";

// the array i want to make a for loop for
string[,] main = new string[3, 3];

// the example array how it should look after the for loop inserted data.
string[,] main_example = new string[3, 3] { { "c", "b", "a" }, { "d", "a", "b" }, { "a", "d", "a" } 
};

// inserts a or b or c or d.
for (int i = 0; i < cards9.Length; i++) {
    main[i, i] += chars[random.Next(chars.Length)];
}

// the foreach to display the data in the console
foreach (string i in main) {
   System.Console.Write("{0} ", i);
}
1
  • Thank you all very much, it actually worked like a charm :) I need to upgrade the code with console.color etc. Commented Dec 7, 2020 at 22:12

3 Answers 3

2

Just use nested for loop, GetLowerBound, and GetUpperBound:

var array = new string[3, 3];

for (var i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
{
    for (var j = array.GetLowerBound(1); j <= array.GetUpperBound(1); j++)
    {
        // replace guids to your initialization code
        array[i, j] = Guid.NewGuid().ToString();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

First issue is that you would liketo have lower case characters, but your chars contains only uppercase. Also, you want to have array of chars and we know that string is basically it, but more clear way would be to use char[].

Second, it would simplify the code, if you could define number of rows and columns for your multidimensional array in one place and then use just parameters.

Third, to loop over mutlidimensional array, you need to use nested loop.

Putting it all together, I would suggest such changes to the code:

private static char[,] GenerateRandomCharArray()
{
    var rnd = new Random();
    var chars = new char[] { 'a', 'b', 'c', 'd' };
    var charLength = chars.Length;
    var rows = 3;
    var columns = 3;
    var result = new char[rows, columns];
    for (int i = 0; i < rows; i++)
        for (int j = 0; j < columns; j++)
            result[i, j] = chars[rnd.Next(charLength)];
    
    return result;
}

Comments

1

Single loop:

  for (int i = 0; i < cards9.Length; i++) {
        var c = chars[random.Next(chars.Length)].ToString();
        main[i / 3, i % 3] = c;
    }

The above code calculates all possible indexes of 3x3 array.

if print using below code then it will printout as follows

   for (var i = main.GetLowerBound(0); i <= main.GetUpperBound(0); i++)
    {
        for (var j = main.GetLowerBound(1); j <=main.GetUpperBound(1); j++)
        {
            // replace guids to your initialization code
            Console.Write(main[i,j] + " ");
        }
        
        Console.WriteLine();
    }


B B D 
C C D 
A A C 

You can check this fiddle -- https://dotnetfiddle.net/6vEu8f

Comments

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.