如何让用户在数独板中输入值?

时间:2013-04-29 16:03:07

标签: c++ multidimensional-array

我在Board中生成和插入数字时遇到问题。以下是我到目前为止的情况:

enter code here
#include <iostream>
#include <string>
#include <cstring>
#include <vector>



using namespace std;


bool Valid_Set(int line[])
{
bool found = true;
for (int t = 1; (t <= 9 && found); ++t)
{
    found = false;
    for (int i = 0; i < 9; ++i)
        if (line[i] == t) found = true;
}
return found; // returns true if all value 1-9 are in array.
}

 bool Valid_Matrix(int sud[9][9])
{
int i, j, check[9];
bool valid = true;

// check each row
for (j = 0; (j < 9) && valid; ++j)
{
    for (i = 0; i < 9; ++i)
        check[i] = sud[i][j];
    valid = Valid_Set(check);
}
// check each column
for (j = 0; (j < 9) && valid; ++j)
{
    for (i = 0; i < 9; ++i)
        check[i] = sud[j][i];
    valid = Valid_Set(check);
}
// check 3x3 area
for (i = 0; (i < 9) && valid; i += 3)
{
    for (j = 0; (j < 9) && valid; j += 3)
    {
        int t = 0;
        for (int x = 0; x < 3; ++x)
            for (int y = 0; y < 3; ++y)
                check[t++] = sud[x + i][y + j];
        valid = Valid_Set(check);
    }
}
return valid;
}

void numGenerator(int A[]){
for (int i=0; i<9; i++){
    A[i]=(1+rand()%9);
}
}

 bool check_for_validity(int A[]){
int counter=0; 
while (counter!=9){
    numGenerator(A);
    counter++;
}
for (int i=0; i<9; i++)
    for (int j=0; j<9; j++){
        if(A[j]==A[i])
            numGenerator(A);
    }

  return true;
 }

 void main (){


//Descriptions and genral ideas 
cout << "WELCOME TO SUDOKU " << endl;
cout << endl;

cout << "RULES:    "<< endl;
cout << endl;

cout << "->You'll be given a 9x9 board with some numbers depending on which level of difficulty you choose to play."<< endl;
cout << endl;

cout << "->You have to arrange numbers from 1 to 9 so that a number shows up once in one row, one column and in a 3x3 box." << endl;
cout << endl;

cout << "So, let's get started" << endl;

cout << endl;
cout <<endl;



char dash[9][9];
for (int array=0; array<9; array++) {
    for (int array2=0; array2<9; array2++) {
        dash[array][array2]='_';
    }
}
char row[9];
char column[9];
int num[81];
int num2[9][9];

int length;
length=strlen(row);

//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. 
for (int i=0; i<length; i++) {
    dash[row[i]][column[i]]=num[i]+'0';
}

//Builds the Sudoko board and outputs the full 9x9 array.
cout << "   0    1   2   3   4   5    6    7   8  " << endl;

cout << "-----------------------------------------" << endl;
int i=0;
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 << "||" << i <<  endl;
    i++;
}
cout << "-----------------------------------------" << endl;
int j=3;
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 << "||" << j << endl;
    j++;
}
cout <<"-----------------------------------------" << endl;
int z=6;
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 << "||" << z << endl;
    z++;
}
cout << "-----------------------------------------" << endl;

for (int row = 0; row < 9; row++) {
    cout << "Enter values for row " << row + 1 << " : ";
    for (int col = 0; col < 9; col++)
        cin >> dash[row][col];
    cout << endl << endl;
}


system("pause");
 }

1 个答案:

答案 0 :(得分:0)

这整段错误

char row[9];
char column[9];
int num[81];
int num2[9][9];

int length;
length=strlen(row);

//Replaces the _'s in the specified rows/columns and replaces them with the integer the user specified. 
for (int i=0; i<length; i++) {
    dash[row[i]][column[i]]=num[i]+'0';
}

您正在使用rowcolumn,即使他们没有任何值。这很可能会导致你的程序崩溃。

很难知道你的预期。也许你可以解释一下?

这是输入值的建议。也许你会发现它很有用

// get the user's values
int row, column, value;
cout << "Enter a row number, column number, and value. All numbers should be between 1 and 9\n";
cin >> row >> column >> value;

// put the value in the board, add '0' to convert the integer to a digit
dash[row][column] = value + '0';