跳到正文

休闲·娱乐

C++扫雷

void_ghost约 60 分钟
原文
文章信息与版本

文章信息

发布
2026年11月16日
收录
2026年11月22日
更新
2026年11月22日
篇幅
约 60 分钟
获赞
1

版本

  1. v1当前

声明:保证此篇文章及代码的人类贡献严格大于AI 贡献。

前言闲话

当时开发产品意识不够强,没考虑后期扩展,导致码风、命名都比较随意,甚至没加注释。后来想加入种子、存档、自定义编辑地图等功能时,因为代码不太好读,便没有继续开发。若有大佬愿意继续开发或发现 bug,欢迎私信。

AI 贡献

这个扫雷是我与纳米 AI、DeepSeek 共同编写的。在原版基础上加入了 6 种角色和个人记录等功能。

AI负责内容
纳米AI隐藏光标、颜色宏定义和部分调试
DeepSeek检查 bug 以及部分调试

注:以上两个 AI 的贡献合计不到十分之一。

游玩须知

以下 xxx 均为你的名字或想要的昵称。

  1. 在桌面创建“扫雷”文件夹,在其中创建:
    • xxx的记录.txt
    • xxx的平均分.txt
    • xxx的信息.txt
    • xxx的最高分.txt
    • xxx的密码.txt
    • 游戏记录.txt
  2. 将代码中的 xxx 替换为你的姓名或其他昵称,要与第一步一致。
  3. 在 xxx的平均分.txt、xxx的最高分.txt 中分别输入 0。
  4. 在 xxx的信息.txt 中输入 all:0 win:0 rate:0.0%(其中只有 1 个空格)。
  5. 在 xxx的密码.txt 中输入你想要的密码。
  6. 保证编译环境为 Windows,编译环境为 C++14。

发展历程

版本迭代Update
  • 1.0:实现简单 5×5,功能较基础。
  • 2.0:加入成功/失败反馈、DFS 自动展开,棋盘变为 10×10。
  • 3.0:改良画质,加入颜色及声音。
  • 4.0:加入更多颜色和地雷数量控制,删除声音效果。
  • 5.0:加入光标系统,用光标控制。
  • 6.0:修复边界判断 bug,加入剩余地雷数量显示。
  • 6.5:反馈加入颜色,修改部分数字及光标颜色。
  • 6.7:加入棋盘大小控制,优化代码格式,行号字母化。
  • 6.71:简化统计数量和数字转字符串,修正地雷数不能超过 100 的 bug,修改部分数字及光标颜色。
  • 6.712:修正 sizes<9 时行数显示以及光标不在中间的 bug。
  • 6.713:修正偶数行光标不在中间的 bug,修改 # 的颜色。
  • 6.715:增加按下后弹开光标的功能。
  • 6.716:增加不弹开机制,修改 0 的颜色,更改键位与光标颜色。
  • 6.72:增加控制键位功能。
  • 6.721:修改键位与提示不统一的 bug,反馈增加最高数字提示,简化部分代码。
  • 6.73:增加 Esc 退出、对局内查询最大数字功能;反馈增加数字统计;初始位置不为雷;更改初始地雷数;标记后弹开。
  • 6.732:增加“幸运日”,显示 10 秒整个棋盘,删除多种键位。
  • 6.735:增加角色系统,更改光标。
  • 6.737:增加拆弹专家触雷后标记,增加部分颜色,修复无法胜利、地雷数统计错误等 bug,添加新角色“幸运儿”。
  • 6.739:增加新角色“风险评估师”,改善询问及输出的部分画质,更改拆弹专家冷却时间。
  • 6.74:增加新角色“药剂师”,修改风险评估师的误判概率,修改幸运儿闪烁频率。
  • 6.741:加入总用时反馈,增加暂停。
  • 6.742:优化生成地雷的算法和光标系统,删除弹开。
  • 7.0:增加文件统计玩家游戏信息,改善游戏体验,加入登录、密码掩码。
  • 7.2:删除风险评估师,加入重来机制、登录密码,更改角色名与计分规则。
  • 7.25:增加记录所有人的文件,以及游客自定义英文名和普通人角色。
  • 7.35:更改计分规则,加入“贝叶斯之眼”AI 辅助。
  • 7.5:最终版,修正 bug。

代码

以下为 7.5 最终版代码:

7.5 最终版
C++
#include <bits/stdc++.h> 
#include <windows.h>
#include <conio.h>
#include <chrono>
#include <iomanip>
#include <random>
#include <fstream>
#define RED_TEXT "\033[31m"
#define GREEN_TEXT "\033[32m"
#define YELLOW_TEXT "\033[33m"
#define BLUE_TEXT "\033[34m"
#define RESET_COLOR "\033[0m"
#define MAGENTA_TEXT "\033[35m"
#define CYAN_TEXT "\033[36m"
#define BRIGHT_RED_TEXT "\033[91m"
#define BRIGHT_GREEN_TEXT "\033[92m"
#define BRIGHT_YELLOW_TEXT "\033[93m"
#define BRIGHT_BLUE_TEXT "\033[94m"
#define BRIGHT_MAGENTA_TEXT "\033[95m"
#define BRIGHT_CYAN_TEXT "\033[96m"
using namespace std ;
typedef long long ll ;
const int N = 1310 ;
ll score , usermax ;
int lucktime = 1000 ;
int playerx , playery , z , sizes = 10 , live = 1 , opennum , usedrink , begintime , who ;
int boom = 20 , opens , boommax , maxcnt , lasttime , choose , nowtime , cntdown , boom2 = 20 ;
int a [N] [N] , b [N] [N] , c [N] , d [N] ;
char isflag = '1' , isopen = '0' ;
bool breakit = false , changewho = true , useai = false ;
bool win = false , hit = false , ask = false , touchboom = false , drink = false , stop = false ;
int usetime = 10 , stoptime , beginstop ;
int userall = 0 , userwin = 0 ;
double density = 0.00 , userrate = 0.00 , average = 0.00 ;
string password = "" , inputit = "" , userpassword = "" , username = "" ;
int boomcnt [10] = { 0 } ;
const int D [8] [2] = { { 0 , -1 } , { 1 , 0 } , { 0 , 1 }  , { -1 , 0 } , { -1 , 1 } , { 1 , -1 } , { -1 , -1 } , { 1 , 1 } } ;

