数独游戏设计问题C++
Sudoku game design problems C++
我有(另一个(关于字符的问题。 感谢那些以前帮助过我的人。在程序的这一点上,我主要尝试做 4 件事。那是:
-
构建一个 9x9 的 2D 数组,并用下划线填充它。
询问一行/ 一列,然后询问用户希望进入该行/列的次数,只要用户想要多少次。
将指定的空白替换为指定的数字。
在 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;
看看另外两个问题的答案。
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- 使用mongocxx驱动程序时包含头文件问题