c语言扫雷游戏源代码

“扫雷”游戏C代码

# include & ltstdio.h & gt

# include & ltmath.h & gt

# include & lttime.h & gt

# include & ltstdlib.h & gt

主( )

{char a[102][102],b[102][102],c[102][102],w;

int i,j;?/*循环变量*/

int x,y,z[999];?/*矿井的位置*/

int t,s;?/*标记*/

int m,n,雷;?/* Count */

int u,v;?/*回车*/

int行,列,格,莫;?/*自定义变量*/

srand((int)time(NULL));?/*启动随机数生成器*/

leb1:?/*选择模式*/

printf(" \ n请选择模式:\ n?1.标准?2.自定义\ n ");

scanf("%d ",& amp莫);

if(mo==2)?/*如果选择用户自定义模式,应输入三个参数*/

{ t = 0;Printf("请输入行数和列数";

scanf("%d%d%d ",& amp悬挂& amp谎言& ampge);

如果(hang & lt2){printf("行数太少\ n ");t = 1;}

if(hang & gt;100){printf("行数太多\ n ");t = 1;}

如果(lie & lt2){printf("列太少\ n ");t = 1;}

if(lie & gt;100){printf("列太多\ n ");t = 1;}

如果(ge & lt1){printf("至少一个雷霆\ n ");t = 1;}

如果(ge & gt=(hang*lie)){printf("太雷\ n ");t = 1;}

} while(t = = 1);

}

else{hang=10,lie=10,ge = 10;}?/*否则,选择标准模式(默认参数*/

for(I = 1;我& lt= gei=i+1)?/*找到地雷*/

{ t = 0;z[I]= rand()%(hang * lie);

for(j = 1;j & lt我;j = j+1){ if(z[I]= = z[j])t = 1;}

} while(t = = 1);

}

for(I = 0;我& lt= hang+1;i=i+1)?/*初始化a、b、c*/

{ for(j = 0;j & lt= lie+1;j = j+1){ a[I][j]= ' 1 ';b[I][j]= ' 1 ';c[I][j]= ' 0 ';} }

for(I = 1;我& lt=挂;i=i+1)

{ for(j = 1;j & lt=撒谎;j = j+1){ a[I][j]= '+';} }

for(I = 1;我& lt= gei=i+1)?/*把雷霆放在c*/

{ x = z[I]/lie+1;y = z[I]% lie+1;c[x][y]= ' # ';}

for(I = 1;我& lt=挂;i=i+1)?/*计算b中的数字*/

{ for(j = 1;j & lt=撒谎;j=j+1)

{ m = 48

if(c[I-1][j-1]= = ' # ')m = m+1;if(c[I][j-1]= = ' # ')m = m+1;

if(c[I-1][j]= = ' # ')m = m+1;?if(c[I+1][j+1]= = ' # ')m = m+1;

if(c[I][j+1]= = ' # ')m = m+1;?if(c[I+1][j]= = ' # ')m = m+1;

if(c[I+1][j-1]= = ' # ')m = m+1;if(c[I-1][j+1]= = ' # ')m = m+1;

b[I][j]= m;

}

}

for(I = 1;我& lt= gei=i+1)?/*把雷霆放在B */

{ x = z[I]/lie+1;y = z[I]% lie+1;b[x][y]= ' # ';}

雷=葛;?/*以下是游戏设计*/

