c语言模拟扫雷小游戏

论坛 期权论坛 编程之家     
选择匿名的用户   2021-6-2 20:56   3164   0

扫雷游戏的原理很简单,我用c语言简单做了一个模拟程序,在获取雷数的时候可能比较繁琐,还有没有简单明了写法,求高手指导。代码附上:


#include <stdio.h>
#include <windows.h>
#define M 10
#define N 10
#define MAX (M*N)
#define MINE  "▲"
#define MAXVALUE (~(unsigned)0)

void swap(unsigned *pa,unsigned *pb);//交换数据
void printmine(unsigned (*arr)[N]);//打印扫雷数据
unsigned getflag(unsigned num);//判断该位置是否有雷
void setminecount(unsigned (*arr)[N],unsigned num);//设置雷数
void messnum(unsigned (*arr)[N]);//洗牌
void initmine(unsigned (*arr)[N]);//初始化扫雷数据
void getminecount(unsigned (*arr)[N],unsigned i,unsigned j);//获取该位置的雷数

void swap(unsigned *pa,unsigned *pb)
{
 unsigned temp = 0;
 temp = *pa;
 *pa = *pb;
 *pb = temp;
}
unsigned getflag(unsigned num)
{
 return (num == MAXVALUE)?1:0;
}

void setminecount(unsigned (*arr)[N],unsigned num)
{
 unsigned i = 0,j = 0,count = 0;
 for(i = 0;i < M;i ++)
 {
  for(j = 0;j < N;j ++)
  {
   arr[i][j] = MAXVALUE;
   count ++;
   if (count == num)
   {
    return;
   }
  }
 }
}

void messnum(unsigned (*arr)[N])
{
 unsigned i = 0,j = 0,x = 0,y = 0;
 srand((unsigned)time(NULL)); 
 for (i = 0;i < M;i++)
 {
  for(j = 0;j < N;j ++)
  {
   x = rand()%M;
   y = rand()%N;
   swap(&arr[x][y],&arr[i][j]);
  }
 }
}

void getminecount(unsigned (*arr)[N],unsigned i,unsigned j)
{
 if (arr[i][j] == MAXVALUE)
 {
  return;
 }
 else
 {
  if (i <= 0)
  {
   if (j <= 0)
   {
    arr[i][j] = getflag(arr[i][j+1]) + getflag(arr[i+1][j]) + getflag(arr[i+1][j+1]);
   }
   else if (j > 0 && j < N-1)
   {
    arr[i][j] = getflag(arr[i][j+1]) + getflag(arr[i][j-1]) + getflag(arr[i+1][j]) +  
     getflag(arr[i+1][j+1]) + getflag(arr[i+1][j-1]);
   }
   else
   {
    arr[i][j] = getflag(arr[i][j-1]) + getflag(arr[i+1][j]) + getflag(arr[i+1][j-1]);
   }
  }
  else if (i > 0 && i < M-1)
  {
   if (j <= 0)
   {
    arr[i][j] = getflag(arr[i][j+1]) + getflag(arr[i+1][j]) + getflag(arr[i-1][j]) + 
     getflag(arr[i+1][j+1]) + getflag(arr[i-1][j+1]);
   }
   else if (j > 0 && j < N-1)
   {
    arr[i][j] = getflag(arr[i][j+1]) + getflag(arr[i][j-1]) + getflag(arr[i+1][j]) + getflag(arr[i-1][j]) + 
     getflag(arr[i+1][j+1]) + getflag(arr[i-1][j-1]) + getflag(arr[i+1][j-1]) + getflag(arr[i-1][j+1]);
   }
   else 
   {
    arr[i][j] = getflag(arr[i][j-1]) + getflag(arr[i+1][j]) + getflag(arr[i-1][j]) + 
     getflag(arr[i-1][j-1]) + getflag(arr[i+1][j-1]);
   }
  }
  else
  {
   if (j <= 0)
   {
    arr[i][j] = getflag(arr[i][j+1]) + getflag(arr[i-1][j]) + getflag(arr[i-1][j+1]);
   }
   else if (j > 0 && j < N-1)
   {
    arr[i][j] = getflag(arr[i][j+1]) + getflag(arr[i][j-1]) + getflag(arr[i-1][j-1]) + 
     getflag(arr[i-1][j]) + getflag(arr[i-1][j+1]);
   }
   else
   {
    arr[i][j] = getflag(arr[i][j-1]) + getflag(arr[i-1][j]) + getflag(arr[i-1][j-1]);
   }
  }
 }
}
void initmine(unsigned (*arr)[N])
{
 unsigned i = 0,j = 0;
 for (i = 0;i < M;i++)
 {
  for(j = 0;j < N;j ++)
  {
   getminecount(arr,i,j);
  }
 }
}
void printmine(unsigned (*arr)[N])
{
 unsigned i = 0,j = 0;
 for (i = 0;i < M;i++)
 {
  for(j = 0;j < N;j ++)
  {
   if (arr[i][j] == MAXVALUE)
   {
    printf(" %s ",MINE);
   }
   else
   {
    printf(" %2u ",arr[i][j]);
   }
  }
  printf("\n\n");
 } 
}
int main(void)
{
 unsigned arr[M][N] = {0};
 unsigned i = 0,j = 0;
 unsigned temp = 0;
 memset(arr,0,sizeof(arr));
 system("color 70");
 setminecount(arr,20); //设置雷数
 messnum(arr);    //洗牌
 initmine(arr);     //初始化扫雷
 printmine(arr);    //打印扫雷
 getch();
 return 0;
}



运行结果:


分享到 :
0 人收藏
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

积分:3875789
帖子:775174
精华:0
期权论坛 期权论坛
发布
内容

下载期权论坛手机APP