#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();//判断是否赢
建立棋盘效果
函数文件
#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];
}
}