C语言实现扫雷游戏(可展开)

来自:互联网
时间:2021-03-02
阅读:

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

# 一、游戏的思路

先理清游戏大概需要实现的功能,菜单功能的实现、棋盘初始化、打印棋盘、布置雷等。运用两个数组,一个放入布置雷的信息,另一个放入排查雷的信息。选一个坐标扫雷,坐标有雷则游戏结束,没有就计算选中坐标的周围8个格子中雷的总数放入选中的坐标中,若选中的坐标周围8个格子中都没有雷则自动展开。考虑到棋盘边框的情况,实际数组要比打印出的棋盘多两行两列。下面是宏定义和函数声明:

ROW、COL为打印行、列,ROWS、COLS为实际的数组行列
EASY_COUNT为雷的个数,可根据需要调整行列和雷的个数

#define ROW 9
#define COL 9

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

#define EASY_COUNT 10

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<Windows.h>

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 FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
void ExcludeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);

二、游戏测试

游戏实现的大致思路体现和菜单的实现,代码如下:

#define _CRT_SECURE_NO_WARNINGS 1

#include"game.h"

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

//布置雷 - 字符组存储 - 雷用1表示,非雷用0表示 - 最外层一圈放0
//排查雷 - 为避免歧义,再用一个字符组存储排查出来的雷的信息 - 未排除的用#表示
//最外层加一圈字符,只在中间设置雷,并打印展示棋盘中间位置,因此实际存放数组要比打印的棋盘多两行两列
void game()
{
 //雷的信息存储
 //1.布置好的雷的信息
 char mine[ROWS][COLS] = { 0 };
 //2.排查出的雷的信息
 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);//测试使用
 //扫雷
 FindMine(mine, show, ROW, COL);
}

void test()
{
 int input = 0;
 srand((unsigned int)time(NULL));
 do
 {
 menu();
 printf("请选择:>");
 scanf("%d", &input);
 switch(input)
 {
  case 1:
  game();
  printf("将返回主菜单\n");
  Sleep(5 * 1000);
  break;
  case 0:
  printf("退出游戏\n");
  break;
  default:
  printf("选择错误,请重新选择\n");
  break;
 }
 } while (input);
}

int main()
{
 test();
 return 0;
}

三、游戏流程

存放函数的源文件需要引用头文件

#define _CRT_SECURE_NO_WARNINGS 1
#include "game.h"

1.初始化棋盘

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

2.棋盘打印

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
 int i = 0;
 int j = 0;
 //打印列号
 for (i = 0; i <= col; i++)
 {
 printf("%d ", i);
 }
 printf("\n");
 for (i = 1; i <= row; i++)
 {
 printf("%d ", i);
 for (j = 1; j <= col; j++)
 {
  printf("%c ", board[i][j]);
 }
 printf("\n");
 }
}

3.布置雷

void SetMine(char board[ROWS][COLS], int row, int col)
{
 int count = EASY_COUNT;
 while (count)
 {
 int x = rand() % row + 1;//1-9
 int y = rand() % col + 1;
 if (board[x][y] == '0')
 {
  board[x][y] = '1';
  count--;
 }
 }
}

4.查找雷和胜负判断

int CheckShow(char show[ROWS][COLS], int row, int col)
{
 int win = 0;
 int i = 0;
 int j = 0;
 for (i = 1; i <= row; i++)
 {
 for (j = 1; j <= col; j++)
 {
  if (show[i][j] == '#')
  win++;
 }
 }
 return win;
}

void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
 int x = 0;
 int y = 0;
 int win = 0;
 //9*9 - 10 = 71
 while (1)
 {
 printf("请输入排查雷的坐标:>");
 scanf("%d%d", &x, &y);
 if (x >= 1 && x <= row && y >= 1 && y <= col)
 {
  //坐标合法
  //1.踩雷
  if (mine[x][y] == '1')
  {
  printf("很遗憾,你被炸死了\n");
  DisplayBoard(mine, row, col);
  break;
  }
  else//不是雷
  {
  //计算x,y坐标周围有几个雷
  ExcludeMine(mine, show, x, y);
  DisplayBoard(show, row, col);
  win = CheckShow(show, row, col);
  if (win == EASY_COUNT)
   break;
  }
 }
 else
 {
  printf("坐标非法,请重新输入!\n");
 }
 }
 if (win == EASY_COUNT)
 {
 printf("恭喜你,排雷成功\n");
 DisplayBoard(mine, row, col);
 }
}

5.扫雷的展开和提醒

//'1' - '0' = 1
int get_mine_count(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 ExcludeMine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
 int count = get_mine_count(mine, x, y);
 if (count != 0)
 {
 show[x][y] = count + '0';
 }
 else
 {
 show[x][y] = ' ';
 if (show[x - 1][y] == '#')
  ExcludeMine(mine, show, x - 1, y);
 if (show[x - 1][y - 1] == '#')
  ExcludeMine(mine, show, x - 1, y - 1);
 if (show[x][y - 1] == '#')
  ExcludeMine(mine, show, x, y - 1);
 if (show[x + 1][y - 1] == '#')
  ExcludeMine(mine, show, x + 1, y - 1);
 if (show[x + 1][y] == '#')
  ExcludeMine(mine, show, x + 1, y);
 if (show[x + 1][y + 1] == '#')
  ExcludeMine(mine, show, x + 1, y + 1);
 if (show[x][y + 1] == '#')
  ExcludeMine(mine, show, x, y + 1);
 if (show[x - 1][y + 1] == '#')
  ExcludeMine(mine, show, x - 1, y + 1);
 }
}

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

返回顶部
顶部