/// <summary> /// /// </summary> /// <returns></returns> private SudokuHelper CreateCopy() { SudokuHelper copy = new SudokuHelper(); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { copy.s[i, j] = s[i, j]; } } return(copy); }
/// <summary> /// /// </summary> /// <returns></returns> public int ComputeErrors() { SudokuHelper S2 = this.CreateCopy(); int errors_count = 0; for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (f[j, i] == 0) { S2.s[j, i] = 0; } } } S2.SolvePuzzle(SolveMethods.All); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (S2.s[j, i] == s[j, i] || s[j, i] == 0) { e[j, i] = 0; } else { e[j, i] = 1; errors_count++; } } } return(errors_count); }
/// <summary> /// 创建一个数独游戏 /// </summary> /// <param name="level">游戏等级(0-简单,1-一般,2-难)</param> public void GenerateGame(int level = 0) { if (PlaySound != null) { PlaySound(SudokuSound.Stop); } UseTemplate(level); int trials_counter = 0; int trials_max = 100; SolveMethods M = SolveMethods.All; List <Point> lstPoint = new List <Point>(); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (s[j, i] > 0) { lstPoint.Add(new Point(i, j)); } } } for (int n = 0; n < 55;) { int index = R.Next(lstPoint.Count); int x = lstPoint[index].X; int y = lstPoint[index].Y; int temp = s[x, y]; if (temp != 0) { s[x, y] = 0; SudokuHelper S2 = CreateCopy(); int solutionStepsCount = S2.ComputePossibleSteps(M).Count(); bool range0Ok = ((n > 35) && (solutionStepsCount > 25)); bool range1Ok = ((n > 15) && (solutionStepsCount > 25)); bool range2Ok = n < 16; bool solutionStepsCountOk = range0Ok || range1Ok || range2Ok; if (solutionStepsCountOk) { bool isSolvable = S2.SolvePuzzle(M); if (isSolvable) { lstPoint.RemoveAt(index); n++; trials_counter = 0; } else { s[x, y] = temp; } } else { s[x, y] = temp; } trials_counter++; } if (trials_counter > trials_max) { break; } } ScrambleGame(); for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (s[i, j] == 0) { f[i, j] = 0; } else { f[i, j] = 1; } } } EnumeratePossibilities(); starttime = DateTime.Now; DisplayMessage = false; if (PlaySound != null) { PlaySound(SudokuSound.NewPuzzle); } if (RequestRepaint != null) { RequestRepaint(); } }
/// <summary> /// /// </summary> /// <param name="M"></param> /// <returns></returns> public bool checkSolvable(SolveMethods M) { SudokuHelper copy = CreateCopy(); return(copy.SolvePuzzle(M)); }