Beispiel #1
0
        private static BoardSquare[,] BuildBoard(string str)
        {
            Debug.Assert(str.Length == 81);

            const int chunkSize = 9;
            var       boardRows = Enumerable
                                  .Range(0, str.Length / chunkSize)
                                  .Select(i => str.Substring(i * chunkSize, chunkSize))
                                  .ToList();

            var board = new BoardSquare[9, 9];

            for (var r = 0; r < boardRows.Count; r++)
            {
                var row = boardRows[r];
                Debug.Assert(row.Length == 9);

                for (var c = 0; c < row.Length; c++)
                {
                    board[r, c] = new BoardSquare(r, c, (int)char.GetNumericValue(row[c]));
                }
            }

            return(board);
        }
Beispiel #2
0
        private void SetMatrixValues(IList <bool> mRow, BoardSquare boardSquare)
        {
            var row    = boardSquare.GetRowIndex();
            var column = boardSquare.GetColumnIndex();
            var value  = boardSquare.GetValue() - 1;

            int RegionIndex()
            {
                var regionSize = (int)Math.Sqrt(size);
                var regionNum1 =
                    (int)Math.Floor(row / (double)regionSize) * regionSize +
                    (int)Math.Floor(column / (double)regionSize);

                return(regionNum1);
            }

            // In order, sets of 81 indexes
            var positionConstraint = row * size + column;
            var rowConstraint      = size * size + row * size + value;
            var columnConstraint   = size * size * 2 + column * size + value;
            var regionConstraint   = size * size * 3 + RegionIndex() * size + value;

            // Initialize matrix where the constraint is satisfied
            mRow[positionConstraint] = true;
            mRow[rowConstraint]      = true;
            mRow[columnConstraint]   = true;
            mRow[regionConstraint]   = true;
        }
Beispiel #3
0
        private IEnumerable <BoardSquare> GetNeighbours(BoardSquare currentSquare)
        {
            var Row      = currentSquare.GetRowIndex();
            var Column   = currentSquare.GetColumnIndex();
            var SubBoard = currentSquare.GetSubBoard(Column, Row);

            var PlacedValues = new List <BoardSquare>();

            PlacedValues.AddRange(_board.GetRow(Row));
            PlacedValues.AddRange(_board.GetColumn(Column));
            PlacedValues.AddRange(_board.GetSubBoard(SubBoard));
            return(PlacedValues);
        }
Beispiel #4
0
        private (DoublyLinkedList <bool> linkedList, List <BoardSquare> squares) CalculateMatrix()
        {
            var matrix  = new List <bool[]>();
            var squares = new List <BoardSquare>();

            for (var row = 0; row < size; row++)
            {
                for (var column = 0; column < size; column++)
                {
                    var boardSquare = _board.GetBoardSquare(row, column);
                    if (boardSquare.IsEmpty())
                    {
                        for (var value = 1; value <= size; value++)
                        {
                            var square = new BoardSquare(row, column, value);

                            matrix.Add(new bool[listLength]);
                            SetMatrixValues(matrix.Last(), square);
                            squares.Add(square);
                        }

                        continue;
                    }

                    matrix.Add(new bool[listLength]);
                    SetMatrixValues(matrix.Last(), boardSquare);
                    squares.Add(boardSquare);
                }
            }

            var linkedList = new DoublyLinkedList <bool>(listLength);

            linkedList.ProcessMatrix(matrix);

            return(linkedList, squares);
        }