int main() ;

std::random_device rd ;  
std::mt19937 gen ( rd() ) ;

inline int rands( int x ) {
    std::uniform_int_distribution <> distrib ( 1 , x ) ;
    return distrib ( gen ) ;
}

inline bool outside( int x , int y ) {
	return ( x < 1 || x > sizes || y < 1 || y > sizes ) ;
}

inline void resetgame() {
	lucktime = 1000 ;
	playerx = playery = 5 ;
	z = 0 ; 
	sizes = 10 ;
	score = usermax = 0LL ;
	live = 1 ;
	opennum = usedrink = begintime = 0 ;
	boom = boom2 = 20 ;
	opens = boommax = maxcnt = lasttime = choose = nowtime = cntdown = 0 ;
	memset ( a , 0 , sizeof ( a ) ) ;
	memset ( b , 0 , sizeof ( b ) ) ;
	memset ( c , 0 , sizeof ( c ) ) ;
	memset ( d , 0 , sizeof ( d ) ) ;
	isflag = '1' , isopen = '0' ;
	breakit = false ;
	changewho = false , useai = false ;
	win = false , hit = false , ask = false , touchboom = false , drink = false , stop = false ;
	usetime = 10 ;
	stoptime = beginstop = 0 ;
	userall = userwin = usermax = 0 ;
	density = userrate = average = 0.00 ;
	password = inputit = userpassword = "" ;
	memset ( boomcnt , 0 , sizeof ( boomcnt ) ) ;
}

inline void showmenu() {
	cout << "\n\n" ;
    cout << "|----------------------------------|\n" ;
    cout << "|        游戏结束                  |\n" ;
    cout << "|----------------------------------|\n" ;
    cout << "|    1. 返回主菜单                 |\n" ;
    cout << "|    2. 退出游戏                   |\n" ;
    cout << "|----------------------------------|\n" ;
    int q ;
    cin >> q ;
    if ( q == 1 ) {
    	resetgame() ;
    	main() ;
	}
    else
    	exit ( 0 ) ;
}

inline int gettime() {
    auto now = std::chrono::system_clock::now() ;
    std::time_t now_time = std::chrono::system_clock::to_time_t ( now ) ;
    std::tm *ptm = std::localtime ( &now_time ) ;
    int hour = ptm->tm_hour ;
    int minute = ptm->tm_min ;
    int second = ptm->tm_sec ;
    return hour * 3600 + minute * 60 + second ;
} 

inline int solvetime( int x ) {
	if ( x == 1 )
		return 40 ;
	if ( x == 2 )
		return 60 ;
	if ( x == 5 )
		return 30 ;
	return 0;
}

inline string paint( int x ) {
	if ( x == 0 )
		return BRIGHT_BLUE_TEXT ;
	if ( x == 1 )
		return GREEN_TEXT ;
	if ( x == 2 )
		return YELLOW_TEXT ;
	if ( x == 3 )
		return MAGENTA_TEXT ;
	if ( x == 4 )
		return CYAN_TEXT ;
	if ( x == 5 )
		return BLUE_TEXT ;
	if ( x == 6 )
		return BRIGHT_GREEN_TEXT ;
	if ( x == 7 )
		return BRIGHT_CYAN_TEXT ;
	if ( x == 8 )
		return BRIGHT_YELLOW_TEXT ;
	return RESET_COLOR ;
}

inline void output() {
	string buffer = "" ;
	for ( int i = 1 ; i <= sizes ; i++ ) {
		for ( int j = 1 ; j <= sizes ; j++ ) {
			if ( a [i] [j] == -1 ) {
				buffer += RED_TEXT ;
				buffer += "x  " ;
				buffer += RESET_COLOR ;
			}
			else {
				buffer += paint ( a [i] [j] ) ;
				buffer += a [i] [j] + '0' ;
				buffer += "  " ;
				buffer += RESET_COLOR ;
			}
		}
		buffer += '\n' ;
	}
	for ( int i = boommax ; i >= 1 ; i-- ) {
		buffer += to_string( i ) ;
		buffer += ": " ;
		buffer += to_string( boomcnt [i] ) ;
		buffer += "   " ;
	}
	cout << buffer ;
}

inline void savefile1( string content ) {
	ofstream outfile( "xxx的记录.txt" , ios::app ) ;
	outfile << content ;
	outfile.close() ;
	string con = "" ;
	con += " all:" ;
	con += to_string( userall ) ;
	con += " win:" ;
	con += to_string( userwin ) ;
	con += " rate:" ;
	con += to_string( userrate ) ;
	con += '%' ;
	ofstream outfile2( "xxx的信息.txt" ) ;
	outfile2 << con ;
	outfile2.close() ;
	ofstream outfile3( "xxx的平均分.txt" ) ;
	outfile3 << to_string( average ) ;
	outfile3.close() ;
	ofstream outfile4( "xxx的最高分.txt" ) ;
	outfile4 << to_string( usermax ) ;
	outfile4.close() ;
}

