跳到正文

xushaotingXST

1426742

这个家伙很懒,什么也没有留下

在洛谷打开
资料、奖项与版本

资料

编号
1426742
抓取
C++
#include<bits/stdc++.h>
#include<windows.h>
#include <conio.h>
using namespace std;
const int N=20;/*长和宽(<100)*/
void sleep(int x){Sleep(x/10);for(int i=1;i<=x/10*9;i++){
		if(_kbhit())break;for(int i=1;i<=4e5;i++);}}
bool lei[N+5][N+5];//是否有雷
int n=N,cnt1/*雷的数量*/,cnt2/*亮了几个*/; 
int zhou[N+5][N+5]/*周围几颗雷*/ ,x,y,biao[N+5][N+5],first=0;
bool f[N+5][N+5];//是否亮出 
void gotoxy(short x,short y){
	COORD pos={x,y};
	HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleCursorPosition(hOut,pos);
} 
void start(){
	srand(time(0));memset(lei,0,sizeof(lei));
	memset(zhou,0,sizeof(zhou));memset(f,0,sizeof(f));
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++){
			if(rand()%8==0)lei[i][j]=1;
			if(lei[i][j])cnt1++;}
	for(int i=1;i<=n;i++)for(int j=1;j<=n;j++)
			for(int a=i-1;a<=i+1;a++)
				for(int b=j-1;b<=j+1;b++)
					if(lei[a][b])zhou[i][j]++;
} 
void out(){
	gotoxy(0,0);cout<<"雷的数量:"<<cnt1<<"\n\n     ";
	for(int i=1;i<=n;i++)cout<<" "<<i<<" ";cout<<"\n     ";
	for(int i=1;i<=n;i++)cout<<"---";cout<<"\n";
	for(int i=1;i<=n;i++){
		if(i<10)cout<<' '<<i<<"|";
		else cout<<i<<"|";
		for(int j=1;j<=n;j++){
			if(f[i][j]){
				if(lei[i][j]){
					if(x==i&&y==j)cout<<"[*]";
					else cout<<" * ";
				}else{
					if(zhou[i][j]!=0)if(x==i&&y==j)cout<<"["<<zhou[i][j]<<"]";
					else cout<<' '<<zhou[i][j]<<' ';
					else if(x==i&&y==j)cout<<"[ ]";
					else cout<<"   ";
				}
			} else{
				if(x==i&&y==j)
					if(biao[i][j])cout<<"[*]";
					else cout<<"[?]";
				else 
					if(biao[i][j])cout<<"(*)";
					else cout<<"(?)";
			}
		}
		cout<<"\n";
	}
	cout<<"\n使用wasd移动,[ ]处是当前光标,按下 O 键翻开,按下 P 键标记/取消标记\n"; 
}
void end(){
	
	cout<<"雷的数量:"<<cnt1<<"\n\n     ";
	for(int i=1;i<=n;i++)cout<<" "<<i<<" ";cout<<"\n     ";
	for(int i=1;i<=n;i++)cout<<"---";cout<<"\n";
	
	for(int i=1;i<=n;i++){
		cout<<' '<<i<<" | ";
		for(int j=1;j<=n;j++){
			if(lei[i][j]){
				cout<<" * ";
			} else{
				if(zhou[i][j]!=0)cout<<' '<<zhou[i][j]<<' ';
				else cout<<"   ";
			}
		}
		cout<<"\n";
	}
}
void fan(int x,int y){
	if(zhou[x][y]==0){
		for(int i=x-1;i<=x+1;i++){
			for(int j=y-1;j<=y+1;j++){
				if(f[i][j]==0&&i>=1&&i<=n&&j>=1&&j<=n){
					cnt2++;
					f[i][j]=1;
					if(zhou[i][j]==0){
						//cout<<x<<','<<y<<"-翻开->"<<i<<','<<j<<'\n';
						//system("pause");
						fan(i,j);
					}
				}
			}
		}
	}
}
void slow_print(string s){
	for(int i=0;i<s.size();i+=2){
		cout<<s[i];
		if(s[i+1]!='\0')cout<<s[i+1];
		sleep(30);
	}
} 
void jianjie(){
	first=1;
	cout<<"   _                    ______________\n";
	cout<<"  | |                  |______  ______|\n";
	cout<<" _| |_    _______      |  ___    ___  |\n";
	cout<<"|_   _|  |_____  |     | ||_||  ||_|| |\n";
	cout<<"  | |     _____| |     |_||_||__||_||_|\n";
	cout<<" _| |_   |_____  |      ______________\n";
	cout<<"|_   _/   _____| |     |  ___    ___  |\n";
	cout<<"  | |    |_______|     | |___|  |___| |\n";
	cout<<"  | |                  |  ___    ___  |\n";
	cout<<" _| |                  | |___|  |___| |\n";
	cout<<" \\__/                  |______________| \n";
	cout<<"";system("pause");system("cls");system("color F0");
	slow_print("欢迎游玩扫雷游戏\n");sleep(500);
	slow_print("作者:Little | Chen\n");sleep(500);
	system("pause");system("cls");
	slow_print("扫雷游戏的规则:\n");sleep(500);
	slow_print("扫雷游戏的目标是点开所有安全格,胜利条件为所有非雷格被打开。\n");sleep(500);
	slow_print("一个非雷格旁的八格里如果存在地雷,那么这个格子被翻开时就会显示周围八个格子中有几个雷\n");sleep(500);
	slow_print("Tips:建议使用英文输入法游玩\n");sleep(500);
	system("pause");
} 
int main(){
	memset(lei,0,sizeof(lei));
	memset(zhou,0,sizeof(zhou));
	memset(biao,0,sizeof(biao));
	memset(f,0,sizeof(f));
	cnt1=cnt2=0; 
	system("color F0");
	//if(first==0)jianjie();//去掉这行就没有自我介绍 
	start();//去掉这行没有雷 
	//end();
	//return 0;
	x=1,y=1; 
	system("cls"); 
	while(cnt2+cnt1!=n*n){
		out();
		char c;
		c=getch();
		if((c=='a'||c=='A')&&y>1)y--;
		else if((c=='w'||c=='W')&&x>1)x--;
		else if((c=='s'||c=='S')&&x<n)x++;
		else if((c=='d'||c=='D')&&y<n)y++;
		else if((c=='p'||c=='P')&&f[x][y]==0)biao[x][y]=!biao[x][y]; 
		if((c=='o'||c=='O')&&biao[x][y]==0){
			if(lei[x][y]){
				system("cls");
				system("color F4");
				cout<<"你输了!\n";
				end();
				system("pause");
				while(1){
					system("cls");
					system("color F0");
					cout<<"按下空格重新开始. . . \n"; 
					char c;
					c=getch();
					if(c==' '){
						main();
						return 0;
					}
				}
				return 0;
			}else{
				if(f[x][y]==0){
					f[x][y]=1;
					cnt2++;
					if(zhou[x][y]==0)fan(x,y);
				}
			}
		}
	}
	system("cls");
	system("color F6");
	cout<<"你赢了!\n";
	end();
	system("pause");
	while(1){
		system("cls");
		system("color F0");
		cout<<"按下空格重新开始. . . \n"; 
		char c;
		c=getch();
		if(c==' '){
			main();
			return 0;
		}
	}
	return 0;
} 


还没有收录这位用户的文章。