Ejemplo n.º 1
0
        public Task <SudokuProblem> GenerateNewProblem()
        {
            SudokuProblem problem = null;

            // Poor man version of implementation
            do
            {
                problem = new SudokuProblem(this.GenerateRowsRandomly());
            }while (!this.ValidateProblem(problem));

            return(Task.FromResult(problem));
        }
Ejemplo n.º 2
0
        public bool ValidateProblem(SudokuProblem problem)
        {
            if (problem == null)
            {
                throw new ArgumentNullException(nameof(problem));
            }

            var set = new HashSet <string>();

            for (var i = 0; i < this.SudokuSize - 1; i++)
            {
                for (var j = 0; j < this.SudokuSize - 1; j++)
                {
                    if (problem.Rows[i] != null && problem.Rows[i].Columns != null && problem.Rows[i].Columns[j] != null && problem.Rows[i].Columns[j].Value != null)
                    {
                        if (!this.isValidNumber(problem.Rows[i].Columns[j].Value))
                        {
                            // Out of range
                            return(false);
                        }
                        else
                        {
                            // We use a hashmap to check if the value is already existed in the current row/column/square
                            var rowStr    = $"R{i}{problem.Rows[i].Columns[j].Value}";
                            var colStr    = $"C{j}{problem.Rows[i].Columns[j].Value}";
                            var squareStr = $"S{i / Math.Sqrt(this.SudokuSize) * Math.Sqrt(this.SudokuSize) + j / Math.Sqrt(this.SudokuSize)}{ problem.Rows[i].Columns[j].Value}";
                            if (set.Contains(rowStr) || set.Contains(colStr) || set.Contains(squareStr))
                            {
                                return(false);
                            }
                            set.Add(rowStr);
                            set.Add(colStr);
                            set.Add(squareStr);
                        }
                    }
                }
            }

            return(true);
        }
Ejemplo n.º 3
0
 public bool ValidateProblem([FromBody] SudokuProblem sudokuProblem)
 {
     return(this.sudokuService.ValidateProblem(sudokuProblem));
 }
Ejemplo n.º 4
0
 public Task <bool> Save(SudokuProblem problem)
 {
     return(Task.FromResult(storage.TryAdd(storage.Count, problem)));
 }