inline void saveall( string content ) {
	ofstream outfile( "游戏记录.txt" , ios::app ) ;
	content += " " ;
	content += username ;
	content += "\n" ;
	outfile << content ;
	outfile.close() ;
}

inline void makecontent( bool win , int num ) {
	string content = "" ;
	if ( win != false )
		content = "win " ;
	else
		content = "lose " ;
	content += to_string( num ) ;
	content += "秒 大小:" ;
	content += to_string( sizes ) ;
	content += " 地雷:" ;
	content += to_string( boom2 ) ;
	content += " 得分:" ;
	content += to_string( score ) ;
	if ( who == 1 )
		content += " \n" ;
	if ( who == 1 ) {
		savefile1( content ) ;
	}
	if ( who == 2 )
		saveall( content ) ;
}

inline double calchard( double x ) {
	if ( x <= 0.10 )
		return 100.00 ;
	else
		if ( x <= 0.150 )
			return 0.16 * x * 10000 + 0.4 ;
		else
			if ( x <= 0.20 )
				return 0.18 * x * 10000 ;
			else
				if ( x <= 0.250 )
					return 0.22 * x * 10000 - 0.6 ;
				else
					if ( x <= 0.30 )
						return 0.26 * x * 10000 - 1.5 ;
					else
						if ( x <= 0.50 )
							return 0.175 * x * 10000 + 1.25 ;
						else
							if ( x <= 0.60 )
								return 0.2 * x * 10000 - 1.5 ;
							else
								return 0.25 * x * 10000 - 4.5 ;
}

inline void iswin() {
	int num = gettime() - begintime - stoptime ;
	if ( num == 0 )
		num = 1 ;
	userall++ , userwin++ ;
	userrate = ( double ) userwin * 1.00 / ( double ) userall * 100.00 ;
	double hard = calchard( density ) ;
	score = round( sizes * density * 1.00 * hard * 1.00 / ( ( double ) num ) * 1.00 * 10000.00 + 0.5 ) * 1LL + 0LL ;
	if ( score <= 500 )
		score = 500 ;
	system ( "cls" ) ;
	Sleep(50) ;
	printf ( "恭喜您,游戏胜利!(最高地雷数为:%d )" , boommax ) ;
	printf ( "\n用时:%d时%d分%d秒" , num / 3600 , num % 3600 / 60 , num % 3600 % 60 ) ;
	Sleep(900) ;
	printf ( "\n" ) ;
	output() ;
	printf ( "\n得分为:%d\n" , score ) ;
	usermax = max ( usermax , score ) ;
	average = ( double ) ( ( double ) ( userall - 1.00 ) * 1.00 * average + ( double ) score ) / userall ;
	makecontent( true , num ) ;
	showmenu() ;
}

inline void islose() {
	int num = gettime() - begintime - stoptime ;
	userall++ ;
	userrate = ( double ) userwin * 1.00 / ( double ) userall * 100.00 ;
	system ( "cls" ) ;
	Sleep(50) ;
	printf ( "很遗憾,您踩到了雷(最高地雷数为:%d )\n" , boommax ) ;
	printf ( "\n" ) ;
	Sleep(900) ;
	output() ;
	score = 0LL ;
	printf ( "\n得分为:%d\n" , score ) ;
	average = ( double ) ( ( double ) ( userall - 1.00 ) * 1.00 * average + ( double ) score ) / userall ;
	makecontent ( false , num ) ;
	showmenu() ;
}

inline int ran() {
	return rands( sizes ) ;
}

inline double calcp() {
	int x = playerx , y = playery , cnt = 0 ;
	if ( b [x] [y] == 1 )
		return 0.00 ;
	if ( b [x] [y] == 2 )
		return 100.00 ;
	double calc [20] ;
	memset ( calc , 0 , sizeof ( calc ) ) ;
	for ( int i = 0 ; i < 8 ; i++ ) {
		int xx = x + D [i] [0] , yy = y + D [i] [1] ;
		if ( outside( xx , yy ) )
			continue ;
		int sum1 = 0 , sum2 = 0 , sum3 = 0 ;
		for ( int j = 0 ; j < 8 ; j++ ) {
			int xxx = xx + D [j] [0] , yyy = yy + D [j] [1] ;
			if ( outside ( xxx , yyy ) )
				continue ;
			sum3++ ;
			if ( b [xxx] [yyy] == 1 )
				sum1++ ;
			if ( b [xxx] [yyy] == 2 )
				sum2++ ;
		}
		if ( b [xx] [yy] == 1 ) {
			double val = ( double ) ( a [xx] [yy] - sum2 ) * 1.00 / ( double ) ( sum3 - sum1 - sum2 ) * 100.00 ;
			if ( val < 0.00 )
				val = 0.00 ;
			if ( val > 100.00 )
				val = 100.00 ;
			calc [++cnt] = val ;
		}
	}
	if ( cnt == 0 ) {
		if ( sizes * sizes - opennum - opens <= 0 )
			return 0.00 ;
		double pp = ( double ) ( boom2 - opens ) * 1.00 / ( sizes * sizes - opennum - opens ) * 100.00 ;
		if ( pp < 0.00 )
			pp = 0.00 ;
		if ( pp > 100.00 )
			pp = 100.00 ;
		return pp ;
	}
	double psum = 1.00 ;
	for ( int i = 1 ; i <= cnt ; i++ ) {
		if ( calc [i] == 100.00 )
			return 100.00 ;
		if ( calc [i] == 0.00 )
			return 0.00 ;
		psum *= calc [i] * 1.00 ;
	}
	psum *= 1.00 ;
	return pow ( psum , 1.00 / ( double ) cnt ) ;
}

