-4

I am learning C# in school, and I am already used to coding in Python. Trying to take what I have learned in Python and apply it in C# has worked pretty well so far, but I have ran into an issue. I just CANT wrap my head around how lists work in C#

Say I'm working on a simple game, Tic Tac Toe. In python, I would make a list, with 3 lists inside, and then have THOSE lists contain the playing field, like this:

# First I generate the lists within a list:
screenMatrix = []
for y in range(3):
    temp = []
    for x in range(3):
        temp.append(" ")
    screenMatrix.append(temp)

# Then I can just change the board at specific X and Y coordinates
screenMatrix[0][2] = "1" #This is top right
screenMatrix[1][1] = "2" #This is the middle


# And then I could print it out like this:
for row in screenMatrix:
    print(f"{screenMatrix[row][0]}{screenMatrix[row][1]}{screenMatrix[row][2]}")

This is a very simple way of doing this, but that is besides the point. How would I write in C# to get this exact same functionality? (being able to change items based on X,Y coords)

If someone could translate the code I wrote into C#, that might help me understand the structure.

TayIsAsleep
  • 79
  • 2
  • 7
  • 1
    What have you tried and what didn't work? – David Nov 05 '20 at 14:24
  • To get the best responses you should take a stab at the C# code and share where you are having problems or questions. I'm not sure you need a list of lists. I think you can just create a model that has a field for the x-axis, y-axis, and an enum for the value of the square (x, o, empty) – ejwill Nov 05 '20 at 14:26
  • `List>`, but `screenMatrix` looks like an [array](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/arrays/) to me (see multidimentional and jagged arrays), rather than *list*, `.append` is probably only relevant to instantiating. – Sinatr Nov 05 '20 at 14:28
  • `var ScreenMatrix = new List>{ new List{0,0,0}, new List{0,0,0}, new List{0,0,0} }; ScreenMatrix[0][0] = 1; foreach (var row in ScreenMatrix){ Console.WriteLine($"{row[0]}{row[1]}{row[2]}"); }` I tried this, and it seems to do what I want, but is this not the "proper" or the smart way of doing it then? – TayIsAsleep Nov 05 '20 at 14:34

2 Answers2

0

The best way would be to use two dimensional array since you know the size of playing field at the start. The best way would be to fill it with chars rather than strings, but you can change it to string aswell.

char[,] matrix = new char[3, 3]

Or if you really want to use lists, which allow you to dynamically change the size, you can do it this way:

List<List<char>> matrix = new List<List<char>>

So the best result would be:

//Filling the array with empty characters
for(int row = 0; row < matrix.GetLength(0); row++) //Iterate through first dimension
{ 
    for(int column = 0; column < matrix.GetLength(1); column++) //Iterate through second dimension
    { 
        matrix[row, column] = ' ';
    }
}

//Accesing values
matrix[0, 0] = 'x'
matrix[0, 1] = 'o'
ZetaxCZ
  • 1
  • 1
  • Is there a noticable performance impact by using lists, or does both work just as well? – TayIsAsleep Nov 05 '20 at 14:42
  • @TayIsAsleep, [that's another question](https://stackoverflow.com/q/454916/1997232) and it was already asked and answered. – Sinatr Nov 05 '20 at 14:48
  • @TayIsAsleep There is not, both work well. Feel free to use list with strings. The characters might be a little confusing for python developers. – ZetaxCZ Nov 05 '20 at 14:55
0

In C# it would look like this:

      string[][] screenMatrix = new string[3][];
      screenMatrix[0] = new string[]{"-","-","-"};
      screenMatrix[1] = new string[]{"-","-","-"};
      screenMatrix[2] = new string[]{"-","-","-"};

      screenMatrix[0][2] = "1";
      screenMatrix[1][1] = "2";

      foreach (var row in screenMatrix)
      {
        foreach (var item in row)
        {
          Console.Write(item);
        }
        Console.WriteLine();
      }
  • Technically not a List> as was mentioned in the title, but does at least solve the heart of their question. – gilliduck Nov 05 '20 at 14:55