public PuzzleGrid InitGrid() { PuzzleGrid tempGrid = new PuzzleGrid { }; int row = 0; int col = 0; int newVal; List <int> valueSet = new List <int>(Enumerable.Range(-9, 9)); List <int> valueSet2 = new List <int>(); Random rnd = new Random(); int randIndex = 0; newVal = valueSet[randIndex]; tempGrid.InitSetCell(row, col, newVal); valueSet.Remove(newVal); for (row = 1; row < 9; row++) { randIndex = rnd.Next(0, valueSet.Count); newVal = valueSet[randIndex]; valueSet2.Add(newVal); valueSet.Remove(newVal); tempGrid.InitSetCell(row, col, newVal); } row = 0; for (col = 1; col < 3; col++) { randIndex = rnd.Next(0, valueSet.Count); newVal = valueSet2[randIndex]; while ((newVal == tempGrid.Grid[1, 0] || (newVal == tempGrid.Grid[2, 0]))) { randIndex = rnd.Next(0, valueSet2.Count); newVal = valueSet2[randIndex]; } valueSet2.Remove(newVal); } for (col = 3; col < 9; col++) { randIndex = rnd.Next(0, valueSet2.Count); newVal = valueSet2[randIndex]; valueSet2.Remove(newVal); tempGrid.InitSetCell(row, col, newVal); } for (col = 3; col < 9; col++) { randIndex = rnd.Next(0, valueSet2.Count); newVal = valueSet2[randIndex]; valueSet2.Remove(newVal); tempGrid.InitSetCell(row, col, newVal); } do { puzzleSolver = new PuzzleSolver(); puzzleSolver.SolveGrid((PuzzleGrid)tempGrid.Clone(), false); SolutionGrid = puzzleSolver.SolutionGrid; } while (SolutionGrid == null || SolutionGrid.IsBlank()); PermaGrid = Blanker(SolutionGrid); return(PermaGrid); }
public PuzzleGrid RandomlyBlank(PuzzleGrid tempGrid, int sym, ref int blankCount) { Random rnd = new Random(); int row = rnd.Next(0, 8); int column = rnd.Next(0, 8); while (tempGrid.Grid[row, column] == 0) { row = rnd.Next(0, 8); column = rnd.Next(0, 8); } tempGrid.InitSetCell(row, column, 0); blankCount++; switch (sym) { case 0: if (tempGrid.Grid[row, 8 - column] != 0) { blankCount++; } tempGrid.InitSetCell(row, 8 - column, 0); break; case 1: if (tempGrid.Grid[column, row] != 0) { blankCount++; } tempGrid.InitSetCell(column, row, 0); break; case 2: if (tempGrid.Grid[column, row] != 0) { blankCount++; } tempGrid.InitSetCell(column, row, 0); break; default: if (tempGrid.Grid[row, 8 - column] != 0) { blankCount++; } tempGrid.InitSetCell(column, row, 0); break; } return(tempGrid); }
/// <summary> /// This function opens a game. /// </summary> public PuzzleGrid OpenFile(String fileName) { PuzzleGrid openedPuzzle = new PuzzleGrid(); int i = 0; //mnemonic: row int j = 0; //mnemonic: column int cellVal = 0; bool eOF = false; using (StreamReader reader = new StreamReader(fileName)) { for (i = 0; i < 9; i++) { string currentLine; if ((currentLine = reader.ReadLine()) != null) { for (j = 0; j < 9; j++) { cellVal = (int)currentLine[2 * j] - '0'; if (currentLine[(2 * j) + 1] == '+') { openedPuzzle.InitSetCell(i, j, cellVal); } else if (currentLine[(2 * j) + 1] == '-') { openedPuzzle.InitSetCell(i, j, -(cellVal)); } } } else { eOF = true; } } } if (eOF) { return(null); } else { return(openedPuzzle); } }
/// <summary> /// This clones the object. /// </summary> /// <returns>A clone of itself.</returns> public object Clone() { //enable cloning for safe copying of the object PuzzleGrid p = new PuzzleGrid(); for (int i = 0; i < Max; i++) { for (int j = 0; j < Max; j++) { p.InitSetCell(i, j, Grid[i, j]); } } return(p); }