inline void draw() {
	COORD coord = { 0 , 0 } ;
	SetConsoleCursorPosition ( GetStdHandle ( STD_OUTPUT_HANDLE ) , coord ) ;
	string buffer = "" ;
	buffer += "用方向键移动,按0翻开,按1或2标记/取消 \n按'a'键询问最大数字,按'd'使用技能 ,'u'键启用/关闭AI,esc退出,空格暂停\n" ;
	if ( useai ) {
		buffer += GREEN_TEXT ;
		buffer += "已启动“贝叶斯之眼”AI大模型辅助\n" ;
		buffer += RESET_COLOR ;
	}
	if ( touchboom ) {
		buffer += RED_TEXT ;
		buffer += "踩雷!!!" ;
		buffer += paint ( 9 ) ;
		buffer += '\n' ;
	}
	if ( drink ) {
		if ( usedrink <= 3 ) {
			buffer += paint ( 1 ) ;
			switch ( usedrink ) {
				case 1 :
					buffer += "生命药剂:  生命增加!\n" ;
					live++ ;
					break ;
				case 2 :
					if ( rands( 2 ) == 1 )
						goto luck ;
					buffer += "变身药剂:  变为" ;
					choose = rands ( 5 ) ;
					if ( choose == 5 )
						choose = 6 ;
					cntdown = solvetime ( choose ) ;
					if ( choose == 1 )
						buffer += "暗格先探员\n" ;
					else
						if ( choose == 2 )
							buffer += "星渊庇佑者\n" ;
						else
							if ( choose == 3 )
								buffer += "幻途纠正师\n" ;
							else
								if ( choose == 4 )
									buffer += "概率守望者\n" ;
								else
									buffer += "普通人\n" ;
					break ;
				case 3 :
					luck :
					buffer += "幸运药剂: 幸运日触发概率降低\n"  ;
					if ( lucktime > 200 )
						lucktime -= 200 ;
					else
						lucktime -= 5 ;
					if ( lucktime < 5 )
						lucktime = 5 ;
					break ;
			}
		}
		else {
			switch ( usedrink ) {
				case 4 :
					if ( rands( 20 ) == 1 ) {
						system ( "cls" ) ;
						Sleep(500) ;
						cout << RED_TEXT << "喝到毒药!!!" << RESET_COLOR ;
						Sleep(1000) ; 
						islose() ;
					}
					buffer += paint ( 2 ) ;
					if ( rands( 2 ) == 1 ) {
						buffer += "疲劳药剂: 10%概率不移动\n" ;
						usedrink = -1 ;
					}
					else
						buffer += "喝了一瓶水……\n" ;
					break ;
			}
		}
	}
	drink = false ;
	buffer += RESET_COLOR ;
	buffer += "    " ;
	for ( int i = 1 ; i <= min ( 9 , sizes ) ; i++ )
		buffer += i + '0' , buffer += "  " ;
	for ( int i = 0 ; i <= sizes - 10 ; i++ ) {
		buffer += i + 'A' ;
		buffer += "  " ;
	}
	buffer += "\n\n" ;
	for ( int i = 1 ; i <= sizes ; i++ ) {
		for ( int j = 0 ; j <= sizes ; j++ ) {
			if ( i == playerx && j == playery ) {
				if ( b [i] [j] == 2 ) {
					buffer += RED_TEXT ;
					buffer += 'x' ;
				}
				else
					if ( b [i] [j] == 1 && a [i] [j] != -1 ) {
						buffer += paint ( a [i] [j] ) ;
						buffer += to_string( a [i] [j] ) ;
					}
					else {
						if ( b [i] [j] == 0 )
							buffer += '#' ;
						else {
							buffer += RED_TEXT ;
							buffer += 'x' ;					
						}
					}
				buffer += RESET_COLOR ;
				continue ;
			}
			if ( j == 0 && i <= 9 ) {
				buffer += i + '0' ;
				buffer += "   " ;
			}
			else 
				if ( j == 0 ) {
					buffer += i - 10 + 'A' ;
					buffer += "   " ;
				}
				else 
					if ( b [i] [j] == 2 ) {
						begins :
						if ( i == playerx && j - 1 == playery ) {
							buffer += BRIGHT_YELLOW_TEXT ;
							buffer += "] " ;
							buffer += RESET_COLOR ;
						}
						buffer += RED_TEXT ;
						buffer += "x " ;
						buffer += RESET_COLOR ;
						if ( i == playerx && j + 1 == playery ) {
							buffer += BRIGHT_YELLOW_TEXT ;
							buffer += '[' ;
							buffer += RESET_COLOR ;
						}
						else
							buffer += ' ' ;
					}
					else 
						if ( b [i] [j] == 1 ) {
							if ( a [i] [j] == -1 )
								goto begins ;
							if ( i == playerx && j - 1 == playery ) {
								buffer += BRIGHT_YELLOW_TEXT ;
								buffer += "] " ;
								buffer += RESET_COLOR ;
							}
							buffer += paint( a [i] [j] ) ;
							buffer += a [i] [j] + '0' ;
							buffer += " " ;
							buffer += RESET_COLOR ;
							if ( i == playerx && j + 1 == playery ) {
								buffer += BRIGHT_YELLOW_TEXT ;
								buffer += '[' ;
								buffer += RESET_COLOR ;
							}
							else
								buffer += ' ' ;
						}
						else {
							if ( i == playerx && j - 1 == playery ) {
								buffer += BRIGHT_YELLOW_TEXT ;
								buffer += "] " ;
							}
							buffer += RESET_COLOR ;
							buffer += "# " ;
							buffer += RESET_COLOR ;
							if ( i == playerx && j + 1 == playery ) {
								buffer += BRIGHT_YELLOW_TEXT ;
								buffer += '[' ;
								buffer += RESET_COLOR ;
							}
							else
								buffer += ' ' ;
						}
		}
		buffer += '\n' ;
	}
	if ( useai ) {
		double p = calcp() ;
		if ( p < 20 )
			buffer += GREEN_TEXT ;
		else
			if ( p < 70 )
				buffer += YELLOW_TEXT ;
			else
				buffer += RED_TEXT ;
		buffer += "当前格子是地雷的概率为:" ;
		buffer += to_string ( p ) ;
		buffer += "%" ;
		buffer += RESET_COLOR ;
	}
	buffer += paint ( 7 ) ;
	buffer += "\n当前角色:" ;
	if ( choose == 1 )
		buffer += "暗格先探员\n技能:随机翻开5个格子,但会使冷却时间不定加上1到10秒  冷却:40秒\n" ;
	else
		if ( choose == 2 )
			buffer += "星渊庇佑者\n技能:增加生命(最大为3),但有5%移动时随机传送   冷却:60秒\n" ;
		else
			if ( choose == 3 )
				buffer += "幻途纠正师\n技能:20%不标记被错误标记的格子(被动无冷却),但8%概率不移动\n" ;
			else
				if ( choose == 4 ) 
					buffer += "概率守望者\n技能:将触发彩蛋的几率变为1%,但会有视野闪烁\n" ;
				else
					if ( choose == 5 )
						buffer += "万物炼药师\n技能:喝下一瓶药剂,里面装的是什么呢? 冷却:30秒\n" ;
					else
						buffer += "普通人\n无技能\n" ;
	buffer += RESET_COLOR ;
	if ( choose == 1 || choose == 2 || choose == 5 ) {
		buffer += paint( 4 ) ; 
		buffer += "剩余冷却时间:" ;
		buffer += to_string ( max ( 0 , cntdown - ( nowtime - lasttime ) ) ) ;
		buffer += RESET_COLOR ;
		buffer += '\n' ;
	}
	buffer += RED_TEXT ;
	buffer += "剩余地雷数:" ;
	int x = boom2 - opens ;
	buffer += to_string ( x ) ;
	buffer += RESET_COLOR ;
	buffer += '\n' ;
	buffer += paint ( 1 ) ;
	buffer += "剩余生命:" ;
	buffer += to_string ( live ) ;
	buffer += RESET_COLOR ;
	buffer += '\n' ;
	buffer += paint ( 8 ) ;
	buffer += "已用时:" ;
	int num = gettime() - begintime - stoptime ;
	buffer += to_string( num / 3600 ) ;
	buffer += "时" ;
	buffer += to_string( num % 3600 / 60 ) ;
	buffer += "分" ;
	buffer += to_string( num % 3600 % 60 ) ;
	buffer += "秒\n" ; 
	buffer += RESET_COLOR ;
	if ( ask ) {
		buffer += "最大地雷数: " ;
		buffer += to_string( boommax ) ;
		buffer += '\n' ;
		buffer += "最大地雷数数量:" ;
		buffer += to_string( maxcnt ) ;
	}
    static HANDLE hConsole = GetStdHandle ( STD_OUTPUT_HANDLE ) ;
    SetConsoleCursorPosition ( hConsole , coord ) ;
    WriteConsoleA ( hConsole , buffer.c_str() , buffer.size() , NULL , NULL ) ;
}

