2013-06-24 75 views
-3

这里的问题是,emptyRow和emptyCol变量在某种程度上并不倾向于工作,并且始终等于0,即使我在初始化时实际为它们赋值! 同样,你看到我在这里可能犯的错误吗?C#数独求解器变量故障

下面的代码:

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(); 
    } 

} 

}

+2

StackOverflow不是用于调试您的错误代码的服务。你应该做的是:用手写出**你认为你的程序应该采取的所有步骤来处理一个不起作用的简单*案例。然后在调试器中按照上述步骤进行操作。当你手写的清单与观察到的行为不符时,这就是错误所在。 –

+1

埃里克的建议在这里绝对是重点,特别是对于这种性质的程序,它自然适合分解为更小,可管理的逻辑块。按照建议进行操作将有助于您的程序结构,一旦您的预期行为在您的脑海中清晰可见,您的错误就会显现出来。 – Chris

+0

所以在发布之前我做了很多次,但是还是不能理解为什么我的两个全局变量emptyRow和emptyCol没有分配值,但保持不变。 – Kamilczak020

回答

1

我发现代码中的几个误区:

线:26 Control[] 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; 
} 
+0

哦,对了,我在那里发布了错误名称的版本。但是,我认为不应该存在的主要问题是(如问题所述)两个全局变量不倾向于正常工作。 – Kamilczak020