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; }
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; }
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); }
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); }
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); }
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)); }
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; }
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; }
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); }
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."); } }
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)); }
public static void WritePuzzle(string filename, Puzzle puzzle) { if (filename != null && puzzle != null) File.WriteAllText(filename, puzzle.ToString()); }
public abstract bool SolveCell(Puzzle puzzle, int r, int c);
public void Init() { puzzle = new Puzzle(4, "1234", "42-1\n---2\n3-2-\n-4-3".Split()); }
public void Init() { puzzle = new Puzzle(4, "1234", rows); }
public void Solve() { string[] solveMeRows = "4444\n----\n----\n----".Split(); Puzzle solveMePuzzle = new Puzzle(4, "1234", solveMeRows); bool solved = solveMePuzzle.Solve(); Assert.IsFalse(solved); }