21xrx.com
2024-11-22 04:01:16 Friday
登录
文章检索 我的文章 写文章
扫雷游戏C++源代码【简体中文】
2023-07-06 10:16:00 深夜i     --     --
扫雷游戏 C++ 源代码 简体中文

扫雷游戏是一款经典的休闲益智游戏,也是程序员入门时常练手的小项目之一。下面是一份使用C++语言编写的简单扫雷游戏代码。

该代码主要分为三个部分:定义全局变量、定义函数、主程序。在定义全局变量时,主要是定义游戏盘面、雷的分布和游戏状态等全局变量。

在定义函数时,主要包含了初始化游戏、展示游戏盘面、打开方块、标记雷、检验是否赢得游戏等功能。其中,展示游戏盘面时使用了递归函数来展示被打开方块周围的方块。

在主程序中,主要是初始化游戏,打印游戏菜单并等待玩家输入命令。随后程序根据玩家的命令进行游戏。当玩家成功打开所有非雷方块时,游戏达到胜利状态。

此代码可作为初学C++时的参考和练习,也可以改进和扩展为更复杂的扫雷游戏。欢迎大家阅读和参考!

源代码如下:


#include <iostream>

#include <ctime>

#include <cstdlib>

using namespace std;

#define ROW 9

#define COL 9

#define MINE 10

#define BLANK 0

#define COVER 9

#define MARK 10

#define CORRECT_MARK 11

#define MINE_CELL 9

int cells[ROW + 2][COL + 2] = {0};

bool opened_cells[ROW + 2][COL + 2] = {false};

bool marked_cells[ROW + 2][COL + 2] = {false};

int row, col, mine_count;

void initialize_game()

{

  int count = 0;

  while (count < mine_count)

  {

    int r = rand() % ROW + 1;

    int c = rand() % COL + 1;

    if (cells[r][c] == BLANK)

    {

      cells[r][c] = MINE_CELL;

      count++;

    }

  }

}

void display_board()

{

  cout << "\nTIC TAC TOE GAME" << endl << endl;

  cout << " ";

  for (int c = 1; c <= COL; c++)

  

    cout << c << " ";

  

  cout << endl;

  cout << " +";

  for (int c = 1; c <= COL; c++)

  {

    cout << "-+";

  }

  cout << endl;

  for (int r = 1; r <= ROW; r++)

  {

    cout << r << "|";

    for (int c = 1; c <= COL; c++)

    {

      if (opened_cells[r][c])

      {

        if (cells[r][c] == MINE_CELL)

        

          cout << "*|";

        *|

          cout << cells[r][c] << "|";

        

        else

        {

          cout << cells[r][c] << "|

          cout << "M|";

        

      }

      else if (marked_cells[r][c])

      {

        if (cells[r][c] == MINE_CELL)

        {

          cout << "M|

          cout << "X|";

        

        else

        {

          cout << "X|";

        }

      }

      else

      

        cout << "?|";

      ?|";

      }

    }

    cout << endl;

    cout << " +";

    for (int c = 1; c <= COL; c++)

    {

      cout << "-+";

    }

    cout << endl;

  }

}

bool open_cell(int r, int c)

{

  if (cells[r][c] == MINE_CELL)

  

    return false;

  

  else if (opened_cells[r][c])

  

    return true;

  

  else

  {

    int count = 0;

    for (int i = -1; i <= 1; i++)

    {

      for (int j = -1; j <= 1; j++)

      {

        if (cells[r + i][c + j] == MINE_CELL)

        {

          count++;

        }

      }

    }

    cells[r][c] = count;

    opened_cells[r][c] = true;

    if (count == BLANK)

    {

      for (int i = -1; i <= 1; i++)

      {

        for (int j = -1; j <= 1; j++)

        {

          open_cell(r + i, c + j);

        }

      }

    }

    return true;

  }

}

void mark_cell(int r, int c)

{

  if (opened_cells[r][c])

  

    return;

  

  else if (marked_cells[r][c])

  {

    marked_cells[r][c] = false;

  }

  else

  {

    marked_cells[r][c] = true;

  }

}

bool check_win()

{

  int opened_count = 0;

  for (int r = 1; r <= ROW; r++)

  {

    for (int c = 1; c <= COL; c++)

    {

      if (opened_cells[r][c])

      {

        opened_count++;

      }

    }

  }

  return (ROW * COL - opened_count) == mine_count;

}

int main()

{

  srand(time(NULL));

  mine_count = MINE;

  initialize_game();

  bool game_over = false;

  while (!game_over)

  {

    display_board();

    int command;

    cout << "\nChoose command (1 to open, 2 to mark): ";

    cin >> command;

    int r, c;

    cout << "Choose cell (row column): ";

    cin >> r >> c;

    if (command == 1)

    {

      bool result = open_cell(r, c);

      if (!result)

      {

        cout << "\nYou lose!" << endl;

        display_board();

        game_over = true;

      }

    }

    else if (command == 2)

    {

      mark_cell(r, c);

    }

    if (check_win())

    {

      cout << "\nYou win!" << endl;

      display_board();

      game_over = true;

    }

  }

  return 0;

}

  
  

评论区

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