首页 > 编程语言 >C++扫雷小代码

C++扫雷小代码

时间:2024-07-17 14:59:40浏览次数:12  
标签:插旗 cout 代码 mine C++ 扫雷 include col row

先声明一下:

玩法介绍
程序会提示输入:<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;
} 

标签:插旗,cout,代码,mine,C++,扫雷,include,col,row
From: https://blog.csdn.net/Aforxiaoxei/article/details/140494645

相关文章

  • 二叉苹果树(C++)
    【题目描述】有一棵二叉苹果树,如果数字有分叉,一定是分两叉,即没有只有一个儿子的节点。这棵树共 N 个节点,标号1 至 N ,树根编号一定为 1 。我们用一根树枝两端连接的节点编号描述一根树枝的位置。一棵有四根树枝的苹果树,因为树枝太多了,需要剪枝。但是一些树枝上长有苹......
  • 小球掉落(C++)
    #include<bits/stdc++.h>usingnamespacestd;structnode{ intid; booldata; intfather; intlson,rson;};nodetree[6000000];intd,i;intmain(){ cin>>d>>i; tree[1].father=0; tree[1].lson=2; tree[1].rson=3; tree[1].data=false;......
  • 找树根和孩子(C++)
    【问题描述】 给定一棵树,输出树的根root,孩子最多的结点max以及他的孩子【输入格式】第一行:n(结点数<=100),m(边数<=200)。以下m行;每行两个结点x和y,表示y是x的孩子(x,y<=1000)。【输出格式】第一行:树根:root。第二行:孩子最多的结点max。第三行:max的孩子,按照从小到大的顺......
  • 基于SSM的校园志愿者管理系统小程序+99213(免费领源码)可做计算机毕业设计JAVA、PHP、爬
    小程序+springboot校园志愿者管理系统摘 要随着我国经济迅速发展,人们对手机的需求越来越大,各种手机软件也都在被广泛应用,但是对于手机进行数据信息管理,对于手机的各种软件也是备受用户的喜爱,校园志愿者管理系统被用户普遍使用,为方便用户能够可以随时进行在线查看校园志愿......
  • 2024年最新版微信小程序批量检测封禁接口实现代码分享
    ​       ​      <?php//要检测的appid列表$appids=array('appid1','appid2','appid3');//使用实际的appid//循环调用接口检测小程序状态foreach($appidsas$appid){    $url='https://down.ychengsnsm.com/xcx/checkxcx.php?appid='......
  • 0195-重构代码逻辑
    环境Time2022-11-15WSL-Ubuntu22.04Rust1.65.0前言说明参考:https://raytracing.github.io/books/RayTracingInOneWeekend.html目标main文件中的逻辑越来越多,考虑将其抽象出来,分成多个文件。hittable.rs可以相交的物体,抽象成一个接口。usecrate::ray::Ray;usec......
  • 好用的开源移动端低代码平台有哪些
    移动APP、H5、小程序曾风靡一时,结合当前无代码/低代码开发技术,有哪些免费开源的移动端H5/小程序软件,不用写代码即可发布H5页面,笔者对市场上主流的开源H5低代码/无代码工具/框架/组件进行了研究和验证,找到了几款比较好用的移动端H5无代码/低代码设计器,供大家选型参考。1、h5-doori......
  • 华为OD机试D卷 --找座位--24年OD统一考试(Java & JS & Python & C & C++)
    文章目录题目描述输入描述输出描述用例题目解析java源码python源码javascript源码c源码c++源码题目描述在一个大型体育场内举办了一场大型活动,由于疫情防控的需要,要求每位观众的必须间隔至少一个空位才允许落座。现在给出一排观众座位分布图,座位中存......
  • 华为OD机试D卷 --密码输入检测--24年OD统一考试(Java & JS & Python & C & C++)
    文章目录题目描述输入描述输出描述用例题目解析java源码python源码javascript源码c源码c++源码题目描述给定用户密码输入流input,输入流中字符‘<’表示退格,可以清除前一个输入的字符,请你编写程序,输出最终得到的密码字符,并判断密码是否满足如下的密......
  • 初学Python必须知道的14个强大单行代码
    引言:Python的魅力与单行代码的重要性Python以其简洁明了的语法、丰富的内置函数和强大的第三方库深受广大开发者喜爱。尤其对于编程小白来说,学习Python就像打开了一扇通向编程世界的大门。而单行代码,作为Python魅力的一部分,不仅能展现其语言的优雅与高效,更能帮助初学者快速掌......