Minesweeper game(Plain version)--C

简介: Minesweeper game(Plain version)--C

前言

学完数组和函数,我们可以简单写一个扫雷游戏。此扫雷游戏是一个简化版的,大家可以尝试写一下。

游戏概述

• 使⽤控制台实现经典的扫雷游戏

• 游戏可以通过菜单实现继续玩或者退出游戏

• 扫雷的棋盘是9*9的格⼦

• 默认随机布置10个雷

• 可以排查雷

◦ 如果位置不是雷,就显⽰周围有⼏个雷

◦ 如果位置是雷,就炸死游戏结束

◦ 把除10个雷之外的所有雷都找出来,排雷成功,游戏结束

游戏界面

游戏分析

创建数组

因为我们需要在9 * 9的棋盘上布置雷的信息和排查雷,我们⾸先想到的就是创建⼀个9*9的数组来存放信息

‘1’代表雷,‘0’代表没有雷

假设我们排查(8,6)这个坐标时,我们访问周围的⼀圈8个⻩⾊位置

在数组的边界,我们会发现判断周边8个格子的时候要判断是否越界

既然这样,我们可以在外面再加一圈,即11*11的网格,只不过最外面的一圈咱们不布置雷就好了。

我们既需要布置雷,又需要知道排查的雷的信息,这时候,我们需要创建两个数组,一个存放布置好的雷,另一个存放排查出的雷的信息。

char mine[ROWS][COLS];//存放布置好的雷
char show[ROWS][COLS];//存放排查出的雷的信息

初始化数组

//初始化数组
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;
    }
  }
}

因为我们有两个数组,并且两个数组存放的都是字符,所以我们可以声明一个char类型数组函数。但是,两个数组存放的字符不一样,所以我们需要多传一个参数:

InitBoard(mine, ROWS, COLS, '0');
InitBoard(show, ROWS, COLS, '*');
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set)

打印棋盘

void DisplayBoard(char board[ROWS][COLS], int row, int col)
{
  printf("--------- Minesweeper game   -------\n");
  int i = 0;
  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)
{
  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--;
    }
  }
}

在9*9的网格中,需要随机布置10个雷(在本节中,咱们就先布置十个雷),随机布置,就需要用到随机数的生成,不能是伪随机数哦!

另外,在设置雷的时候,如果本来就没有雷(即‘0’)那么就可以设雷,如果本身就是雷(即‘1’)那么就没必要再设置了。

在while循环中,每设置一个雷,就减1,直到减为0,为0,while不会再循环,这样就布置好了10个雷。

找你所输入的坐标周边有多少个雷

那么,如何知道周边雷的个数呢?

字符‘0’为 48

字符‘1’ 为49

49-‘0’=1

如果我们将周边的八个,每一个减去‘0’,再相加,就可以得到雷的个数。

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);
  }
}

代码实现

⽂件结构设计

test.c //⽂件中写游戏的测试逻辑 
game.c //⽂件中写游戏中函数的实现等
game.h //⽂件中写游戏需要的数据类型和函数声明等

test.c

# define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include"game.h"
void menu()
{
  printf("*****    Minesweeper game    *********\n");
  printf("**************************************\n");
  printf("**********  1. play ******************\n");
  printf("**********  2. exit ******************\n");
  printf("**************************************\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;
  do {
    menu();
    srand((unsigned int)time(NULL));
    printf("Please choose:>");
    scanf("%d", &input);
    switch (input) {
      case 1:
        game();
        break;
      case 2:
        printf("退出游戏\n");
        break;
      default:
        printf("选择错误,请重新选择\n");
        break;
    }
  } while (input);
  return 0;
}

game.h

#pragma once
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
#define EASY_COUNT 18
//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char set);
//打印棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);
//布置雷(随机布置)(布置10个雷)
void SetMine(char board[ROWS][COLS], int row, int col);
//排查雷
void FindMine(char mine[ROWS][COLS],char show[ROWS][COLS],int row, int col);

game.c

# define _CRT_SECURE_NO_WARNINGS
#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)
{
  printf("--------- Minesweeper game   -------\n");
  int i = 0;
  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)
{
  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);
  }
}


目录
相关文章
|
1月前
|
SQL Android开发
创建 SAP ABAP CDS View 保存失败 - Dependencies DDL source - View Entity not written
创建 SAP ABAP CDS View 保存失败 - Dependencies DDL source - View Entity not written
15 0
创建 SAP ABAP CDS View 保存失败 - Dependencies DDL source - View Entity not written
|
1月前
|
XML 存储 JSON
SAP UI5 XML Templating Preprocessor 的 template:with 指令使用介绍
SAP UI5 XML Templating Preprocessor 的 template:with 指令使用介绍
25 0
|
9月前
|
机器学习/深度学习
D1. Mocha and Diana (Easy Version)<738,div2>
D1. Mocha and Diana (Easy Version)<738,div2>
31 0
|
Java 数据库
IDEA不能运行 Add Configuration/Press the + button to create a new Configuration which cannot be loaded
IDEA不能运行 Add Configuration/Press the + button to create a new Configuration which cannot be loaded
IDEA不能运行 Add Configuration/Press the + button to create a new Configuration which cannot be loaded
|
应用服务中间件
Cannot change version of project facet Dynamic Web Module to 3.0
Cannot change version of project facet Dynamic Web Module to 3.0
|
机器人 Unix API
move_group_python_interface_tutorial代码注释
move_group_python_interface_tutorial代码注释
|
数据采集 Web App开发 JavaScript
Chrome View Source Code 那些事
Chrome View Source Code 那些事
Chrome View Source Code 那些事
|
Android开发 iOS开发
where is os type and version determined for a ui5 html
Created by Jerry Wang, last modified on Jun 27, 2015
where is os type and version determined for a ui5 html
另一种无法enable ABAP source code tool的原因
另一种无法enable ABAP source code tool的原因
73 0
ABAP Text table implementation
Created by Wang, Jerry, last modified on Dec 20, 2014
76 0
ABAP Text table implementation