private void loadValues() { //Clear values in the cell, if any foreach (var cell in cells) { cell.value = 0; cell.Clear(); } //Call recursively until it finds suitable values for each cell. GameMechanics.findValueForNextCell(0, -1, cells); }
public static bool findValueForNextCell(int i, int j, GridModel[,] cells) { //Increment i and j to the next cell. if (++j > 8) { j = 0; if (++i > 8) { return(true); } } var value = 0; var numsLeft = new List <int> { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; //Find a valid number for the cell and go to the next cell and //check if it can be allocated a random number do { //If there's no suitable numbers then change the previous cell. if (numsLeft.Count < 1) { cells[i, j].value = 0; return(false); } //Take a random number taken from the list value = numsLeft[GameVariablesModel.random.Next(0, numsLeft.Count)]; cells[i, j].value = value; //Remove allocated value numsLeft.Remove(value); } while (!GameMechanics.isValidNumber(value, i, j, cells) || !GameMechanics.findValueForNextCell(i, j, cells)); //Remove line after testing //cells[i, j].Text = value.ToString(); return(true); }