/* Parsers */ public static string ParseDataFromBoard(AbstractBoard board) { StringBuilder text = new StringBuilder(); for (int i = 0; i != board.GetBoardSize(); i++) { // Add newline separators after the first line if (i > 0) text.Append(Environment.NewLine); for (int j = 0; j != board.GetBoardSize(); j++) { // Add comma separators after the first number if (j > 0) text.Append(","); // Add non-blank numbers if (!board.IsNumberBlank(i, j)) { text.Append(board.GetNumber(i, j)); text.Append(board.IsNumberPredefined(i, j) ? "T" : "F"); } } } return text.ToString(); }
public static bool ValidateSection(AbstractBoard board, IEnumerable<Coordinate> cells, bool completeCheck = false) { HashSet<int> previousValues = new HashSet<int>(); foreach (Coordinate cell in cells) { // Get current value int value = board.GetNumber(cell.Row, cell.Column); // Check if the current value is blank if (board.IsNumberBlank(cell.Row, cell.Column)) { // For a complete check; none of the numbers are allowed to be blank // Otherwise skip to next cell if (completeCheck) return false; else continue; } // Check if the current value is duplicated if (previousValues.Contains(value)) return false; // Store the current value previousValues.Add(value); } // No duplicates return true; }
public static bool ValidateCell(AbstractBoard board, int row, int column, bool oneSectionIsCompleted = false) { // Validate row, column and block bool rowValidation = ValidateSection(board, board.GetRowCells(row), oneSectionIsCompleted); bool columnValidation = ValidateSection(board, board.GetColumnCells(column), oneSectionIsCompleted); bool blockValidation = ValidateSection(board, board.GetBlockCells(row, column), oneSectionIsCompleted); // Combine result bool andResult = (rowValidation && columnValidation && blockValidation); bool orResult = (rowValidation || columnValidation || blockValidation); return (!oneSectionIsCompleted) ? andResult : orResult; }
/* Validate methods */ public static bool ValidateBoard(AbstractBoard board, bool completeCheck = false) { bool validation = true; for (int i = 0; i != board.GetBoardSize(); i++) { // Validate row, column and block validation &= ValidateSection(board, board.GetRowCells(i), completeCheck); validation &= ValidateSection(board, board.GetColumnCells(i), completeCheck); validation &= ValidateSection(board, board.GetBlockCells(i), completeCheck); } return validation; }
/* Constructors */ public Generator(AbstractBoard board) { // Initialize fields _board = board; }
/* Solve methods */ // Solves a copy of the current board private bool SolveBoard() { if (_solvedBoard != null) return true; // Copy the existing board and remove all non-predefined values AbstractBoard _copyOfBoard = (AbstractBoard)_board.Clone(); _copyOfBoard.Clear(); // Solve the copied board and assign to solved board Solver solver = new Solver(_copyOfBoard); if (solver.SolveBoard()) { _solvedBoard = _copyOfBoard; return true; } else throw new UnsolvableBoardException(); }
// Creates a blank board board -- Waits for LockNumbers() public bool NewGame(int size) { try { // Create a blank board _board = BoardFactory.CreateBoard(size); _solvedBoard = null; return true; } catch (UnsupportedBoardSize) { return false; } }
// Resets the current game and imports board from a file public bool ImportBoard(string boardData) { try { // Import board _board = Import.ParseDataIntoBoard(boardData); _solvedBoard = null; return true; } catch (UnsupportedBoardSize) { return false; } }
/* Constructors */ public Solver(AbstractBoard board) { _board = board; }