C++实现扫雷小游戏(控制台)

 更新时间:2022年05月07日 11:45:34   作者:张小桐  
这篇文章主要为大家详细介绍了C++实现扫雷小游戏,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C++实现扫雷小游戏的具体代码,供大家参考,具体内容如下

1.问题描述

用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.运行结果部分截图

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • C++解析obj模型文件方法介绍

    C++解析obj模型文件方法介绍

    由于本人打算使用Assimp来加载模型,这里记录一下tinyobjloader库的使用。之前也研究过fbxsdk,除了骨骼动画暂未读取外,代码自认为还算可靠
    2022-09-09
  • 详细分析C++ 数据封装和数据抽象

    详细分析C++ 数据封装和数据抽象

    这篇文章主要介绍了C++ 数据封装和数据抽象的的相关资料,文中代码非常详细,帮助大家更好的理解和学习,感兴趣的朋友可以了解下
    2020-06-06
  • C++链表类的封装详情介绍

    C++链表类的封装详情介绍

    这篇文章主要介绍了C++链表类的封装,文章基于C++的相关资料展开主题的详细内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-04-04
  • C++实现航空订票程序

    C++实现航空订票程序

    这篇文章主要为大家详细介绍了C++实现航空订票程序,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-01-01
  • Linux C 时间函数应用

    Linux C 时间函数应用

    本文是关于Linux C时间函数 time_t struct tm 进行了详细的分析介绍并有应用实例,希望能帮到有需要的同学
    2016-07-07
  • 基于C++自动化编译工具的使用详解

    基于C++自动化编译工具的使用详解

    本篇文章是对C++中自动化编译工具的使用进行了详细的分析介绍,需要的朋友参考下
    2013-05-05
  • c++连接mysql5.6的出错问题总结

    c++连接mysql5.6的出错问题总结

    下面小编就为大家带来一篇c++连接mysql5.6的出错问题总结。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧,祝大家游戏愉快哦
    2016-12-12
  • C++设计模式之迭代器模式

    C++设计模式之迭代器模式

    这篇文章主要介绍了C++设计模式之迭代器模式,本文讲解了什么是迭代器模式、迭代器模式的代码实例等内容,需要的朋友可以参考下
    2014-10-10
  • 关于C语言操作符的那些事(超级全)

    关于C语言操作符的那些事(超级全)

    这篇文章主要给大家介绍了关于C语言操作符的那些事儿,c语言的操作符有很多,包括算术操作符、移位操作符、位操作符、赋值操作符、单目操作符、关系操作符、逻辑操作符、条件操作符、逗号表达式、下标引用、函数调用和结构成员,需要的朋友可以参考下
    2021-08-08
  • c++ 入门——浅析构造函数和析构函数

    c++ 入门——浅析构造函数和析构函数

    这篇文章主要介绍了c++ 浅析构造函数和析构函数的相关资料,帮助大家入门c++ 编程,感兴趣的朋友可以了解下
    2020-08-08

最新评论