赞
踩
目录
建议先看几遍视频在下文。
QQ录屏扫雷游戏
这款游戏有三个难度(11,11,9,9,10)(18,18,16,16,40)(18,32,16,30,99)
一共有五个参数,意思是,棋盘大小为9*9,为了后续操作方便我们使用11*11的字符数组来存放棋盘,将棋盘放在数组的中间。九九八十一个格子中我们我安置10个雷,也就是第五个参数。
所以我们的game 函数就可以写为:(我们可以通过调节参数参数大小来调节游戏难度)
void game(int ROWS,int COLS,int ROW,int COL,int EASY_COUNT);
接下来,我们根据游戏规则开始设计,我们以简单模式为例进行详解(因为游戏难度只需调节参数即可),在棋盘中的隐藏这十枚炸弹,它们的位置我们并不知道,但是当我们选择一个坐标进行排雷是,棋盘又会时时给予我们信息反馈,很明显,我们至少需要两个字符数组,用来埋雷起名为mine[11][11],一个用来展示起名为show[11][11]。游戏开始十之初,这两个就已经初始化好了,先是把mine[11][11]全部初始化为字符零 '0',然后随机产生十个坐标埋下十颗炸弹,show[11][11]的元素全部初始化为星号 '*'。然后我们就可以开始输入坐标排雷了,哈哈哈。我们选择一个地方(x,y)排雷,如果这个地方就是炸弹,不好意思游戏结束,如果这个地方不是炸弹,那么就要统计这个位置周围一圈八个地方有几颗炸弹即get_num()操作,有几颗炸弹就把show[x][y]改为几,如果这八个位置没有炸弹,那么就把这个位置show[x][y]==' '设置为空格,并对其周围八个位置重复上述操作,这里我们是使用递归来实现的。
重难点全在注释里面了
game.h
- #include <iostream>
- #include <time.h>
- using namespace std;
- #define MAXSIZE 50
- //#define ROW 9
- //#define COL 9
- //#define ROWS ROW+2
- //#define COLS COL+2
- //#define EASY_COUNT 10
- /*
- #define ROW 30
- #define COL 16
- #define ROWS ROW+2
- #define COLS COL+2
- #define EASY_COUNT 99
- */
-
- //#define ROW 9
- //#define COL 9
- //#define ROWS ROW+2
- //#define COLS COL+2
- //#define EASY_COUNT 10
-
- //初始化棋盘
- void Init_qipan(char board[MAXSIZE][MAXSIZE], int row, int col, char set);
-
- //打印棋盘
- void Display_qipan(char board[MAXSIZE][MAXSIZE], int rows, int cols);
- //布置雷
- void Set_zhadan(char board[MAXSIZE][MAXSIZE], int row, int col,int EASY_COUNT);
- //扫雷
- int Fine_zhadan3(char mine[MAXSIZE][MAXSIZE], char show[MAXSIZE][MAXSIZE], int row, int col, int x, int y,int *win);
-
源.cpp
- #include "game.h"
- using namespace std;
- void game(int ROWS,int COLS,int ROW,int COL,int EASY_COUNT)
- {
- //为了避免重复判断数组越界问题,为9*9的棋盘横纵各加两组空间
- char mine[MAXSIZE][MAXSIZE];//布置雷的隐藏棋盘
- char show[MAXSIZE][MAXSIZE];//展示给玩家的棋盘
- Init_qipan(mine, ROWS, COLS, '0');
- Init_qipan(show, ROWS, COLS, '*');
- Set_zhadan(mine, ROW, COL, EASY_COUNT);//布置雷
- //排查雷
- int win = 0;
- while (win<ROW*COL-EASY_COUNT)
- {
- Display_qipan(show, ROWS, COLS);
- cout << "请输入要排查的坐标(x,y)>";
- int x = 0, y = 0;
- cin >> x >> y;
- if (x>=1 && x<=ROW && y>=1 && y<=COL)//坐标合法
- {
- if (mine[x][y] == '#')
- {
- cout << "哈哈哈,你被炸死了!" << endl;
- cout << "给你看看地雷的位置,别让你死不瞑目!" << endl;
- Display_qipan(mine, ROWS, COLS);//让你死得瞑目
- cout << "扫雷失败,游戏结束!" << endl;
- break;
- }
- else if (show[x][y] != '*')
- {
- system("cls");
- cout << "该坐标已排查,请勿重复排查!" << endl;
- }
- else{
- system("cls");
- Fine_zhadan3(mine, show, ROW, COL, x, y, &win);
- cout << "排雷成功,请继续扫雷!游戏进度: " << win<<'/' << ROW * COL - EASY_COUNT << endl;
- }
- }
- else
- {
- system("cls");
- cout << "输入坐标不合法,请重新输入!" << endl;
- }
- }
- if (win == ROW * COL - EASY_COUNT)
- {
- cout << "扫雷成功,游戏胜利!" << endl;
- Display_qipan(mine, ROWS, COLS);//让你死得瞑目
- }
- }
-
- void caidan()
- {
- cout << "**************************************" << endl;
- cout << "******* 1.play( 9 * 9 ) ********" << endl;
- cout << "******* 2.play(16 * 16) ********" << endl;
- cout << "******* 3.play(30 * 30) ********" << endl;
- cout << "******* 0.exit ********" << endl;
- cout << "**************************************" << endl;
- }
- void test01()
- {
- int input = 0;
- do
- {
- srand((unsigned int)time(NULL));
- caidan();
- cout << "请选择:>";
- cin >> input;
- switch (input)
- {
- case 3:
- system("cls");
- cout << "扫雷游戏开始:" << endl;
- game(18,32,16,30,99);
- break;
- case 2:
- system("cls");
- cout << "扫雷游戏开始:" << endl;
- game(18,18,16,16,40);
- break;
- case 1:
- system("cls");
- cout << "扫雷游戏开始:" << endl;
- game(11,11,9,9,10);
- break;
- case 0:
- cout << "游戏已退出!" << endl;
- break;
- default:
- cout << "选择错误,请重新选择>" << endl;
- break;
- }
- } while (input);
- }
- int main() {
- test01();
- return 0;
- }
-
game.cpp
- #include "game.h"
- void Init_qipan(char board[MAXSIZE][MAXSIZE], int row, int col, char set)
- {
- int i = 0;
- for (i = 0; i < row; i++)
- {
- int j = 0;
- for (j = 0; j < col; j++)
- {
- board[i][j] = set;
- }
-
- }
- }
-
- void Display_qipan(char board[MAXSIZE][MAXSIZE], int rows, int cols)
- {
- int i = 0;
- cout << "-------------扫雷-------------" << endl;
- for (i = 0; i < rows - 1; i++)
- {
- int j = 0;
- for (j = 0; j < cols - 1; j++)
- {
- if (i == 0 || j == 0)
- {
- cout <<' ' << "\033[36;1m" << (i + j)%10 << "\033[0m" << ' ';
- }
- else if (board[i][j] == '4')
- {
- cout << ' ' << "\033[31;1m" << board[i][j] << "\033[0m" << ' ';
- }
- else if (board[i][j] == '3')
- {
- cout << ' ' << "\033[34;1m" << board[i][j] << "\033[0m" << ' ';
- }
- else if (board[i][j] == '2')
- {
- cout << ' ' << "\033[33;1m" << board[i][j] << "\033[0m" << ' ';
- }
- else if (board[i][j] == '1')
- {
- cout << ' ' << "\033[32;1m" << board[i][j] << "\033[0m" << ' ';
- }
- else if (board[i][j] == '#')
- {
- cout << ' ' << "\033[31;1m" << board[i][j] << "\033[0m" << ' ';
- }
- else
- {
- cout << ' ' << board[i][j] << ' ';
- }
- }
- cout << endl;
- }
- }
- // 布置雷
- void Set_zhadan(char board[MAXSIZE][MAXSIZE], int row, int col,int EASY_COUNT)
- {
- //生成随机坐标
- int count = EASY_COUNT;
- int x = 0, y = 0;
- while (count)
- {
- x = rand() % row + 1;
- y = rand() % col + 1;
- if (board[x][y] == '0')
- {
- board[x][y] = '#';
- count--;
- }
- }
-
- }
- //
- int get_num(char mine[MAXSIZE][MAXSIZE], int x, int y)
- {
- int i = x - 1;//+3
- int j = y - 1;//+3
- int count = 0;
- for (i = x - 1; i < x - 1 + 3; i++)
- {
- for (j = y - 1; j < y - 1 + 3; j++)
- {
- if (mine[i][j] == '#')
- {
- count++;
- }
- }
- }
- return count;
- }
-
- int Fine_zhadan3(char mine[MAXSIZE][MAXSIZE], char show[MAXSIZE][MAXSIZE], int row, int col, int x, int y,int *win)//它的作用是只能排一次雷。
- {
- if (x<1 || x>row || y<1 || y>col)
- {
- return -1;//传入坐标非法(非法原因:数组越界)可以在函数外实现,该处仅用于维护递归,防止递归过度!!!,
- }
- if (show[x][y] != '*')
- {
- return 1;//传入坐标非法(非法原因:游戏规则不允许在已经成功排雷的地方继续排雷)
- }
- if (mine[x][y] != '#')//如果不是雷,就显示周围雷的个数,表示排雷成功!或者显示空格自动递归排雷
- {
- int num = get_num(mine, x, y);
- //排雷数加一
- (*win) += 1;
- if (num == 0)//如果周围没有雷,那么就要展开一片
- {
- show[x][y] = ' ';
- int i = x - 1;//+3
- int j = y - 1;//+3
- int count = 0;
- for (i = x - 1; i < x - 1 + 3; i++)
- {
- for (j = y - 1; j < y - 1 + 3; j++)
- {
- Fine_zhadan3(mine, show, row, col, i, j, win);
- }
- }
- }
- else
- {
- show[x][y] = '0' + num;
- return 1;//排雷成功,游戏继续,继续输入坐标
- }
- //Display_qipan(show, ROWS, COLS);
- }
- }
-
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。