C语言实现简易扫雷小游戏

所属分类: 软件编程 / C 语言 阅读数: 92
收藏 0 赞 0 分享

我们经常在电脑上面玩的扫雷游戏,很考验我们的判断能力,但是实现一个扫雷游戏并不是很困难,只要多注意一些细节就好,就可以将一个简单的扫雷游戏写出来!

接下来先介绍扫雷游戏要实现的功能:

首先,要对雷阵进行初始化,在初始化的时候要注意要定义两个数组,一个是让我们扫雷的阵,另外一个就是显示某一个地方的周围的雷的总个数的矩阵,在初始化的时候要注意为了避免传址的问题,我们把它写在主函数里面。

char mine[rows][cols];
char show[rows][cols];
 int i = 0;
 int j = 0;
 for (i = 0; i < rows - 1; i++)
 {
 for (j = 0; j < cols - 1; j++)
 {
  mine[i][j] = '0';
  show[i][j] = '*';
 }
 }

接下来就是电脑在随机布局雷阵的函数,这个函数要用到rand()&nbsp;函数,来产生随机值,在雷阵里面随机布雷。

void set_mine(char mine[rows][cols])
{
 int count = Count;
 int x = 0;
 int y = 0;
 srand((unsigned)time(NULL));
 while (count)
 {
 x = rand() % 9 + 1;
 y = rand() % 9 + 1;
 if (mine[x][y] == '0')
 {
  mine[x][y] = '1';
  count--;
 }
 }
}

再有就是计算雷的个数的函数,要讲某一个坐标位置的周围8个位置的雷的个数算出来,并且将个数显示出来

int get_num(char mine[rows][cols], int x, int y)
{
 int count = 0;
 if (mine[x - 1][y - 1] == '1')//左上方
 {
 count++;
 }
 if (mine[x - 1][y] == '1')//左边
 {
 count++;
 }
 if (mine[x - 1][y + 1] == '1')//左下方
 {
 count++;
 }
 if (mine[x][y - 1] == '1')//上方
 {
 count++;
 }
 if (mine[x][y + 1] == '1')//下方
 {
 count++;
 }
 if (mine[x + 1][y - 1] == '1')//右上方
 {
 count++;
 }
 if (mine[x + 1][y] == '1')//右方
 {
 count++;
 }
 if (mine[x + 1][y + 1] == '1')//右下方
 {
 count++;
 }
 return count;
}

将扫雷函数的各个函数都实现了之后,我们来看一下完整的代码

头文件game.h 

#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>
#define rows 11
#define cols 11
#define Count 10
 
int menu();//菜单函数
void display(char show[rows][cols]);
int Game(char mine[rows][cols],char show[rows][cols]);//游戏
void set_mine(char mine[rows][cols]);//设置雷的位置
int Sweep(char mine[rows][cols], char show[rows][cols]);//开始扫雷
int get_num(char mine[rows][cols], int x, int y);//计算雷的个数

实现函数 game.c

#include"game.h"
 
//菜单函数
int menu()
{
 printf("********************************************\n");
 printf("********************************************\n");
 printf("*************welcome to saolei*************\n");
 printf("*************  1.   play  *************\n");
 printf("*************  0.   exit  *************\n");
 printf("********************************************\n");
 printf("********************************************\n");
 return 0;
}
 
 
//设置雷的位置
void set_mine(char mine[rows][cols])
{
 int count = Count;
 int x = 0;
 int y = 0;
 srand((unsigned)time(NULL));
 while (count)
 {
 x = rand() % 9 + 1;
 y = rand() % 9 + 1;
 if (mine[x][y] == '0')
 {
  mine[x][y] = '1';
  count--;
 }
 }
}
 
//打印下棋完了显示的界面
void display(char show[rows][cols]) 
{
 int i = 0;
 int j = 0;
 printf(" ");
 for (i = 1; i < cols - 1; i++)
 {
 printf(" %d ", i);
 }
 printf("\n");
 for (i = 1; i < rows - 1; i++)
 {
 printf("%d", i);
 for (j = 1; j < cols - 1; j++)
 {
  printf(" %c ", show[i][j]);
 }
 printf("\n");
 }
}
 
