public void AddSolutionTest_HasSolution() { var result = new SudokuResult(); result.AddSolution(new int[1, 1]); Assert.IsTrue(result.HasSolution); }
private async Task SendSudokuError(SudokuResult error) { if (error == SudokuResult.BadCoords) { await ReplyAsync("Invalid set of board coordinates. To specify a cell, enter the row letter followed by the column number. Ex: \"B7\""); } else if (error == SudokuResult.GameNotExists) { await ReplyAsync("There is no active sudoku game. Use \"!sudoku new\" to create a new sudoku game."); } else if (error == SudokuResult.GameExists) { await ReplyAsync("There is a currently active sudoku game. Use \"!sudoku show\" to re-display the board, or \"!sudoku end\" to end the game."); } else if (error == SudokuResult.BadVal) { await ReplyAsync("Cannot insert the specified value. Value must be in the range 1 to 9 inclusively."); } else if (error == SudokuResult.Conflict) { await ReplyAsync("Cannot insert value at the specified location as it conflicts with another cell in the same row, column, or box."); } else if (error == SudokuResult.Occupied) { await ReplyAsync("The specified location already has a value. You can remove the value using \"!sudoku del\""); } else if (error == SudokuResult.Unoccupied) { await ReplyAsync("The specified location does not have a value. You can add a value using \"!sudoku set\""); } else if (error == SudokuResult.Immutable) { await ReplyAsync("The specified location cannot be modified as it is part of the original puzzle."); } }
public void AddSolutionTest_NotUnique() { var result = new SudokuResult(); result.AddSolution(new int[1, 1]); result.AddSolution(new int[1, 1]); Assert.IsFalse(result.IsUnique); }
public ISudokuResult GenerateSolved() { int[,] data = new int[BlockCount, BlockCount]; var result = new SudokuResult(); FindAllCandidates(data); Solve(data, true, result); return(result); }
public ISudokuResult Solve(int[,] data) { var result = new SudokuResult(); FindAllCandidates(data); int[,] tempData = new int[BlockCount, BlockCount]; Array.Copy(data, tempData, BlockCount * BlockCount); Solve(tempData, false, result); return(result); }
// Save date, time and level of a completed game into a json file private void SaveResultToFile() { SudokuResult result = new SudokuResult() { date = System.DateTime.Now.ToShortDateString(), time = SudokuGameManager.GetTimeString(SudokuGameManager.GameTime), level = SudokuGameManager.GameMode.ToString() }; string jsonResult = JsonUtility.ToJson(result); File.WriteAllText(Application.dataPath + "/sudoku_results.txt", jsonResult); Debug.Log("Sudoku result saved"); }
private ISudokuResult RemovePossibleValue(int[,] data, ISudokuLevel level) { var notSuitableCells = new List <SudokuCell>(); while (_cellsForHiding.Count > 0) { var cell = _cellsForHiding.Dequeue(); if (notSuitableCells.Contains(cell)) { continue; } int value = data[cell.X, cell.Y]; data[cell.X, cell.Y] = 0; var result = _solver.Solve(data); if (result.HasUniqueSolution) { if (level.IsMatching(result)) { var generateResult = new SudokuResult { BackTrackCount = result.BackTrackCount, ComplexitiesScore = result.ComplexitiesScore }; generateResult.AddSolution(data); return(generateResult); } if (!level.IsMaxLimitExceeded(result)) { result = RemovePossibleValue(data, level); if (result != null) { return(result); } } } data[cell.X, cell.Y] = value; notSuitableCells.Add(cell); } foreach (var cell in notSuitableCells) { _cellsForHiding.Enqueue(cell); } return(null); }
private Tuple <SudokuCell, List <int> > FindNextCell(SudokuResult result) { var cellsWithNonEmptyCandidates = _cellCandidatesDictionary.Values.Where(x => x.Count > 0).ToList(); if (cellsWithNonEmptyCandidates.Any()) { var levelDictionary = new Dictionary <int, int>(); foreach (var values in _cellCandidatesDictionary.Values) { if (!levelDictionary.ContainsKey(values.Count)) { levelDictionary.Add(values.Count, 0); } levelDictionary[values.Count]++; } result.ComplexitiesScore += levelDictionary.Select(x => x.Key * x.Value).Sum(); int minWeight = cellsWithNonEmptyCandidates.Min(x => x.Count); return(_cellCandidatesDictionary.Where(x => x.Value.Count == minWeight) .Select(x => new Tuple <SudokuCell, List <int> >(x.Key, x.Value.ToList())) .FirstOrDefault()); } return(null); }
private bool Solve(int[,] data, bool useRandom, SudokuResult result) { var keyValuesPair = FindNextCell(result); if (keyValuesPair == null) { var solveResult = new int[BlockCount, BlockCount]; Array.Copy(data, solveResult, BlockCount * BlockCount); result.AddSolution(solveResult); return(true); } result.BackTrackCount++; List <int> candidateList = useRandom ? keyValuesPair.Item2.OrderBy(a => Guid.NewGuid()).ToList() : keyValuesPair.Item2; foreach (int value in candidateList) { var cells = ApplyCandidate(keyValuesPair.Item1, value); if (ValidateIntegrity(cells)) { data[keyValuesPair.Item1.X, keyValuesPair.Item1.Y] = value; var values = new List <int>(_cellCandidatesDictionary[keyValuesPair.Item1]); _cellCandidatesDictionary[keyValuesPair.Item1].Clear(); if (Solve(data, useRandom, result) && !result.IsUnique) { return(true); } data[keyValuesPair.Item1.X, keyValuesPair.Item1.Y] = 0; _cellCandidatesDictionary[keyValuesPair.Item1].AddRange(values); } UndoApplyCandidate(cells, value); } return(false); }
public void SudokuResult_NoSolution() { var result = new SudokuResult(); Assert.IsFalse(result.HasSolution); }
public void SudokuResult_Unique() { var result = new SudokuResult(); Assert.IsTrue(result.IsUnique); }