Ejemplo n.º 1
0
        public override bool SolveCell(Puzzle puzzle, int r, int c)
        {
            if (r == puzzle.TotalRows)
                return true;

            if (puzzle.Cells[r, c].Value != '-')
            {
                if (SolveNext(puzzle, r, c))
                    return true;
                return false;
            }

            foreach (var nextValue in puzzle.CharacterSet)
            {
                if (IsValidMove(puzzle, r, c, nextValue))
                {
                    puzzle.Cells[r, c].Value = nextValue;
                    if (SolveNext(puzzle, r, c))
                        return true;
                }
            }

            puzzle.Cells[r, c].Value = '-';
            return false;
        }
Ejemplo n.º 2
0
 public bool IsSolved(Puzzle puzzle)
 {
     for (int i = 0; i < puzzle.TotalRows; i++)
         for (int j = 0; j < puzzle.TotalRows; j++)
             if (puzzle.Cells[i, j].Value == '-')
                 return false;
     return true;
 }
Ejemplo n.º 3
0
        public void CantSolve()
        {
            string[] solvedRows = "4231\n1342\n3124\n2413".Split();
            Puzzle solvedPuzzle = new Puzzle(4, "1234", solvedRows);
            bool solved = puzzle.Solve();

            Assert.IsTrue(solved);
            CollectionAssert.AreEqual(puzzle.Cells, solvedPuzzle.Cells);
        }
Ejemplo n.º 4
0
        public void ReadPuzzle9x9()
        {
            string[] expectedRows = "---------\n---------\n---------\n---------\n---------\n---------\n---------\n---------\n---------".Split();
            Puzzle expected9 = new Puzzle(9, "123456789", expectedRows);
            Puzzle actual9 = PuzzleReader.ReadPuzzle("test9.txt");

            CollectionAssert.AreEqual(expected9.Cells, actual9.Cells);
            CollectionAssert.AreEqual(expected9.CharacterSet, actual9.CharacterSet);
            Assert.AreEqual(expected9.TotalRows, actual9.TotalRows);
        }
Ejemplo n.º 5
0
        public void ReadPuzzle4x4()
        {
            string[] expectedRows = "42-1\n---2\n3-2-\n-4-3".Split();
            Puzzle expected4 = new Puzzle(4, "1234", expectedRows);
            Puzzle actual4 = PuzzleReader.ReadPuzzle("test4.txt");

            CollectionAssert.AreEqual(expected4.Cells, actual4.Cells);
            CollectionAssert.AreEqual(expected4.CharacterSet, actual4.CharacterSet);
            Assert.AreEqual(expected4.TotalRows, actual4.TotalRows);
        }
Ejemplo n.º 6
0
        private bool IsValidMove(Puzzle puzzle, int r, int c, char value)
        {
            var row = puzzle.RowAt(r);
            var col = puzzle.ColumnAt(c);
            var box = puzzle.BoxAt(r, c);

            return !(row.Any(cell => cell.Value == value) ||
                     col.Any(cell => cell.Value == value) ||
                     box.Any(cell => cell.Value == value));
        }
Ejemplo n.º 7
0
        private bool SolveNext(Puzzle puzzle, int r, int c)
        {
            if (c == puzzle.TotalRows - 1)
            {
                if (SolveCell(puzzle, r + 1, 0))
                    return true;
            }
            else if (SolveCell(puzzle, r, c + 1))
                return true;

            return false;
        }
Ejemplo n.º 8
0
        private bool FindLastOne(Puzzle puzzle, int r, int c, Cell[] searchMe)
        {
            var numDashes = searchMe.Where(cell => cell.Value == '-')
                                    .Count();
            if (numDashes == 1)
            {
                puzzle.Cells[r, c].Value = puzzle.CharacterSet.Except( searchMe.Select(cell => cell.Value) )
                                                              .Single();
                return true;
            }

            return false;
        }
Ejemplo n.º 9
0
        public bool SolvePuzzle(Puzzle puzzle)
        {
            bool tryAgain = true;

            while (tryAgain)
            {
                tryAgain = false;
                for (int i = 0; i < puzzle.TotalRows; i++)
                    for (int j = 0; j < puzzle.TotalRows; j++)
                        if (puzzle.Cells[i, j].Value == '-')
                            tryAgain = SolveCell(puzzle, i, j) ? true : tryAgain;
            }
            return IsSolved(puzzle);
        }
Ejemplo n.º 10
0
 private void loadToolStripMenuItem_Click(object sender, EventArgs e)
 {
     if(openFileDialog.ShowDialog() == DialogResult.OK)
     {
         puzzle = PuzzleReader.ReadPuzzle(openFileDialog.FileName);
         if (puzzle != null)
         {
             RefreshPuzzle();
             solveButton.Enabled = true;
         }
         else
             MessageBox.Show("File is corrupted, check format.");
     }
 }
Ejemplo n.º 11
0
 public override bool SolveCell(Puzzle puzzle, int r, int c)
 {
     return FindLastOne(puzzle, r, c, puzzle.BoxAt(r, c)) ||
            FindLastOne(puzzle, r, c, puzzle.ColumnAt(c)) ||
            FindLastOne(puzzle, r, c, puzzle.RowAt(r));
 }
Ejemplo n.º 12
0
 public static void WritePuzzle(string filename, Puzzle puzzle)
 {
     if (filename != null && puzzle != null)
         File.WriteAllText(filename, puzzle.ToString());
 }
Ejemplo n.º 13
0
 public abstract bool SolveCell(Puzzle puzzle, int r, int c);
Ejemplo n.º 14
0
 public void Init()
 {
     puzzle = new Puzzle(4, "1234", "42-1\n---2\n3-2-\n-4-3".Split());
 }
Ejemplo n.º 15
0
 public void Init()
 {
     puzzle = new Puzzle(4, "1234", rows);
 }
Ejemplo n.º 16
0
        public void Solve()
        {
            string[] solveMeRows = "4444\n----\n----\n----".Split();
            Puzzle solveMePuzzle = new Puzzle(4, "1234", solveMeRows);
            bool solved = solveMePuzzle.Solve();

            Assert.IsFalse(solved);
        }