1、game.cpp
游戏代码的实现
//#include"game.h"
//int flag = 0;
//void RefreshBoard(char board[Row][Col], int row, int col) {
// int i = 0, j = 0;
// for (int i = 0; i < row; i++) {
// for (int j = 0; j < col; j++) {
// board[i][j] = ' '; // 一定得是单引号 不可以是双引号
// }
// }
//}
//void ShowBoard(char board[Row][Col], int row, int col)
//{
// for (int i = 0; i < row; i++)
// {
// for (int j = 0; j < col; j++)
// {
// cout << " " << board[i][j] << " ";
//
// if (j < col-1) cout << "|" ; // 最后一列不打印
// }
// cout << endl; // 是俩层循环 所以第一行第打印完之后 在输出endl;
// // 打印分割信息
// if (i < row - 1)
// {
// for (int j = 0; j < col; j++)
// {
// printf("---");
// if (j < col - 1) cout << "|";
// }
// cout << endl; // 道理和上面一样
// }
// }
//}
//void PlayMove(char board[Row][Col], int row, int col) {
// int x, y = 0;
// cout << "玩家请输入" << endl;
// while (1) {
// cout << "输入坐标" << endl;
// cin >> x >> y;
// // 此时该判断是否超出 况且 玩家是不知道第一个坐标是0,0的 即数组首位
// if (x >= 1 && x <= row && y >= 1 && y <= col) {
//
// if (board[x - 1][y - 1] == ' ') {
// board[x - 1][y - 1] = '*';
// break;
// }
// else {
// cout << "该位置已被占用" << endl;
// }
// }
// else cout << "坐标非法 no~" << endl;
// }
//}
//void ComputerMove(char board[3][3], int row, int col) {
// //if (board[0][0] || board[0][Col - 1] || board[Row - 1][0] || board[Row - 1][Col - 1] != ' ') {
//
// //}
// // 先写一个随机下棋 等讲完了获胜条件 就自己更改
// cout << "电脑下棋:" << endl;
// int x, y = 0;
// while (1) {
// x = rand() % row; y = rand() % col; // 0~2
// if (board[x][y] == ' ') {
// board[x][y] = '#'; break;
// }
// }
//}
//
//int IsFull(char board[Row][Col], int row, int col) {
// int a = 0;
// for (int i = 0; i < row; i++) {
// for (int j = 0; j < col; j++) {
// if (board[i][j] == ' ') a=0;
// else a=1;
// }
// }
// return a;
//}
//char Iswin(char board[Row][Col], int row, int col)
//{
// //行
// int i = 0;
// for (i = 0; i < row; i++)
// {
// if (board[i][0] == board[i][1] && board[i][1] == board[i][2] && board[i][1] != ' ')
// {
// return board[i][1];//
// }
// }
//
// //列
// int j = 0;
// for (j = 0; j < col; j++)
// {
// if (board[0][j] == board[1][j] && board[1][j] == board[2][j] && board[1][j] != ' ')
// {
// return board[1][j];
// }
// }
// //对角线
// if (board[0][0] == board[1][1] && board[1][1] == board[2][2] && board[1][1] != ' ')
// {
// return board[1][1];
// }
//
// if (board[0][2] == board[1][1] && board[1][1] == board[2][0] && board[1][1] != ' ')
// {
// return board[1][1];
// }
// //没有人赢,就要平局
// if (IsFull(board,row,col))
// {
// return 'Q';
// }
//
// //游戏继续
// return 'C';
//}
//
#include"game.h"
void InitBoard(char board[ROWS][COLS], int rows, int cols,char whitchone) {
for (int i = 0; i <= rows; i++) {
for (int j = 0; j <= cols; j++) {
// 为了分清楚到底初始化哪一个数组 对写一个参数
board[i][j] = whitchone;
}
}
}
// 注意 我想输出DisPlay 是99 的样子 但是 我传show数组的时候 他可是11*11的 所以只好写成ROWS
void DisPlay(char board[ROWS][COLS], int rows, int cols) {
for (int k = 0; k <= cols; k++) { // 打印列
cout << k << " ";
}cout << endl;
for (int i = 1; i <= 9; i++) {
cout << i << " "; // 为了打印行号
for (int j = 1; j <= 9; j++) {
cout << board[i][j]<< " ";
}cout << endl;
}cout <<"---------扫雷游戏-----------" << endl;
}
void SetMine(char board[ROWS][COLS], int rows, int cols) {
int count = EASY_COUNT;
while (count) { // 不是0 则布置雷
int x = rand() % ROW + 1; // 由于我的行列是从 1 开始的 这样对用户比较友好
int y = rand() % COL + 1;
if (board[x][y] == '0') {
board[x][y] = '1';
count--; // 这样使得布置雷最多是 EASY_COUNT 个
}
}
}
int getmine(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';
}
// 思路 范围的合法性 while
// 如果是 输出死了 并且雷的坐标
// 如果不是 现在就要输出 周围雷的个数 但是 八个if语句 !!难受 而且我周围放的可是字符1啊!
// ‘1’-‘0’=1 ‘0’-‘0’=0 则利用ascll码值 之后将周围的字符相加 即可
//get_mine 函数 统计mine数组中 xy 坐标 周围有几个雷
// win 是为了找到排雷个数 要不 就会使死循环
// 且打印出雷现在的情况 且小心重复排查 if else 解决
void Delete(char board2[ROWS][COLS], int x, int y) {
if (x >= 1 && x <= ROW && y >= 1 && y <= COL) {
//一定要小心重复
if (board2[x][y] == ' ' || board2[x][y]!='*') return;
else {
board2[x][y] = ' ';
Delete(board2, x-1, y );
Delete(board2, x + 1, y);
Delete(board2, x , y-1);
Delete(board2, x, y + 1);
}
}return;
}
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS],int rows, int cols) {
int x = 0, y = 0;
int count = EASY_COUNT;
int over = 0;
while (over> x >> y;
if (x >= 1 && x <= rows && y >= 1 && y <= cols) {
if (mine[x][y] == '1') {
cout << "game over!" << endl;
DisPlay(mine, ROW, COL);
cout << endl; cout << endl; cout << endl; cout << endl; cout << endl;
break;
}
else {
// 统计 mine数组 周围的坐标雷数
over++;
int sum = getmine(mine, x, y);
show[x][y] = sum + '0';
Delete(show, x, y);
DisPlay(show, rows, cols);
}
}
else {
cout << "非法重新输入" << endl;
}
}
if (over == ROW*COL-EASY_COUNT ) // 最多81-10次
{
cout << " 排雷成功" << endl;
}
}
2、game.h
这个文件用来必要的头文件与函数的声明部分 //#include//#include //#include //using namespace std; //#define Row 3 // 好处 : 如果将来不想玩三 更改起来会很方便 //#define Col 3 初始化棋盘 //void RefreshBoard(char board[3][3], int row, int col); 打印棋盘 //void ShowBoard(char board[3][3], int row, int col); 玩家下棋 //void PlayMove(char board[3][3], int row, int col); 电脑下棋 //void ComputerMove(char board[3][3], int row, int col); 玩家赢-->* || 电脑--># || 平局-->Q || 继续-->C 四种状态 //char Iswin(char board[3][3],int row,int col); // 不可以void 因为要检测返回的字符 此时需要 ans来判断 判断是否满的函数 //int IsFull(char board[3][3], int row, int col); #include #include #include using namespace std; #define ROW 9 #define COL 9 #define ROWS ROW+2 #define COLS COL+2 #define EASY_COUNT 10 void InitBoard(char board[ROWS][COLS], int rows, int cols, char whitchone); void DisPlay(char board[ROWS][COLS], int rows, int cols); void SetMine(char board[ROWS][COLS], int rows, int cols); void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols); void Delete(char board2[ROWS][COLS] ,int x, int y);
3、test.cpp
游戏代码的进行
//#include"game.h"
这里不可以写game.cpp
//void menu() {
// cout << " **************" << endl;
// cout << " 1.play**0.exit" << endl;
// cout << " **************" << endl;
//}
//void game() {
// char ans = 0;
// char board[Row][Col] = { 0 };
// RefreshBoard(board, Row, Col); // 初始化为空格 初始化棋盘的功能
// ShowBoard(board, Row, Col);
// // 下棋
// while (1) {
// PlayMove(board, Row, Col);
// ShowBoard(board, Row, Col);
// // 判断输赢
// ans=Iswin(board,Row,Col);
// if (ans != 'C') {
// break;
// }// 判断是否继续
// ComputerMove(board, Row, Col);
// ShowBoard(board,Row,Col);
// // 判断输赢
// ans = Iswin(board, Row, Col);
// if (ans != 'C') {
// break;
// }// 与上方同理
// }
// // 这个时候已经退出了循环 则该开始判断谁输谁赢
// if (ans == '*') cout << "玩家win!" << endl << endl << endl;
// if (ans == '#')cout << "电脑win!" << endl << endl << endl;
// if (ans == 'Q')cout << "我丢!平局~" << endl << endl << endl;
//}
//
//int main()
//{
// srand((unsigned int)time(NULL)); // 设置随机数的生成起点
// int input = 0;
// do {
// menu();// 打印菜单
// cout << "请选择:" << endl;
// cin >> input;
// switch (input)
// {
// case 1:
// game(); // 只要走进 case 1 就可以开始游戏
// cout << "子宫格" << endl; break;
// case 0: cout << "退出游戏" << endl; break;
// default:cout << "选择错误!" << endl; break;
// }
// } while (input); // 目的是可以返回的去 要是选择错误 可以重新选择 或者这场游戏玩完了还想再玩
// return 0;
//}
#include"game.h"
void menu() {
cout << "********" << endl;
cout << "********" << endl;
cout << "********" << endl;
cout << "********" << endl;
}
void game() {
char mine[ROWS][COLS] = { 0 }; // 存放雷
char show[ROWS][COLS] = { 0 }; // 排查出的信息
//初始化
InitBoard(mine, ROW, COL,'0'); // 好处 使得行数与数组中的0 概念不同 有更多操作性
InitBoard(show, ROW, COL, '*');
// 设置雷
SetMine(mine,ROW,COL);
//打印 行号列号也是需要滴 且不打印mine数组
DisPlay(show,ROW,COL);
// 排查雷 在一数组中看 在二数组中输出
FindMine(mine, show, ROW, COL);
}
int main()
{
int input = 0;
srand((unsigned int)time(NULL));
do {
menu();
cout << "请选择:";
cin >> input;
switch (input) {
case 1: game(); break;
case 0: cout << "退出游戏"<



