关于扫雷布雷和显示,我改不出错误了,看了好久。。来个大神帮忙看看~~
程序代码:
#include <stdio.h> #include<stdlib.h> #include<time.h> #define N 9 void main() { void bl(int **p,int m,int n); void fb(int **p,int m,int n); void xs(int **p,int m,int n); int n1,n2,i,j; int **mine; printf("输入地雷阵行数:"); scanf("%d",&n1); printf("输入地雷阵列数:"); scanf("%d",&n2); mine=(int **)malloc(sizeof(int *)*n1); for(i=0;i<n1;i++) mine[i]=(int *)malloc(sizeof(int *)*n2); for(i=0;i<n1;i++) for(j=0;j<n2;j++) mine[i][j]=0; bl(mine,n1,n2); fb(mine,n1,n2); xs(mine,n1,n2); } void bl(int **p,int m,int n) { int ms,i,j,k; printf("输入“地雷”数:"); scanf("%d",&ms); srand(time(NULL)); for(k=0;k<ms;k++) { i=rand()%m; j=rand()%n; if(p[i][j]!=0) { k--; continue; } p[i][j]=N; } } void fb(int **p,int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) if(p[i][j]==N) { if(i==0&&j==0) { p[0][1]++; p[1][0]++; } if(i==n-1&&j==n-1) { p[n-1][n-2]++; p[n-2][n-1]++; } if(i==n-1&&j==0) { p[n-1][1]++; p[n-2][0]++; } if(j==n-1&&i==0) { p[1][n-1]++; p[0][n-2]++; } if(i==0&&j!=0&&j!=n-1) { p[i][j-1]++; p[i][j+1]++; p[i+1][j-1]++; p[i+1][j]++; p[i+1][j+1]++; } if(i==n-1&&j!=n-1&&j!=0) { p[i-1][j-1]++; p[i-1][j]++; p[i-1][j+1]++; p[i][j-1]++; p[i][j+1]++; } if(i!=0&&i!=n-1&&j==0) { p[i-1][j]++; p[i-1][j+1]++; p[i][j+1]++; p[i+1][j]++; p[i+1][j+1]++; } if(i!=0&&i!=n-1&&j==n-1) { p[i-1][j-1]++; p[i-1][j]++; p[i][j-1]++; p[i+1][j-1]++; p[i+1][j]++; } if(i!=0&&i!=n-1&&j!=n-1&&j!=0) { p[i-1][j-1]++; p[i-1][j]++; p[i-1][j+1]++; p[i][j-1]++; p[i][j+1]++; p[i+1][j-1]++; p[i+1][j]++; p[i+1][j+1]++; } } } } void xs(int **p,int m,int n) { int i,j; for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(p[i][j]==N) printf("%3c",'*'); else if(p[i][j]!=0) printf("%3d",p[i][j]); if(p[i][j]==0) printf("%3d",0); } printf("\n"); } }程序有点长。。。因为中间的函数实在是想不出什么妙方法。。 拜托了!!