社区讨论

求助

灌水区参与者 1已保存回复 0

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
0 条
当前快照
1 份
快照标识符
@m374maql
此快照首次捕获于
2024/11/07 17:49
去年
此快照最后确认于
2025/11/04 15:10
4 个月前
查看原帖
当高宽均大于5时部分格子很奇怪 一大片空白区域非得我分几次打开 目前不知道怎么修
CPP
    /* Copyright Flintmall, 2px Studio, 2024. All rights reserved. */
   /*                     Minesweeper Ver. F                      */
  /*                       作者:Flintmall                        */
 /*                     扫雷F版 Release 1.2                     */
/*                     致谢 Olive__ WeiBoYu                    */
#include <bits/stdc++.h>
#include <conio.h>
#include <windows.h>
using namespace std;
void setColor(int color){
	SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),color);
	return ;
}
void cls(){
	system("cls");
	return ;
}
void wait(double n){
	_sleep(n * 1000);
	return ;
}
void wncls(double n){
	_sleep(n * 1000);
	cls();
	return ;
}
void waitEnter(){
	while(true){
		if(_kbhit()){
			int ch = _getch();
			if(ch == 13){
				break;
			}
		}
	}
}
int dx[4] = {1,-1,0,0};
int dy[4] = {0,0,1,-1};
int vis[7][9];

