0

I'm kind of new to C# and I'm trying to make a sudoku game. I'm using a for loop to loop through the column arrays and add a random number to the spots but I'm using Random and Next() so it allows the numbers to repeat. But because I can't have more than one of a number in each column (array) for it to function, how can I replace the repeated number with another number that isn't repeated? I don't know how to do this.

Also the columns are stored in other arrays that I've called rows just so that I can say Row1[0][2] to access position 3 in column 1 for example.

Here's the method & for loop I'm using to replace add the numbers to the arrays:

void populateColumns(int[][] arr) // goes through column arrays and replaces the numbers with random ones from Num()
{
    int i;
    int j = 0;

    for (int l = 0; l <= 8; l++)
    {
        for (i = 0; i <= 8; i++)
        {
            arr[j][i] = Num();
            int currentPos = arr[j][i];
            Console.Write(currentPos);
        }

        j = l;
    }
}
goldern
  • 1
  • 2
  • What do you intend with this incomplete `switch` statement? As written now, it will give a compile error. – Klaus Gütter Dec 24 '22 at 05:36
  • 1
    Does this answer your question? [How to generate valid Sudoku board? \[ and difficulty rating \]](https://stackoverflow.com/questions/14001935/how-to-generate-valid-sudoku-board-and-difficulty-rating) – NineBerry Dec 24 '22 at 11:51
  • See also https://stackoverflow.com/questions/6924216/how-to-generate-sudoku-boards-with-unique-solutions – NineBerry Dec 24 '22 at 11:57
  • Your naive approach just filling random numbers won't work well because you'll constantly run into dead ends where no number will be allowed in the next cell. – NineBerry Dec 24 '22 at 11:59
  • So, a fairly well written question, but a simple Google search would have answered this. https://www.google.com/search?q=random+numbers+in+range+without+repeats+C%23&oq=random+numbers+in+range+without+repeats+C%23&aqs=chrome..69i57j33i22i29i30.6440j0j7&sourceid=chrome&ie=UTF-8 – Aaron Goselin Dec 24 '22 at 16:38

0 Answers0