Beispiel #1
0
        static void Main(string[] args)
        {
            int[,] arr = new int[, ] {
                { 1, 2, 3, 4, 5, 6, 7, 8, 9 },
                { 2, 3, 4, 5, 7, 7, 8, 9, 1 },
                { 3, 4, 5, 6, 7, 8, 9, 1, 2 },

                { 4, 5, 6, 7, 8, 9, 1, 2, 3 },
                { 5, 6, 7, 8, 9, 1, 2, 3, 4 },
                { 6, 7, 8, 9, 1, 2, 3, 4, 5 },

                { 7, 8, 9, 1, 2, 3, 4, 5, 6 },
                { 8, 9, 1, 2, 3, 4, 5, 6, 7 },
                { 9, 1, 2, 3, 4, 5, 6, 7, 8 }
            };

            var result = SudokuValidator.Validate(arr);

            if (result.isValid)
            {
                Console.WriteLine("Sudoku is valid");
            }
            else
            {
                Console.WriteLine(result.message);
            }
        }
        public void Validate_CorrectInput_ReturnsValid()
        {
            //arrange
            int[,] init = new int[, ]
            {
                { 5, 3, 4, 6, 7, 8, 9, 1, 2 },
                { 6, 7, 2, 1, 9, 5, 3, 4, 8 },
                { 1, 9, 8, 3, 4, 2, 5, 6, 7 },
                { 8, 5, 9, 7, 6, 1, 4, 2, 3 },
                { 4, 2, 6, 8, 5, 3, 7, 9, 1 },
                { 7, 1, 3, 9, 2, 4, 8, 5, 6 },
                { 9, 6, 1, 5, 3, 7, 2, 8, 4 },
                { 2, 8, 7, 4, 1, 9, 6, 3, 5 },
                { 3, 4, 5, 2, 8, 6, 1, 7, 9 }
            };

            //act
            bool result = sudo.Validate(init);

            //assert
            Assert.That(result, Is.True);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            SudokuSolver ss = new SudokuSolver();

            SudokuFieldBuilder sudokuFieldBuilder = new SudokuFieldBuilder();
            SudokuValidator    sudokuValidator    = new SudokuValidator();

            var rang     = 9;
            var mass     = sudokuFieldBuilder.InitField().Mix().Build().Clone() as int?[, ];
            var gameMass = sudokuFieldBuilder.SetEmptyCount(35).DeleteNumbers().Build().Clone() as int?[, ];

            Console.WindowWidth = 200;



            for (int i = 0; i < rang; i++)
            {
                for (int j = 0; j < rang; j++)
                {
                    Console.Write((mass[i, j] ?? 0) + " ");
                }

                Console.WriteLine();
            }

            Console.WriteLine();


            ss.Solve(gameMass);

            Console.WriteLine();

game:
            for (int i = 0; i < rang; i++)
            {
                for (int j = 0; j < rang; j++)
                {
                    Console.Write(gameMass[i, j] + " ");
                }

                Console.WriteLine();
            }


            Console.Write("Row:");
            var row = Convert.ToInt32(Console.ReadLine());

            Console.Write("Column:");
            var column = Convert.ToInt32(Console.ReadLine());

            Console.Write("Value:");
            var value = Convert.ToInt32(Console.ReadLine());


            try
            {
                sudokuValidator.Validate(gameMass, new CellModel {
                    Column = column, Row = row, Value = value
                });

                gameMass[row, column] = value;
            }
            catch (AlreadyExistException ex)
            {
                Console.WriteLine(ex.Message + " " + ex.Cell.ToString());
            }

            goto game;
        }