Exemplo n.º 1
0
        /* 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();
        }
Exemplo n.º 2
0
        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;
        }
Exemplo n.º 3
0
        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;
        }
Exemplo n.º 4
0
        /* 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;
        }
Exemplo n.º 5
0
 /* Constructors */
 public Generator(AbstractBoard board)
 {
     // Initialize fields
     _board = board;
 }
Exemplo n.º 6
0
        /* 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();
        }
Exemplo n.º 7
0
 // 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;
     }
 }
Exemplo n.º 8
0
 // 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;
     }
 }
Exemplo n.º 9
0
 /* Constructors */
 public Solver(AbstractBoard board)
 {
     _board = board;
 }