{leb2:?/*输出*/

系统(“cls”);printf(" \ n \ n \ n \ n ");

printf(" ");

for(I = 1;我& lt=撒谎;i=i+1)

{ w =(I-1)/10+48;printf("%c ",w);

w =(I-1)% 10+48;printf("%c?,w);

}

printf("\n?|");

for(I = 1;我& lt=撒谎;I = I+1){ printf("-| ");}

printf(" \ n ");

for(I = 1;我& lt=挂;i=i+1)

{ w =(I-1)/10+48;printf("%c ",w);

w =(I-1)% 10+48;printf("%c | ",w);

for(j = 1;j & lt=撒谎;j=j+1)

{ if(a[I][j]= = ' 0 ')printf(" | ");

else printf(" %c | ",a[I][j]);

}

If(i==2)printf(“剩余地雷数”);

if(i==3)printf(" %d ",雷);

printf(" \ n | ");

for(j = 1;j & lt=撒谎;j = j+1){ printf("-| ");}

printf(" \ n ");

}

scanf("%d%c%d ",& ampu & amp;w & amp;五);?/*回车*/

u=u+1,v = v+1;

如果(w!= ' # ' & amp& ampa[u][v]=='@ ')

转到leb2

if(w=='# ')

{ if(a[u][v]= = '+'){ a[u][v]= ' @ ';雷=雷-1;}

else if(a[u][v]=='@'){a[u][v]= '?';雷=雷+1;}

else if(a[u][v]== '?'){ a[u][v]= '+';}

转到leb2

}

a[u][v]= b[u][v];

leb3:?/*开放区域0 */

t = 0;

if(a[u][v]=='0 ')

{ for(I = 1;我& lt=挂;i=i+1)

{ for(j = 1;j & lt=撒谎;j=j+1)

{ s = 0;

if(a[I-1][j-1]= = ' 0 ')s = 1;if(a[I-1][j+1]= = ' 0 ')s = 1;

if(a[I-1][j]= = ' 0 ')s = 1;?if(a[I+1][j-1]= = ' 0 ')s = 1;

if(a[I+1][j+1]= = ' 0 ')s = 1;if(a[I+1][j]= = ' 0 ')s = 1;

if(a[I][j-1]= = ' 0 ')s = 1;?if(a[I][j+1]= = ' 0 ')s = 1;

if(s = = 1)a[I][j]= b[I][j];

}

}

for(I = 1;我& lt=挂;i=i+1)

{ for(j = lie;j & gt=1;j=j-1)

{ s = 0;

if(a[I-1][j-1]= = ' 0 ')s = 1;if(a[I-1][j+1]= = ' 0 ')s = 1;

if(a[I-1][j]= = ' 0 ')s = 1;?if(a[I+1][j-1]= = ' 0 ')s = 1;

if(a[I+1][j+1]= = ' 0 ')s = 1;if(a[I+1][j]= = ' 0 ')s = 1;

if(a[I][j-1]= = ' 0 ')s = 1;if(a[I][j+1]= = ' 0 ')s = 1;

if(s = = 1)a[I][j]= b[I][j];

}

}

for(I = hang;我& gt=1;i=i-1)

{ for(j = 1;j & lt=撒谎;j=j+1)

{ s = 0;

if(a[I-1][j-1]= = ' 0 ')s = 1;if(a[I-1][j+1]= = ' 0 ')s = 1;

if(a[I-1][j]= = ' 0 ')s = 1;?if(a[I+1][j-1]= = ' 0 ')s = 1;

if(a[I+1][j+1]= = ' 0 ')s = 1;if(a[I+1][j]= = ' 0 ')s = 1;

if(a[I][j-1]= = ' 0 ')s = 1;?if(a[I][j+1]= = ' 0 ')s = 1;

if(s = = 1)a[I][j]= b[I][j];

}

}

for(I = hang;我& gt=1;i=i-1)

{ for(j = lie;j & gt=1;j=j-1)

{ s = 0;

if(a[I-1][j-1]= = ' 0 ')s = 1;if(a[I-1][j+1]= = ' 0 ')s = 1;

if(a[I-1][j]= = ' 0 ')s = 1;?if(a[I+1][j-1]= = ' 0 ')s = 1;

if(a[I+1][j+1]= = ' 0 ')s = 1;if(a[I+1][j]= = ' 0 ')s = 1;

if(a[I][j-1]= = ' 0 ')s = 1;?if(a[I][j+1]= = ' 0 ')s = 1;

if(s = = 1)a[I][j]= b[I][j];

}

}

for(I = 1;我& lt=挂;i=i+1)?/*检测区域0 */

{ for(j = 1;j & lt=撒谎;j=j+1)

{if(a[i][j]=='0 ')

{ if(a[I-1][j-1]= = '+' | | a[I-1][j-1]= = ' @ ' | | a[I-1][j-1]= = '?')t = 1;

if(a[I-1][j+1]= = '+' | | a[I-1][j+1]= = ' @ ' | | a[I-1][j+1]= = '?')t = 1;

if(a[I+1][j-1]= = '+' | | a[I+1][j-1]= = ' @ ' | | a[I+1][j-1]= = '?')t = 1;

if(a[I+1][j+1]= = '+' | | a[I+1][j+1]= = ' @ ' | | a[I+1][j+1]= = '?')t = 1;

if(a[I+1][j]= = '+' | | a[I+1][j]= = ' @ ' | | a[I+1][j]= = '?')t = 1;

if(a[I][j+1]= = '+' | | a[I][j+1]= = ' @ ' | | a[I][j+1]= = '?')t = 1;

if(a[I][j-1]= = '+' | | a[I][j-1]= = ' @ ' | | a[I][j-1]= = '?')t = 1;

if(a[I-1][j]= = '+' | | a[I-1][j]= = ' @ ' | | a[I-1][j]= = '?')t = 1;

}

}

}

if(t==1)转到leb3

}

n = 0;?/*检查结束*/

for(I = 1;我& lt=挂;i=i+1)

{ for(j = 1;j & lt=撒谎;j=j+1)

{if(a[i][j]!= '+' & amp;& ampa[i][j]!= ' @ ' & amp& ampa[i][j]!='?')n = n+1;}

}

}

while(a[u][v]!= ' # ' & amp& ampn!=(杭*列-格));

for(I = 1;我& lt= gei=i+1)?/*游戏结束*/

{ x = z[I]/lie+1;y = z[I]% lie+1;a[x][y]= ' # ';}

printf(" ");

for(I = 1;我& lt=撒谎;i=i+1)

{ w =(I-1)/10+48;printf("%c ",w);

w =(I-1)% 10+48;printf("%c?,w);

}

printf("\n?|");

for(I = 1;我& lt=撒谎;I = I+1){ printf("-| ");}

printf(" \ n ");

for(I = 1;我& lt=挂;i=i+1)

{ w =(I-1)/10+48;printf("%c ",w);

w =(I-1)% 10+48;printf("%c | ",w);

for(j = 1;j & lt=撒谎;j=j+1)

{ if(a[I][j]= = ' 0 ')printf(" | ");

不然呢?printf(" %c | ",a[I][j]);

}

If(i==2)printf(“剩余地雷数”);

if(i==3)printf(" %d ",雷);printf(" \ n | ");

for(j = 1;j & lt=撒谎;j = j+1){ printf("-| ");}

printf(" \ n ");

}

If(n==(hang*lie-ge)) printf("你成功了!\ n ");

Else printf("游戏结束!\ n ");

Printf("请输入1\n "进行重播);

t = 0;

scanf("%d ",& ampt);

if(t==1)转到leb 1;

}

/*注意:在DEV c++上运行pass。行号和列号都从0开始。例如,要确定第0行和第9列不是“雷”,只需在0和9之间添加一个字母。您可以输入三个字符0a9,然后按回车键。如果3行7列不是迅雷,输入3a7,回车;第8行第5列是雷声,输入8#5并回车,第9行第0列是雷声,输入9#0并回车*/