❔ Possibilities in simplifying these loops?

I have these 5 loops, however, they look all quite similar, is there a method in simplifying these?
string[] sudokuStrings = filecontent.Split("\r\n");

//First sudoku (top left) min x = 0, min y = 0, max x = 8, max y = 8
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[0][index].ToString());
//Console.WriteLine($"Cell ({x}, {y}): {value}");
}
}

//Second sudoku (top right) min x = 18, min y = 0, max x = 26, max y = 8
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[1][index].ToString());
// Console.WriteLine($"Cell ({18+x}, {18+y}): {value}");
}
}

//Third sudoku (center) min x = 9, min y = 9, max x = 17, max y = 17
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[2][index].ToString());
//Console.WriteLine($"Cell ({9 + x}, {9 + y}): {value}");
}
}

//Fourth sudoku (bottom left) min x = 0, min y = 18, max x = 8, max y = 26
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[3][index].ToString());
//Console.WriteLine($"Cell ({x}, {18 + y}): {value}");
}
}
string[] sudokuStrings = filecontent.Split("\r\n");

//First sudoku (top left) min x = 0, min y = 0, max x = 8, max y = 8
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[0][index].ToString());
//Console.WriteLine($"Cell ({x}, {y}): {value}");
}
}

//Second sudoku (top right) min x = 18, min y = 0, max x = 26, max y = 8
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[1][index].ToString());
// Console.WriteLine($"Cell ({18+x}, {18+y}): {value}");
}
}

//Third sudoku (center) min x = 9, min y = 9, max x = 17, max y = 17
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[2][index].ToString());
//Console.WriteLine($"Cell ({9 + x}, {9 + y}): {value}");
}
}

//Fourth sudoku (bottom left) min x = 0, min y = 18, max x = 8, max y = 26
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[3][index].ToString());
//Console.WriteLine($"Cell ({x}, {18 + y}): {value}");
}
}
5 Replies
electronic heartbreak.
the remaining:
//Fifth sudoku (bottom right) min x = 18, min y = 18, max x = 26, max y = 26
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[4][index].ToString());
Console.WriteLine($"Cell ({18+x}, {18 + y}): {value}");
}
}
//Fifth sudoku (bottom right) min x = 18, min y = 18, max x = 26, max y = 26
for (int y = 0; y < 9; y++)
{
for (int x = 0; x < 9; x++)
{
int index = y * 9 + x;
int value = int.Parse(sudokuStrings[4][index].ToString());
Console.WriteLine($"Cell ({18+x}, {18 + y}): {value}");
}
}
ero
ero2y ago
There is a more underlying issue with how this data is stored and retrieved, certainly But sure, just make this a method into which you pass the index of the strings array Or the line itself
electronic heartbreak.
Thats indeed a good option! I can then pass the 18, 9.. too
FestivalDelGelato
int.Parse(stuff.ToString()) seems like our db code from 15 years ago at work
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.

Did you find this page helpful?