c++实现地图控制扫雷(界面优化版

首先这种游戏需要地图刷新

大家最先想到的一定是system("cls");了

但是这有一个很大的缺陷

只要走的太快就会闪屏

所以这时需要的就是重设控制台输出位置来保证不闪屏

函数如下:


void gotoxy(int y,int x){  
    COORD pos;  
    pos.X=x;  
    pos.Y=y;  
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); 
}//刷新屏幕 
 

但是,这么写光标依然会乱跳,怎么办呢,那就隐藏光标好了

	CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//光标隐藏 

库函数 Windows.h

接下来做按键判断

需要_getch(); 以及 _kbhit();前者得到按下结果, 后者判断键盘是否按下

代码我就不放了

其他都很简单

但有一点,判断一样的数自动显示,这就需要我们万恶的电风扇DFS了

函数代码:


void dianpd(int i, int j) {//深搜点出相同的点	
	if(_isMap[i - 1][j - 1] == _isMap[youy][youx] && mapflag[i - 1][j - 1] == false) {
		mapflag[i - 1][j - 1] = true;
		dianpd(i - 1, j - 1);
	}
	if(_isMap[i - 1][j] ==  _isMap[youy][youx]&& mapflag[i - 1][j] == false){
		mapflag[i - 1][j] = true;
		dianpd(i - 1, j);
	}
	if(_isMap[i - 1][j + 1] == _isMap[youy][youx] && mapflag[i - 1][j + 1] == false) {
		mapflag[i - 1][j + 1] = true;
		dianpd(i - 1, j + 1);
	}
	if(_isMap[i][j - 1] == _isMap[youy][youx] && mapflag[i][j - 1] == false) {	
		mapflag[i][j - 1] = true;
		dianpd(i, j - 1);
	}
	if(_isMap[i][j + 1] == _isMap[youy][youx] && mapflag[i][j + 1] == false) {	
		mapflag[i ][j + 1] = true;
		dianpd(i , j + 1);
	}
	if(_isMap[i + 1][j - 1] == _isMap[youy][youx] && mapflag[i + 1][j - 1] == false) {
		mapflag[i + 1][j - 1] = true;
		dianpd(i + 1, j - 1);
	}	  
	if(_isMap[i + 1][j] == _isMap[youy][youx]  && mapflag[i + 1][j] == false) {
		mapflag[i + 1][j] = true;
		dianpd(i + 1, j);
	}
	if(_isMap[i + 1][j + 1] == _isMap[youy][youx] && mapflag[i + 1][j + 1] == false) {
		mapflag[i + 1][j + 1] = true;
		dianpd(i + 1, j + 1);
	}				
}		
	

嗯接下来基本没有什么了

全代码如下:

//_kbhit() 按下键 
//_getch() 读取键
#include<bits/stdc++.h>
#include <conio.h>
#include <windows.h>

using namespace std;

void gotoxy(int y,int x){  
    COORD pos;  
    pos.X=x;  
    pos.Y=y;  
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),pos); 
}//刷新屏幕 
 
int A; 
int youx = 1;
int res;
int youy = 1;
int x;
int win = 0;
int y;
//你的x,y坐标
bool mapflag[102][102] = {false}; 
char themap[102][102] = {'.'};//地图 
char _isMap[102][102] = {0};
bool flag = 0;

void dianpd(int i, int j) {//深搜点出相同的点	
	if(_isMap[i - 1][j - 1] == _isMap[youy][youx] && mapflag[i - 1][j - 1] == false) {
		mapflag[i - 1][j - 1] = true;
		dianpd(i - 1, j - 1);
	}
	if(_isMap[i - 1][j] ==  _isMap[youy][youx]&& mapflag[i - 1][j] == false){
		mapflag[i - 1][j] = true;
		dianpd(i - 1, j);
	}
	if(_isMap[i - 1][j + 1] == _isMap[youy][youx] && mapflag[i - 1][j + 1] == false) {
		mapflag[i - 1][j + 1] = true;
		dianpd(i - 1, j + 1);
	}
	if(_isMap[i][j - 1] == _isMap[youy][youx] && mapflag[i][j - 1] == false) {	
		mapflag[i][j - 1] = true;
		dianpd(i, j - 1);
	}
	if(_isMap[i][j + 1] == _isMap[youy][youx] && mapflag[i][j + 1] == false) {	
		mapflag[i ][j + 1] = true;
		dianpd(i , j + 1);
	}
	if(_isMap[i + 1][j - 1] == _isMap[youy][youx] && mapflag[i + 1][j - 1] == false) {
		mapflag[i + 1][j - 1] = true;
		dianpd(i + 1, j - 1);
	}	  
	if(_isMap[i + 1][j] == _isMap[youy][youx]  && mapflag[i + 1][j] == false) {
		mapflag[i + 1][j] = true;
		dianpd(i + 1, j);
	}
	if(_isMap[i + 1][j + 1] == _isMap[youy][youx] && mapflag[i + 1][j + 1] == false) {
		mapflag[i + 1][j + 1] = true;
		dianpd(i + 1, j + 1);
	}				
}		
		