inline void slove() {
	int safes = sizes * sizes - boom2 ;
	int opened = 0 ;
	for ( int i = 1 ; i <= sizes ; i++ ) {
		for ( int j = 1 ; j <= sizes ; j++ ) {
			if ( b [i] [j] == 1 && a [i] [j] != -1 )
				opened++ ;
		}
	}
	win = ( opened == safes ) ;
}

inline void dfs( int x , int y ) {
	for ( int i = 0 ; i < 8 ; i++ ) {
		int xx = x + D [i] [0] , yy = y + D [i] [1] ;
		if ( xx < 1 || xx > sizes || yy < 1 || yy > sizes )
			continue ;
		if ( a [xx] [yy] == 0 && b [xx] [yy] == 0 )
			b [xx] [yy] = 1 , opennum++ , dfs( xx , yy ) ;
		else 
			if ( a [xx] [yy] != -1 && b [xx] [yy] == 0 )
				b [xx] [yy] = 1 , opennum++ ;
	}
}

inline void use1() {
	slove() ;
	if ( win )
		iswin() ;
	int cx [N] , dx [N] ;
	memset ( cx , 0 , sizeof ( cx ) ) ;
	memset ( dx , 0 , sizeof ( dx ) ) ;
	int xycnt = 0 ;
	for ( int i = 1 ; i <= sizes ; i++ ) {
		for ( int j = 1 ; j <= sizes ; j++ ) {
			int x = i , y = j ;
			if ( b [x] [y] == 1 || b [x] [y] == 2 )
				continue ;
			cx [++xycnt] = x , dx [xycnt] = y ;
		}
	}
	if ( xycnt <= 5 ) {
		for ( int i = 1 ; i <= xycnt ; i++ ) {
			int x = cx [i] , y = dx [i] ;
			if ( a [x] [y] == -1 ) {
				b [x] [y] = 2 ;
				opens++ ;
				continue ;
			}
			b [ cx [i] ] [ dx [i] ] = 1 ;
			opennum++ ;
			if ( a [x] [y] == 0 )
				dfs ( x , y ) ;
		}
		slove() ;
		if ( win )
			iswin() ;
		return ;
	}
	for ( int i = 1 ; i <= 5 ; i++ ) {
		int r = rands ( xycnt ) ;
		int x = cx [r] , y = dx [r] ;
		if ( x == 0 || y == 0 ) {
			--i ;
			continue ;
		}
		if ( a [x] [y] == -1 ) {
			b [x] [y] = 2 ;
			opens++ ;
		}
		else {
			b [x] [y] = 1 ;
			opennum++ ;
			if ( a [x] [y] == 0 )
				dfs( x , y ) ;
		}
		cx [r] = 0 , dx [r] = 0 ;
	}
	slove() ;
	if ( win )
		iswin() ;	
}

