c++ 数独
各路神仙 求帮助
运行不了
#include<iostream>
#include<ctime>
#include<string>
#include<conio.h>
#include<iomanip>
using namespace std;
int Random[9][9]=
{
//这是填好以后,正确的数独
{3,9,6,1,2,4,7,8,5},
{7,8,1,9,5,6,2,4,3},
{2,4,5,7,8,3,6,1,9},
{6,3,2,8,4,1,5,9,7},
{1,7,4,5,3,9,8,2,6},
{9,5,8,2,6,7,4,3,1},
{4,1,7,6,9,8,3,5,2},
{5,6,3,4,1,2,9,7,8},
{8,2,9,3,7,5,1,6,4}
};
int a[9][9]; //用于显示的数组
bool flag[9][9]; //用于标记的数组
int cnt=0; //用于标记剩余的方框数
class shudu
{
public:
void gapshoujiemian();
void copyArray();
void formal();
void newgame();
void ShowMenu();
void FillCell();
void ClearCell();
}
void shudu::gapshoujiemian() //显示第一个界面
{
cout<<" =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*="<<endl;
cout<<"\n\n\n\n\n\n\n\n\n\n 高手数独\n\n\n\n\n\n 高手公司重磅推出\n 请按任意键继续"<<endl;
cout<<"\n =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=";
getch(); //要用#include<conio.h>;函数用途:从控制台读取一个字符但不显示在屏幕上
system("cls"); //执行DOS下的清屏命令。
}
void shudu::formal(int h[9][9],int f[9][9]) //初始化数组,h就是ramdom,f就是a
{
int m,n,i,j,l,k;
while(true)
{
m=rand()%9; //9以内的随机数,不包括9
if(m==0||m==3||m==6)
{
break;
}
}
srand(time(NULL)); //利用时间设置随机种子,随机数初始化
while(true)
{
l=rand()%9;
if(l==1||l==2)
{
break;
}
}
srand(time(NULL));
while(true)
{
n=rand()%9;
if(n==0||n==3||n==6)
{
break;
}
}
srand(time(NULL));
for(i=0;i<9;i++)
{
int u;
u=h[i][m];
h[i][m]=h[i][m+l];
h[i][m+l]=u;
} //两列交换
while(true)
{
k=rand()%9;
if(k==1||k==2)
{
break;
}
}
for(j=0;j<9;j++)
{
int p;
p=h[n][j];
h[n][j]=h[n+k][j];
h[n+k][j]=p;
} //两列交换
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
f[i][j]=h[i][j]; //把交换好的数组给要用于显示的数组 h 给f.
}
}
}
shudu shudu::copyArray(int from[9][9],int to[9][9])//还原数组
{
int m,n,i,j,l,k;
while(true)
{
srand(time(NULL)); //利用时间设置随机种子,随机数初始化
while(true)
{
n=rand()%9;
if(n==1||n==2||n==6)
{
break;
}
}
srand(time(NULL));
while(true)
{
l=rand()%9;
if(l==1||l==2)
{
break;
}
}
srand(time(NULL));
while(true)
{
k=rand()%9;
if(k==1||k==2)
{
break;
}
}
for(i=0;i<9;i++)
{
int u;
u=from[i][m];
from[i][m]=from[i][m+l];
from[i][m+l]=u;
}
for(j=0;j<9;j++)
{
int p;
p=from[n][j];
from[n][j]=from[n+k][j];
from[n+k][j]=p;
}
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
to[i][j]=from[i][j];
}
}
for(i=0;i<9;i++)
{
for(j=0;j<9;j++)
{
to[i][j]=from[i][j];
}
}
}
void shudu::newgame(int a[9][9],bool flag[9][9],int level) //挖去操作,设置难易程度
{
fill(&flag[0][0],&flag[8][8]+1,false);//把flag数组里面的每个成员都标记为false
int r,p=0; //r表示挖去的空格;
if(level==0)
{
p=1;r=2;
}
else if(level==1)
{
p=1;r=15;
}
else if(level==2)
{
p=1;r=20;
}
else if(level==3)
{
p=1;r=25;
}
else if(level==4)
{
p=1;r=30;
}
if(p==1)
{
srand(time(NULL)); //随机数初始化
copyArray(Random,a); //这里在打乱数组。
while(r--) //随机的在某个地方标记,用于挖空
{
int i=rand()%9;
int j=rand()%9;
if(a[i][j]==0)
{
r++;
continue;
}// 随机产生的a[i][j]是0的话,重新循环一次
if(a[i][j]!=0)
{
a[i][j]=0;
flag[i][j]=true; //把a[i][j]弄为0再把这个地方的flag[i][j]标记为true}}}
else //否则,这个level不存在。
{
cout<<"Invalid Game Level"<<endl;
system("pause"); //暂停
}
}
}
}
}
void showarray(int a[9][9],bool flag[9][9]) //显示数独当前状态
{
char col[9]={'0','1','2','3','4','5','6','7','8'};
system("cls"); //清屏
cout<<" 0 1 2 3 4 5 6 7 8"<<endl;
cout<<"╔═════╤═════╤═════╗"<<endl;
int i,j;
for(i=0;i<9;i++)
{
cout<<col[i]<<"||";
for(j=0;j<9;j++)
{
if(a[i][j]!=0&&flag[i][j]==false)
{
cout<<setw(3)<<a[i][j];
}
if(a[i][j]==0)
{
cnt++; //cnt是用来标记方框数
cout<<setw(3)<<"□"; //setw是空格
}
if(a[i][j]!=0&&flag[i][j]==true) //原来是方框的地方,现在输入了值了,那就如下这样
{
cout<<setw(2)<<"_"<<a[i][j];
}
if((j+1)%3==0&&j!=8) //什么时候显示|
{
cout<<"|";
}
}
cout<<"||"<<endl;
if((i+1)%3==0&&i!=8)
{
cout<<"╟─────┼─────┼─────╢"<<endl;
}
}
cout<<"╚═════╧═════╧═════╝"<<endl;
}
void ShowMenu()//显示菜单
{
cout<<"============="<<cnt<<"spaces left============="<<endl;
cout<<"============SuDoKu Game(数独)==============="<<endl;
cout<<"=============================================="<<endl;
cout<<"E row col value:Edit cell "<<endl;
cout<<"C row col :Clear cell "<<endl;
cout<<"N Level :Reatart with Level(0-4)"<<endl;
cout<<"Q :Quit Game "<<endl;
cout<<"================================================="<<endl;
}
void FillCell(int a[9][9],bool flag[9][9],int r,int c,int value) //用于赋值的函数
{
if(flag[r][c]==true) //true是方框的地方
{
int i,j;
int k=0; //下面就把行和列的数与value值比较,用于确认,输入的值是否正确
for(j=0;j<9;j++)
{
if(a[r][j]==value)
{
k=1;break;
}
}
if(k==0)
{
for(i=0;i<9;i++)
{
if(a[i][c]==value)
{
k=1;
}
break;
}
}
if(k==0) //如果比较后完全正确的话,那么就把值给这个方框里
{
a[r][c]=value;
}
else
{
cout<<"irregular value"<<endl;
system("pause");
}
}
}
void ClearCell(int a[9][9],bool flag[9][9],int r,int c) //清除输入的数的函数,即是返回前一个状态
{
if(flag[r][c]==true)
{
a[r][c]=0;
}
else
{
cout<<"Fixed cell cannot be changed!"<<endl;
system("pause");
}
}
int main(int argc,char* argv[])
{
gaoshoujiemian(); //显示第一界面
formal(Random,a); //打乱数组
newgame(a,flag,0); //设置难易程度,默认为0level
while(true) //这是循环,是关键
{
showArray(a,flag); //显示数独当前状态
ShowMenu(); //显示菜单
string s;
cin>>s;
int r,c,value;
if(s[0]=='Q'||s[0]=='q'&&s.size()==1)//长度要是一个字符,防止输入qwe也会退出
{
break;
}
else if(s[0]=='N'||s[0]=='n'&&s.size()==1)
{
int level;
cin>>level;
newgame(a,flag,level);
}
else if(s[0]=='E'||s[0]=='e'&&s.size()==1)
{
cin>>r>>c>>value;
FillCell(a,flag,r,c,value);
cnt--;
}
else if(s[0]=='C'||s[0]=='c'&&s.size()==1)
{
cin>>r>>c;
ClearCell(a,flag,r,c);
cnt++;
}
int i,j,p=0;
for(i=0;i<9;i++)
{
for(j=o;j<9;j++)
{
if(a[i][j]!=Random[i][j]) //如果a数组里面有一个值没有和random原数组里面的值相等,就如下
{
p=1;
break;
}
}
}
if(p==0) //填好的数组,与random数组相同后,就进行如下操作
{
showArray(a,flag);
ShowMenu();
cout<<"Congradulation!\n";
system("pause");
formal(Random,a);
newgame(a,flag,0);
}
cnt=0; //cnt计数清零,防止累加。这个一定要加的。
}
return 0;
}
[解决办法]
你这个程序是拼凑的吧,自己写个四不像的类干嘛
改了点语法错误,语义还没研究..
#include<iostream>#include<ctime>#include<string>#include<conio.h>#include<iomanip>using namespace std;int Random[9][9]={ //这是填好以后,正确的数独 {3,9,6,1,2,4,7,8,5}, {7,8,1,9,5,6,2,4,3}, {2,4,5,7,8,3,6,1,9}, {6,3,2,8,4,1,5,9,7}, {1,7,4,5,3,9,8,2,6}, {9,5,8,2,6,7,4,3,1}, {4,1,7,6,9,8,3,5,2}, {5,6,3,4,1,2,9,7,8}, {8,2,9,3,7,5,1,6,4}};int a[9][9]; //用于显示的数组bool flag[9][9]; //用于标记的数组int cnt=0; //用于标记剩余的方框数void gaoshoujiemian() //显示第一个界面{ cout<<" =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*="<<endl; cout<<"\n\n\n\n\n\n\n\n\n\n 高手数独\n\n\n\n\n\n 高手公司重磅推出\n 请按任意键继续"<<endl; cout<<"\n =*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*=*="; getch(); //要用#include<conio.h>;函数用途:从控制台读取一个字符但不显示在屏幕上 system("cls"); //执行DOS下的清屏命令。}void formal(int h[9][9],int f[9][9]) //初始化数组,h就是ramdom,f就是a{ int m,n,i,j,l,k; while(true) { m=rand()%9; //9以内的随机数,不包括9 if(m==0||m==3||m==6) { break; } } srand(time(NULL)); //利用时间设置随机种子,随机数初始化 while(true) { l=rand()%9; if(l==1||l==2) { break; } } srand(time(NULL)); while(true) { n=rand()%9; if(n==0||n==3||n==6) { break; } } srand(time(NULL)); for(i=0;i<9;i++) { int u; u=h[i][m]; h[i][m]=h[i][m+l]; h[i][m+l]=u; } //两列交换 while(true) { k=rand()%9; if(k==1||k==2) { break; } } for(j=0;j<9;j++) { int p; p=h[n][j]; h[n][j]=h[n+k][j]; h[n+k][j]=p; } //两列交换 for(i=0;i<9;i++) { for(j=0;j<9;j++) { f[i][j]=h[i][j]; //把交换好的数组给要用于显示的数组 h 给f. } }}void copyArray(int from[9][9],int to[9][9])//还原数组{ int m=0,n=0,i=0,j=0,l=0,k=0; //while(true) //{ srand(time(NULL)); //利用时间设置随机种子,随机数初始化 while(true) { n=rand()%9; if(n==1||n==2||n==6) { break; } } srand(time(NULL)); while(true) { l=rand()%9; if(l==1||l==2) { break; } } srand(time(NULL)); while(true) { k=rand()%9; if(k==1||k==2) { break; } } for(i=0;i<9;i++) { int u; u=from[i][m]; from[i][m]=from[i][m+l]; from[i][m+l]=u; } for(j=0;j<9;j++) { int p; p=from[n][j]; from[n][j]=from[n+k][j]; from[n+k][j]=p; } for(i=0;i<9;i++) { for(j=0;j<9;j++) { to[i][j]=from[i][j]; } } for(i=0;i<9;i++) { for(j=0;j<9;j++) { to[i][j]=from[i][j]; } } //}}void newgame(int a[9][9],bool flag[9][9],int level) //挖去操作,设置难易程度{ fill(&flag[0][0],&flag[8][8]+1,false);//把flag数组里面的每个成员都标记为false int r,p=0; //r表示挖去的空格; if(level==0) { p=1;r=2; } else if(level==1) { p=1;r=15; } else if(level==2) { p=1;r=20; } else if(level==3) { p=1;r=25; } else if(level==4) { p=1;r=30; } if(p==1) { srand(time(NULL)); //随机数初始化 copyArray(Random,a); //这里在打乱数组。 while(r--) //随机的在某个地方标记,用于挖空 { int i=rand()%9; int j=rand()%9; if(a[i][j]==0) { r++; continue; }// 随机产生的a[i][j]是0的话,重新循环一次 if(a[i][j]!=0) { a[i][j]=0; flag[i][j]=true; //把a[i][j]弄为0再把这个地方的flag[i][j]标记为true}} } else //否则,这个level不存在。 { cout<<"Invalid Game Level"<<endl; system("pause"); //暂停 } } }}void showArray(int a[][9],bool flag[][9]) //显示数独当前状态{ char col[9]={'0','1','2','3','4','5','6','7','8'}; system("cls"); //清屏 cout<<" 0 1 2 3 4 5 6 7 8"<<endl; cout<<"╔═════╤═════╤═════╗"<<endl; int i,j; for(i=0;i<9;i++) { cout<<col[i]<<"||"; for(j=0;j<9;j++) { if(a[i][j]!=0&&flag[i][j]==false) { cout<<setw(3)<<a[i][j]; } if(a[i][j]==0) { cnt++; //cnt是用来标记方框数 cout<<setw(3)<<"□"; //setw是空格 } if(a[i][j]!=0&&flag[i][j]==true) //原来是方框的地方,现在输入了值了,那就如下这样 { cout<<setw(2)<<"_"<<a[i][j]; } if((j+1)%3==0&&j!=8) //什么时候显示| { cout<<"|"; } } cout<<"||"<<endl; if((i+1)%3==0&&i!=8) { cout<<"╟─────┼─────┼─────╢"<<endl; } } cout<<"╚═════╧═════╧═════╝"<<endl;}void ShowMenu()//显示菜单{ cout<<"============="<<cnt<<"spaces left============="<<endl; cout<<"============SuDoKu Game(数独)==============="<<endl; cout<<"=============================================="<<endl; cout<<"E row col value:Edit cell "<<endl; cout<<"C row col :Clear cell "<<endl; cout<<"N Level :Reatart with Level(0-4)"<<endl; cout<<"Q :Quit Game "<<endl; cout<<"================================================="<<endl;}void FillCell(int a[9][9],bool flag[9][9],int r,int c,int value) //用于赋值的函数{ if(flag[r][c]==true) //true是方框的地方 { int i,j; int k=0; //下面就把行和列的数与value值比较,用于确认,输入的值是否正确 for(j=0;j<9;j++) { if(a[r][j]==value) { k=1;break; } } if(k==0) { for(i=0;i<9;i++) { if(a[i][c]==value) { k=1; } break; } } if(k==0) //如果比较后完全正确的话,那么就把值给这个方框里 { a[r][c]=value; } else { cout<<"irregular value"<<endl; system("pause"); } }}void ClearCell(int a[9][9],bool flag[9][9],int r,int c) //清除输入的数的函数,即是返回前一个状态{ if(flag[r][c]==true) { a[r][c]=0; } else { cout<<"Fixed cell cannot be changed!"<<endl; system("pause"); }}int main(int argc,char* argv[]){ gaoshoujiemian(); //显示第一界面 formal(Random,a); //打乱数组 newgame(a,flag,0); //设置难易程度,默认为0level while(true) //这是循环,是关键 { showArray(a,flag); //显示数独当前状态 ShowMenu(); //显示菜单 string s; cin>>s; int r,c,value; if(s[0]=='Q'||s[0]=='q'&&s.size()==1)//长度要是一个字符,防止输入qwe也会退出 { break; } else if(s[0]=='N'||s[0]=='n'&&s.size()==1) { int level; cin>>level; newgame(a,flag,level); } else if(s[0]=='E'||s[0]=='e'&&s.size()==1) { cin>>r>>c>>value; FillCell(a,flag,r,c,value); cnt--; } else if(s[0]=='C'||s[0]=='c'&&s.size()==1) { cin>>r>>c; ClearCell(a,flag,r,c); cnt++; } int i,j,p=0; for(i=0;i<9;i++) { for(j=0;j<9;j++) { if(a[i][j]!=Random[i][j]) //如果a数组里面有一个值没有和random原数组里面的值相等,就如下 { p=1; break; } } } if(p==0) //填好的数组,与random数组相同后,就进行如下操作 { showArray(a,flag); ShowMenu(); cout<<"Congradulation!\n"; system("pause"); formal(Random,a); newgame(a,flag,0); } cnt=0; //cnt计数清零,防止累加。这个一定要加的。 } return 0;}