void keypd(char key) {//判断按下的键
 
   	if((key == 'W' || key == 'w') && youy != 1) {
   	 	youy --;
	}
	else if((key == 'S' || key == 's') && youy != x ) {
		youy ++;
	}
	else if((key == 'A' || key == 'a') && youx != 1 ) {
	  	youx --;
	}
	else if((key == 'D' || key == 'd') && youx != y ) {
	 	youx ++;
	} 
	else if((key == 'Z' || key == 'z') && _isMap[youy][youx] == '*') {
		cout << "你死了";
		win = 1;
		return;
	} 
	else if((key == 'Z' || key == 'z') && _isMap[youy][youx] != '*') {
		dianpd(youy, youx);//深搜寻找相同 
		mapflag[youy][youx] = true;
	}//判断雷的数量 
	else if(key == 'Q' ) {
		if(!flag) flag  = 1;
		else flag = 0;
	}
}
int main() {//运行
	CONSOLE_CURSOR_INFO cursor_info = {1, 0}; 
	SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);//光标隐藏 
	K:
	win = 0;
	youy = 1;
	youx = 1;
	system("cls");
	cout << "请输入长, 宽, 地雷数量:";
	cin >> x >> y >> A;
	system("cls");
	for(int i = 1; i <= x; i ++) {
		for(int j = 1; j <= y; j ++) {
			mapflag[i][j] = 0;
			_isMap[i][j] = 0;
			themap[i][j] = '.';
		}
	}
    srand(time(0));
	for(int i = 1; i <= A; i++){
		int q, w;
		a:q = rand() % x + 1;
		w = rand() % y + 1;
		if((q <= 1 && w <= 1) || q > x || w > y || _isMap[q][w] == '*') {
			goto a;
		}//随机生成雷 
		else {
			_isMap[q][w] = '*';
		}
	}
	for(int i = 1; i <= x; i ++) {
    	for(int j = 1; j <= y; j ++) {
    		if(_isMap[i][j] != '*') {
    			_isMap[i][j] = '0';
				if(_isMap[i - 1][j - 1] == '*')
					_isMap[i][j]++;
				if(_isMap[i - 1][j] == '*')
					_isMap[i][j]++;
				if(_isMap[i - 1][j + 1] == '*')
					_isMap[i][j]++;
				if(_isMap[i][j - 1] == '*')
					_isMap[i][j]++;
				if(_isMap[i][j + 1] == '*')
					_isMap[i][j]++;
				if(_isMap[i + 1][j - 1] == '*')
					_isMap[i][j]++;
				if(_isMap[i + 1][j] == '*')
					_isMap[i][j]++;
				if(_isMap[i + 1][j + 1] == '*')
					_isMap[i][j]++; 
			}
			if(i == youy && j == youx) themap[i][j] = '&';
    		else themap[i][j] = '.';
    		cout << themap[i][j] << " ";
		}
		cout << endl;
	}
	cout << "boom:" << A << endl << endl << endl;
    while(1) {
    	if(_kbhit()) {
    		while(_kbhit()) {
    			keypd(_getch());
	     	}
	     	if(win) {
	     		Sleep(3000);
	     		goto K;
			}
			gotoxy(0,0);
	     		//system("cls");
	     	res = 0;
        	for(int i = 1; i <= x; i ++) {
    		for(int j = 1; j <= y; j ++) {
    			if(i == youy && j == youx) themap[i][j] = '&';
    			else if(mapflag[i][j] || flag == 1) themap[i][j] = _isMap[i][j];
				else themap[i][j] = '.';
    			cout << themap[i][j] << " ";
    			if(themap[i][j] == '.') res ++;
			}
			cout << endl;
			}
			cout << "boom:" << A << endl;
			if(((res == A && mapflag[youy][youx] != 0))) {
				cout << "你赢了";
				Sleep(3000);
				goto K;
			}
	//	Sleep(500); 
	    } 
	}
	return 0;
} 

本人qq1353652522欢迎交友!

代码是几年前刚学的时候写的所以写的很丑请见谅)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值