C++#include<bits/stdc++.h>
#include<windows.h>
#include<time.h>
#include<conio.h>
#include<unistd.h>
using namespace std;
int fx[8][2]= {{-1,0},{1,0},{0,1},{0,-1},{-1,1},{1,1},{-1,-1},{1,-1}},size1,a[105][105],num1;
int px,py,sum;
bool ifcheck=0,ifGame=0;
string C[20]= {"+","═","║","╔","╚","╗","╝","?","*"},b[105][105];
void color(int a) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);
}
void gotoxy(int x,int y) {
COORD position;
position.X=2*x;
position.Y=y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), position);
}
void gameover () {
system("cls");
gotoxy(0,0);
color(7);
printf("The Game is Over\n");
ifGame=1;
for(int i=0; i<=size1+1; i++) {
for(int j=0; j<=size1+1; j++) {
if(i>0&&i<size1+1&&j>0&&j<size1+1) {
b[i][j]=a[i][j]+'0';
if(b[i][j]=="/") {
color(4);
b[i][j]="*";
} else if(b[i][j]=="0")color(8);
else color(2);
} else color(7);
cout<<b[i][j]<<" ";
}
cout<<endl;
}
printf("感谢您的游玩\n制作者:TanYu\n");
exit;
}
void Win () {
system("cls");
gotoxy(0,0);
color(7);
printf("Your are Winner\n");
ifGame=1;
for(int i=0; i<=size1+1; i++) {
for(int j=0; j<=size1+1; j++) {
if(i>0&&i<size1+1&&j>0&&j<size1+1) {
b[i][j]=a[i][j]+'0';
if(b[i][j]=="/") {
color(4);
b[i][j]="*";
} else if(b[i][j]=="0")color(8);
else color(2);
} else color(7);
cout<<b[i][j]<<" ";
}
cout<<endl;
}
printf("感谢您的游玩\n制作者:TanYu\n");
exit;
}
void print() {
system("cls");
sum=0;
for(int i=0; i<=size1+1; i++) {
for(int j=0; j<=size1+1; j++) {
if(i>0&&i<size1+1&&j>0&&j<size1+1) {
if(b[i][j]=="+") {
color(3);
sum++;
} else if(b[i][j]=="?") {
color(4);
sum++;
} else if(b[i][j]=="0")color(8);
else color(2);
} else color(7);
if(b[i][j]=="/") {
gameover();
return;
}
cout<<b[i][j]<<" ";
}
cout<<endl;
}
gotoxy(px,py);
if(sum==num1) Win();
}
void reset() {
printf("请输入雷区大小(最大100):");
cin>>size1;
size1=max(1,size1);
size1=min(100,size1);
for (int i=1; i<=size1; i++) {
b[0][i]=C[1];
b[size1+1][i]=C[1];
b[i][0]=C[2];
b[i][size1+1]=C[2];
}
b[0][0]=C[3];
b[0][size1+1]=C[5];
b[size1+1][0]=C[4];
b[size1+1][size1+1]=C[6];
for(int i=1; i<=size1; i++)
for(int j=1; j<=size1; j++) {
b[i][j]=C[0];
}
}
void rand_() {
for(int i=1; i<=num1; i++) {
int x=rand()%size1+1,y=rand()%size1+1;
while(a[x][y]==-1||(py==x&&px==y)) {
x=rand()%size1+1;
y=rand()%size1+1;
}
a[x][y]=-1;
}
for(int i=1; i<=size1; i++)
for(int j=1; j<=size1; j++)
if(a[i][j]!=-1) {
for(int h=0; h<8; h++) {
if(a[i+fx[h][0]][j+fx[h][1]]==-1)a[i][j]++;
}
}
}
void check(int y,int x) {
if(!ifcheck) {
rand_();
ifcheck=1;
}
b[x][y]=a[x][y]+'0';
if(a[x][y]==0) {
for(int i=0; i<4; i++) {
if(b[x+fx[i][1]][y+fx[i][0]]=="+")check(y+fx[i][0],x+fx[i][1]);
}
}
}
void getch_() {
char c=getch();
if (c=='w') {
if (py>1) py--;
} else if (c=='s') {
if (py<size1) py++;
} else if (c=='a') {
if (px>1) px--;
} else if (c=='d') {
if (px<size1)px++;
} else if (c==' ') {
if(b[py][px]=="+") {
check(px,py);
print();
return;
}
} else {
if(b[py][px]=="?")b[py][px]="+";
else if(b[py][px]=="+")b[py][px]="?";
print();
return;
}
gotoxy(px,py);
}
main() {
CONSOLE_CURSOR_INFO cursor_info= {1,0};
srand((unsigned)time(NULL));
printf("规则:和扫雷一样,wasd移动,空格扫雷,除以上任意键标记\n\n");
reset();
px=py=size1/2;
printf("请输入雷的数量(最大不超过雷区大小的平方-1):");
cin>>num1;
num1=max(1,num1);
num1=min(size1*size1-1,num1);
system("cls");
print();
while(1) {
if(ifGame)return 0;
getch_();
}
}