Beispiel #1
0
        private static int Print(String filename)
        {
            String text;

            try
            {
                text = File.ReadAllText(filename);
            }

            catch
            {
                Console.WriteLine(Program.FileNotFoundErrorMessage);
                return(Program.FileNotFoundError);
            }

            try
            {
                Console.WriteLine(SudokuPuzzle.Parse(text));
                return(0);
            }

            catch (FormatException)
            {
                Console.WriteLine(Program.FileFormatIncorrectErrorMessage);
                return(Program.FileFormatIncorrectError);
            }
        }
        internal Window(String filename) : this()
        {
            String text = File.ReadAllText(filename);

            this.Sudoku     = SudokuPuzzle.Parse(text);
            this.Size       = this.Sudoku.Size;
            this.UpperLimit = this.Size - 1;
        }
        public void Initialize()
        {
            const String SUDOKU_FILENAME = "Sudoku.txt";
            String       sudokuText;

            try
            {
                sudokuText = File.ReadAllText(SUDOKU_FILENAME);
            }

            catch
            {
                Assert.Fail($"A sudoku puzzle must be exist in the current directory. The file must be called \"{SUDOKU_FILENAME}\".");
                return;
            }

            const String SOLUTION_FILENAME = "Solution.txt";
            String       solutionText;

            try
            {
                solutionText = File.ReadAllText(SOLUTION_FILENAME);
            }

            catch
            {
                Assert.Fail($"A sudoku solution must be exist in the current directory. The file must be called \"{SOLUTION_FILENAME}\".");
                return;
            }

            try
            {
                this.Sudoku = SudokuPuzzle.Parse(sudokuText);
            }

            catch (FormatException)
            {
                Assert.Fail($"The sudoku puzzle specified in the file {SUDOKU_FILENAME} was not in the correct format");
                return;
            }

            try
            {
                this.Solution = SudokuPuzzle.Parse(solutionText);
            }

            catch (FormatException)
            {
                Assert.Fail($"The sudoku solution specified in the file {SOLUTION_FILENAME} was not in the correct format");
                return;
            }
        }
