增加了一个题目,题目用红色表示,玩家填的数字用白色表示。
#include <iostream>
#include<ctime>
#include<conio.h>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<windows.h>
using namespace std;
const int tms=7,pdh=45,pdj=362880;
struct wj{
int gq;
bool b[tms+1];
void cj(){
gq=1;
for(int i=1;i<=tms;i++)
b[i]=0;
}
}wj;
string sdtm[tms+1][10]={
{
" "
},
{//1
" ",
" 61 3 2 ",
" 5 81 7",
" 7 34",
" 9 6378",
" 32795 ",
" 57 3 9 2",
" 19 76 ",
" 8 24 76 ",
" 64 1 25 "
},
{//
" ",
" 1 83 2",
" 57 1 ",
" 5 9 64",
" 7 4 859 ",
" 3 1 4 ",
" 514 3 6",
" 36 7 4 ",
" 6 79",
" 8 52 3"
},
{
" ",
" 4 9 ",
" 8 2 7 ",
" 2 5 71 6",
" 3 8 6 ",
" 76 31",
" 1 6 2",
" 2 59 8 4 ",
" 9 7 1 ",
" 6 5 "
},
{
" ",
" 945",
" 6 ",
" 52 1 38 7",
" 9 31 ",
" 3 8 1 ",
" 46 2 ",
" 7 52 8 19",
" 3 ",
" 861 "
},
{
" ",
" 7 1 3 6 ",
" 5 7 ",
" 3 5 1",
" 5 3 4 8",
" 4 7 1 2",
" 9 7 2 4",
" 2 7 3",
" 3 4 ",
" 6 5 9 2 "
},
{
" ",
" 9 64 ",
" 4 ",
" 1 36 72",
" 46 9",
" 9 3 ",
" 2 54 ",
" 92 57 8",
" 5",
" 34 6 "
},
{
" ",
" 7 9 ",
" 2 5 7 6",
" 8 1 4 7 ",
" 4 1 3 ",
" 6 1 6 9",
" 9 8 6 ",
" 5 8 9 1 ",
" 1 6 3 2",
" 6 3 ",
},
};
//基本函数
void sc(string s[],int b[10][10]);
void scpd(char t);
bool pd(string s[],int x);
bool pd1(string s[]);
void out(string s);
void zt();
void color(const char *s, int color);
//
void ksjm();
void yx();
void xy(int x);
int main(){
ksjm();
yx();
return 0;
}
void color(const char *s, int color)
{
HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | color);
printf(s);
SetConsoleTextAttribute(handle, FOREGROUND_INTENSITY | 7);
}
void scpd(char t){
if(t=='1')color("1",4);
if(t=='2')color("2",4);
if(t=='3')color("3",4);
if(t=='4')color("4",4);
if(t=='5')color("5",4);
if(t=='6')color("6",4);
if(t=='7')color("7",4);
if(t=='8')color("8",4);
if(t=='9')color("9",4);
return;
}
void sc(string s[],int b[10][10]){
cout<<"___________________\n";
for(int i=1;i<=9;i++){
cout<<"|";
for(int j=1;j<=9;j++){
if(b[i][j]==1){
scpd(s[i][j]);
}
else
cout<<s[i][j];
if(j%3==0)cout<<"|";
else
cout<<' ';
}
if(i%3==0)cout<<"\n|_____|_____|_____|";
cout<<"\n";
}
return;
}
void out(string s){
for(int i=0;i<s.size();i++)
{
cout<<s[i];
Sleep(30);
}
return;
}
void zt(){
out("请按任意键继续. . .");
char a=_getch();
return;
}
void ksjm(){
cout<<"------------------------------------数独游戏------------------------------------\n\n";
out("欢迎来到数独游戏 出版人:YYF");
cout<<"\n\n";
zt();
wj.cj();
system("cls");
return;
}
void xy(int x){
int b[10][10];
string a[10];
for(int i=1;i<10;i++){
a[i]=sdtm[x][i];
for(int j=1;j<10;j++){
b[i][j]=(sdtm[x][i][j]!=' ');
}
}
while(1){
system("cls");
int tot=0;
for(int i=1;i<=9;i++)
for(int j=1;j<=9;j++)
if(a[i][j]!=' ')tot++;
if(tot==81){
if(pd1(a)){
sc(a,b);
system("cls");
out("恭喜您通关了这个数独");
wj.b[x]=1;
wj.gq++;
cout<<"\n";
zt();
return;
}
else{
system("cls");
out("很抱歉,您的填写有误,是否重新开始(y/n)");
char qw=_getch();
while(qw!='y'&&qw!='n')qw=_getch();
if(qw=='y')xy(x);
}
system("cls");
}
cout<<"您正在游戏的是"<<x<<"号数独\n";
sc(a,b);
Sleep(400);
cout<<"0:退出 1:填写 2:删除\n";
char s=_getch();
while(s<'0'||s>'2')s=_getch();
s-='0';
if(s==0){
system("cls");
out("已成功退出!!!");
zt();
return;
}
if(s==1){
cout<<"请输入填写的坐标(先行再列)以及填写的数";
int x,y,k;
cin>>x>>y>>k;
while(x<1||x>9||y<1||y>9||k<1||k>9||b[x][y]==1){
Sleep(400);
system("cls");
if(b[x][y]==1){
cout<<"这个格子是系统产生的数,不可以随意更改!!!";
}
else
cout<<"输入错误,请重新输入!!!";
zt();
system("cls");
sc(a,b);
cout<<"0:退出 1:填写 2:删除\n";
cout<<"请输入填写的坐标(先行再列)以及填写的数";
cin>>x>>y>>k;
}
if(b[x][y]==2)
out("更改成功!!!");
else
out("填写成功!!!");
b[x][y]=2,a[x][y]=k+'0';
}
if(s==2){
cout<<"请输入需要删除的坐标(先行再列)";
int x,y;
cin>>x>>y;
while(x<1||x>9||y<1||y>9||b[x][y]==1){
Sleep(400);
system("cls");
if(b[x][y]==1){
cout<<"这个格子是系统产生的数,不可以随意删除!!!";
}
else
cout<<"输入错误,请重新输入!!!";
zt();
system("cls");
sc(a,b);
cout<<"请输入需要删除的坐标(先行再列)";
cin>>x>>y;
}
out("删除成功!!!");
a[x][y]=' ';
b[x][y]=0;
system("cls");
continue;
}
}
}
void yx(){
srand(time(0));
int x=rand()%tms+1;
xy(x);
}
bool pd1(string s[]){
int H,J;
for(int i=1;i<10;i++){
H=0,J=1;
for(int j=1;j<10;j++)
H+=s[i][j]-'0',J*=s[i][j]-'0';
if(H!=pdh||J!=pdj)return 0;
}
for(int i=1;i<10;i++){
H=0,J=1;
for(int j=1;j<10;j++)
H+=s[j][i]-'0',J*=s[j][i]-'0';
if(H!=pdh||J!=pdj)return 0;
}
for(int i=0;i<=6;i+=3){
for(int j=0;j<=6;j+=3){
H=0,J=1;
for(int l=1;l<=3;l++){
for(int k=1;k<=3;k++){
int x=l+i,y=k+j;
H+=s[x][y]-'0',J*=s[x][y]-'0';
}
}
if(H!=pdh||J!=pdj)return 0;
}
}
H=0,J=1;
return 1;
}