C++扫雷小代码

先声明一下:

玩法介绍
程序会提示输入:<ROW> <COL> <HEALTH> <MINE_SUM>,分别代表行数,列数(均小于等于30),生命值(小于行数×列数),雷数(小于行数×列数),有数据判断
操作符 q p c a +X,Y分别代表:在 (x,y) ²(见注释) 翻开/插旗/撤销插旗/使用Ai(作为起始点)³,以下为详细介绍
q x y: 翻开位于 (x,y) 的方格,如果这是你的首次点击并且踩到雷,程序会自动帮你插旗并不扣血,如果不是,扣一滴血,自动插旗;如果当前格为空白(0),自动帮你翻开周围8格内所有的空白
p x y: 在 (x,y) 插旗,表示你认为这里为雷,旗子没有数量限制,但是只有插对地方且数量正确才会胜利
c x y: 撤销在 (x,y) 的插旗,如果该位置没有旗则不会操作
a x y:在 (x,y) 排雷并插旗
h x y: 在 (x,y) 的范围内输出矩阵
NOTICE: 只需输入Ai终点,会自动在 (1,1) 到终点坐标排雷,更加便捷

先点个赞再复制呗!    求求了~~~(^人^)

源代码:

//Made by DingDang 2024-2
//UTF-8 Coding
//Compile Mode:C++11 or higher version System: Win7+ 64x
#include<bits/stdc++.h>
#include "windows.h" //for Windows-Client
/*
#include <iostream>
#include <cstdlib>
#include <windows.h>
#include <ctime>
#include <iomanip>
*/
#define random(x) 1+rand()%(x)
/*
Operator description:
1, q means to open the grid at the coordinate, continue to enter x and y, means to open the y row x
2, p indicates that the flag is planted at the coordinates (the coordinates of the user are thunder)
3, c means cancel the flag at the coordinate (if any)
4. a indicates ai automatic demining, and the last two coordinates indicate Ai demining range
*/
using namespace std;

void print(string s){ //100% created originally
	//getline(cin,s);
	char ch[s.size()];
	for(int i=0;i<s.size();i++) ch[i]=s[i];
	for(int i=0;i<s.size();i++){
		cout<<ch[i];
		Sleep(70);
	} 	
}

int ui[105][105]={0},b[105][105]={0}; 
int lives,mine_sum;
bool firstClick=true;
int row,col;
int k=0,k1=0;