Beispiel #4
0
        public void GetsValueOfABox()
        {
            byte?[] expectedBox1 = { 5, 3, 4, 6, 7, 2, 1, 9, 8 };
            byte?[] expectedBox2 = { 6, 7, 8, 1, 9, 5, 3, 4, 2 };
            byte?[] expectedBox3 = { 9, 1, 2, 3, 4, 8, 5, 6, 7 };
            byte?[] expectedBox4 = { 8, 5, 9, 4, 2, 6, 7, 1, 3 };
            byte?[] expectedBox5 = { 7, 6, 1, 8, 5, 3, 9, 2, 4 };
            byte?[] expectedBox6 = { 4, 2, 3, 7, 9, 1, 8, 5, 6 };
            byte?[] expectedBox7 = { 9, 6, 1, 2, 8, 7, 3, 4, 5 };
            byte?[] expectedBox8 = { 5, 3, 7, 4, 1, 9, 2, 8, 6 };
            byte?[] expectedBox9 = { 2, 8, 4, 6, 3, 5, 1, 7, 9 };

            var puzzle = SudokuPuzzle.Parse(@"
534 678 912
672 195 348
198 342 567

859 761 423
426 853 791
713 924 856

961 537 284
287 419 635
345 286 179
");

            var box1 = puzzle.GetValuesOfBox(1);
            var box2 = puzzle.GetValuesOfBox(2);
            var box3 = puzzle.GetValuesOfBox(3);
            var box4 = puzzle.GetValuesOfBox(4);
            var box5 = puzzle.GetValuesOfBox(5);
            var box6 = puzzle.GetValuesOfBox(6);
            var box7 = puzzle.GetValuesOfBox(7);
            var box8 = puzzle.GetValuesOfBox(8);
            var box9 = puzzle.GetValuesOfBox(9);

            Assert.Equal(expectedBox1, box1);
            Assert.Equal(expectedBox2, box2);
            Assert.Equal(expectedBox3, box3);
            Assert.Equal(expectedBox4, box4);
            Assert.Equal(expectedBox5, box5);
            Assert.Equal(expectedBox6, box6);
            Assert.Equal(expectedBox7, box7);
            Assert.Equal(expectedBox8, box8);
            Assert.Equal(expectedBox9, box9);
        }
        public void ChecksNumbersOfEachSection()
        {
            var invalidPuzzleAtSection1 = SudokuPuzzle.Parse(@"
534|678|912
672|195|348
398|142|567
-----------
859|764|123
426|853|791
743|921|856
-----------
961|537|284
287|419|635
135|286|479");

            var result = this.testee.Validate(invalidPuzzleAtSection1);

            result.Valid.Should().BeFalse($"the puzzle is invalid because of a double 3 in section 1");
        }
        public void ChecksNumbersOfEachColumn()
        {
            var invalidPuzzleAtColumn4 = SudokuPuzzle.Parse(@"
514|678|932
672|195|348
398|142|567
-----------
859|764|123
426|853|791
743|921|856
-----------
961|537|284
287|139|615
135|286|479");

            var result = this.testee.Validate(invalidPuzzleAtColumn4);

            result.Valid.Should().BeFalse($"the puzzle is invalid because of a double 1 in column 4");
        }
        public void ChecksNumbersOfEachRow()
        {
            var invalidPuzzleAtRow5 = SudokuPuzzle.Parse(@"
534|678|912
672|195|348
198|342|567
-----------
859|761|423
426|853|891
713|924|856
-----------
961|537|284
287|419|635
345|286|179");

            var result = this.testee.Validate(invalidPuzzleAtRow5);

            result.Valid.Should().BeFalse($"the puzzle is invalid because of a double 8 in row 5");
        }
        public void ChecksSolvedPuzzle()
        {
            var puzzle = SudokuPuzzle.Parse(@"
534|678|912
672|195|348
198|342|567
-----------
859|761|423
426|853|791
713|924|856
-----------
961|537|284
287|419|635
345|286|179");

            var result = this.testee.Validate(puzzle);

            result.Valid.Should().BeTrue($"the puzzle is valid - {result.Message}");
        }
Beispiel #9
0
        public void SolvesVeryEasyPuzzle()
        {
            var puzzle = SudokuPuzzle.Parse(@"
534 678 912
672 195 348
198 342 567

859 761 423
426 853 791
713 924 856

961 537 284
287 419 635
345 286 179
");

            var result = this.testee.Solve(puzzle);

            Assert.False(true, "Not yet implemented");
        }
        public void TestParse()
        {
            Assert.ThrowsException <ArgumentNullException>(() => SudokuPuzzle.Parse(null));
            Assert.ThrowsException <FormatException>(() => SudokuPuzzle.Parse(String.Empty));
            int[,] array = new int[SudokuPuzzle.MaximumSupportedSize, SudokuPuzzle.MaximumSupportedSize];
            Random        random = new Random();
            StringBuilder sb     = new StringBuilder();

            for (int i = 0; i < SudokuPuzzle.MaximumSupportedSize; i++)
            {
                for (int j = 0; j < SudokuPuzzle.MaximumSupportedSize; j++)
                {
                    sb.Append(array[i, j] = random.Next(SudokuPuzzle.MaximumSupportedSize) + 1);
                }
            }

            SudokuPuzzle puzzle;

            try
            {
                puzzle = SudokuPuzzle.Parse(sb.ToString());
            }

            catch (Exception ex)
            {
                Assert.Fail($"Threw exception: {ex}");
                return;
            }

            Assert.AreEqual(puzzle.Size, SudokuPuzzle.MaximumSupportedSize);
            Assert.AreEqual(puzzle.Difficulty, SudokuDifficulty.None);

            for (int i = 0; i < SudokuPuzzle.MaximumSupportedSize; i++)
            {
                for (int j = 0; j < SudokuPuzzle.MaximumSupportedSize; j++)
                {
                    Assert.AreEqual(puzzle[i, j], array[i, j]);
                }
            }
        }
Beispiel #11
0
        private static int Compare(String file1, String file2)
        {
            String text1, text2;

            try
            {
                text1 = File.ReadAllText(file1);
                text2 = File.ReadAllText(file2);
            }

            catch
            {
                Console.WriteLine(Program.FileNotFoundErrorMessage);
                return(Program.FileNotFoundError);
            }

            SudokuPuzzle a, b;

            try
            {
                a = SudokuPuzzle.Parse(text1);
                b = SudokuPuzzle.Parse(text2);
            }

            catch (FormatException)
            {
                Console.WriteLine(Program.FileFormatIncorrectErrorMessage);
                return(Program.FileFormatIncorrectError);
            }

            Console.WriteLine("Sudoku puzzle A:");
            Console.WriteLine(a);
            Console.WriteLine();
            Console.WriteLine("Sudoku puzzle B:");
            Console.WriteLine(b);
            Console.WriteLine();
            Console.WriteLine(a == b ? "The two puzzles are equal." : "The two puzzles are NOT equal.");
            return(0);
        }
Beispiel #12
0
        public void ParsesPuzzle()
        {
            var puzzle = SudokuPuzzle.Parse(@"
534 678 912
672 195 348
198 342 567

859 761 423
426 853 791
713 924 856

961 537 284
287 419 635
345 286 179
");

            puzzle[1, 1].Should().Be(5);
            puzzle[1, 2].Should().Be(3);
            puzzle[1, 3].Should().Be(4);
            puzzle[1, 4].Should().Be(6);
            puzzle[1, 5].Should().Be(7);
            puzzle[1, 6].Should().Be(8);
            puzzle[1, 7].Should().Be(9);
            puzzle[1, 8].Should().Be(1);
            puzzle[1, 9].Should().Be(2);

            puzzle[2, 1].Should().Be(6);
            puzzle[2, 2].Should().Be(7);
            puzzle[2, 3].Should().Be(2);
            puzzle[2, 4].Should().Be(1);
            puzzle[2, 5].Should().Be(9);
            puzzle[2, 6].Should().Be(5);
            puzzle[2, 7].Should().Be(3);
            puzzle[2, 8].Should().Be(4);
            puzzle[2, 9].Should().Be(8);

            puzzle[3, 1].Should().Be(1);
            puzzle[3, 2].Should().Be(9);
            puzzle[3, 3].Should().Be(8);
            puzzle[3, 4].Should().Be(3);
            puzzle[3, 5].Should().Be(4);
            puzzle[3, 6].Should().Be(2);
            puzzle[3, 7].Should().Be(5);
            puzzle[3, 8].Should().Be(6);
            puzzle[3, 9].Should().Be(7);

            puzzle[4, 1].Should().Be(8);
            puzzle[4, 2].Should().Be(5);
            puzzle[4, 3].Should().Be(9);
            puzzle[4, 4].Should().Be(7);
            puzzle[4, 5].Should().Be(6);
            puzzle[4, 6].Should().Be(1);
            puzzle[4, 7].Should().Be(4);
            puzzle[4, 8].Should().Be(2);
            puzzle[4, 9].Should().Be(3);

            puzzle[5, 1].Should().Be(4);
            puzzle[5, 2].Should().Be(2);
            puzzle[5, 3].Should().Be(6);
            puzzle[5, 4].Should().Be(8);
            puzzle[5, 5].Should().Be(5);
            puzzle[5, 6].Should().Be(3);
            puzzle[5, 7].Should().Be(7);
            puzzle[5, 8].Should().Be(9);
            puzzle[5, 9].Should().Be(1);

            puzzle[6, 1].Should().Be(7);
            puzzle[6, 2].Should().Be(1);
            puzzle[6, 3].Should().Be(3);
            puzzle[6, 4].Should().Be(9);
            puzzle[6, 5].Should().Be(2);
            puzzle[6, 6].Should().Be(4);
            puzzle[6, 7].Should().Be(8);
            puzzle[6, 8].Should().Be(5);
            puzzle[6, 9].Should().Be(6);

            puzzle[7, 1].Should().Be(9);
            puzzle[7, 2].Should().Be(6);
            puzzle[7, 3].Should().Be(1);
            puzzle[7, 4].Should().Be(5);
            puzzle[7, 5].Should().Be(3);
            puzzle[7, 6].Should().Be(7);
            puzzle[7, 7].Should().Be(2);
            puzzle[7, 8].Should().Be(8);
            puzzle[7, 9].Should().Be(4);

            puzzle[8, 1].Should().Be(2);
            puzzle[8, 2].Should().Be(8);
            puzzle[8, 3].Should().Be(7);
            puzzle[8, 4].Should().Be(4);
            puzzle[8, 5].Should().Be(1);
            puzzle[8, 6].Should().Be(9);
            puzzle[8, 7].Should().Be(6);
            puzzle[8, 8].Should().Be(3);
            puzzle[8, 9].Should().Be(5);

            puzzle[9, 1].Should().Be(3);
            puzzle[9, 2].Should().Be(4);
            puzzle[9, 3].Should().Be(5);
            puzzle[9, 4].Should().Be(2);
            puzzle[9, 5].Should().Be(8);
            puzzle[9, 6].Should().Be(6);
            puzzle[9, 7].Should().Be(1);
            puzzle[9, 8].Should().Be(7);
            puzzle[9, 9].Should().Be(9);
        }
Beispiel #13
0
        private static int Enumerate(String filename)
        {
            String text;

            if (!File.Exists(filename))
            {
                Console.WriteLine(Program.FileNotFoundErrorMessage);
                return(Program.FileNotFoundError);
            }

            try
            {
                text = File.ReadAllText(filename);
            }

            catch
            {
                Console.WriteLine(Program.FileFormatIncorrectErrorMessage);
                return(Program.FileFormatIncorrectError);
            }

            SudokuPuzzle sudoku;

            try
            {
                sudoku = SudokuPuzzle.Parse(text);
            }

            catch (FormatException)
            {
                Console.WriteLine(Program.FileFormatIncorrectErrorMessage);
                return(Program.FileFormatIncorrectError);
            }

            bool solvable = SudokuSolver.CheckSolvable(sudoku, out bool multipleSolutions);

            if (solvable)
            {
                Console.WriteLine("The specified puzzle is solvable.");

                if (multipleSolutions)
                {
                    Console.WriteLine("The specified puzzle has multiple possible solutions.");
                }

                else
                {
                    Console.WriteLine("The specified puzzle has a single valid solution.");
                    Stopwatch stopwatch = new Stopwatch();
                    stopwatch.Start();
                    SudokuSolver.RecursiveSolve(sudoku);
                    stopwatch.Stop();
                    Console.WriteLine(sudoku);
                    Console.WriteLine($"Puzzle solved recursively in {stopwatch.ElapsedMilliseconds}ms");
                }
            }

            else
            {
                Console.WriteLine("The specified puzzle is NOT solvable.");
            }

            return(0);
        }
Beispiel #14
0
        private static int Solve(String filename, String outfile)
        {
            String text;

            try
            {
                text = File.ReadAllText(filename);
            }

            catch
            {
                Console.WriteLine(Program.FileNotFoundErrorMessage);
                return(Program.FileNotFoundError);
            }

            SudokuPuzzle sudoku;

            try
            {
                sudoku = SudokuPuzzle.Parse(text);
            }

            catch (FormatException)
            {
                Console.WriteLine(Program.FileFormatIncorrectErrorMessage);
                return(Program.FileFormatIncorrectError);
            }

            StringBuilder sb = new StringBuilder();

            sb.AppendLine("Sudoku from file, unsolved:");
            sb.AppendLine(sudoku.ToString());
            sb.AppendLine();
            Stopwatch    stopwatch = new Stopwatch();
            SudokuSolver solver    = new SudokuSolver(sudoku);

            stopwatch.Start();
            IEnumerable <SudokuMove> moves = solver.Solve();

            stopwatch.Stop();
            double       time      = stopwatch.ElapsedMilliseconds;
            int          count     = 0;
            const int    PADDING   = 16;
            const String SEPARATOR = "\t";

            sb.AppendLine(String.Join(SEPARATOR, '#', "Row(s)".PadRight(PADDING), "Column(s)".PadRight(PADDING), "Number(s)".PadRight(PADDING), "Pattern".PadRight(PADDING), "Possible".PadRight(PADDING)));

            foreach (SudokuMove move in moves)
            {
                sb.AppendLine(String.Join('\t', ++count, move));
            }

            sb.AppendLine($"Solved puzzle using {count} moves in {time}ms");

            /*if (!sudoku.IsComplete)
             * {
             *      stopwatch.Restart();
             *      SudokuSolver.RecursiveSolve(sudoku);
             *      stopwatch.Stop();
             *      double newTime = stopwatch.ElapsedMilliseconds;
             *      sb.AppendLine();
             *      sb.AppendLine(sudoku.ToString());
             *      sb.AppendLine();
             *      sb.AppendLine($"Solved remainder of puzzle using recursion in {newTime}ms");
             *      sb.AppendLine($"Total time for solving is {time + newTime} ms");
             * }*/

            sb.AppendLine();
            sb.AppendLine("Solved sudoku:");
            sb.AppendLine(sudoku.ToString());
            String output = sb.ToString();

            if (outfile is null)
            {
                Console.WriteLine(output);
            }

            else
            {
                if (Program.WriteToFile(output, outfile))
                {
                    Console.WriteLine($"Solved puzzle using {count} moves in {time}ms");
                    Console.WriteLine($"Moves used to solve sudoku written to {outfile}");
                }

                else
                {
                    Console.WriteLine(Program.FileOutputErrorMessage);
                    return(Program.FileOutputError);
                }
            }

            return(0);
        }