-3

ここでの問題は、 emptyRow 変数と emptyCol 変数がどういうわけか機能しない傾向があり、初期化時に実際に値を割り当てたとしても、常に 0 になることです! また、ここで私が犯した可能性のある間違いはありますか?

コードは次のとおりです。

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    // Declaration of Fields, used to conjoin methods
    RichTextBox[,] Grid = new RichTextBox[9, 9];
    int emptyRow, emptyCol;

    private void Form1_Load(object sender, EventArgs e)
    {
        // Creating a grid of Textboxes, for further use in a solving algorithm
        // and setting alignment to center for all boxes
        int i = 0;
        for (int row = 0; row < 9; row++)
        {
            for (int col = 0; col < 9; col++)
            {
                i++;
                Control[] foundControls = this.Controls.Find("Grid" + i.ToString(), false);
                foreach (Control currentControl in foundControls)
                {
                    if (currentControl.GetType() == typeof(RichTextBox))
                    {
                        RichTextBox currentRichTextBox = (RichTextBox)currentControl;
                        Grid[row, col] = currentRichTextBox;
                        currentRichTextBox.SelectionAlignment = HorizontalAlignment.Center;
                    }
                }
            }
        }
    }
    bool SolveSudoku()
    {
        FindUnassignedLocation();
        for (int num = 1; num <= 9; num++)
        {
            if (NoConflicts(emptyRow, emptyCol, num))
            {
                Grid[emptyRow, emptyCol].Text = num.ToString();
                return true;
            }
        }
        return false;
    }
    // Method to determine wether any fields are empty and if so, returning the first found
    bool FindUnassignedLocation()
    {
        for (int row = 0; row < 9; row++)
        {
            for (int col = 0; col < 9; col++)
            {
                if (Grid[row, col].Text == "")
                {
                    emptyRow = row;
                    emptyCol = col;
                    return true;
                }
            }
        }
        return false;
    }
    // Check if there are any conflicts in row or col or box
    bool NoConflicts(int row, int col, int num)
    {
        return !UsedInRow(row, num) && !UsedInCol(col, num) &&
            !UsedInBox(row - row % 3, col - col % 3, num);
    }
    // Check if there are any conflicts in row 
    bool UsedInRow(int row, int num)
    {
        for (int col = 0; col < 9; col++)
        {
            if (Grid[row, col].Text == num.ToString())
            {
                return true;
            }
        }
        return false;
    }
    // Check if there are any conflicts in column
    bool UsedInCol(int col, int num)
    {
        for (int row = 0; row < 9; row++)
        {
            if (Grid[row, col].Text == num.ToString())
            {
                return true;
            }
        }
        return false;
    // Check if there are any conflicts in box
    }
    bool UsedInBox(int boxStartRow, int boxStartCol, int num)
    {
        for (int row = 0; row < 3; row++)
        {
            for (int col = 0; col < 3; col++)
            {
                if (Grid[row + boxStartRow, col + boxStartCol].Text == num.ToString())
                {
                    return true;
                }
            }
        }
        return false;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        SolveSudoku();
    }

}

}

4

1 に答える 1

1

コードにいくつかのエラーが見つかりました:

Line:26Control[] controlsFound = this.Controls.Find("Grid" + i.ToString(), false); 変数controlsFoundは使用されていません。foreach以下のループで使用する必要があるように思えます。

あなたの主な問題はFindUnassignedLocation();ブール値を返す呼び出しだと思いますが、チェックしていません。おそらく次のようになります。

bool SolveSudoku()
{
    if (FindUnassignedLocation())
    {
        for (int num = 1; num <= 9; num++)
        {
            if (NoConflicts(emptyRow, emptyCol, num))
            {
                Grid[emptyRow, emptyCol].Text = num.ToString();
                return true;
            }
        }
    }
    return false;
}
于 2013-06-24T18:59:58.177 に答える