2013-04-29 56 views
0

我在生成和插入董事会内部数字时遇到了麻烦。以下是我迄今为止:如何让用户将数值输入到数独板?

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"); 
} 
+0

那么是怎么回事? – john 2013-04-29 16:09:14

+0

我的主板是一个二维数组。我希望用户能够在板内输入数值。我怎样才能做到这一点?而且我怎么能生成一个char数组的int?我只是迷茫而迷失。 – sis007 2013-04-29 16:12:59

+0

@ sis007我会用一个建议来更新我的答案,但是,如果'将int生成一个char数组',那么你为什么要这么做。 – john 2013-04-29 16:14:54

回答

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'; 
+0

char dash [9] [9];对于(int array2 = 0; array2 <9; array2 ++){ dash [array] [array2] ='_';(int array = 0; array <9; array ++){ 这是董事会的阵列。我想也许我可以用您用用户输入的数字表示的for循环替换“_”。 } } – sis007 2013-04-29 16:18:57

+0

您是否希望用户一次输入所有数字? – john 2013-04-29 16:24:41

+0

没有。逐个。 – sis007 2013-04-29 16:31:52

相关问题