数独游戏设计问题C ++

时间:2012-01-02 02:47:51

标签: c++ arrays integer char

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

  1. 构建一个二维数组9x9并用下划线填充。

  2. 询问行/列,然后询问用户希望进入该行/列的次数,与用户想要的次数相同。

  3. 用指定的数字替换指定的空白。

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

  5. (稍后会解决。)

    我的问题是,当我输入行/列和我要进入该行/列的数字时,最初在该点中的短划线消失,但我输入的数字不会出现在其位置。< / p>

    以下是目前的代码:

    #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;
    }
    

2 个答案:

答案 0 :(得分:1)

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

//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.
}

您正在为字符数组&amp;分配一个整数值。因此,当您显示时,您将获得 ascii 值&amp;的相应字符。不是整数。尝试更改分配,如下所示:

//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和1之间。如果您选择使用上述观察,也建议使用9 请添加对行和行的检查。作为输入值输入的列不是b / w 1&amp;如果输入的值不是b / w 1&amp; 1,则由于访问超出绑定的数组元素将导致未定义的行为。 9.
同样如本杰明林德利所述,请更新strlen代码 希望这有帮助!

答案 1 :(得分:0)

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终止字符串。

最后,你在this questionthis question犯了同样的错误。你为什么不这样做?字符显示不同于整数。以下代码不会显示数字1:

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

看看其他两个问题的答案。