21xrx.com
2024-12-22 20:25:51 Sunday
登录
文章检索 我的文章 写文章
C++扫雷游戏源代码
2023-07-11 08:26:29 深夜i     --     --
C++ 扫雷游戏 源代码 游戏开发 工程实践

扫雷游戏是一款很受欢迎的益智类游戏。这里提供C++语言实现的扫雷游戏源代码。

实现方法:

1. 设置扫雷地图,然后在地图上随机布雷。

2. 生成随机数,让玩家选择行和列,然后根据玩家选择的位置判断地图上的属性。

3. 根据判断出的属性,判断是否踩雷。如果踩雷则游戏结束,否则继续游戏。

4. 记录游戏时间和雷的数量,判断游戏是否胜利。

源代码如下:


#include <iostream>

#include <conio.h>

#include <ctime>

#include <cstdlib>

using namespace std;

const int MAX_ROW = 10;

const int MAX_COL = 10;

const int NUM_MINES = 10;

//地图每个格子的属性

enum Square

  MINE;

//雷属性的数据结构

struct MineSquare

  Square m_display;

  bool m_mine;

;

//用于选择翻开的位置的数据结构

struct Coordinate

  int row;

  int col;

;

//生产随机数函数

int getRandomNumber(int min, int max)

{

  static const double fraction = 1.0 / (RAND_MAX + 1.0); 

  return min + static_cast<int>((max - min + 1) * (rand() * fraction));

}

//用于布雷的函数

void setRandomMines(MineSquare board[][MAX_COL])

{

  int count = 0;

  while (count < NUM_MINES)

  {

    int row = getRandomNumber(0, MAX_ROW - 1);

    int col = getRandomNumber(0, MAX_COL - 1);

    if (board[row][col].m_display != MINE)

    {

      board[row][col].m_display = MINE;

      board[row][col].m_mine = true;

      ++count;

    }

  }

}

//输出整个地图

void printBoard(MineSquare board[][MAX_COL], bool showMines)

{

  cout << " ";

  for (int col = 0; col < MAX_COL; ++col)

  

    cout << col << " ";

  

  cout << endl;

  for (int row = 0; row < MAX_ROW; ++row)

  {

    cout << row << " ";

    for (int col = 0; col < MAX_COL; ++col)

    {

      if (board[row][col].m_display == HIDDEN)

      

        cout << "# ";

      

      else if (board[row][col].m_display == MINE)

      {

        if (showMines)

        {

          cout << "* ";

        }

        else

        

          cout << "# ";

        

      }

      else

      {

        int cnt = 0;

        if (row > 0 && col > 0 && board[row - 1][col - 1].m_mine)

          ++cnt;

        if (row > 0 && board[row - 1][col].m_mine)

          ++cnt;

        if (row > 0 && col < MAX_COL - 1 && board[row - 1][col + 1].m_mine)

          ++cnt;

        if (col > 0 && board[row][col - 1].m_mine)

          ++cnt;

        if (col < MAX_COL - 1 && board[row][col + 1].m_mine)

          ++cnt;

        if (row < MAX_ROW - 1 && col > 0 && board[row + 1][col - 1].m_mine)

          ++cnt;

        if (row < MAX_ROW - 1 && board[row + 1][col].m_mine)

          ++cnt;

        if (row < MAX_ROW - 1 && col < MAX_COL - 1 && board[row + 1][col + 1].m_mine)

          ++cnt;

        cout << cnt << " ";

      }

    }

    cout << endl;

  }

}

//检查地图是否全部翻开

bool areAllMinesUncovered(MineSquare board[][MAX_COL])

{

  for (int row = 0; row < MAX_ROW; ++row)

  {

    for (int col = 0; col < MAX_COL; ++col)

    {

      if (board[row][col].m_display == HIDDEN && !board[row][col].m_mine)

      

        return false;

      

    }

  }

  return true;

}

//操作游戏界面函数

void revealBoard(MineSquare board[][MAX_COL], int row, int col)

{

  if (row < 0 || col < 0 || row >= MAX_ROW || col >= MAX_COL)

    return;

  if (board[row][col].m_display == SHOWN)

    return;

  if (board[row][col].m_display == MINE)

  {

    board[row][col].m_display = SHOWN;

    return;

  }

  board[row][col].m_display = SHOWN;

  int cnt = 0;

  if (row > 0 && col > 0 && board[row - 1][col - 1].m_mine)

    ++cnt;

  if (row > 0 && board[row - 1][col].m_mine)

    ++cnt;

  if (row > 0 && col < MAX_COL - 1 && board[row - 1][col + 1].m_mine)

    ++cnt;

  if (col > 0 && board[row][col - 1].m_mine)

    ++cnt;

  if (col < MAX_COL - 1 && board[row][col + 1].m_mine)

    ++cnt;

  if (row < MAX_ROW - 1 && col > 0 && board[row + 1][col - 1].m_mine)

    ++cnt;

  if (row < MAX_ROW - 1 && board[row + 1][col].m_mine)

    ++cnt;

  if (row < MAX_ROW - 1 && col < MAX_COL - 1 && board[row + 1][col + 1].m_mine)

    ++cnt;

  if (cnt == 0)

  {

    revealBoard(board, row, col - 1);

    revealBoard(board, row, col + 1);

    revealBoard(board, row - 1, col);

    revealBoard(board, row + 1, col);

    revealBoard(board, row - 1, col - 1);

    revealBoard(board, row - 1, col + 1);

    revealBoard(board, row + 1, col - 1);

    revealBoard(board, row + 1, col + 1);

  }

}

int main()

{

  srand(static_cast<unsigned int>(time(0)));

  rand();

  MineSquare board[MAX_ROW][MAX_COL];

  for (int row = 0; row < MAX_ROW; ++row)

  {

    for (int col = 0; col < MAX_COL; ++col)

    {

      board[row][col].m_display = HIDDEN; //初始化地图为未翻开

      board[row][col].m_mine = false; //初始化地图没有雷

    }

  }

  setRandomMines(board);

  cout << "扫雷游戏开始!输入坐标以翻开地图,可能的坐标从0到"

    << MAX_ROW - 1 << "行,从0到" << MAX_COL - 1 << "列。" << endl;

  int row, col;

  bool gameOver = false;

  while (!gameOver)

  {

    printBoard(board, false);

    cout << "输入坐标(使用空格或回车键分隔行和列):";

    cin >> row >> col;

    if (board[row][col].m_mine)

    {

      gameOver = true;

      board[row][col].m_display = SHOWN;

      printBoard(board, true);

      cout << "踩到雷了,你输了!" << endl;

    }

    else

    {

      revealBoard(board, row, col);

      if (areAllMinesUncovered(board))

      {

        gameOver = true;

        printBoard(board, true);

        cout << "你赢了!" << endl;

      }

    }

  }

  _getch();

  return 0;

}

本次提供的扫雷游戏代码只是一个基础的版本,游戏逻辑可以进一步完善,加入计时,增加难度等等。欢迎大家拿去学习、改进。

  
  

评论区

{{item['qq_nickname']}}
()
回复
回复