Example #1
0
        public List<Cell> GetSolution()
        {
            int[,] arr = new int[9, 9];
            foreach (Cell cell in _puzzle)
            {
                if (cell.X >= arr.GetLength(0) || cell.Y >= arr.GetLength(1) || cell.X < 0 || cell.Y < 0)
                {
                    throw new IndexOutOfRangeException();
                }

                int v = 0;
                if(int.TryParse(cell.Value, out v))
                {
                    if (v < 0 || v > 9)
                    {
                        throw new ArgumentOutOfRangeException();
                    }

                    arr[cell.X, cell.Y] = v;
                }
            }

            PuzzleSolver solver = new PuzzleSolver(arr);
            return ArrayToCells(solver.GetSolution());
        }
        public void TEST_ConstructorThrowsDuplicateValueExceptionOnRow()
        {
            int[,] puzzle = new int[,]
            {
                { 0, 7, 1, 0, 0, 0, 7, 0, 4 },
                { 5, 0, 0, 0, 7, 0, 0, 6, 0 },
                { 0, 6, 0, 1, 4, 0, 0, 0, 7 },
                { 0, 0, 0, 8, 3, 0, 0, 4, 5 },
                { 0, 3, 0, 0, 5, 0, 0, 7, 0 },
                { 4, 8, 0, 0, 1, 6, 0, 0, 0 },
                { 2, 0, 0, 0, 6, 3, 0, 1, 0 },
                { 0, 5, 0, 0, 8, 0, 0, 0, 6 },
                { 8, 0, 0, 0, 0, 0, 7, 2, 0 }
            };

            IPuzzleAnalyzer solver = new PuzzleSolver(puzzle);
        }
 public void TEST_Constructor()
 {
     IPuzzleAnalyzer solver = new PuzzleSolver(GetPuzzle());
     Assert.IsNotNull(solver);
 }
        public void TEST_GetSolution_IsNullWhenNoValidSolution()
        {
            int[,] puzzle = new int[,]
            {
                { 5, 1, 3, 0, 0, 4, 0, 0, 0, },
                { 0, 6, 0, 3, 0, 8, 1, 0, 0, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 0, 7, 0, 6, 0, 0, 0, },
                { 6, 5, 8, 0, 0, 0, 4, 3, 7, },
                { 0, 0, 0, 1, 0, 3, 0, 0, 0, },
                { 0, 0, 0, 0, 0, 0, 0, 0, 0, },
                { 0, 0, 6, 4, 0, 7, 0, 1, 0, },
                { 0, 0, 0, 8, 0, 0, 7, 4, 6, }
            };
            IPuzzleAnalyzer solver = new PuzzleSolver(puzzle);
            int[,] solution = solver.GetSolution();

            Assert.IsNull(solution);
        }
        public void TEST_GetSolution()
        {
            IPuzzleAnalyzer solver = new PuzzleSolver(GetPuzzle());
            int[,] solution = solver.GetSolution();

            Assert.IsNotNull(solution);
            Assert.AreNotEqual(GetPuzzle(), solution);
            Assert.Equals(solution, GetSolution());
        }