游戏系列~扫雷
Posted yszhyhm
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了游戏系列~扫雷相关的知识,希望对你有一定的参考价值。
#include <cstdio>
#include <cmath>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <iomanip>
#include <cstring>
#include <ctime>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <stack>
#include <windows.h>
#include <conio.h>
#define H 100
#define L 100
using namespace std;
char a[H][L],e=275;
int s[H][L];
int n,m,flag,kkk=259,b=260,c=261,d=262;
unsigned START;
void SetColor(unsigned short ForeColor,unsigned short BackGroundColor) {
HANDLE hCon=GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleTextAttribute(hCon,ForeColor|BackGroundColor);
}
void HideCursor() {
CONSOLE_CURSOR_INFO cursor_info = {1, 0};
SetConsoleCursorInfo(GetStdHandle(STD_OUTPUT_HANDLE), &cursor_info);
}
void Initial();
inline int g(int i,int j) {
return (a[i][j-1]==e)
+(a[i][j+1]==e)
+(a[i+1][j]==e)
+(a[i-1][j]==e)
+(a[i-1][j-1]==e)
+(a[i-1][j+1]==e)
+(a[i+1][j-1]==e)
+(a[i+1][j+1]==e);
}
inline int h(int i,int j) {
return (a[i][j+1]==e)
+(a[i+1][j]==e)
+(a[i-1][j]==e)
+(a[i+1][j+1]==e)
+(a[i-1][j+1]==e);
}
inline int z(int i,int j) {
return (a[i][j-1]==e)
+(a[i][j+1]==e)
+(a[i+1][j]==e)
+(a[i+1][j-1]==e)
+(a[i+1][j+1]==e);
}
void csh() {
srand(time(0));
int c;
n=m=c=0;
scanf ("%d",&flag);
if (flag==1)n=m=10;
else if (flag==2)n=m=16;
else if (flag==3)n=16,m=30;
else if (flag==4) {
printf ("请输入行数(<100)\n");
cin>>n;
printf ("请输入列数(<100)\n");
cin>>m;
}
if (flag==1)c=10;
else if (flag==2)c=40;
else if (flag==3)c=99;
else if (flag==4) {
printf ("请输入雷数(<%d)\n",n*m/4);
cin>>c;
}
memset(a,‘0‘,sizeof(a));
memset(s,0,sizeof(s));
while (c--) {
int x=rand()%n,y=rand()%m;
while (a[x][y]==e)x=rand()%n,y=rand()%m;
a[x][y]=e;
}
for (int x=0; x<n; x++)
for (int y=0; y<n; y++)
if (x>0&&y>0) {
if (a[x][y]!=e)a[x][y]=g(x,y)+‘0‘;
} else if (x>0&&!y) {
if (a[x][y]!=e)a[x][y]=h(x,y)+‘0‘;
} else if (!x&&y>0) {
if (a[x][y]!=e)a[x][y]=z(x,y)+‘0‘;
} else if (!x&&!y) {
if (a[x][y]!=e)a[x][y]=(a[x][y+1]==e)
+(a[x+1][y]==e)
+(a[x+1][y+1]==e)+‘0‘;
}
}
inline void f1(int jkjk,int kjkj) {
s[jkjk][kjkj]=a[jkjk][kjkj]==‘0‘?-1:1;
s[jkjk][kjkj-1]=a[jkjk][kjkj-1]==‘0‘?-1:1;
s[jkjk][kjkj+1]=a[jkjk][kjkj+1]==‘0‘?-1:1;
s[jkjk-1][kjkj-1]=a[jkjk-1][kjkj-1]==‘0‘?-1:1;
s[jkjk-1][kjkj+1]=a[jkjk-1][kjkj+1]==‘0‘?-1:1;
s[jkjk-1][kjkj]=a[jkjk-1][kjkj]==‘0‘?-1:1;
s[jkjk+1][kjkj-1]=a[jkjk+1][kjkj-1]==‘0‘?-1:1;
s[jkjk+1][kjkj+1]=a[jkjk+1][kjkj+1]==‘0‘?-1:1;
s[jkjk+1][kjkj]=a[jkjk+1][kjkj]==‘0‘?-1:1;
}
inline void f2(int jkjk,int kjkj) {
s[jkjk][kjkj]=a[jkjk][kjkj]==‘0‘?-1:1;
s[jkjk][kjkj+1]=a[jkjk][kjkj+1]==‘0‘?-1:1;
s[jkjk-1][kjkj+1]=a[jkjk-1][kjkj+1]==‘0‘?-1:1;
s[jkjk-1][kjkj]=a[jkjk-1][kjkj]==‘0‘?-1:1;
s[jkjk+1][kjkj+1]=a[jkjk+1][kjkj+1]==‘0‘?-1:1;
s[jkjk+1][kjkj]=a[jkjk+1][kjkj]==‘0‘?-1:1;
}
inline void f3(int jkjk,int kjkj) {
s[jkjk][kjkj]=a[jkjk][kjkj]==‘0‘?-1:1;
s[jkjk][kjkj-1]=a[jkjk][kjkj-1]==‘0‘?-1:1;
s[jkjk][kjkj+1]=a[jkjk][kjkj+1]==‘0‘?-1:1;
s[jkjk+1][kjkj-1]=a[jkjk+1][kjkj-1]==‘0‘?-1:1;
s[jkjk+1][kjkj+1]=a[jkjk+1][kjkj+1]==‘0‘?-1:1;
s[jkjk+1][kjkj]=a[jkjk+1][kjkj]==‘0‘?-1:1;
}
inline void f4(int jkjk,int kjkj) {
s[jkjk][kjkj]=a[jkjk][kjkj]==‘0‘?-1:1;
s[jkjk+1][kjkj]=a[jkjk+1][kjkj]==‘0‘?-1:1;
s[jkjk+1][kjkj+1]=a[jkjk+1][kjkj+1]==‘0‘?-1:1;
s[jkjk][kjkj+1]=a[jkjk][kjkj+1]==‘0‘?-1:1;
}
inline void bs(char jx) {
if (jx==‘0‘)SetColor(0,0);
if (jx==‘1‘)SetColor(1,1);
if (jx==‘2‘)SetColor(2,2);
if (jx==‘3‘)SetColor(5,5);
if (jx==‘4‘)SetColor(3,3);
if (jx==‘5‘)SetColor(4,4);
if (jx==‘6‘)SetColor(3,3);
if (jx==‘7‘)SetColor(6,6);
if (jx==‘8‘)SetColor(4,4);
}
void js() {
SetColor (7,7);
printf ("\n ");
for (int jjjj=1; jjjj<=m; jjjj++)printf ("%02d",jjjj);
printf ("\n ");
printf("╔");
for (int jjjj=1; jjjj<=m; jjjj++)printf ("═");
puts("╗");
for (int jjjj=0; jjjj<n; jjjj++) {
SetColor(7,7);
printf ("%02d║",jjjj+1);
bs(a[jjjj][0]);
printf ("%c",a[jjjj][0]);
for (int kkkk=1; kkkk<m; kkkk++) {
SetColor(7,7);
printf ("|");
bs(a[jjjj][kkkk]);
printf ("%c",a[jjjj][kkkk]);
}
SetColor(7,7);
printf (" ║");
printf ("%02d",jjjj+1);
printf ("\n");
}
printf (" ╚");
for (int kkkk=0; kkkk<m; kkkk++)printf ("═");
puts("╝");
printf (" ");
for (int kkkk=0; kkkk<m; kkkk++)printf ("%02d",kkkk+1);
printf ("\n");
system("pause");
}
void sx() {
system("cls");
void HideCursor();
SetColor(6,6);
printf ("欢迎玩扫雷!\n%c %c %c %c\n1:10*10(10个雷)\n2:16*16(40个雷)\n3:16*30(99个雷)\n4:自定义\n",kkk,b,c,d);
printf ("游戏玩法:\n"
"输入两个数,\n"
"第一个表示行数,\n"
"第二个表示列数,\n"
"第三个表示是把它标记为雷还是翻开,\n"
"1表示翻开,2表示视为雷\n"
"温馨提示:\n"
"本游戏中,\n"
"%c 表示在地图中这里是雷\n"
"%c 表示在地图中这里未翻开\n"
"* 表示在地图中这里被标记为雷\n"
"引领天下(U39863)原创\n",e,258
);
printf ("\n ");
SetColor(7,7);
bool isok=true,okis=true;
for (int nm=0; nm<n; nm++)
for (int mn=0; mn<m; mn++)
if (a[nm][mn]!=e&&!s[nm][mn]&1)isok=false;
else if (a[nm][mn]==e&&s[nm][mn]!=2)okis=false;
if (isok||okis) {
system("cls");
SetColor (6,6);
printf ("你赢了!恭喜你!\n");
printf ("用时:%d毫秒\n",clock()-START);
js();
return;
}
for (int jjjj=1; jjjj<=m; jjjj++)printf ("%02d",jjjj);
printf ("\n ");
printf("╔");
for (int jjjj=1; jjjj<=m; jjjj++)printf ("═");
puts("╗");
for (int jjjj=0; jjjj<n; jjjj++) {
printf ("%02d║",jjjj+1);
if (s[jjjj][0]==1||s[jjjj][0]==-1) {
bs(a[jjjj][0]);
printf ("%c",a[jjjj][0]);
} else if (s[jjjj][0]==2) {
SetColor(7,7);
printf ("*");
} else if (!s[jjjj][0]) {
SetColor(7,7);
printf ("%c",258);
}
for (int kkkk=1; kkkk<m; kkkk++) {
if (s[jjjj][kkkk]==1||s[jjjj][kkkk]==-1) {
SetColor(7,7);
printf ("|");
bs(a[jjjj][kkkk]);
printf ("%c",a[jjjj][kkkk]);
} else if (s[jjjj][kkkk]==2) {
SetColor(7,7);
printf ("|*");
} else if (s[jjjj][kkkk]==0) {
SetColor(7,7);
printf ("|%c",258);
}
}
SetColor(7,7);
printf (" ║");
printf ("%02d",jjjj+1);
printf ("\n");
}
printf (" ╚");
for (int kkkk=0; kkkk<m; kkkk++)printf ("═");
puts("╝");
printf (" ");
for (int kkkk=0; kkkk<m; kkkk++)printf ("%02d",kkkk+1);
printf ("\n");
int i,j,fu;
cin>>i>>j>>fu;
s[--i][--j]=fu;
if (a[i][j]==‘0‘&&s[i][j]==1) {
if (i>0&&j>0)f1(i,j);
else if (i>0&&!j)f2(i,j);
else if (!i&&j>0)f3(i,j);
else if (!i&&!j)f4(i,j);
for (int jkjk=0; jkjk<n; jkjk++)
for (int kjkj=0; kjkj<m; kjkj++)
if (s[jkjk][kjkj]<0) {
if (jkjk>0&&kjkj>0)f1(jkjk,kjkj);
else if (jkjk>0&&!kjkj)f2(jkjk,kjkj);
else if (!jkjk&&kjkj>0)f3(jkjk,kjkj);
else if (!jkjk&&!kjkj)f4(jkjk,kjkj);
}
for (int jkjk=n-1; jkjk>=0; jkjk--)
for (int kjkj=m-1; kjkj>=0; kjkj--)
if (s[jkjk][kjkj]<0) {
if (jkjk>0&&kjkj>0)f1(jkjk,kjkj);
else if (jkjk>0&&!kjkj)f2(jkjk,kjkj);
else if (!jkjk&&kjkj>0)f3(jkjk,kjkj);
else if (!jkjk&&!kjkj)f4(jkjk,kjkj);
}
}
if (a[i][j]==e&&s[i][j]==1) {
system("cls");
SetColor (6,6);
printf ("你输了\n");
printf("用时:%d毫秒\n",clock()-START);
js();
return;
}
isok=true,okis=true;
for (int nm=0; nm<n; nm++)
for (int mn=0; mn<m; mn++)
if (a[nm][mn]!=e&&!s[nm][mn]&1)isok=false;
else if (a[nm][mn]==e&&s[nm][mn]!=2)okis=false;
if (isok||okis) {
system("cls");
SetColor (6,6);
printf ("你赢了!恭喜你!\n");
printf ("用时:%d毫秒\n",clock()-START);
js();
return;
}
sx();
}
void Initial() {
printf("\n\n\n\n\n\t\t按回车键开始程序");
getchar();
system("cls");
int iiii;
printf("\n\n\n\n\n\t\t");
printf("●玩命加载中〉");
for(iiii=0; iiii<=100; iiii++) {
SetColor(iiii%7+8,0);
printf("%3d",iiii);
SetColor(15,0);
printf(" %%");
SetColor(iiii%7+8,0);
printf(" Written by Ekko");
Sleep(10);
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
}
for(iiii=0; iiii<=20; iiii++) {
SetColor(iiii%7+8,0);
printf("%3d",iiii*5);
SetColor(15,0);
printf(" %%");
SetColor(iiii%7+8,0);
printf(" 正在读取作者QQ......");
Sleep(50);
printf("\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b\b");
}
Sleep(100);
printf("\n\n\n\n\n\t\t");
cout<<"qq:2326439151";
Sleep(200);
system("cls");
system("title 扫雷");
printf ("欢迎玩扫雷!\n%c %c %c %c\n1:10*10(10个雷)\n2:16*16(40个雷)\n3:16*30(99个雷)\n4:自定义\n",kkk,b,c,d);
csh();
printf ("地图初始化完成!\n");
START=clock();
sx();
}
int main() {
/*freopen(".in","r",stdin);
freopen(".out","w",stdout);*/
START=clock();
Initial();
return 0;
}
以上是关于游戏系列~扫雷的主要内容,如果未能解决你的问题,请参考以下文章