c语言实现扫雷

简介: c语言实现扫雷

📕博主介绍:目前大一正在学习c语言,数据结构,计算机网络

c语言学习,是为了更好的学习其他的编程语言,C语言是母体语言,是人机交互接近底层的桥梁。

本章来写一个扫雷吧。

让我们开启c语言学习之旅吧!

游戏的主要功能

1.打印一个棋盘,用来存雷

2.设置雷并排查雷

3.触发雷就游戏结束

需要三个文件

test.c -- 测试游戏逻辑

game.c --  函数实现

game.h  --  函数声明

游戏流程

1.构建游戏菜单

2.创建数组并初始化

3.布置雷

4.排查雷

游戏菜单的打印

进入游戏首先有一个菜单,让我们选择是否进入游戏

把头文件放在game.h中,然后包含一下#include"game"

代码

#include "game.h"
void menu()
{
  printf("**********************\n");
  printf("******  1.paly  ******\n");
  printf("******  0.exit  ******\n");
  printf("**********************\n");
}
int main()
{
  int input = 0;
  do
  {
  menu();
  printf("请选择:>");
  scanf("%d", &input);
    switch (input)
    {
    case 1 :
      printf("扫雷\n");
      break;
    case 0:
      printf("退出游戏\n");
      break;
    default:
      printf("输入错误\n");
    }
  } while (input);
  return 0;
}

创建数组并初始化

创建两个数组

char mine;    存放布置好的雷

char show;    存放排查出雷的信息

在统计周围雷的个数的时候会发现,如果是最边上的时候就会访问越界

想要解决这个问题很简单,比原来的数组大一圈就行了。

比如(9*9,换成11*11)

代码:

//初始化棋盘
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 <= col; i++)
  {
    printf("%d ",i);
  }
  printf("\n");
  for (i = 1; i <= row; i++)
  {
    printf("%d ", i);
    int j = 0;
    for (j = 1; j <= col; j++)
    {
      printf("%c ", board[i][i]);
    }
    printf("\n");
  }
}

运行结果

布置雷

操作mine数组,把雷布置到mine数组里面

1.要在9*9的数组内布置

2.布雷需要rand()函数来随机布置

3.用rand()%row+1和rand()%col+1来获取坐标,rand函数要用srand((unsigned int)(NULL)),并且要包含#include<time.h>和#include <stdlib.h>

代码如下:

void SetMine(char board[ROWS][COLS], int row, int col)
{
  //布置10个雷
  //生成随机的坐标,布置雷
  int count = EASY_COUNT;
  while (count)
  {
    int x = rand() % row + 1;
    int y = rand() % col + 1;
    if (board[x][y] == '0')
    {
      board[x][y] = '1';
      count--;
    }
  }
}

运行结果

已经成功布置10个雷

排查雷

这里我们用FindMne()函数来排查

如果坐标不是雷,就要统计周围有几个雷,统计用GetMineCount函数来排查周围坐标

因为是雷里面存放的是‘1’,不是雷存放的是‘0’,所以我们只需要把周围坐标里面的值加起来然后转化成字符型就好了。

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 FindMine(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_COUNT)
  {
    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 count = GetMineCount(mine, x, y);
        show[x][y] = count + '0';
        DisplayBoard(show, ROW, COL);
        win++;
      }
    }
    else
    {
      printf("坐标非法,重新输入\n");
    }
  }
  if (win == row * col - EASY_COUNT)
  {
    printf("恭喜你,排雷成功\n");
    DisplayBoard(mine, ROW, COL);
  }
}

完整代码

tset.c

# define  _CRT_SECURE_NO_WARNINGS 1
#include "game.h"
void menu()
{
  printf("***********************\n");
  printf("*****   1. play   *****\n");
  printf("*****   0. exit   *****\n");
  printf("***********************\n");
}
void game()
{
  char mine[ROWS][COLS];//存放布置好的雷
  char show[ROWS][COLS];//存放排查出的雷的信息
  //初始化棋盘
  //1. mine数组最开始是全'0'
  //2. show数组最开始是全'*'
  InitBoard(mine, ROWS, COLS, '0');
  InitBoard(show, ROWS, COLS, '*');
  //打印棋盘
  //DisplayBoard(mine, ROW, COL);
  DisplayBoard(show, ROW, COL);
  //1. 布置雷
  SetMine(mine, ROW, COL);
  //DisplayBoard(mine, ROW, COL);
  //2. 排查雷
  FindMine(mine, show, ROW, COL);
}
int main()
{
  int input = 0;
  srand((unsigned int)time(NULL));
  do
  {
    menu();
    printf("请选择:>");
    scanf("%d", &input);
    switch (input)
    {
    case 1:
      game();
      break;
    case 0:
      printf("退出游戏\n");
      break;
    default:
      printf("选择错误,重新选择\n");
      break;
    }
  } while (input);
  return 0;
}

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 <= col; i++)
  {
    printf("%d ", i);
  }
  printf("\n");
  for (i = 1; i <= row; i++)
  {
    printf("%d ", i);
    int j = 0;
    for (j = 1; j <= col; j++)
    {
      printf("%c ", board[i][j]);
    }
    printf("\n");
  }
}
void SetMine(char board[ROWS][COLS], int row, int col)
{
  //布置10个雷
  //生成随机的坐标,布置雷
  int count = EASY_COUNT;
  while (count)
  {
    int x = rand() % row + 1;
    int y = rand() % col + 1;
    if (board[x][y] == '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 FindMine(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_COUNT)
  {
    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 count = GetMineCount(mine, x, y);
        show[x][y] = count + '0';
        DisplayBoard(show, ROW, COL);
        win++;
      }
    }
    else
    {
      printf("坐标非法,重新输入\n");
    }
  }
  if (win == row * col - EASY_COUNT)
  {
    printf("恭喜你,排雷成功\n");
    DisplayBoard(mine, ROW, COL);
  }
}

game.h

#pragma once
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define EASY_COUNT 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
//初始化棋盘
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);
目录
相关文章
|
8天前
|
C语言
【海贼王编程冒险 - C语言海上篇】怎样用C语言实现简单的扫雷游戏?
【海贼王编程冒险 - C语言海上篇】怎样用C语言实现简单的扫雷游戏?
6 1
|
9天前
|
存储 安全 Serverless
扫雷游戏C语言代码实现——万字长文超详细,手把手教你实现,新手也能学会
扫雷游戏C语言代码实现——万字长文超详细,手把手教你实现,新手也能学会
|
9天前
|
存储 C语言
c语言实现扫雷
c语言实现扫雷
5 0
|
10天前
|
存储 C语言
C语言实战——扫雷游戏
C语言实战——扫雷游戏
11 0
|
10天前
|
C语言
扫雷游戏(C语言基础版)
扫雷游戏(C语言基础版)
6 0
|
13天前
|
C语言
·扫雷游戏(C语言版)
·扫雷游戏(C语言版)
|
13天前
|
存储 C语言
C语言---扫雷游戏的实现
C语言---扫雷游戏的实现
10 0
|
2月前
|
C语言
C语言实现扫雷
C语言实现扫雷
24 1
|
2月前
|
C语言
【C语言入门小游戏】扫雷
【C语言入门小游戏】扫雷
26 1
TU^
|
2月前
|
存储 C语言
C语言实现扫雷游戏
扫雷游戏是一款十分经典的游戏,运用到C语言中的数组,循环和条件语句,随机数的生成,函数,递归等知识点。
TU^
37 1