/// <summary>
        /// Finds the next position by finding the next character using the MRV
        /// </summary>
        /// <param name="sudokuBoard">SudokuBoard to find the next position for</param>
        /// <returns>Position of the next blank character using MRV if one is found, null otherwise</returns>
        protected override Position FindNextPosition(SudokuBoard sudokuBoard)
        {
            Position position = null;
            var foundChars = int.MaxValue;

            for (var row = 0; row < SudokuBoard.Rows; row++)
            {
                for (var col = 0; col < SudokuBoard.Columns; col++)
                {
                    if (sudokuBoard[col, row] == '-')
                    {
                        var availableChars = sudokuBoard.AvailableCharsAtPosition(col, row);
                        var count = availableChars.Count();
                        if (count < foundChars)
                        {
                            foundChars = count;

                            if (position == null)
                            {
                                position = new Position(col, row);
                            }
                            else
                            {
                                position.Column = col;
                                position.Row = row;
                            }
                        }
                    }
                }
            }

            return position;
        }
Example #2
0
 private void newMenuItem_Click(object sender, EventArgs e)
 {
     TotalAssignmentsLabel.Text = string.Empty;
     SudokuBoard = new SudokuBoard();
     BoardPictureBox.Refresh();
 }
        /// <summary>
        /// Creates a copy of the board
        /// </summary>
        /// <returns></returns>
        public SudokuBoard Copy()
        {
            var copy = new SudokuBoard();

            for (var col = 0; col < Columns; col++)
            {
                for (var row = 0; row < Rows; row++)
                {
                    copy[col, row] = Board[col, row];
                }
            }

            return copy;
        }
        /// <summary>
        /// Loads a text file into a board
        /// </summary>
        /// <param name="path">Path of the text file to load</param>
        /// <returns>Loaded board</returns>
        public static SudokuBoard Load(string path)
        {
            var sudokuBoard = new SudokuBoard();

            using (var reader = new StreamReader(path))
            {
                string line;
                var rowCount = 0;

                while ((line = reader.ReadLine()) != null)
                {
                    line = line.Trim();

                    if (line.Length == 0)
                    {
                        continue;
                    }

                    if (rowCount >= Rows || line.Length != Columns || line.Where(c => !ValidCharacters.Contains(c)).Any())
                    {
                        return null;
                    }

                    for (var index = 0; index < Columns; index++)
                    {
                        sudokuBoard.Board[index, rowCount] = line[index];
                    }

                    rowCount++;
                }
            }

            return sudokuBoard;
        }