int main(){
	srand(time(NULL)); //random seed
	//system("color 1B");
	system("title MineSweeper");//set window title
	//system("mode con cols=50 lines=30");//set window size
	system("echo [console]variables initialization succeeded");
	cout<<'\n';
	Sleep(200);
	cout<<"inputting|format:<HEIGHT> <WIDTH> <HEALTH> <MINE_SUM>\n";//col width row height
	print("example: 10 10 3 10");cout<<'\n';
	
	cin>>row>>col>>lives>>mine_sum;
	
	while(row>=30||row<=0||col>=30||col<=0||
		lives>=(row*col)||lives==0||
		mine_sum>=(row*col)||mine_sum<=0){ //judge input data
			cout<<"failed to process data, please reset map size/health/mine_sum\n";
			cin>>row>>col>>lives>>mine_sum;
	}
	print("generating map...");cout<<'\n';
	/*for(int i=0;i<=100;i+=10){
		cout<<i<<"%... ";
		Sleep(random(row*col*7));
	}*/
	for(int i=1;i<=10;i++){
		int x=i;
		cout<<"[";
		for(int j=1;j<=x;j++) cout<<"*";
		for(int g=1;g<=(10-x);g++) cout<<' ';
		cout<<"] "<<x*10<<"%"<<'\n';
		Sleep(random(row*col*3));
	}
	cout<<'\n'<<'\n';
	int tempCalc=mine_sum;
	while(mine_sum){
		int x=random(row);//a+rand()%b = [a, a+b-1]
		int y=random(col);
		if(!ui[x][y]){//if(ui[x][y]==0){
			ui[x][y]=9;
			mine_sum--;
		}
	}
	for(int i=1;i<=row;i++){
		for(int j=1;j<=col;j++){
			int sum=0;//sum is the current block number
			if(ui[i][j]!=9){
				if(ui[i-1][j-1]==9) sum++;
				if(ui[i-1][j]==9) sum++;//generate number 
				if(ui[i-1][j+1]==9) sum++;
				if(ui[i][j-1]==9) sum++;
				if(ui[i][j+1]==9) sum++;
				if(ui[i+1][j-1]==9) sum++;
				if(ui[i+1][j]==9) sum++;
				if(ui[i+1][j+1]==9) sum++;
				//sum=!(ui[i-1][j-1]-9)+!(ui[i-1][j]-9)+!(ui[i-1][j+1]-9)+!(ui[i][j-1]-9)+!(ui[i][j+1]-9)+!(ui[i+1][j-1]-9)+!(ui[i+1][j]-9)+!(ui[i+1][j+1]-9);
				ui[i][j]=sum;
				
			}
		}
	}
	
	/*for(int i=1;i<=10;i++){
		for(int j=1;j<=10;j++){
			cout<<ui[i][j]<<' '; //output the numbers
		}
		cout<<endl;
	}*/
	
	while(true){
		/*for(int i=1;i<=row;i++){
			for(int j=1;j<=col;j++){
				cout<<ui[i][j]<<' '; //output the numbers
			}
			cout<<endl;
		}cout<<endl; */
	//for debug
	
		for(int i=1;i<=row;i++){
			for(int j=1;j<=col;j++){ //state of b[k][m]: =0 closed | =1 opened | =2 flagged
				if(b[i][j]==0){
					cout<<"#"<<' ';
				}else if(b[i][j]==1){
					cout<<ui[i][j]<<' ';
				}else if(b[i][j]==2){
					cout<<"P"<<' ';
				}
			}cout<<endl;
			//Judge the status of the grid (opened, closed, flag 
		}
		cout<<"Lives: "<<lives<<'\n';
		char op;//input operator
		cin>>op;
		int x,y;//input coord
		cin>>x>>y;
		while(x>row||y>col){
			cout<<"invalid input number"<<endl;
			cin>>op>>x>>y;continue;
		}
		
		
		if(op=='q'){
			if(ui[x][y]==9){//losing
				system("cls");
				if(firstClick){
					firstClick=false;
					b[x][y]=2;
					k++;k1++;
					continue;
				}else{
					if(b[x][y]==2) continue;//anti-blood on a flag
					lives--;
					system("color 47");
					print("oops! you just clicked a mine");cout<<endl<<endl;
					Sleep(200);
					system("color 07");
					if(lives==0){
						print("You Lose!");
						return 0;
					}
					//auto-flag after one death
					b[x][y]=2;
					k++;k1++;
				}
			}
			else{
				firstClick=false;
				system("cls");
				b[x][y]=1;
				if(b[x][y]==1&&ui[x][y]==0){
					if(ui[x-1][y-1]==0) b[x-1][y-1]=1;
					if(ui[x-1][y]==0) b[x-1][y]=1;
					if(ui[x-1][y+1]==0) b[x-1][y+1]=1;
					if(ui[x][y-1]==0) b[x][y-1]=1;
					if(ui[x][y]==0) b[x][y]=1;
					if(ui[x][y+1]==0) b[x][y+1]=1;
					if(ui[x+1][y-1]==0) b[x+1][y-1]=1;
					if(ui[x+1][y]==0) b[x+1][y]=1;
					if(ui[x+1][y+1]==0) b[x+1][y+1]=1;	
				}
				//Determine if there are any blank squares around, and if there are, open them automatically
			}
		}
		else if(op=='p'){//flagging
			firstClick=false; //k stores the correct location of the mine (objective),
			  			      //k1 is the user's marking location (subjective); Function: Store the total number of mines
			system("cls"); 
			k1++;
		 	if(ui[x][y]==9){
				k++;
			}	
			b[x][y]=2;
		}
		else if(op=='c'){//unflagging 
			system("cls");
			if(b[x][y]==2){
				k1--;
				if(ui[x][y]==9){
					k--;
				}
			b[x][y]=0;
			}else{
				system("cls");
				continue;
			}
		}
		else if(op=='a'){
			firstClick=false;
			print("ai-mode enabled");cout<<'\n';
			cout<<"looking for mines in: "<<'('<<x<<','<<y<<')'<<endl; 
			int new_mine=0;
			for(int i=1;i<=x;i++){
				for(int j=1;j<=y;j++){
					if(ui[i][j]==9){
						if(b[i][j]!=2){
							k++;k1++;
							b[i][j]=2;
							new_mine++;
						}
						cout<<"mine position: "<<i<<' '<<j<<endl;
						Sleep(200);
					}
				}
			}
			cout<<new_mine<<" NEW mine(s) was(were) found\n";
			cout<<"output array?(y/n)"<<' ';
			char ans;
			cin>>ans;
			if(ans=='y'){
				cout<<"outputting..."<<'\n';
				Sleep(1000);
				system("cls");
				for(int i=1;i<=x;i++){
					for(int j=1;j<=y;j++){
						cout<<ui[i][j]<<' ';
					}cout<<endl;
				}cout<<endl;
				
			}
			Sleep(1500);
			cout<<endl;
		}else if(op=='h'){
			firstClick=false;
			for(int i=1;i<=x;i++){
				for(int j=1;j<=y;j++){
					cout<<ui[i][j]<<' ';
				}cout<<endl;
			}cout<<'\n';	
		}else{
			print("invalid operator");cout<<'\n';continue;
		}
		if(k==tempCalc && k==k1){//winning
			print("You Win!");
			for(int i=1;i<=2;i++){
				system("color 1a");
				Sleep(100);
				system("color 2b");
				Sleep(100);
				system("color 3c");
				Sleep(100);
				system("color 4d");
				Sleep(100);
				system("color 5e");
			}
			system("color 07");
			scanf("%d");
		}
	}
    return 0;
} 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值