用户登录
用户注册

分享至

C语言实现扫雷小项目

  • 作者: 说不出LVE
  • 来源: 51数据库
  • 2021-08-04
这篇文章主要为大家详细介绍了C语言实现扫雷小项目,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C语言实现扫雷小项目的具体代码,供大家参考,具体内容如下

游戏的基本设计流程如下:

菜单实现:

void menu() {
 printf("##############\n");
 printf("1.enter 0.exit\n");
 printf("##############\n");
}

这里输入之后要判断是否进入游戏,所以这里我们使用switch函数实现:

switch函数实现:

do 
 {
  menu();
  printf("请输入自己的选择\n");
  scanf("%d", &input);
  switch (input) 
  {
  case 1:
   game();
   break;
  case 0:
   printf("退出游戏\n");
   break;
  default:
   printf("输入有误,请重新输入!\n");
   break;
  
  }
while(input);

游戏函数的实现:

game()函数为这次游戏的主体,在game(0函数内我们将不同功能函数分别实现;首先游戏的设计上采用了两个不同的数组来实现,一个数组用于存放雷的信息,一个数组用于存放排雷情况。

为了进一步扩大游戏的扩展性,我们使用宏来定义数组的行和列;

#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2

对于棋盘的特殊地方比如棋盘的边界,如果去遍历周边的数组,就会产生数组越界访问,所以为了解决这种情况我们使用了,给其定义了 ROW+2,但是在棋盘的展示我们只展示ROW的情况。

棋盘初始化实现:

我们定义存放雷的数组初始化为‘0',存放排雷情况的数组为‘*'。

void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) 
{
 int i = 0;
 for (i = 0; i < rows; i++) 
 {
  int j = 0;
  for (j = 0; j < cols; j++) 
  {
  
   board[i][j] = set;
  
  }
 
 }
}

棋盘的打印实现:

void DisplayBoard(char board[ROWS][COLS], int row, int col) 
{
 int i = 0;
 printf("-----------------------------\n");
 for (i = 0; i <= row; i++) 
 {
  printf("%d ", i);
 
 }//实现行的提示
 printf("\n");
 for (i = 1; i <= row; i++) 
 {
  int j = 0;
 
   printf("%d ", i);//实现列的提示
  
  for (j = 1; j <= col; j++) 
  {
   printf("%c ", board[i][j]);/
 
  }
 
 printf("\n");
 
 }
 
 printf("-----------------------------\n");
 
 
}

布雷实现:

#define EASY_MODLE 10
void SetMine(char board[ROWS][COLS], int row, int col) 
{
 
 int count = EASY_MODEL;//用来定义雷的个数
 while (count) 
 {
  int x = rand() % row + 1;//产生1-9的随机数
  int y = rand() % col + 1;
  if (board[x][y] != '1')//布雷初始化为‘0',所以需要进行判断不是雷的情况下才能布雷
  {
   board[x][y] = '1';
   count--;
  }
 
 
 }
 
}

扫雷实现:

int GetMineCount(char mine[ROWS][COLS], int x,int y) //统计输入坐标周边的雷的个数
{
  return (mine[x - 1][y] +
   mine[x - 1][y - 1] +
   mine[x][y - 1] +
   mine[x + 1][y - 1] +
   mine[x + 1][y] +
   mine[x + 1][y + 1] +
   mine[x][y + 1] +
   mine[x - 1][y + 1] - 8 * '0');
}
 
void FineMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) 
{
 int x = 0;
 int y = 0;
 int win = 0;
 while (win< row*col-EASY_MODEL) {
  printf("请输入坐标:");
  scanf("%d%d", &x, &y);
  if (x >= 1 && x <= row && y >= 1 && y <= col)
  {
   if (mine[x][y] == '1')
   {
    printf("很遗憾,你被炸死了\n");
    DisplayBoard(mine, ROW, COL);
    break;
   }
   else 
   {
    int cout= GetMineCount(mine, x, y);//此函数是统计遍历周边数组存放多少雷
    show[x][y] = '0' + cout;//此处进行字符和数字的转换 ‘1'=‘0'+1
    DisplayBoard(show, ROW, COL);
    win++;
   
   }
  }
  else
  {
   printf("输入坐标超出范围,请重新输入有效坐标!\n");
  }
 }
 if (win == row * col - EASY_MODEL)
 {
  printf("恭喜你,排雷成功\n");
  DisplayBoard(mine, ROW, COL);
 }
 
 
}

