Example #1
0
 public int[] GetValidNumbers(Square square)
 {
     return GetValidNumbers(square.Row, square.Column);
 }
Example #2
0
        public bool IsValidMove(int row, int col, int number, out Square[] invalidSquares)
        {
            HashSet<Square> temp = new HashSet<Square>();

            /* I set temporary the square value to 0, so I won't have a match when the ifs below test the square
             * against itself. I'll restore the value before I return from the method */
            _grid[row, col].Value = 0;

            // I check if in the given row and column I have the given number
            for (int i = 0; i < Columns; i++)
            {
                if (_grid[row, i].Value == number)
                {
                    temp.Add(_grid[row, i]);
                }
                else if (_grid[i, col].Value == number)
                {
                    temp.Add(_grid[i, col]);
                }
            }

            // Since I have previously saved the row and column where every box starts, it's easy to check the 9 squares of the box
            int boxIndex = GetBoxIndex(row, col);
            int boxStartingRow = Boxes[boxIndex].Item1;
            int boxStartingColumn = Boxes[boxIndex].Item2;

            for (int i = boxStartingRow; i < boxStartingRow + 3; i++)
            {
                for (int j = boxStartingColumn; j < boxStartingColumn + 3; j++)
                {
                    if (_grid[i, j].Value == number)
                    {
                        temp.Add(_grid[i, j]);
                    }
                }
            }

            _grid[row, col].Value = number;

            invalidSquares = temp.ToArray();
            return invalidSquares.Length == 0;
        }
Example #3
0
        private void PopulateSudokuGrid(string sudoku)
        {
            _fixedNumbersPosition = new List<int>();

            int i = 0;
            for (int row = 0; row < Rows; row++)
            {
                for (int col = 0; col < Columns; col++)
                {
                    _grid[row, col] = new Square()
                                          {
                                              Type = SquareType.Empty,
                                              Row = row,
                                              Column = col,
                                              Value = sudoku[i] == '.' ? 0 : int.Parse(sudoku[i].ToString())
                                          };

                    if (_grid[row, col].Value != 0)
                    {
                        _grid[row, col].Type = SquareType.FixedNumber;
                        _fixedNumbersPosition.Add(row * Columns + col);
                    }
                    i++;
                }
            }
        }
Example #4
0
 public bool IsValidMove(Square square)
 {
     Square[] invalidSquares;
     return IsValidMove(square.Row, square.Column, square.Value, out invalidSquares);
 }