//计算雷的个数
int get_num(char mine[rows][cols], int x, int y)
{
 int count = 0;
 if (mine[x - 1][y - 1] == '1')//左上方
 {
 count++;
 }
 if (mine[x - 1][y] == '1')//左边
 {
 count++;
 }
 if (mine[x - 1][y + 1] == '1')//左下方
 {
 count++;
 }
 if (mine[x][y - 1] == '1')//上方
 {
 count++;
 }
 if (mine[x][y + 1] == '1')//下方
 {
 count++;
 }
 if (mine[x + 1][y - 1] == '1')//右上方
 {
 count++;
 }
 if (mine[x + 1][y] == '1')//右方
 {
 count++;
 }
 if (mine[x + 1][y + 1] == '1')//右下方
 {
 count++;
 }
 return count;
}
//扫雷
int Sweep(char mine[rows][cols], char show[rows][cols])
{
 int count = 0;
 int x = 0;
 int y = 0;
 while (count!=((rows-2)*(cols-2)-Count))
 {
 printf("请输入坐标:\n");
 scanf("%d%d", &x, &y);
 if (mine[x][y] == '1')
 {
  printf("你踩到雷了!\n");
  return 0;
 }
 else
 {
  int ret = get_num(mine, x, y);
  show[x][y] = ret + '0';
  //set_mine(mine);
  display(show);
  count++;
 }
 }
 printf("恭喜你赢了!\n");
 display(mine);
 return 0;
}
 
 
//游戏
int Game(char mine[rows][cols],char show[rows][cols])
{
 set_mine(mine);
 display(show);
 //display(mine);//可以将雷的位置显示出来
 Sweep(mine,show);
 return 0;
}

最后就是测试函数 text.c

#include"game.h"
 
int main()
{
 int input = 0;
 char mine[rows][cols];
 char show[rows][cols];
 int i = 0;
 int j = 0;
 for (i = 0; i < rows - 1; i++)
 {
 for (j = 0; j < cols - 1; j++)
 {
  mine[i][j] = '0';
  show[i][j] = '*';
 }
 }
 menu();
 while (1)
 {
 printf("请选择:");
 scanf("%d", &input);
 if (input == 1)
 {
  printf("进入游戏\n");
  Game(mine,show);
  break;
 }
 else if (input == 0)
 {
  printf("退出游戏!\n");
  exit(0);
  break;
 }
 else
 {
  printf("输入有误!\n");
 }
 }
 return 0;
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

C++中四种对象生存期和作用域以及static的用法总结分析

以下是对C++中四种对象生存期和作用域以及static的用法进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++嵌套类与局部类详细解析

从作用域的角度看,嵌套类被隐藏在外围类之中,该类名只能在外围类中使用。如果在外围类之外的作用域使用该类名时,需要加名字限定
收藏 0 赞 0 分享

C++空类详解

以下是对C++中的空类进行了详细的介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C++之友元:友元函数和友元类详解

友元是一种允许非类成员函数访问类的非公有成员的一种机制。可以把一个函数指定为类的友元,也可以把整个类指定为另一个类的友元
收藏 0 赞 0 分享

C++中返回指向函数的指针示例

int (*ff(int)) (int *,int);表示:ff(int)是一个函数,带有一个int型的形参,该函数返回int (*) (int *,int),它是一个指向函数的指针,所指向的函数返回int型并带有两个分别是Int*和int型的形参
收藏 0 赞 0 分享

C数据结构之单链表详细示例分析

以下是对C语言中的单链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

C数据结构之双链表详细示例分析

以下是对c语言中的双链表进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅析如何在c语言中调用Linux脚本

如何在c语言中调用Linux脚本呢?下面小编就为大家详细的介绍一下吧!需要的朋友可以过来参考下
收藏 0 赞 0 分享

深入解析unsigned int 和 int

以下是对unsigned int和int进行了详细的分析介绍,需要的朋友可以过来参考下
收藏 0 赞 0 分享

浅谈C++中的string 类型占几个字节

本篇文章小编并不是为大家讲解string类型的用法,而是讲解我个人比较好奇的问题,就是string 类型占几个字节
收藏 0 赞 0 分享
查看更多