char white[22][22] = {
	{' '},
	{' ','0','1','2','3','4','5','6','7','8','9'},
	{' ','1'},
	{' ','2'},
	{' ','3'},
	{' ','4'},
	{' ','5'},
	{' ','6'},
	{' ','7'},
	{' ','8'},
	{' ','9'}
};
char black[22][22];
void dfs(int x,int y,int width,int height){
	vis[y][x] = 1;
	if((black[y][x] >= '1' && black[y][x] <= '9') || black[y][x] == '.'){
		white[y][x] = black[y][x];
	}
	if(black[y][x] == '.'){
		for(int i = 0;i < 4;i++){
			int nx = x + dx[i],ny = y + dy[i];
			if(nx > 1 && ny > 1 && nx <= width && ny <= height && !vis[ny][nx]){
				dfs(nx,ny,width,height);
			}
		}
	}
}
int random(int x){
	srand(time(0));
	int m = rand() % x + 1;
	return m;
}
int flag,bomb;
void init(int width,int height){
	for(int i = 1;i <= height;i++){
		for(int j = 1;j <= width;j++){
			vis[i][j] = 0;
			black[i][j] = '.';
			if(i > 1 && j > 1){
				white[i][j] = '#';
			}
		}
	}
	flag = 0,bomb = 0;
	for(int i = 1;i <= width;i++){
		int a = random(i),b = random(random(i + 1));
		black[a][b] = '*';
	}
	for(int i = 1;i <= height;i++){
		for(int j = 1;j <= width;j++){
			if(black[i][j] != '*' || i == 1 || j == 1 || i == height || j == width){
				black[i][j] = '.';
			}
		}
	}
	for(int i = 1;i <= height;i++){
		for(int j = 1;j <= width;j++){
			int sum = 0;
			if(black[i][j] != '*'){
				if(black[i - 1][j - 1] == '*'){
					sum++;
				}
				if(black[i - 1][j] == '*'){
					sum++;
				}
				if(black[i - 1][j + 1] == '*'){
					sum++;
				}
				if(black[i][j - 1] == '*'){
					sum++;
				}
				if(black[i][j + 1] == '*'){
					sum++;
				}
				if(black[i + 1][j - 1] == '*'){
					sum++;
				}
				if(black[i + 1][j] == '*'){
					sum++;
				}
				if(black[i + 1][j + 1] == '*'){
					sum++;
				}
				if(sum == 0){
					black[i][j] = '.';
				}
				else{
					black[i][j] = sum + 48;
				}
			}
			else{
				flag += 1;
			}
		}
	}
	for(int i = 1;i <= height;i++){
		for(int j = 1;j <= width;j++){
			if(!(black[i][j] >= '1' && black[i][j] <= '9') && !black[i][j] == '*'){
				black[i][j] = '.';
			}
		}
	} 
	bomb = flag;
	return ;
}
void calculate(int width,int height){
	for(int i = 1;i <= width;i++){
		for(int j = 1;j <= height;j++){
			int sum = 0;
			if(black[i][j] != '*'){
				if(black[i - 1][j - 1] == '*'){
					sum++;
				}
				if(black[i - 1][j] == '*'){
					sum++;
				}
				if(black[i - 1][j + 1] == '*'){
					sum++;
				}
				if(black[i][j - 1] == '*'){
					sum++;
				}
				if(black[i][j + 1] == '*'){
					sum++;
				}
				if(black[i + 1][j - 1] == '*'){
					sum++;
				}
				if(black[i + 1][j] == '*'){
					sum++;
				}
				if(black[i + 1][j + 1] == '*'){
					sum++;
				}
				if(sum == 0){
					black[i][j] = '.';
				}
				else{
					black[i][j] = sum + 48;
				}
			}
			else{
				flag += 1;
			}
		}
	}
	for(int i = 1;i <= height;i++){
		for(int j = 1;j <= width;j++){
			if(!(black[i][j] >= '1' && black[i][j] <= '9') && !black[i][j] == '*'){
				black[i][j] = '.';
			}
		}
	} 
	bomb = flag;
}
int click = 0;
int width = 21,height = 21;
int main(){
	system("color F");
	int wintimes = 0;
	while(width > 9){
		cls();
		cout<<"输入雷区宽度:";
		cin>>width;
		if(width > 20){
			cout<<"雷区宽度必须<10!请重新输入。"<<endl;
			wait(1000);
		}
	}
	while(height > 9){
		cls();
		cout<<"雷区宽度:"<<width<<endl;
		cout<<"输入雷区高度:";
		cin>>height;
		if(height > 20){
			cout<<"雷区高度必须<10!请重新输入。"<<endl;
			wait(1000);
		}
	}
	cls();
	width++;
	height++;
	for(int i = 3;i > 0;i--){
		cout<<"生成随机种子中……"<<endl;
		cout<<"还有 "<<i<<" 秒开始游戏……"<<endl;
		wncls(1);
	}
	init(width,height);
	while(true){
		cout<<"            -= 扫雷F版 Release v1.2 =-"<<endl;
		cout<<"              请输入操作符来进行操作"<<endl;
		cout<<"            q x y:打开位置在(x,y)的格子"<<endl;
		cout<<"         w x y:将位置在(x,y)的格子插上旗帜"<<endl;
		cout<<"                g:打开游戏操作指南"<<endl;
		cout<<"                  u:打开更新日志"<<endl; 
		cout<<"r a b:重新开始一局游戏,并重新设置雷区大小至a行b列"<<endl;
		cout<<"                       连胜:"<<wintimes<<endl;
		cout<<"                         :)       "<<endl;
		cout<<"    -===========================================-"<<endl;
		for(int i = 1;i <= height;i++){
			cout<<"        ";
			for(int j = 1;j <= width;j++){
				if(j != 1 && i != 1){
					if(white[i][j] == '1'){
						setColor(3);
					}
					else if(white[i][j] == '2'){
						setColor(2);
					}
					else if(white[i][j] == '3'){
						setColor(4);
					}
					else if(white[i][j] == '4'){
						setColor(8);
					}
					else if(white[i][j] == '5'){
						setColor(6);
					}
					else if(white[i][j] == '6'){
						setColor(14);
					}
					else if(white[i][j] == '7'){
						setColor(1);
					}
					else if(white[i][j] == '8'){
						setColor(15);
					}
					else{
						setColor(8);
					}
				}
				else{
					setColor(15);
				}
				cout<<white[i][j]<<" ";
				setColor(15);
			}
			cout<<endl;
		}
		cout<<"    -===========================================-"<<endl;
		cout<<"                   你剩下的旗帜:";
		if(click == 0){
			cout<<"?"<<endl;
		}
		else{
			cout<<flag<<endl;
		}
		cout<<endl;
		cout<<"输入操作|";
		char chose;
		int x,y; 
		cin>>chose; 
		if(chose == 'q' || chose == 'w' || chose == 'e'){
			cin>>x>>y;
		}
		if(chose == 'q' && x > 0 && y > 0 && x <= width && y <= height){
			x++;
			y++;
			click++;
			if(black[y][x] != '*' || (black[y][x] == '*' && click == 1)){
				white[y][x] = black[y][x];
			}
			if(white[y][x] == '!'){
				flag += 1;
			}
			if(black[y][x] == '*'){
				if(click != 1){
					cls();
					cout<<"    -===========================================-"<<endl;
					cout<<"                         你炸了!"<<endl;
					cout<<"                           x("<<endl;
					cout<<"    -===========================================-"<<endl;
					wintimes = 0;
					for(int i = 1;i <= height;i++){
						cout<<"        ";
						for(int j = 1;j <= width;j++){
							if(j != 1 && i != 1){
								if(black[i][j] == '1'){
									setColor(3);
								}
								else if(black[i][j] == '2'){
									setColor(2);
								}
								else if(black[i][j] == '3' || black[i][j] == '*'){
									setColor(4);
								}
								else if(black[i][j] == '4'){
									setColor(8);
								}
								else if(black[i][j] == '5'){
									setColor(6);
								}
								else if(black[i][j] == '6'){
									setColor(14);
								}
								else if(black[i][j] == '7'){
									setColor(1);
								}
								else if(black[i][j] == '8'){
									setColor(15);
								}
								else{
									setColor(8);
								}
								cout<<black[i][j]<<" ";
							}
							else{
								setColor(15);
								cout<<white[i][j]<<" ";
							}
							setColor(15);
						}
						cout<<endl;
					}
					cout<<"输入c再来一次,输入t退出游戏。"<<endl;
					char continueOrNot;
					cin>>continueOrNot;
					if(continueOrNot == 'c'){
						click = 0;
						init(width,height);
					}
					else{
						break;
						return 0;
					}
				}
				else{
					black[y][x] = '.';
					calculate(width,height);
				}
			}
			else if(black[y][x] == '.'){
				dfs(x,y,width,height);
			}
		}
		else if(chose == 'w' && x > 0 && y > 0 && x < 8 && y < 6){
			x++;
			y++;
			white[y][x] = '!';
			flag -= 1;
			if(black[y][x] == '*'){
				bomb -= 1;
			}
		}
		else if(chose == 'u'){
			cls();
			char version = 'a';
			while(true){
				cout<<"更新日志"<<endl;
				if(version == 'a'){
					cout<<endl;
					cout<<"2024/8/6 8:56 扫雷F版 Alpha v1.0.9:"<<endl;
					cout<<"    ·增加了输后在这一局游戏的基础上再来一局的功能。"<<endl;
					cout<<"    ·增加了赢后再来一局的功能。"<<endl;
					cout<<endl;
					cout<<"2024/8/5 11:20 扫雷F版 Alpha v1.0.8:"<<endl;
					cout<<"    ·修复了显示空格时旁侧的数字会消失的BUG。"<<endl;
					cout<<endl;
					cout<<"2024/8/3 11:48 扫雷F版 Alpha v1.0.7:"<<endl;
					cout<<"    ·更改了空格显示机制。"<<endl;
					cout<<endl;
					cout<<"2024/8/3 11:27 扫雷F版 Alpha v1.0.6:"<<endl;
					cout<<"    ·添加了1个彩蛋。:)"<<endl;
					cout<<endl;
					cout<<"2024/8/3 10:24 扫雷F版 Alpha v1.0.5:"<<endl;
					cout<<"    ·更改了操作符。"<<endl;
					cout<<endl;
					cout<<"2024/3/3 9:18 扫雷F版 Alpha v1.0.4:"<<endl;
					cout<<"    ·添加了数字颜色。"<<endl;
					cout<<endl; 
					cout<<"2023/12/24 10:55 扫雷F版 Alpha v1.0.3:"<<endl;
					cout<<"    ·优化了打开更新日志的方式。"<<endl;
					cout<<"    ·缩短了开始时间。"<<endl;
					cout<<endl;
					cout<<"2023/12/10 10:49 扫雷F版 Alpha v1.0.2:"<<endl;
					cout<<"    ·更改了底层代码。"<<endl;
					cout<<endl;
					cout<<"2023/11/12 11:45 扫雷F版 Alpha v1.0.1:"<<endl;
					cout<<"    ·增加了“你赢了!”页面。"<<endl;
					cout<<"    ·对“你炸了!”页面进行修改。"<<endl;
					cout<<"    ·增加了游戏结束后的“游戏结束!”语句。"<<endl;
					cout<<endl;
					cout<<"2023/11/12 11:20 扫雷F版 Alpha v1.0:"<<endl;
					cout<<"    ·增加了随机雷区。"<<endl;
					cout<<"    ·增加了更新日志。"<<endl;
					cout<<endl;
				}
				else if(version == 'b'){
					cout<<endl;
					cout<<"2024/11/3 11:58 扫雷F版 Beta v1.2.3:"<<endl;
					cout<<"    ·增加了第一步保护机制,现在第一次点击必定不会是雷了。"<<endl;
					cout<<endl;
					cout<<"2024/10/18 15:03 扫雷F版 Beta v1.2.2:"<<endl;
					cout<<"    ·简化了游戏底层代码。"<<endl;
					cout<<"    ·修改了失败后再来一局的机制,现在失败后再来一局是随机的一局新游戏了。"<<endl;
					cout<<endl;
					cout<<"2024/10/18 14:44 扫雷F版 Beta v1.2.1:"<<endl;
					cout<<"    ·修复了再来一局后空格旁格显示有误的BUG。"<<endl;
					cout<<endl;
					cout<<"2024/10/13 10:45 扫雷F版 Beta v1.2:"<<endl;
					cout<<"    ·完善了空格旁格显示机制。"<<endl;
					cout<<endl;
					cout<<"2024/8/6 17:30 扫雷F版 Beta v1.0"<<endl; 
					cout<<"    ·修复了打开更新日志后关不掉的BUG。"<<endl;
					cout<<"    ·修复了更新日志不会刷新页面的BUG。"<<endl;
				}
				else if(version == 'r'){
					cout<<"2024/11/3 21:14 扫雷F版 Release v1.2:"<<endl;
					cout<<"    ·添加了自定义雷区大小的功能。现在在开局时或输入r键时可以自定义雷区大小了。";
					cout<<"    ·删除了e键添加问号的功能。"<<endl; 
					cout<<endl;
					cout<<"2024/8/6 17:50 扫雷F版 Release v1.1:"<<endl;
					cout<<"    ·添加了2个彩蛋。;D"<<endl;
					cout<<"    ·对版本日志页面重新布局。"<<endl;
					cout<<endl;
					cout<<"2024/8/6 14:00 扫雷F版 Release v1.0:"<<endl;
					cout<<"    ·正式版发布。"<<endl;
					cout<<"    ·增加连胜显示。"<<endl;
				}
				else if(version == 't'){
					break;
				}
				cout<<"输入a查看Alpha版本的版本日志,输入b查看先遣版(Beta)的版本日志,输入r查看正式版(Release)的版本日志,输入t关闭版本日志。"<<endl;
				cin>>version;
				system("cls");
			}
		}
		else if(chose == 'k'){
			cls();
			while(true){
				cout<<"嘟嘟嗒嘟嘟嗒·ω·"<<endl;
				cout<<"你找到了柯哀小彩蛋!"<<endl;
				cout<<"既然找到小彩蛋,不如休息一下吧qwq"<<endl;
				waitEnter();
				break;
			}
		}
		else if(chose == 'i'){
			cls();
			int egg = 7;
			while(true){
				cout<<"享受柯哀小音效吧=)"<<endl;
				cout<<"Enter关闭"<<endl;
				cout<<char(egg);
				cout<<char(egg);
				cout<<char(egg);
				waitEnter();
				break;
			}
		}
		else if(chose == 'f'){
			cls();
			while(true){
				cout<<"扫雷F版的作者是Flintmall,"<<endl;
				cout<<"这里有他想说的话。"<<endl;
				cout<<endl;
				cout<<"距离第一次开始开发扫雷类型的游戏,已经过去超过一年了。"<<endl;
				cout<<"为什么我一直坚持做扫雷?"<<endl;
				cout<<"扫雷是我童年在Windows自带游戏中唯一能玩懂的游戏,"<<endl;
				cout<<"它策略,它有趣,它简单,它几近完美。"<<endl;
				cout<<"而对扫雷的热爱,"<<endl;
				cout<<"驱使着我做完了这个游戏。"<<endl;
				cout<<"感谢你,也感谢所有游玩游戏的玩家。"<<endl;
				cout<<"(按下Enter退出)"<<endl;
				waitEnter();
				break;
			}
		}
		else if(chose == 'r'){
			cin>>width>>height;
			width++;
			height++;
			if(width < 10 && height < 10){
				init(width,height);
			}
		}
		else{
			cout<<"NULL 请重启游戏 NULL";
		}
		if(bomb == 0 && flag == 0){
			cls();
			cout<<"    -===========================================-"<<endl;
			cout<<"                        你赢了!"<<endl;
			cout<<"                          :D"<<endl;
			cout<<"    -===========================================-"<<endl;
			for(int i = 1;i <= height;i++){
				cout<<"        ";
				for(int j = 1;j <= width;j++){
					if(j != 1 && i != 1){
						if(black[i][j] == '1'){
							setColor(3);
						}
						else if(black[i][j] == '2'){
							setColor(2);
						}
						else if(black[i][j] == '3' || black[i][j] == '*'){
							setColor(4);
						}
						else if(black[i][j] == '4'){
							setColor(8);
						}
						else if(black[i][j] == '5'){
							setColor(6);
						}
						else if(black[i][j] == '6'){
							setColor(14);
						}
						else if(black[i][j] == '7'){
							setColor(1);
						}
						else if(black[i][j] == '8'){
							setColor(15);
						}
						else{
							setColor(8);
						}
						cout<<black[i][j]<<" ";
					} 
					else{
						setColor(15);
						cout<<white[i][j]<<" ";
					}
					setColor(15);
				}
				cout<<endl;
			}
			wintimes++;
			cout<<"输入a再来一局新游戏,输入t退出游戏。"<<endl;
			char againOrNot;
			cin>>againOrNot;
			if(againOrNot == 'a'){
				click = 0;
				init(width,height);
			}
			else{
				break;
				return 0;
			}
		}
		cls();
	}
	cout<<"游戏结束!";
	system("pause");
	return 0;
}

回复

0 条回复,欢迎继续交流。

正在加载回复...