C#数独解算器变量故障
这里的问题是,emptyRow和emptyCol变量在某种程度上不起作用,并且总是等于0,即使我在初始化时实际为它们赋值! 还有,你看到我在这里可能犯的错误了吗 代码如下:C#数独解算器变量故障,c#,variables,global-variables,sudoku,C#,Variables,Global Variables,Sudoku,这里的问题是,emptyRow和emptyCol变量在某种程度上不起作用,并且总是等于0,即使我在初始化时实际为它们赋值! 还有,你看到我在这里可能犯的错误了吗 代码如下: public partial class Form1 : Form { public Form1() { InitializeComponent(); } // Declaration of Fields, used to conjoin methods RichTex
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();
}
}
公共部分类表单1:表单
{
公共表格1()
{
初始化组件();
}
//字段声明,用于连接方法
RichTextBox[,]Grid=新的RichTextBox[9,9];
int-emptyRow,emptyCol;
私有void Form1\u加载(对象发送方、事件参数e)
{
//创建文本框网格,以便在求解算法中进一步使用
//并将所有框的对齐设置为中心
int i=0;
对于(int行=0;行<9;行++)
{
for(int col=0;col<9;col++)
{
i++;
Control[]foundControls=this.Controls.Find(“网格”+i.ToString(),false);
foreach(foundControls中的控制电流控制)
{
if(currentControl.GetType()==typeof(RichTextBox))
{
RichTextBox currentRichTextBox=(RichTextBox)currentControl;
网格[行,列]=currentRichTextBox;
currentRichTextBox.SelectionAlignment=HorizontalAlignment.Center;
}
}
}
}
}
布尔数独
{
FindUnassignedLocation();
对于(int num=1;num我在您的代码中发现了一些错误:
行:26Control[]controlsFound=this.Controls.Find(“网格”+i.ToString(),false);
变量controlsFound
从未使用过。在我看来,您应该在下面的foreach
循环中使用它
我认为您的主要问题是调用findunsignedlocation();
它返回bool,但您没有检查它。它可能是:
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;
}
bool数独()
{
如果(FindUnassignedLocation())
{
对于(int num=1;num StackOverflow不是调试错误代码的服务。你应该做的是:手工写出你认为你的程序在一个简单的不起作用的情况下应该采取的所有步骤。然后在调试器中遵循这些步骤中的每一个。当你手工编写的列表与观察到的行为不匹配时,这就是错误就在这里。Eric的建议绝对正确,特别是对于这种性质的程序,它自然会被分解成更小的、可管理的逻辑块。按照建议去做将有助于程序的结构,一旦你的预期行为在你的脑海中清晰了,你的错误就会突出。我也做过很多次在这里发布之前,但仍然无法理解为什么我的两个全局变量emptyRow和emptyCol不接受赋值,但保持不变。@Kamilczak020:您是否确实在FindUnassignedLocation()中最内层的if()内添加了断点?可能RichTextBox的Text属性有一些空白,应该先清除。@sgorozco:我检查了它,它没有。在我第一次运行Solve方法后,它会在第一个框中输入值,但是,即使FindUnassignedLocation方法在第1行找到下一个空列,它也不会将其传输到emptyCol变量。噢是的,我在那里发布了一个名称错误的版本。但我认为不应该存在的主要问题是(如问题所附)两个全局变量不可能正常工作。