コード例 #1
0
ファイル: Generator.cs プロジェクト: RyanPLehan/Sudoku
        /// <summary>
        /// This will randomly pick a cell from the given puzzle
        /// </summary>
        /// <remarks>
        /// If the picked cell has a value or is locked, then a new cell will be randomly chosen
        /// </remarks>
        /// <param name="puzzle"></param>
        /// <returns></returns>
        private Puzzle.Cell GetRandomCell(Puzzle puzzle)
        {
            int row = 0;
            int col = 0;

            Puzzle.Cell cell   = null;
            Random      random = new Random();

            do
            {
                row  = random.Next(0, Puzzle.PUZZLE_GRID_SIZE);
                col  = random.Next(0, Puzzle.PUZZLE_GRID_SIZE);
                cell = puzzle.GetCell(row, col);
            } while (cell.IsLocked || cell.Value.HasValue);

            return(cell);
        }
コード例 #2
0
ファイル: Puzzle.cs プロジェクト: RyanPLehan/Sudoku
        /// <summary>
        /// Clone this puzzle.
        /// </summary>
        /// <remarks>
        /// This is useful for keeping a solved puzzle, ie to use for hints, and the one the player is working on
        /// </remarks>
        /// <returns>Puzzle</returns>
        public Puzzle Clone()
        {
            Puzzle clonePuzzle = new Puzzle();
            Cell   cloneCell   = null;

            // Iterate through this puzzle and set the values of the cloned puzzle
            IEnumerable <Cell> cells = GetCells();

            foreach (Cell cell in cells)
            {
                // Technically, we can access the private member _puzzle within the clonePuzzle.  Some people view it as bad practice
                // cloneCell = clonePuzzle._puzzle[cell.Row, cell.Column];

                cloneCell          = clonePuzzle.GetCell(cell.Row, cell.Column);
                cloneCell.Value    = cell.Value;
                cloneCell.IsLocked = cell.IsLocked;
            }

            return(clonePuzzle);
        }