Exemplo n.º 1
0
    /// <summary>
    /// Creates rows, columns, and boxes in data to be used later.
    /// </summary>
    private void applySetRelationships()
    {
        #region Load Rows
        for (int index = 1; index <= 9; index++)
        {
            Row currentRow = new Row(index);
            currentRow.UnsolvedValues.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            for (int node = 1; node <= 9; node++)
            {
                //
                //
                SNode sNode = Sudoku[node - 1, index - 1];
                currentRow.Add(sNode);
                sNode.Row = currentRow;

                if (sNode.Value != 0)
                {
                    currentRow.ValueSolved(sNode.Value);
                }
                //
                //
            }

            Rows.Add(currentRow);
        }
        #endregion

        #region Load Columns
        for (int index = 1; index <= 9; index++)
        {
            Column currentColumn = new Column(index);
            currentColumn.UnsolvedValues.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            for (int node = 1; node <= 9; node++)
            {
                //
                //
                SNode sNode = Sudoku[index - 1, node - 1];
                currentColumn.Add(sNode);
                sNode.Column = currentColumn;

                if (sNode.Value != 0)
                {
                    currentColumn.ValueSolved(sNode.Value);
                }
                //
                //
            }

            Columns.Add(currentColumn);
        }
        #endregion

        #region Load Boxes
        int startRow    = 0;
        int startColumn = 0;

        for (int index = 1; index <= 9; index++)
        {
            Box currentBox = new Box(index);
            currentBox.UnsolvedValues.AddRange(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 });
            for (int down = 0; down < 3; down++)
            {
                for (int right = 0; right < 3; right++)
                {
                    //
                    //
                    SNode sNode = Sudoku[startColumn + right, startRow + down];
                    currentBox.Add(sNode);
                    sNode.Box = currentBox;

                    if (sNode.Value != 0)
                    {
                        currentBox.ValueSolved(sNode.Value);
                    }
                    //
                    //
                }
            }

            Boxes.Add(currentBox);

            currentBox.AddColumn(currentBox[0].Column);
            currentBox.AddRow(currentBox[0].Row);
            currentBox.AddColumn(currentBox[4].Column);
            currentBox.AddRow(currentBox[4].Row);
            currentBox.AddColumn(currentBox[8].Column);
            currentBox.AddRow(currentBox[8].Row);

            // Sets up the starter indices for each box
            if (startColumn == 6)
            {
                startColumn = 0;
                startRow   += 3;
            }
            else
            {
                startColumn += 3;
            }
        }
        #endregion
    }