数独游戏设计问题C++

Sudoku game design problems C++

本文关键字:问题 C++ 游戏      更新时间:2023-10-16

我有(另一个(关于字符的问题。 感谢那些以前帮助过我的人。在程序的这一点上,我主要尝试做 4 件事。那是:

  1. 构建一个 9x9 的 2D 数组,并用下划线填充它。

  2. 询问一行/
  3. 一列,然后询问用户希望进入该行/列的次数,只要用户想要多少次。

  4. 将指定的空白替换为指定的数字。

  5. 在 ASCII 艺术数独板上输出整个 9x9 字符数组。

(稍后会解决。

我的问题是,当我输入行/列

和我想进入该行/列的数字时,最初在该位置的破折号消失了,但我输入的数字没有出现在它的位置。

这是到目前为止的代码:

#include <iostream>
#include <string>
#include <cstring>
using namespace std;
int main () {
//Builds 9x9 char array.
char dash[9][9];
for (int array=0; array<9; array++) {
    for (int array2=0; array2<9; array2++) {
        dash[array][array2]='_';
    }
}
cout << "Input the row #, then the column #, then the number that you wish to fill that spot." << endl;
cout << "Remember that a Sudoku board is 9x9." << endl;
cout << "When you wish to finish input and solve, type all 0's and press enter." << endl;
int rowb;
char row[99];
int columnb;
char column[99];
int numb;
char num[99];
//Inputs the row/column and number to go into specified row/column.
int control=0;
while (rowb!=0){
    control++;
    cout << "Row: ";
    cin >> rowb;
    cout << "Column: ";
    cin >> columnb;
    cout << "Number: ";
    cin >> numb;
    row[control]=rowb-1;
    column[control]=columnb-1;
    num[control]=numb;
}
int length;
length=strlen(row);
//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. This is where I think I'm having the problem.
for (control=0; control<length; control++) {
    dash[row[control]][column[control]]=num[control];
}
//Builds the Sudoko board and outputs the full 9x9 array.
cout << "╔═══════════╦═══════════╦═══════════╗" << endl;
for (int count=0; count<3; count++) {
    for (int count2=0; count2<3; count2++) {
        cout << "║_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
    }
        cout << "║" << endl;
}
cout << "╠═══════════╬═══════════╬═══════════╣" << endl;
for (int count=3; count<6; count++) {
    for (int count2=0; count2<3; count2++) {
        cout << "║_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
    }
    cout << "║" << endl;
}
cout << "╠═══════════╬═══════════╬═══════════╣" << endl;
for (int count=6; count<9; count++) {
    for (int count2=0; count2<3; count2++) {
        cout << "║_" << dash[count][count2*3] << "_|_" << dash[count][count2*3+1] << "_|_" << dash[count][count2*3+2] << "_";   
    }
    cout << "║" << endl;
}
cout << "╚═══════════╩═══════════╩═══════════╝" << endl;
return 0;
}

循环中输入的数字分配有问题。

//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. This is where I think I'm having the problem.
for (control=0; control<length; control++) {
    dash[row[control]][column[control]]=num[control]; //<<<--- Assignment issue.
}

你在字符数组中分配一个整数值,因此当你显示时,你将得到ascii值的相应字符,而不是整数。尝试按如下方式更改分配:

//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. This is where I think I'm having the problem.
for (control=0; control<length; control++) {
    dash[row[control]][column[control]]=num[control] + '0'; // Convert to ascii value of the integer, but will fail if not b/w 0 & 9.
}

如果您选择使用上述观察结果,也建议您检查输入的数字是否在 1 和 9 之间。
请添加对输入的行和列的检查,因为输入的值不是黑白1和9,如果输入的值不是黑白1和9,则由于访问超出边界的数组元素而导致未定义的行为。
同样如本杰明林德利所述,请更新strlen代码。
希望这有帮助!

length=strlen(row);

这是未定义的行为,因为 row[0] 从未初始化过,并且您永远不会以 null 终止字符串。

char row[99];
...
int control=0;
while (rowb!=0){
    control++;
    ...
    row[control]=rowb-1;
    ...

请注意,第一次通过循环时,控件为 1。 因此,您正在设置 row[1] 的值,而不是 row[0]。 将增量移动到循环的末尾。 可能还有其他一些问题,但这是导致您看到的行为的主要原因。

此外,要使 strlen 正常工作,您需要以 null 终止字符串。

最后,你犯了你在这个问题和这个问题中犯过的同样的错误。 你为什么看起来不明白呢? 字符的显示方式与整数不同。 以下代码不会显示数字 1:

char c = 1;
std::cout << c;

看看另外两个问题的答案。