2013-06-24 107 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