Example #1
0
        public void Solve_Test2()
        {
            var sb = new StringBuilder();

            sb.AppendLine("0 2 3 0");
            sb.AppendLine("0 0 0 0");
            sb.AppendLine("3 1 2 4");
            sb.AppendLine("0 0 0 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var solver = new PuzzleSolver(puzzle);
            var result = solver.Solve();

            Assert.True(result);

            sb.Clear();
            sb.AppendLine("4 2 3 1");
            sb.AppendLine("1 3 4 2");
            sb.AppendLine("3 1 2 4");
            sb.AppendLine("2 4 1 3");
            var expectedSolution = sb.ToString();

            Assert.Equal(expectedSolution, puzzle.ToString());
        }
Example #2
0
        public void GetColumn_4x4Puzzle_ReturnFThirdColumn()
        {
            var sb = new StringBuilder();

            sb.AppendLine("0 0 1 0");
            sb.AppendLine("0 0 2 0");
            sb.AppendLine("0 0 3 0");
            sb.AppendLine("0 0 4 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var column = puzzle.GetColumn(2);

            Assert.Equal("1 2 3 4", string.Join(" ", column));
        }
Example #3
0
        public void GetSquare_4x4Puzzle_ReturnFourthSquare()
        {
            var sb = new StringBuilder();

            sb.AppendLine("0 0 0 0");
            sb.AppendLine("0 0 0 0");
            sb.AppendLine("0 0 1 2");
            sb.AppendLine("0 0 3 4");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var square = puzzle.GetSquare(3);

            Assert.Equal("1 2 3 4", string.Join(" ", square));
        }
Example #4
0
        public void GetRow_4x4Puzzle_ReturnFThirdRow()
        {
            var sb = new StringBuilder();

            sb.AppendLine("0 0 0 0");
            sb.AppendLine("0 0 0 0");
            sb.AppendLine("1 2 3 4");
            sb.AppendLine("0 0 0 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var row = puzzle.GetRow(2);

            Assert.Equal("1 2 3 4", string.Join(" ", row));
        }
Example #5
0
        public void GetColumn_4x4Puzzle_ReturnFirstColumn()
        {
            var sb = new StringBuilder();

            sb.AppendLine("1 0 0 0");
            sb.AppendLine("2 0 0 0");
            sb.AppendLine("3 0 0 0");
            sb.AppendLine("4 0 0 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var column = puzzle.GetColumn(0);

            Assert.Equal("1 2 3 4", string.Join(" ", column));
        }
        public void IsValid_SameNumberInColumn_ReturnsFalse()
        {
            var sb = new StringBuilder();

            sb.AppendLine("0 0 0 0");
            sb.AppendLine("0 1 0 0");
            sb.AppendLine("0 2 0 0");
            sb.AppendLine("0 1 0 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var validator = new PuzzleValidator(puzzle);
            var actual    = validator.Valid;

            Assert.False(actual);
        }
        public void IsSolved_SolvedPuzzle_ReturnsTrue()
        {
            var sb = new StringBuilder();

            sb.AppendLine("1 4 3 2");
            sb.AppendLine("2 3 1 4");
            sb.AppendLine("3 2 4 1");
            sb.AppendLine("4 1 2 3");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var validator = new PuzzleValidator(puzzle);
            var actual    = validator.Solved;

            Assert.True(actual);
        }
        public void ValidMove_InvalidSquare()
        {
            var sb = new StringBuilder();

            sb.AppendLine("1 2 0 0");
            sb.AppendLine("3 0 0 0");
            sb.AppendLine("0 0 0 0");
            sb.AppendLine("0 0 0 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var validator = new PuzzleValidator(puzzle);
            var actual    = validator.ValidMove(1, 1, 1);

            Assert.False(actual);
        }
Example #9
0
        public void Solved_FullValidPuzzleButInvalidSetNumber_ReturnFalse()
        {
            var sb = new StringBuilder();

            sb.AppendLine("1 4 3 2");
            sb.AppendLine("2 3 1 4");
            sb.AppendLine("3 2 0 1");
            sb.AppendLine("4 1 2 3");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            puzzle.SetNumber(3, 2, 2);

            var actual = puzzle.Solved;

            Assert.False(actual);
        }
Example #10
0
        public void Valid_ValidPuzzleValidSetNumber_ReturnTrue()
        {
            var sb = new StringBuilder();

            sb.AppendLine("1 4 3 2");
            sb.AppendLine("2 3 1 4");
            sb.AppendLine("3 2 0 1");
            sb.AppendLine("4 1 2 3");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            puzzle.SetNumber(4, 2, 2);

            var actual = puzzle.Valid;

            Assert.True(actual);
        }
        public void ValidMove_NoStateChangeToPuzzle()
        {
            var sb = new StringBuilder();

            sb.AppendLine("1 2 0 0");
            sb.AppendLine("3 0 0 0");
            sb.AppendLine("0 0 0 0");
            sb.AppendLine("0 0 0 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var validator = new PuzzleValidator(puzzle);

            validator.ValidMove(4, 1, 1);

            Assert.Equal(0, puzzle.GetNumber(1, 1));
        }
Example #12
0
        public void GetSquare_9x9Puzzle_ReturnFifthSquare()
        {
            var sb = new StringBuilder();

            sb.AppendLine("0 0 0 0 0 0 0 0 0");
            sb.AppendLine("0 0 0 0 0 0 0 0 0");
            sb.AppendLine("0 0 0 0 0 0 0 0 0");
            sb.AppendLine("0 0 0 1 2 3 0 0 0");
            sb.AppendLine("0 0 0 4 5 6 0 0 0");
            sb.AppendLine("0 0 0 7 8 9 0 0 0");
            sb.AppendLine("0 0 0 0 0 0 0 0 0");
            sb.AppendLine("0 0 0 0 0 0 0 0 0");
            sb.AppendLine("0 0 0 0 0 0 0 0 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var square = puzzle.GetSquare(4);

            Assert.Equal("1 2 3 4 5 6 7 8 9", string.Join(" ", square));
        }
Example #13
0
        public void Solve_Test4()
        {
            var sb = new StringBuilder();

            sb.AppendLine("0 6 0 0 0 3 0 0 0");
            sb.AppendLine("0 0 4 6 0 0 3 7 0");
            sb.AppendLine("0 3 5 4 0 0 2 8 6");
            sb.AppendLine("6 2 8 5 0 0 0 0 4");
            sb.AppendLine("4 7 0 0 0 2 5 0 8");
            sb.AppendLine("3 0 0 9 0 8 0 6 0");
            sb.AppendLine("5 9 2 3 0 6 0 4 0");
            sb.AppendLine("0 8 6 7 5 0 0 2 0");
            sb.AppendLine("7 0 3 8 2 0 0 5 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var solver = new PuzzleSolver(puzzle);
            var result = solver.Solve();

            Assert.True(result);
        }
Example #14
0
        public void Solve_Test5()
        {
            var sb = new StringBuilder();

            sb.AppendLine("0 0 0 8 7 0 4 0 0");
            sb.AppendLine("0 0 0 6 0 0 9 5 3");
            sb.AppendLine("0 0 0 0 0 0 0 0 7");

            sb.AppendLine("7 0 4 0 9 0 2 0 0");
            sb.AppendLine("0 9 5 0 0 2 3 4 0");
            sb.AppendLine("0 0 0 0 0 5 0 0 0");

            sb.AppendLine("0 3 0 0 5 0 0 0 8");
            sb.AppendLine("0 0 0 2 8 0 0 7 0");
            sb.AppendLine("0 0 0 0 0 7 0 0 0");
            var puzzleString = sb.ToString();

            var puzzle = PuzzleHelper.BuildPuzzle(puzzleString);

            var solver = new PuzzleSolver(puzzle);
            var result = solver.Solve();

            Assert.True(result);
        }