inline void use5() {
	usedrink = rands ( 4 ) ;
}

inline void useit() {
	nowtime = gettime() ;
	int sum = cntdown - ( nowtime - lasttime ) ;
	if ( sum > 0 )
		return ;
	if ( choose == 2 && live == 3 ) {
		lasttime = nowtime ;
		return ;
	}
	if ( choose == 1 ) {
		lasttime = nowtime ;
		cntdown += rands ( 10 ) ;
		use1() ;
	}
	else {
		if ( choose == 2 ) {
			lasttime = nowtime ;
			live = min ( 3 , ++live ) ;
		}
		else {
			if ( choose == 5 ) {
				drink = true ;
				use5() ;
				lasttime = nowtime ;
			}
		}
	}
}

inline void input() {
	int x = playerx , y = playery ;
	if ( _kbhit() ) {
		int key = _getch() ;
		hit = true ;
		z = 0 ;
		if ( key == isflag )
			z = 2 ;
		else
			if ( key == isopen )
				z = 1 ;
			else
				switch ( key ) {
					case 32 :
						stop = true ;
						break ;
					case 'u' :
						useai = !useai ;
						break ;
					case 'd' :
						if ( choose == 1 || choose == 2 || choose == 5 )
							useit() ;
						break ;
					case 'a' :
						ask = !ask ;
						break ;
					case 27 :
						breakit = true ;
						break ;
					case '2' :
						z = 2 ;
						break ;
					case 72 :
						if ( !outside( playerx - 1 , playery ) )
							playerx-- ;
						break ;
					case 80 :
						if ( !outside( playerx + 1 , playery ) )
							playerx++ ;
						break ;
					case 75 :
						if ( !outside( playerx , playery - 1 ) )
							playery-- ;
						break ;
					case 77 :
						if ( !outside( playerx , playery + 1 ) )
							playery++ ;
						break ;
					default :
						hit = false ;
						break ;
				}
	}
	if ( choose == 2 && ( playerx != x || playery != y ) ) {
		if ( rands ( 20 ) == 1 ) {
			playerx = ran() , playery = ran() ;
		}
	}
	if ( choose == 3 || ( choose == 5 && usedrink == -1 ) ) {
		if ( rands ( 13 ) == 1 ) {
			playerx = x , playery = y ;
		}
	}
}

inline void randboom() {
	memset ( a , 0 , sizeof ( a ) ) ;
	playerx = playery = ( sizes + 1 ) / 2 ;
    vector < pair < int , int > > cell ;
    for ( int i = 1 ; i <= sizes ; i++ ) {
        for ( int j = 1 ; j <= sizes ; j++ ) {
            if ( i != playerx || j != playery ) {
                cell.emplace_back( i , j ) ;
            }
        }
    }
    std::shuffle ( cell.begin() , cell.end() , gen ) ;
    int maxboom = min ( boom , ( int ) cell.size() ) ;
    for ( int i = 0 ; i < maxboom ; i++ ) {
        int r = cell [i].first ;
        int c = cell [i].second ;
        a [r] [c] = -1 ;
    }
}

inline void putname() {
	if ( who == 1 )
		cout << "xxx" ;
	else
		if ( who == 2 )
			cout << "游客  " ;
}

inline void readuser1() {
    ifstream file( "xxx的信息.txt" ) ;
    if ( !file.is_open() ) {
        cerr << "文件打开失败!" ;
        return ;
    }
    string line = "" ;
    while ( getline ( file , line ) ) {
        int len = line.size() ;
        for ( int i = 0 ; i < len ; ) {
            if ( i + 4 <= len && line.substr ( i , 4 ) == "all:" ) {
                i += 4 ;
                userall = 0 ;
                while ( i < len && isdigit ( line [i] ) ) {
                    userall = userall * 10 + ( line [i] - '0' ) ;
                    i++ ;
                }
                continue ;
            }
            if ( i + 4 <= len && line.substr ( i , 4 ) == "win:" ) {
                i += 4 ;
                userwin = 0 ;
                while ( i < len && isdigit ( line [i] ) )  {
                    userwin = userwin * 10 + ( line [i] - '0' ) ;
                    i++ ;
            	}
                continue ;
            }
            if ( i + 5 <= len && line.substr ( i , 5 ) == "rate:" ) {
                i += 5 ;
                string rate_str = "" ;
                while ( i < len && ( isdigit ( line [i] ) || line [i] == '.' || line [i] == '%' ) ) {
                    if ( line [i] != '%' ) {
                        rate_str += line [i] ;
                    }
                    i++ ;
                }
                if ( !rate_str.empty() ) {
                    userrate = stod( rate_str ) ;
                }
                continue ;
            }
            i++ ;
        }
    }
    file.close() ;
    ifstream file2( "xxx的平均分.txt" ) ;
    if ( !file2.is_open() ) {
		cerr << "文件打开失败!!!" ;
		return ;
	}
	getline ( file2 , line ) ;
	if ( !line.empty() )
    	average = ( double ) stod( line ) ;
    ifstream file3( "xxx的最高分.txt" ) ;
    if ( !file3.is_open() ) {
		cerr << "文件打开失败!!!" ;
		return ;
	}
	getline ( file3 , line ) ;
	if ( !line.empty() )
    	usermax = ( double ) stod( line ) ;
}

