c++数独游戏1.0

最近花了一些时间做了一个小游戏,绝对原创。数字后面带~的表示题目,不能更改;带~的是玩家自己填的数字,可以更改。若发现bug请即使私信我。

#include <iostream>
#include<ctime>
#include<conio.h>
#include<fstream>
#include<iomanip>
#include<cstdlib>
#include<windows.h>
using namespace std;
const int tms=6,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  "
	}
};
//基本函数 
void sc(string s[],int b[10][10]);
bool pd(string s[],int x);
bool pd1(string s[]);
void out(string s);
void zt();
//
void ksjm();
void yx();
void xy(int x);
int main(){
	ksjm();
	yx();
	return 0;
}
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++){
			cout<<s[i][j];
			if(b[i][j]!=1)cout<<"  ";
			else
			cout<<"~ ";
			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;
} 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值