I'm working on a program to generate random grids, latin squares, and sudoku. I am working on the latin squares and pretty much have it all working except I am in a continuous loop. If I break them up they work fine. There is probably something small I am doing wrong and I can't find it. Can you spot what's wrong?
EDIT: For those that don't know what Latin Square is (if someone doesn't know) its usually a 9x9 grid that doesn't have repeats in either rows nor columns.
UPDATE: I found a problem with the notSame equaling true right before if(notSame) statement. It was always equaling true so wouldn't finish checking the row. Now when I run it is no longer in continuous loop but instead the rows have no repeats but columns still do.
UPDATE #2: I now redid a lot of the coding for the columns. My professor asked me to change a few things but it still puts me in to a continuous loop.
int row = 0, col = 0, count = 0;
bool notSame = true;
// setting up rows and columns
for (row = 0; row < grid.GetLength(0); row++)
{
for (col = 0; col < grid.GetLength(1); col++)
{
grid[row, col] = rnd.Next(1, 10);
//for loop to check rows for repeats
for (int c = 0; c < col; c++)
{
// if there is repeat go back a column and set bool = false
if (grid[row, col] == grid[row, c])
{
col--;
count++;
notSame = false;
break;
}
//notSame = true;
}
// if bool = true loop to check columns for repeats
if (notSame)
{
for (int r = 0; r < row; r++)
{
// if repeat then go back row
if (grid[row, col] == grid[r, col])
{
notSame = false;
count++;
break;
}
}
if (notSame == false && count <= 50)
{
row--;
//break;
}
else if (notSame == false && count > 50)
{
count = 0;
col = 0;
row = 0;
break;
}
}
}
}
I am using a 2D array called grid.
My problem was an extra count in the check for rows. The count would always go over 50 and therefore would cause an infinite loop. To clarify:
The count++ would increment and would at times end up > = 50 which would then kick in this code:
Which then cause everything to be set back to 0 and would restart. Therefore, it caused an infinite loop. Thanks all for the help!