代码:
#include<bits/stdc++.h>
#include<windows.h>
#include<conio.h>
using namespace std;
int x,y,l,l2;
bool s,c[12][12],flag;
char a[12][12],b[12][12],f;
int statistics_1(int j,int i);
int statistics_2(int j,int i);
void around(int j,int i);
void csh();
void color(int r);
void gotoxy(int xx,int yy);
void _cout();
void _cin();
void _yiwei();
void _cout2();
int main(){
csh();
while(true){
_cout();
if(s){
color(4);
printf("Game over!\n");
break;
}
if(l==0&&l==l2){
color(6);
printf("Win!\n");
break;
}
_cin();
_yiwei();
}
color(7);
system("pause");
_cout2();
gotoxy(12,0);
color(7);
system("pause");
return 0;
}
int statistics_1(int j,int i){
return ((b[i+1][j]=='*')+(b[i-1][j]=='*')+(b[i][j+1]=='*')+(b[i][j-1]=='*')+(b[i+1][j+1]=='*')+(b[i+1][j-1]=='*')+(b[i-1][j+1]=='*')+(b[i-1][j-1]=='*'));
}
int statistics_2(int j,int i){
return ((a[i+1][j]=='+')+(a[i-1][j]=='+')+(a[i][j+1]=='+')+(a[i][j-1]=='+')+(a[i+1][j+1]=='+')+(a[i+1][j-1]=='+')+(a[i-1][j+1]=='+')+(a[i-1][j-1]=='+'));
}
void around(int j,int i){
if(b[i+1][j]!='*'&&c[i+1][j]&&statistics_1(j,i+1)<=statistics_2(j,i+1)&&i<10) c[i+1][j]=false,around(j,i+1);
if(b[i-1][j]!='*'&&c[i-1][j]&&statistics_1(j,i-1)<=statistics_2(j,i-1)&&i>1) c[i-1][j]=false,around(j,i-1);
if(b[i][j+1]!='*'&&c[i][j+1]&&statistics_1(j+1,i)<=statistics_2(j+1,i)&&j<10) c[i][j+1]=false,around(j+1,i);
if(b[i][j-1]!='*'&&c[i][j-1]&&statistics_1(j-1,i)<=statistics_2(j-1,i)&&j>1) c[i][j-1]=false,around(j-1,i);
if(b[i+1][j+1]!='*'&&c[i+1][j+1]&&statistics_1(j+1,i+1)<=statistics_2(j+1,i+1)&&i<10&&j<10) c[i+1][j+1]=false,around(j+1,i+1);
if(b[i-1][j-1]!='*'&&c[i-1][j-1]&&statistics_1(j-1,i-1)<=statistics_2(j-1,i-1)&&i>1&&j>1) c[i-1][j-1]=false,around(j-1,i-1);
if(b[i-1][j+1]!='*'&&c[i-1][j+1]&&statistics_1(j+1,i-1)<=statistics_2(j+1,i-1)&&i>1&&j<10) c[i-1][j+1]=false,around(j+1,i-1);
if(b[i+1][j-1]!='*'&&c[i+1][j-1]&&statistics_1(j-1,i+1)<=statistics_2(j-1,i+1)&&i<10&&j>1) c[i+1][j-1]=false,around(j-1,i+1);
if(a[i+1][j]!='+') a[i+1][j]=b[i+1][j],c[i+1][j]=false;
if(a[i-1][j]!='+') a[i-1][j]=b[i-1][j],c[i-1][j]=false;
if(a[i][j+1]!='+') a[i][j+1]=b[i][j+1],c[i][j+1]=false;
if(a[i][j-1]!='+') a[i][j-1]=b[i][j-1],c[i][j-1]=false;
if(a[i+1][j+1]!='+') a[i+1][j+1]=b[i+1][j+1],c[i+1][j+1]=false;
if(a[i-1][j-1]!='+') a[i-1][j-1]=b[i-1][j-1],c[i-1][j-1]=false;
if(a[i-1][j+1]!='+') a[i-1][j+1]=b[i-1][j+1],c[i-1][j+1]=false;
if(a[i+1][j-1]!='+') a[i+1][j-1]=b[i+1][j-1],c[i+1][j-1]=false;
return;
}
void csh(){
l=15;l2=15;s=false;
CONSOLE_CURSOR_INFO cursor_info={1,0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE),&cursor_info);
for(int i=0;i<=11;i++)
for(int j=0;j<=11;j++)
a[i][j]='#',b[i][j]='0',c[i][j]=true;
srand(time(NULL));
for(int i=0;i<l;){
x=rand()%10+1;y=rand()%10+1;
if(b[y][x]!='*') i++,b[y][x]='*';
}
for(int i=1;i<=10;i++)
for(int j=1;j<=10;j++)
if(b[i][j]!='*')
b[i][j]+=(b[i+1][j]=='*')+(b[i-1][j]=='*')+(b[i][j+1]=='*')+(b[i][j-1]=='*')+(b[i+1][j+1]=='*')+(b[i+1][j-1]=='*')+(b[i-1][j+1]=='*')+(b[i-1][j-1]=='*');
x=5,y=5;
gotoxy(1,22);
printf("w,a,s,d控制坐标移动");
gotoxy(3,22);
printf(" 1键插旗,2键拔旗");
gotoxy(5,22);
printf(" 按空格键有惊喜!");
return;
}
void gotoxy(int i,int j){
COORD position={j,i};
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),position);
return;
}
void color(int r){
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),r);
return;
}
void _cout(){
gotoxy(0,0);
for(int i=1;i<=10;i++){
for(int j=1;j<=10;j++){
if(i==y&&j==x){color(7);printf("×");}
else switch(a[i][j]){
case '0':color(7);printf("□");break;
case '#':color(1);printf("■");break;
case '+':color(4);printf("▲");break;
case '*':color(4);printf("*");s=true;break;
case '1':color(1);printf("1");break;
case '2':color(2);printf("2");break;
case '3':color(4);printf("3");break;
case '4':color(1);printf("4");break;
case '5':color(4);printf("5");break;
case '6':color(7);printf("6");break;
case '7':color(7);printf("7");break;
case '8':color(7);printf("8");break;
}
}
printf("\n");
}
color(4);
printf(" *:%d \n",l2);
return;
}
void _cin(){
f=getch();
switch(f){
case 'W':case 'w':if(y>1) y--;break;
case 'S':case 's':if(y<10) y++;break;
case 'A':case 'a':if(x>1) x--;break;
case 'D':case 'd':if(x<10) x++;break;
case '1':if(b[y][x]=='*'&&c[y][x])l--;
if(c[y][x]){
l2--;a[y][x]='+';
}
c[y][x]=false;
break;
case '2':if(a[y][x]=='+'){
c[y][x]=true;
a[y][x]='#';
l2++;
if(b[y][x]=='*')
l++;
}
break;
case ' ':if(a[y][x]!='+'){
a[y][x]=b[y][x];c[y][x]=false;
if(b[y][x]=='*') s=true;
if(statistics_1(x,y)<=statistics_2(x,y)&&b[y][x]!='*') around(x,y);
}
break;
default:break;
}
return;
}
void _yiwei(){
if(f==' '||f=='1'){
flag=false;
for(int i=1;i<10;i+=2){
for(int j=1;j<=i;j++){
if(y>1) y--;
if(c[y][x]){
flag=true;break;
}
}
if(flag) break;
for(int j=1;j<=i;j++){
if(x<10) x++;
if(c[y][x]){
flag=true;break;
}
}
if(flag) break;
for(int j=1;j<=i+1;j++){
if(y<10) y++;
if(c[y][x]){
flag=true;break;
}
}
if(flag) break;
for(int j=1;j<=i+1;j++){
if(x>1) x--;
if(c[y][x]){
flag=true;break;
}
}
if(flag) break;
}
}
return;
}
void _cout2(){
gotoxy(0,0);
for(int i=1;i<=10;i++){
for(int j=1;j<=10;j++){
switch(b[i][j]){
case '0':color(7);printf("□");break;
case '#':color(1);printf("■");break;
case '+':color(4);printf("▲");break;
case '*':color(4);printf("*");break;
case '1':color(1);printf("1");break;
case '2':color(2);printf("2");break;
case '3':color(4);printf("3");break;
case '4':color(1);printf("4");break;
case '5':color(4);printf("5");break;
case '6':color(7);printf("6");break;
case '7':color(7);printf("7");break;
case '8':color(7);printf("8");break;
}
}
printf("\n");
}
return;
}