+ 6

Jagged Lists (Or just List of Lists)

So I'm making a sudoku program and Lists are the way to go. But in order to check compatibility with the column, I need to at y == 0 create a List for each x and add the current generated value to it and after that (for any y != 0) at each x it needs to access the list for current x, check if it contains the value that was added before, and if it doesn't it adds to it. So I think that to keep the code short and it's easier to make it, I need to use a list of lists (List<List<string>> ColCompat for example) and I've been using two for loops to go through the sudoku grid and add the validated number to each cell. In theory it'd be something like this: string[,] Sudoku = new string[9, 9]; //9x9 Array. List<List<string>> ColCompat = new List<List<string>>(); //List of lists for each x. string cell; /*Variable that temporarly holds a random value gotten from GetRand class I created.*/ for (int y = 0; y < 9; y++) //Goes through each row. { for (int x = 0; x < 9; x++) //Goes through each column. { cell = GetRand(); //This will atribute a random number from 1 to 9 if (y == 0) /*It only need to create lists at y == 0, after that it only need to access them.*/ { ColCompat[x].Add(List<string>) ColCompat[x].Add(cell) /*I think this is where I'm getting it wrong, because I don't know how to use the list inside the list, just how to access it and work with the ColCompat but not its elements*/ Sudoku[y, x] = cell } else //So any row below the first one. { cell = GetRand(); while (ColCompat[x].Contains(cell)) /*As long as the random number is one already contained in that column, it will keep atributing a new one to cell.*/ cell = GetRand Sudoku[y, x] = cell; } } } This doesn't quite appear to work and so I'm asking you how to do it properly. Of course I'm also not showing a bunch of code that already verifies compatiblity with the row and 3x3

21st Dec 2018, 11:41 AM
klappa
klappa - avatar
1 Answer
+ 3
About random I could not find GetRand(); so I used Random r = new Random(); and r.Next(1,10); https://www.dotnetperls.com/random To add a list. First create a single list. Then add this list to the list that is holding all list. https://www.dotnetperls.com/nested-list https://code.sololearn.com/czFMgwdPI2NO This is my code so far, did not solve all your problems. But this is how you work with Nested List. Nice work, keep coding
21st Dec 2018, 10:35 PM
sneeze
sneeze - avatar