以上为个个模块的实现。

此次代码实现分为两个源文件(test.c和game.c)一个头文件(game.h),一下为代码的全部内容

game.h

#define _CRT_SECURE_NO_WARNINGS 1
#pragma once
#include<stdio.h>
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_MODEL 10
//初始化棋盘
 void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
 //显示棋盘
 void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布雷
 void SetMine( char board[ROWS][COLS],int row, int col);
//排雷
 void FineMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);

game.c

#define _CRT_SECURE_NO_WARNINGS 1
 
#include"game.h"
//初始化
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set) 
{
 int i = 0;
 for (i = 0; i < rows; i++) 
 {
  int j = 0;
  for (j = 0; j < cols; j++) 
  {
  
   board[i][j] = set;
  
  }
 
 
 }
 
 
}
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col) 
{
 int i = 0;
 printf("-----------------------------\n");
 for (i = 0; i <= row; i++) 
 {
  printf("%d ", i);
 
 }
 printf("\n");
 for (i = 1; i <= row; i++) 
 {
  int j = 0;
 
   printf("%d ", i);
  
  for (j = 1; j <= col; j++) 
  {
   printf("%c ", board[i][j]);
 
  }
 
 printf("\n");
 
 }
 
 printf("-----------------------------\n");
 
 
}
//布雷
void SetMine(char board[ROWS][COLS], int row, int col) 
{
 
 int count = EASY_MODEL;
 while (count) 
 {
  int x = rand() % row + 1;
  int y = rand() % col + 1;
  if (board[x][y] != '1')
  {
   board[x][y] = '1';
   count--;
  }
 
 
 }
 
}
//统计输入坐标周边雷的个数
 int GetMineCount(char mine[ROWS][COLS], int x,int y) 
{
  return (mine[x - 1][y] +
   mine[x - 1][y - 1] +
   mine[x][y - 1] +
   mine[x + 1][y - 1] +
   mine[x + 1][y] +
   mine[x + 1][y + 1] +
   mine[x][y + 1] +
   mine[x - 1][y + 1] - 8 * '0');
 
}
//排雷
void FineMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col) 
{
 int x = 0;
 int y = 0;
 int win = 0;
 while (win< row*col-EASY_MODEL) {
  printf("请输入坐标:");
  scanf("%d%d", &x, &y);
  if (x >= 1 && x <= row && y >= 1 && y <= col)
  {
   if (mine[x][y] == '1')
   {
    printf("很遗憾,你被炸死了\n");
    DisplayBoard(mine, ROW, COL);
    break;
 
   }
   else 
   {
    int cout= GetMineCount(mine, x, y);
    show[x][y] = '0' + cout;
    DisplayBoard(show, ROW, COL);
    win++;
   
   }
 
  }
  else
  {
   printf("输入坐标超出范围,请重新输入有效坐标!\n");
  }
 
 }
 if (win == row * col - EASY_MODEL)
 {
  printf("恭喜你,排雷成功\n");
  DisplayBoard(mine, ROW, COL);
 }
 
 
 
}

test.c

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
 
void menu() {
 printf("##############\n");
 printf("1.enter 0.exit\n");
 printf("##############\n");
}
 
void  game() 
{
 char mine[ROWS][COLS] = { 0 };//存放雷的信息
 char show[ROWS][COLS] = { 0 };//存放排查出的雷的信息
 //初始化棋盘
 InitBoard(mine, ROWS, COLS, '0');
 InitBoard(show, ROWS, COLS, '*');
 DisplayBoard(mine, ROW, COL);
 DisplayBoard(show, ROW, COL);
 
 //布置雷
 SetMine(mine, ROW, COL);
 DisplayBoard(mine, ROW, COL);
 
 //排查雷
 FineMine(mine, show, ROW, COL);
 
}
 
int main() {
 int input = 0;
 srand((unsigned int)time(NULL));
 
 do 
 {
  menu();
  printf("请输入自己的选择\n");
  scanf("%d", &input);
  switch (input) 
  {
  case 1:
   game();
   break;
  case 0:
   printf("退出游戏\n");
   break;
  default:
   printf("输入有误,请重新输入!\n");
   break;

  
  }
 
 
 
 } while (input);
 
 return 0;
}

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

软件
前端设计
程序设计
Java相关