inline string readpassword() {
	string readit = "" ;
	while ( true ) {
		char ch = _getch() ;
		if ( ch == '\r' )
			break ;
		if ( ch == '\b' ) {
			if ( !readit.empty() ) {
				readit.pop_back() ;
				cout << "\b \b" ;
			}
			continue ;
		}
		readit += ch ;
		cout << "*" ;
	}
	return readit ;
}

inline bool solvepass1() {
	ifstream file( "xxx的密码.txt" ) ;
	string line = "" ;
	getline ( file , line ) ;
	return ( line == userpassword ) ;
}

inline void change1( string change ) {
	ofstream file( "xxx的密码.txt" ) ;
	file << change ;
	file.close() ;
}

int main() {
	if ( changewho ) {
		resetgame() ;
		changewho = true ;
	}
	nameagain :
	if ( changewho ) {
		system ( "cls" ) ;
		who = 0 ;
		cout << "请输入您的姓名:\n( 1.xxx 2.自定义名字)\n" ;
		cin >> username ;
		if ( username == "1" )
			who = 1 ;
		else
			who = 2 ;
		if ( who == 2 ) {
			cout << "请输入自定义名字:\n" ;
			cin >> username ;
		}
		changewho = false ;
		if ( who == 1 ) {
			cout << "请输入密码:" ;
			userpassword = readpassword() ;
			bool pass = false ;
			if ( who == 1 )
				pass = solvepass1() ;
			if ( !pass ) {
				cout << '\n' << RED_TEXT << "错误!!!" << RESET_COLOR ;
				exit ( 0 ) ;
			}
		}
	}
	system ( "cls" ) ;
	if ( who == 2 ) {
		resetgame() ;
	}
	showagain :
	cout << "|----------------------------------|\n" ;
	cout << "|       欢迎您," ;
	putname() ;    cout << "             |\n" ;
	cout << "|                                  |\n" ;
	if ( who == 1 )
		cout << "|    0.修改密码                    |\n" ;
	cout << "|    1.开始游戏                    |\n" ;
	cout << "|    2.退出登录                    |\n" ;
	cout << "|    3.退出游戏                    |\n" ;
	cout << "|                                  |\n" ;
	if ( who == 1 ) {
		userall = userwin = usermax = 0 ;
		userrate = average = 0.00 ;
		if ( who == 1 )
			readuser1() ;
		cout << "  总场数:" << userall << " 胜利场数:" << userwin << " 胜率:" << userrate << "%\n" ;
		cout << "  最高得分:" << usermax << "  平均分:" << average << '\n' ;
	}
	cout << "|----------------------------------|\n" ;
	int firstchoose = 4 ;
	cin >> firstchoose ;
	if ( firstchoose > 3 || ( firstchoose < 1 && who == 2 ) )
		exit( 0 ) ;
	if ( firstchoose == 0 ) {
		Sleep(50) ;
		system ( "cls" ) ;
		cout << "请输入更改后的密码:" ;
		string change = readpassword() ;
		if ( who == 1 )
			change1( change ) ;
		cout << '\n' << GREEN_TEXT << "成功!" << RESET_COLOR ;
		Sleep(1000) ;
		system ( "cls" ) ;
		goto showagain ;
	}
	if ( firstchoose == 2 ) {
		changewho = true ;
		goto nameagain ;
	}
	if ( firstchoose == 3 )
		exit ( 0 ) ;
	Sleep(50) ;
	system ( "cls" ) ;
	cout << "请输入您想要的大小:(1为10*10,0为直接跳过)\n" ;
	int q ;
	cin >> q ;
	if ( q == 1 )
		goto start1 ;
	if ( q == 0 )
		goto start2 ;
	if ( q < 5 || q > 35 )
		exit ( 0 ) ;
	sizes = q ;
	playerx = playery = ( sizes + 1 ) / 2 ;
	start1 :
	cout << "请输入您想要的地雷数:(上限为70%)\n" ;
	cin >> boom ;
	boom2 = boom ;
	density = ( double ) ( boom * 1.00 ) / ( double ) ( 1.00 * sizes * sizes ) ;
	if ( boom <= 0 || boom > sizes * sizes - 1 || density > 0.700 )
		exit ( 0 ) ;
	start2 :
	playerx = playery = ( sizes + 1 ) / 2 ;
	system ( "cls" ) ;
	cout << "请选择您想要的角色:\n" ;
	string buffer = "" ;
	buffer += paint ( 0 ) ;
	buffer += "1.暗格先探员    技能:随机翻开5个格子,但会使冷却时间不定加上1到10秒   冷却:40秒\n\n" ;
	buffer += RESET_COLOR ;
	buffer += paint ( 1 ) ;
	buffer += "2.星渊庇佑者    技能:生命增加1(最高为3),但有5%移动时随机传送   冷却:60秒\n\n" ;
	buffer += RESET_COLOR ;	
	buffer += paint ( 2 ) ;
	buffer += "3.幻途纠正师    技能:20%不标记被错误标记的格子(被动无冷却),但8%概率不移动\n\n" ;
	buffer += RESET_COLOR ;
	buffer += paint ( 3 ) ;
	buffer += "4.概率守望者    技能:将触发彩蛋的几率变为1%,但会有视野闪烁\n\n" ;
	buffer += RESET_COLOR ;
	buffer += paint ( 4 ) ;
	buffer += "5.万物炼药师    技能:喝下一瓶药剂,里面装的是什么呢?  冷却:30秒\n\n" ;
	buffer += RESET_COLOR ;
	buffer += paint ( 5 ) ;
	buffer += "6.普通人        无技能\n" ;
	buffer += RESET_COLOR ;
	cout << buffer ;
	cin >> choose ;
	if ( choose < 1 || choose > 6 )
		exit ( 0 ) ;
	Sleep(100) ;
	system ( "cls" ) ;
	randboom() ;
	for ( int i = 1 ; i <= sizes ; i++ ) {
		for ( int j = 1 ; j <= sizes ; j++ ) {
			if ( a [i] [j] == -1 )
				continue ;
			for ( int k = 0 ; k < 8 ; k++ ) {
				int xx = i + D [k] [0] , yy = j + D [k] [1] ;
				if ( xx < 1 || xx > sizes || yy < 1 || yy > sizes )
					continue ;
				if ( a [xx] [yy] == -1 )
					a [i] [j]++ ;
			}
			boomcnt [ a [i] [j] ]++ ;
			boommax = max ( boommax , a [i] [j] ) ;
		}
	}
	maxcnt = boomcnt [ boommax ] ;
	printf ( "游戏即将开始... ..." ) ;
	Sleep(500) ;
	system ( "cls" ) ;
	Sleep(50) ;
	if ( choose == 4 )
		lucktime = 100 ;
	cntdown = solvetime( choose ) ;
	lasttime = gettime() ;
	boom2 = boom ;
	begintime = gettime() ;
	while ( true ) {
		if ( choose == 4 )
			lucktime = 100 ;
		nowtime = gettime() ;
		draw() ;
		hit = false ;
		z = 0 ;
		while ( !hit ) {
			input() ;
			if ( choose == 4 && rands ( 100000 ) == 1 ) {
				system ( "cls" ) ;
				break ;
			}
		}
		bool in ;
		in = true ;
		if ( stop )
			beginstop = gettime() ;
		while ( stop ) {
			if ( in ) {
				system ( "cls" ) ;
				Sleep(50) ;
				cout << "请输入解锁密钥:" ;
				password.clear() ;
				password = readpassword() ;
				system ( "cls" ) ;
				in = false ;
			}
			cout << "请输入密码解锁:" ;
			inputit.clear() ;
			inputit = readpassword() ;
			if ( usetime - 1 <= 0 && password != inputit ) {
				cout << "即将强制结束" ;
				Sleep(500) ;
				exit ( 0 ) ;
			}
			else {
				if ( password == inputit ) {
					Sleep(500) ;
					system ( "cls" ) ;
					cout << GREEN_TEXT << "成功!" << RESET_COLOR ;
					Sleep(1000) ;
					system ( "cls" ) ;
					stop = false ;
					stoptime += gettime() - beginstop ;
				}
				else {
					system ( "cls" ) ;
					cout << RED_TEXT << "错误!" << RESET_COLOR ;
					Sleep(1000) ;
					system ( "cls" ) ; 
					usetime-- ;
				}
			}
		}
		if ( rands ( lucktime ) == lucktime ) {
			system ( "cls" ) ;
			beginstop = gettime() ;
			cout << "恭喜您触发了“幸运日”,整个棋盘将显示10秒,抓紧时间记下来吧!\n" ;
			Sleep(2000) ;
			output() ;
			Sleep(10000) ;
			system ( "cls" ) ;
			cout << "即将回归... ..." ;
			Sleep(2000) ;
			stoptime += gettime() - beginstop ;
		}
		if ( breakit ) {
			system ( "cls" ) ;
			output() ;
			Sleep(100) ;
			showmenu() ;
			breakit = false ;
		}
		if ( z == 1 ) {
			if ( a [playerx] [playery] == -1 ) {
				live-- ;
				touchboom = true ;
				if ( live == 0 )
					islose() ;
				b [playerx] [playery] = 2 ;
				Sleep(20) ;
				system ( "cls" ) ;
				draw() ;
			}
			else {
				b [playerx] [playery] = 1 ;
				opennum++ ;
				if ( a [playerx] [playery] == 0 ) {
					dfs( playerx , playery ) ;
				}
				slove() ;
			}
		} 
		else 
			if ( z == 2 ) {
				if ( b [playerx] [playery] == 0 ) {
					if ( choose == 3 && a [playerx] [playery] != -1 ) {
						if ( rands ( 5 ) != 1 )
							b [playerx] [playery] = 2 , opens++ ;
					}
					else
						b [playerx] [playery] = 2 , opens++ ;
				}
				else 
					if ( b [playerx] [playery] == 2 )
						b [playerx] [playery] = 0 , opens-- ;
				slove() ;
			}
		nowtime = gettime() ;
		slove() ;
		if ( win ) {
			iswin() ;
			exit ( 0 ) ;
		}
		system ( "cls" ) ;
		if ( rands( 5 ) == 1 )
			touchboom = false ;
	}
	exit ( 0 ) ;
}

完结撒花!

(本篇文章使用 Deepseek 优化了格式。)。

原文