我正在尝试为学校项目制作Sudoku拼图生成器,并且我已经为该程序解决了大部分编码和逻辑问题,这只是我用来为Sudoku网格分配值的随机数生成器生成的数字超出了适当的范围,我无法弄清楚为什么它无法正常运行。这是数字生成器的代码:
int Sudoku::RNG(int range, int start){
int randNum;
randNum = (rand()%range+start);
return randNum;
}
我也种过
srand(time(NULL));
在我的主要方法的开始。
这是使用RNG()函数中的数字填充Sudoku网格的方法的代码:
void Sudoku::gridPopulate(int grid [9][9]){
Solution s;
int num;
bool safe;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
do{
safe = false;
num= RNG(9,1);
if(s.rowCheck(grid, i, num)&& s.colCheck(grid, j, num) && s.gridCheck(grid, i, j, num)){
grid[i][j] = num;
cout << "Number entered into grid" << endl;
safe = true;
}
}while(safe);
}
}
主要方法:
int main()
{
//Program Start
srand(time(NULL));
int difficulty;
int choice;
int grid[9][9];
cout << "Welcome to my Sudoku puzzle generator! \nTest your mental muscles and see if you can solve the puzzle!" << endl;
//Puzzle Generator
Sudoku game;
game.gridPopulate(grid);
cout << "board populated"<< endl;
cout << "Successful board made" << endl;
game.displayBoard(grid);
}
下面错误的数独板示例(由于数量巨大,格式已关闭,请忽略虚线和垂直线):
1 5013192 4981028 9 |20064 8 5 |1 2 3 |
2 8 5 4 |1995768265 0 2114351727 |28 7670880 7670908 |
3 1 7274056 7 |3 0 7670880 |4 6 7670880 |
-----------------4 6 7 2 |4199040 1 8 |7670912 -1196314433 4 |
5 1953657218 1 0 |5 4 32 |7 3 7274140 |
6 5 8 1953722109 |2 9 7670916 |7274116 4199040 4358512 |
-----------------7 0 1953722297 1 |9 1953787893 3 |7274204 4 8 |
8 1953722109 1953722083 8 |4199040 4199040 0 |9 7274160 2 |
9 3 1953746112 1198757840 |6 7274216 4 |4358512 7274368 4358606 |
-----------------
1 2 3 4 5 6 7 8 9
它给我的数字有时很大,有时没有。此刻让我感到困惑,任何帮助或建议将不胜感激!
编辑/更新#1:
我已经修改了gridPopulate代码,并添加了一些cout语句以进行故障排除。该方法现在将保留在每个单独的数组元素上,直到被填充为止,我现在遇到的主要问题是代码将被卡在发生RNG()的do / while语句中。我正在使用计数器跟踪在将安全值传递到网格之前经历了多少次RNG迭代。问题在于,RNG在生成安全值之前要经过太多次迭代,有时甚至要继续进行数千次迭代。在某个时候,它将陷入无休止地产生随机数而无法继续前进的状态。'
void Sudoku::gridPopulate(int grid [9][9]){
Solution s;
int num;
do{
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
int counter = 0;
do{
srand(time(NULL));
num = RNG(9,1);
counter++;
cout << "RNG attempts: " << counter << endl;
}while(!(s.rowCheck(grid, i, num)&& s.colCheck(grid, j, num) && s.gridCheck(grid, i, j, num)));
grid[i][j]=num;
cout << grid[i][j] << endl;
cout << "Row #: " << i << endl;
cout << "Col #: " << j << endl;
}//end j loop
displayBoard(grid);
}//end i loop
displayBoard(grid);
}while(!validityCheck(grid));
这也是我的displayBoard方法,尽管我认为它与问题无关。
void Sudoku::displayBoard(int grid[9][9]){
/**********************************************************/
cout << " ----------------------" << endl;
for(int i=0; i<9;i++){
if(i==0)
cout << "1 | ";
if(i==1)
cout << "2 | ";
if(i==2)
cout << "3 | ";
if(i==3)
cout << "4 | ";
if(i==4)
cout << "5 | ";
if(i==5)
cout << "6 | ";
if(i==6)
cout << "7 | ";
if(i==7)
cout << "8 | ";
if(i==8)
cout << "9 | ";
for(int j=0;j<9;j++){
cout << grid[i][j] << " ";
if(j==2)
cout << "| ";
if(j==5)
cout << "| ";
if(j==8)
cout << "| ";
}
cout << endl;
if(i==2)
cout << " ------------------------" << endl;
if(i==5)
cout << " ------------------------" << endl;
if(i==8)
cout << " ------------------------" << endl;
}
cout << "\n";
cout << " 1 2 3 4 5 6 7 8 9 " << endl;
/**********************************************************/
}
Nothing wrong with the RNG function, all we need is debugging skills
## Try this to see what is the immediate value of the grid[i][j] ##
`void Sudoku::void gridPopulate(int grid[9][9])
{
Solution s;
int num;
bool safe;
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
do {
safe = false;
num = RNG(9, 1);
if (s.rowCheck(grid, i, num) && s.colCheck(grid, j, num) && s.gridCheck(grid, i, j, num)) {
grid[i`enter code here`][j] = num;
//cout << "Number entered into grid" << endl;
safe = true;
}
} while (safe);
cout << grid[i][j] << " ";
}
cout << endl;
}
}`