评论

收藏

[C++] 扫雷游戏(简单易懂)

编程语言 编程语言 发布于:2021-12-09 16:38 | 阅读数:265 | 评论:0

该扫雷游戏简单易懂易玩,与五子棋做法像似,甚至比五子棋还简单
有兴趣可以试试!
首先建立俩个棋盘,第一棋盘为游戏棋盘,第二棋盘用来赋值雷的位置和雷旁边的数字
根据用户所选的位置,第二棋盘把这位置周围的数字赋值给第一棋盘,从而让第一棋盘构成雷被隐藏的效果,如果所选的位置有雷,则游戏结束
用“*”来表示未扫描的位置,用”#“来表示雷
如果扫描完无(”*“),只剩下雷被覆盖的”*“,则玩家成功扫完所有的雷
头文件
#pragma once
#include<iostream>
using namespace std;
#include<string>
#include<ctime>
void buildboard();//建立棋盘
void displayer();//打印棋盘
void buildboard();//初始化棋盘
void game();//运行游戏
int addmath(int temprow, int tempcol);//按照周围的雷数,增加数字
void add2(int temprow, int tempcol);//按照周围的雷数,在第二棋盘打印数字
void passboard(int getrow, int getcol);//根据所扫描的位置,在这位置的周围把第二棋盘的数字覆盖到第一棋盘
int iswin();//判断是否赢
建立棋盘效果
DSC0000.png
函数文件
#include"扫雷.h"
#define row 12
#define col 12//10x10的棋盘
#define bomb 12//雷的数量
string board[row][col];//第一棋盘
string board2[row][col];//第二棋盘
void buildboard()//初始化棋盘
{
  for (int i = 0; i < row; i++)//
  {
  for (int k = 0; k < col; k++)
  {
    board[i][k] = " * ";
    board2[i][k] = "   ";
  }
  }
}
void displayer()//打印棋盘
{
  for (int i = 1; i < row - 1; i++)
  {
  cout << "|";
  cout << "---";
  }
  cout << "|";
  cout << endl;
  for (int i = 1; i < row-1; i++)
  {
  for (int k = 1; k < col-1; k++)
  {
    cout << "|";
    cout << board[i][k];
  }
  cout << "|";
  cout << endl;
  cout << "|";
  for (int k = 1; k < col - 1; k++)
  {
    cout << "---";
    cout << "|";
  }
  cout << endl;
  }
  cout << endl;
}
void game()
{
  buildboard();
  srand((unsigned int)time(NULL));
  for (int i = 1; i <= bomb; i++)
  {
  while (true)
  {
    int temprow = rand() % 10 + 1;//1~10
    int tempcol = rand() % 10 + 1;
    if (board2[temprow][tempcol] == "   ")
    {
    board2[temprow][tempcol] = " # ";
    add2( temprow,  tempcol);
    break;
    }
  }
  }
  int getrow, getcol;
  while (true)
  {
  displayer();
  cout << "请输入扫描的位置(先输入行数再输入列数):";
  cin >> getrow >> getcol;
  if (getrow >= 1 && getrow <= 10 && getcol <= 10 && getcol >= 1)
  {
    if (board2[getrow][getcol] == " # ")
    {
    for (int i = 1; i < row - 1; i++)
    {
      for (int k = 1; k < col; k++)
      {
      if (board2[i][k] == " # ")
      {
        board[i][k] = " # ";
      }
      }
    }
    cout << "第" << getrow << "行" << "第" << getcol << "列有雷!!!" << endl;
    displayer();
    cout << "您已触雷,游戏结束!" << endl;
    break;
    }
    else
    {
    passboard(getrow, getcol);
    if (iswin())
    {
      cout << "恭喜你赢了!" << endl;
      break;
    }
    }
  }
  else
  {
    cout << "您已越界,请重新输入!" << endl;
  }
  }
}

int addmath(int temprow, int tempcol)
{
  if ( board2[temprow][tempcol] == "   ")
  {
  board2[temprow][tempcol] = " 1 ";
  return 0;
  }
  if (board2[temprow][tempcol] == " 1 ")
  {
  board2[temprow][tempcol] = " 2 ";
  return 0;
  }
  if (board2[temprow][tempcol] == " 2 ")
  {
  board2[temprow][tempcol] = " 3 ";
  return 0;
  }
  if (board2[temprow][tempcol] == " 3 ")
  {
  board2[temprow][tempcol] = " 4 ";
  return 0;
  }
  if (board2[temprow][tempcol] == " 4 ")
  {
  board2[temprow][tempcol] = " 5 ";
  return 0;
  }
  if (board2[temprow][tempcol] == " 5 ")
  {
  board2[temprow][tempcol] = " 6 ";
  return 0;
  }
  if (board[temprow][tempcol] == " 6 ")
  {
  board[temprow][tempcol] = " 7 ";
  return 0;
  }
  if (board[temprow][tempcol] == " 7 ")
  {
  board[temprow][tempcol] = " 8 ";
  return 0;
  }
}
int iswin()
{
  for (int i = 1; i < row - 1; i++)
  {
  for (int k = 1; k < col - 1; k++)
  {
    if (board2[i][k] == " # ")
    {
    continue;
    }
    if (board[i][k] == " * ")
    {
    return 0;
    } 
  }
  }
  return 1;
}
void add2(int temprow, int tempcol)
{
  for (int i = tempcol - 1; i <= tempcol + 1; i++)
  {
  addmath(temprow - 1, i);
  addmath(temprow + 1, i);
  }
  addmath(temprow, tempcol - 1);
  addmath(temprow, tempcol + 1);
}
void passboard(int getrow, int getcol)
{
  board[getrow][getcol] = board2[getrow][getcol];
  for (int i = getcol - 1; i <= getcol + 1; i++)
  {
  if (board2[getrow - 1][i] != " # ")
  {
    board[getrow - 1][i] = board2[getrow - 1][i];
  }
  if (board2[getrow +1][i] != " # ")
  {
    board[getrow + 1][i] = board2[getrow +1][i];
  }
  }
  if (board2[getrow][getcol - 1] != " # ")
  {
  board[getrow][getcol - 1] = board2[getrow][getcol - 1];
  }
  if (board2[getrow][getcol+1] != " # ")
  {
  board[getrow][getcol + 1] = board2[getrow][getcol + 1];
  }
}
主函数文件
#include"扫雷.h"
#include<iostream>
using namespace std;
#include<string>
int main()
{
  cout << "**************<<扫雷游戏>>**************" << endl;
  cout << "**************1.进入游戏****************" << endl;
  cout << "**************2.退出游戏****************" << endl;
  int select;
  cout << "请输入数字进行操作:";
  cin >> select;
  if (select >= -1000 && select <= 1000)
  {
  while (true)
  {
    
    switch (select)
    {
    case 1:
    {
    game();
    cout << "1.再玩一次"<<endl;
    cout << "2.退出游戏"<<endl;
    break;
    }
    case 2:
    {
    cout << "欢迎下次使用!"<<endl;
    return 0;
    break;

    }
    default:
    cout << "输入错误,请重新输入!"<<endl;
    }
    cout << "请输入数字进行操作:";
    cin >> select;
  }
  }
  else
  {
  cout << "输入错误,请退出重试!";
  return 0;
  }
  return 0;
}
运行效果
DSC0001.png
DSC0002.png
DSC0003.png

关注下面的标签,发现更多相似文章