自己做的扫雷
吾辈之拙码,还请高人指点..这也太假了
一些更新
2020/10/23 更新了游戏帮助
2020/11/5 更改了界面图形,解决了由于win7与win10显示不同而出现的格式错误 其实就是换了个字符
#include<cstdio>
#include<Windows.h>
#include<cstdlib>
#include<ctime>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
bool xx[20][35],qz[20][35];
int mp[20][35],mode,zx,zy,lei,sy,color,nx=0,ny=0;
int mx[8]= {-1,0,1,1,1,0,-1,-1},my[8]= {-1,-1,-1,0,1,1,1,0};
HANDLE hOut;
struct jgt {
int x,y;
} ls;
queue<jgt>dl;
void ptwc(const char w[],int color){
SetConsoleTextAttribute(hOut,color);
printf("%s",w);
SetConsoleTextAttribute(hOut,7);
}
void print(bool a) { //打印
system("cls");
printf(" ");
for(int x=1; x<zx; x++)printf("-- ");
printf("--\n");
for(int y=0; y<zy; y++) {
printf("|");
for(int x=0; x<zx; x++) {
if(a&&mp[x][y]==10) {
ptwc("雷",4);
printf("|");
} else if(xx[x][y]) {
if(mp[x][y]) {
color=7;
switch(mp[x][y]) {
case 1:
color=9;break;
case 2:
color=2;break;
case 3:
color=12;break;
case 4:
color=1;break;
case 5:
color=12;break;
}
if(x==nx&&y==ny)color=color|96;
SetConsoleTextAttribute(hOut,color);
printf("%2d",mp[x][y]);
SetConsoleTextAttribute(hOut,7);
printf("|");
} else {
if(x==nx&&ny==y) {
ptwc(" ",96);
printf("|");
} else printf(" |");
}
} else {
color=7;
if(qz[x][y])color=4;
if(x==nx&&ny==y)color=color|96;
ptwc("><",color);
printf("|");
}
}
printf("\n ");
for(int i=0;i<zx;i++)printf("-- ");
printf("\n");
}
}
void bfs(int x,int y) { //广搜
ls.x=x;
ls.y=y;
dl.push(ls);
int kx,ky;
while(!dl.empty()) {
x=dl.front().x;
y=dl.front().y;
dl.pop();
for(int i=0; i<8; i++) {
kx=x+mx[i];
ky=y+my[i];
if(kx>=0&&kx<zx&&ky>=0&&ky<zy) {
if(!xx[kx][ky]) {
if(mp[kx][ky]!=10)
xx[kx][ky]=1,sy--;
if(mp[kx][ky]==0) {
ls.x=kx;ls.y=ky;
dl.push(ls);
}
}
}
}
}
}
void gxh() { //个性化函数
//system("color f0");
printf("游戏规则:你是一个");
ptwc("智障",4);
printf("你被捉去扫雷,快扫雷!\n");
}
void init() { //初始化
memset(xx,0,sizeof(xx));
memset(mp,0,sizeof(mp));
}
void choose_mode() { //选择难度
while(!zx) {
system("cls");
gxh();
printf("1.简单(9x9)\n");
printf("2.中级(16x16)\n");
printf("3.游戏规则\n");
scanf("%d",&mode);
switch(mode) {
case 1:
zx=9;zy=9;lei=10;sy=71;break;
case 2:
zx=16;zy=16;lei=40;sy=216;break;
case 3:
system("cls");
printf("\n很少有人不知道扫雷不会玩,而你就是其中之一.....\n\n");
printf("游戏目标:找出所有雷(我不觉得你能做到)\n\n");
printf("操作方式:橙色块是你所在位置,上下左右移动\n\n");
printf(" Enter点击,Shift插旗,其余跟原版一样\n\n");
printf("回车返回");getchar();getchar();
}
}
}
void put_boom() { //放置地雷
srand(time(0));
int x,y;
for(int i=0; i<lei;) {
x=rand()%zx;
y=rand()%zy;
if(mp[x][y]!=10) {
i++;
mp[x][y]=10;
for(int f=0; f<8; f++)
if(mp[x+mx[f]][y+my[f]]!=10)
mp[x+mx[f]][y+my[f]]++;
}
}
}
void start_game() { //游戏主体
while(GetKeyState(VK_RETURN)&0x8000);
print(0);
bool pr=0;
while(1) {
if(sy==0) {
print(1);
printf("什么,你把雷都找出来了!?\n");
Sleep(2000);
return;
}
if(GetKeyState(VK_LEFT)&0x8000)nx=max(0,nx-1),pr=1;
else if(GetKeyState(VK_RIGHT)&0x8000)nx=min(zx-1,nx+1),pr=1;
else if(GetKeyState(VK_UP)&0x8000)ny=max(0,ny-1),pr=1;
else if(GetKeyState(VK_DOWN)&0x8000)ny=min(zy-1,ny+1),pr=1;
else if(GetKeyState(VK_RETURN)&0x8000) {
if(!qz[nx][ny]) {
if(mp[nx][ny]==10) {
print(1);
ptwc("你失败了!!!垃圾/xyx\n",15);
Sleep(2000);
return;
} else {
sy--;
xx[nx][ny]=1;
if(mp[nx][ny]==0)bfs(nx,ny);
}
pr=1;
}
} else if(GetKeyState(VK_SHIFT)&0x8000) {
qz[nx][ny]=!qz[nx][ny];
if(qz[nx][ny])lei--;
else lei++;
pr=1;
}
if(pr) {
pr=0;
print(0);
printf("还有");
SetConsoleTextAttribute(hOut,4);
printf("%d",lei);
SetConsoleTextAttribute(hOut,7);
printf("个雷");
}
Sleep(50);
}
}
int main() {
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
init();
choose_mode();
put_boom();
start_game();
return 0;
}
//SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_GREEN);
//system("cls");