Esempio n. 1
0
        public ActionResult SolveSudoku(string sudokuToProcess)
        {
            var stopWatch = Stopwatch.StartNew();

            SortedList <string, string> extendedGridValues         = SudokuGenerator.PopulateGridWithPossibleValues(sudokuToProcess);
            SortedList <string, string> oneDimensionalSudokuResult = SudokuGenerator.Search(extendedGridValues);

            string[,] SudokuArrayOutput = null;
            if (oneDimensionalSudokuResult != null && oneDimensionalSudokuResult.Count > 0)
            {
                SudokuArrayOutput = new string[9, 9];
                int elementPosition = 0;
                for (int row = 0; row < 9; row++)
                {
                    for (int cell = 0; cell < 9; cell++)
                    {
                        SudokuArrayOutput[row, cell] = oneDimensionalSudokuResult.Values.ToArray()[elementPosition];
                        elementPosition++;
                    }
                }
            }

            stopWatch.Stop();

            var returnObject = new { timeTakenToProcess = Convert.ToInt32(stopWatch.Elapsed.TotalMilliseconds), sudokuOutput = SudokuArrayOutput };

            return(Json(returnObject));
        }
Esempio n. 2
0
        public void When_Invalid_Sudoku_Do_Not_Populate_Grid_With_Possible_Values()
        {
            var invalidSudoku = "A5...24..72......9..4.........1.7..23.5...9...4...........8..7..17..........36.4.";
            SortedList <string, string> sortedList = SudokuGenerator.PopulateGridWithPossibleValues(invalidSudoku);

            Assert.AreEqual(0, sortedList == null ? 0 : 1, "Invalid sudoku.");
        }
Esempio n. 3
0
        public void When_Invalid_Sudoku_Search_To_Fail()
        {
            var  sudokuWithInvalidPositions = "44....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......";
            bool actual = SudokuGenerator.IsInputSudokuValid(sudokuWithInvalidPositions);

            Assert.IsTrue(actual, "Valid length, degits in sudoku");

            //Populate list will be called once
            var sortedList = SudokuGenerator.PopulateGridWithPossibleValues(sudokuWithInvalidPositions);

            Assert.AreEqual(1, sortedList == null ? 0 : 1, "Populating grid with possible values successfull");

            //Search will be called and will retun null as result
            sortedList = SudokuGenerator.Search(sortedList);
            Assert.AreEqual(0, sortedList == null ? 0 : 1, "Search fails as only choice fails for the second degit and retuns null as output");
        }
Esempio n. 4
0
        public void Verify_Processed_Sudoku_Count()
        {
            int count   = 0;
            var sudokus = @"4.....8.5.3..........7......2.....6.....8.4......1.......6.3.7.5..2.....1.4......
						52...6.........7.13...........4..8..6......5...........418.........3..2...87.....
						6.....8.3.4.7.................5.4.7.3..2.....1.6.......2.....5.....8.6......1....
						48.3............71.2.......7.5....6....2..8.............1.76...3.....4......5....
						....14....3....2...7..........9...3.6.1.............8.2.....1.4....5.6.....7.8..."                        .Split('\n');

            foreach (var sudokuInput in sudokus)
            {
                var inputSudokuString = sudokuInput.Trim();

                SortedList <string, string> sortedList = new SortedList <string, string>();
                try
                {
                    sortedList = SudokuGenerator.Search(SudokuGenerator.PopulateGridWithPossibleValues(inputSudokuString));

                    if (sortedList != null)
                    {
                        var width = 1 + (from s in boxes select sortedList[s].Length).Max();
                        var line  = "\n" + String.Join("+", Enumerable.Repeat(new String('-', width * 3), 3).ToArray());

                        Console.WriteLine("Output of Sudoku " + inputSudokuString + " - is as below : ");

                        foreach (var r in rows)
                        {
                            Console.WriteLine(String.Join("",
                                                          (from c in columns
                                                           select sortedList["" + r + c] + ("36".Contains(c) ? "|" : "")).ToArray())
                                              + ("CF".Contains(r) ? line : ""));
                        }

                        Console.WriteLine();
                    }
                    count++;
                }
                catch (Exception)
                {
                    Console.WriteLine("Unable to process : " + inputSudokuString);
                    Console.WriteLine();
                }
            }

            //Validate if 5 sudoku processed successfully and loop called exactly 5 times
            Assert.AreEqual(5, count, "Loop called exactly 5 times");
        }