C++扫雷小游戏
张小桐 人气:21.问题描述
用c++写一个扫雷小游戏,扫雷大家都玩过吧,先任意点一个方格,没有爆炸时,会出现一个数字,这个数字是以它为中心的9个格子内所有雷的个数。一般围在一堆数字中间的有可能是雷,你在你认为是雷的那里右击,就可以把它设定为雷,然后在数字区用鼠标左右键双击,可以打开非雷区,所有雷被标记后,就赢了。
今天我们写的程序需要能实现以下几个功能
(1).输入坐标打开一个格子,此格子若是雷则游戏结束,若不是则显示周围雷的个数。
(2).输入坐标为格子插上棋子和取消插旗子。
2.设计思路
(1)创建两个数组,一个是开发者数组,一个是玩家数组。生成两个界面,开发者界面显示雷和数字,玩家界面显示问号和数字。
(2)初始化两个雷阵,然后用随机数布雷。
(3)开始游戏,点到不是雷的地方将周围无雷的地方展开,如果点到雷游戏结束。
其他详细内容见代码
3.上代码
#include "pch.h" #include <iostream> #include <stdlib.h> #include<cstdlib> #include<ctime> using namespace std; int shuzu1[12][12]; char show[12][12]; void wjm() { cout << " 1 2 3 4 5 6 7 8 9 10 " << endl << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[1][j] << " |"; } cout << " 1" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[2][j] << " |"; } cout << " 2" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[3][j] << " |"; } cout << " 3" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[4][j] << " |"; } cout << " 4" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[5][j] << " |"; } cout << " 5" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[6][j] << " |"; } cout << " 6" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[7][j] << " |"; } cout << " 7" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[8][j] << " |"; } cout << " 8" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[9][j] << " |"; } cout << " 9" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << shuzu1[10][j] << " |"; } cout << " 10" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; } //开发者界面 void first()//初始化 { for (int i = 0; i < 12; i++) { for (int j = 0; j < 12; j++) { shuzu1[i][j] = 0;//开发者数组 } } for (int i = 0; i < 12; i++) { for (int j = 0; j <12; j++) { show[i][j] = '?';//玩家数组 } } } //初始化两个雷阵 void jm()//界面 { cout << " 1 2 3 4 5 6 7 8 9 10 " << endl << endl; for (int j = 1; j < 11; j++) { cout << " " << show[1][j] << " |"; } cout << " 1" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << show[2][j] << " |"; } cout << " 2" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << show[3][j] << " |"; } cout << " 3" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << show[4][j] << " |"; } cout << " 4" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << show[5][j] << " |"; } cout << " 5" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << show[6][j] << " |"; } cout << " 6" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << show[7][j] << " |"; } cout << " 7" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << show[8][j] << " |"; } cout << " 8" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << show[9][j] << " |"; } cout << " 9" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; for (int j = 1; j < 11; j++) { cout << " " << show[10][j] << " |"; } cout << " 10" << endl << "-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+" << endl; cout << '\n' << "选项" << '\n' << "提示:输入横坐标后回车再输入纵坐标\n" << "1-点击(x,y)" << '\n' << "2-在(x,y)插旗子" << '\n' << "3-取消插旗子(x,y)" << '\n' << "4-老子不玩了" << endl; } //玩家界面 void bulei() { srand(time(NULL)); for (int a=0; a <10; a++)//生成10个雷 { int m = rand() % 10 + 1; int n = rand() % 10 + 1; if (shuzu1[m][n] != 9) { shuzu1[m][n] = 9; } } } //布雷 void number() { int count = 0; for (int x = 1; x < 11; x++) { for (int y = 1; y < 11; y++) { if (shuzu1[x][y] == 9) { if(shuzu1[x - 1][y - 1]!=9) shuzu1[x - 1][y-1]++; if(shuzu1[x - 1][y]!=9) shuzu1[x - 1][y]++; if(shuzu1[x - 1][y + 1]!=9) shuzu1[x - 1][y + 1]++; if(shuzu1[x][y - 1]!=9) shuzu1[x][y - 1]++; if (shuzu1[x][y + 1] != 9) shuzu1[x][y + 1]++; if (shuzu1[x + 1][y - 1] != 9) shuzu1[x + 1][y - 1]++; if (shuzu1[x + 1][y] != 9) shuzu1[x + 1][y]++; if (shuzu1[x + 1][y + 1] != 9) shuzu1[x + 1][y + 1]++; } } } } //生成数字 void unfold(int x,int y) { if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { if (shuzu1[x][y] == 0) { show[x][y] = ' '; if (show[x][y + 1] == '?') unfold(x, y + 1); if (show[x][y - 1] == '?') unfold(x, y - 1); if (show[x + 1][y] == '?') unfold(x + 1, y); if (show[x - 1][y] == '?') unfold(x - 1, y); } if (shuzu1[x][y] != 0 && shuzu1[x][y] != 9) { show[x][y] = shuzu1[x][y] + '0'; } } } //无雷展开 void flag(int x, int y) { show[x][y] = 'F'; jm(); } //插旗子 void unflag(int x, int y) { if (show[x][y] == 'F') { show[x][y] = '?'; jm(); } else { cout << "错误"; } } //取消插旗子 void start(int x,int y) { if (shuzu1[x][y] == 9) { cout << "你输了"; exit(0); } if (shuzu1[x][y] != 9 && shuzu1[x][y] != 0) { show[x][y] = shuzu1[x][y]+'0'; jm(); } if (shuzu1[x][y] == 0) { unfold(x, y); jm(); } } //展开格子 void end() { int count = 0; for (int i = 1; i <= 10; i++) { for (int j = 1; j <= 10; j++) { if (show[i][j] == '?'||show[i][j]=='F') { count++; } } } if (count == 10) { cout << "你赢了"; exit(0); } } //结束游戏 int main() { int x = 5; int y = 8; int z; first(); bulei(); number(); jm(); for (;;) { cin >> z; switch (z) { case 1: { cin >> x >> y; if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { start(x, y); } else { cout << "错误"; break; } }break; case 2: { cin >> x >> y; if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { flag(x, y); } else { cout << "错误"; } }break; case 3: { cin >> x >> y; if (x >= 1 && x <= 10 && y >= 1 && y <= 10) { unflag(x, y); } else { cout << "错误"; } }break; case 4: { exit(0); }break; } end(); } }
4.运行结果部分截图
加载全部内容