C++实现趣味扫雷游戏

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

流程设计

1.初始化阵列。
2.输入坐标点。
3.选择:挖掘,标记,取消标记,重启,退出游戏。
如果选了挖掘,判断坐标点是地雷则游戏结束,是数字则显示数字并回到2,是空格则显示周围8个元素值并直到连带的空格显示完了回到2;
如果选了标记,将该点的元素值设为-2并回到2;
如果选了取消标记,初始化该点,回到2;
如果选了重启,则初始化阵列,回到2;
如果选了退出游戏,则exit。
4.挖掘完所有非地雷点后,游戏胜利,选择是否再来一局,是则回到1,否则exit

面向对象设计思想

创建一个bombsweep类,存储几个方法:

calculate:统计以(x,y)为中心周围8个点的地雷数目。

game:模拟游戏过程。

print:打印阵列。

check:检查是否满足胜利条件。

在main函数中,在需要的时候根据bombsweep类创建bs对象,调用bs里面的相关方法。

程序代码

#include <ctime>
#include <cstdlib>
#include <iostream>
#include <cstring>
using namespace std;
int map[12][12];    // ??????????,????????????1
int derection[3] = { 0, 1, -1 };  //????????8?????
int type;
class bombsweep
{
public:
    int calculate ( int x, int y )
    {
        int counter = 0;
        for ( int i = 0; i < 3; i++ )
            for ( int j = 0; j < 3; j++ )
                if ( map[ x+derection[i]][ y+derection[j] ] == 9 )
                    counter++;                 // ???(x,y)?????8???????
        return counter;
    }
    void game ( int x, int y )
    {
        if ( calculate ( x, y ) == 0 )
        {
            map[x][y] = 0;
            for ( int i = 0; i < 3; i++ )
            {
                // ???????,?????????
                for ( int j = 0; j < 3; j++ )
                    if ( x+derection[i] <= 9 && y+derection[j] <= 9 && x+derection[i] >= 1 && y+derection[j] >= 1
                            && !( derection[i] == 0 && derection[j] == 0 ) &&  map[x+derection[i]][y+derection[j]] == -1 )
                        game( x+derection[i], y+derection[j] ); // ???????????????0,????????!
            }                                                      //????????.???????????
        }
        else
            map[x][y] = calculate(x,y);
    }

    void print (int x,int y)
    {
        cout << "  |";
        for (int i=1; i<10; i++)
            cout << " " << i;
        cout << endl;
        cout << "__|__________________Y" ;
        cout << endl;
        for ( int i = 1; i < 10; i++ )
        {
            cout << i << " |";
            for ( int j = 1; j < 10; j++ )
            {
                if(map[i][j]==-2)
                    cout <<" B";
                else if ( map[i][j] == -1 || map[i][j] == 9 )
                    cout << " #";
                else
                    cout << " "<< map[i][j];

            }
            cout << "\n";
        }
        cout << "  X\n";
    }
    bool check ()
    {
        int counter = 0;
        for ( int i = 1; i < 10; i++ )
            for ( int j = 1; j < 10; j++ )
                if ( map[i][j] != -1 )
                    counter++;
        if ( counter == 10 )
            return true;
        else
            return false;
    }
};

int main ()
{

    int i, j, x, y;
    char ch;
    srand ( time ( 0 ) );

    do
    {
        //?????
        memset ( map, -1, sizeof(map) );

        for ( i = 0; i < 10;  )
        {
            x = rand()%9 + 1;
            y = rand()%9 + 1;
            if ( map[x][y] != 9 )
            {
                map[x][y] = 9;
                i++;
            }
        }

        cout << "  |";
        for (i=1; i<10; i++)
            cout << " " << i;
        cout << endl;
        cout << "__|__________________Y" ;
        cout << endl;
        for ( i = 1; i < 10; i++ )
        {
            cout << i << " |";
            for ( j = 1; j < 10; j++ )
                cout << " "<< "#";
            cout << "\n";
        }
        cout << "  X\n";
        cout << "Please input location x,press enter then input location y: \n";
        while ( cin >> x >> y )
        {
            cout << "Please select:1.dig, 2.sign, 3.cancel sign, 4.restart, 5.exit: \n";
            cin >>type;
            switch(type)
            {
            case 1:
            {
                if ( map[x][y] == 9 || map[x][y]==-2)
                {
                    cout << "YOU LOSE!" << endl;
                    cout << "  |";
                    for (i=1; i<10; i++)
                        cout << " " << i;
                    cout << endl;
                    cout << "__|__________________Y"<<endl ;
                    for ( i = 1; i < 10; i++ )
                    {
                        cout << i << " |";
                        for ( j = 1; j < 10; j++ )
                        {
                            if ( map[i][j] == 9 || map[i][j]==-2)
                                cout << " @";
                            else
                                cout << " #";
                        }
                        cout << "\n";
                    }
                    cout << "  X\n";
                    exit(0);
                }

                bombsweep bs;
                bs.game(x,y);
                bs.print(x,y);
                cout << "Please input location x,press enter then input location y: \n";

                if ( bs.check())
                {
                    cout << "YOU WIN" << endl;
                    break;
                }
                continue;
            }

            case 2:
            {
                bombsweep bs;
                map[x][y]=-2;
                bs.print(x,y);
                cout << "Please input location x,press enter then input location y: \n";
                continue;
            }

            case 3:
            {
                bombsweep bs;
                map[x][y]=-1;
                bs.print(x,y);
                cout << "Please input location x,press enter then input location y: \n";
                continue;
            }

            case 4:
            {
                memset ( map, -1, sizeof(map) );

                for ( i = 0; i < 10;  )
                {
                    x = rand()%9 + 1;
                    y = rand()%9 + 1;
                    if ( map[x][y] != 9 )
                    {
                        map[x][y] = 9;
                        i++;
                    }
                }

                cout << "  |";
                for (i=1; i<10; i++)
                    cout << " " << i;
                cout << endl;
                cout << "__|__________________Y" ;
                cout << endl;
                for ( i = 1; i < 10; i++ )
                {
                    cout << i << " |";
                    for ( j = 1; j < 10; j++ )
                        cout << " "<< "#";
                    cout << "\n";
                }
                cout << "  X\n";
                cout << "Please input location x,press enter then input location y: \n";
                continue;
            }
            case 5:
                cout << "Game Ended\n";
                exit(0);
                break;
            default:
                cout<< "Invalid input, try again: \n";
                continue;
            }//end switch

        }//end while(cin >> x >>y)
        cout << "Do you want to play again?(y/n):" << endl;
        cin >> ch;
    }//end do
    while ( ch == 'y' );
    return 0;
}//end main()

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

(0)

相关推荐

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

    本文为大家分享了C++实现扫雷小游戏的具体代码,供大家参考,具体内容如下 程序功能: 提供三种模式:初级.中级.高级 操作模式:wsad控制光标移动,空格键打开方块 提供扫雷地图的类 map.h #ifndef MAP_H_ #define MAP_H_ #define MAX_LENGTH 32 //可以提供的地图最大长度 #define MAX_WIDTH 18 //可以提供的地图最大宽度 #define UP_EDGE 1 //上边界 #define DOWN_EDGE _wid //下边

  • 利用c++和easyx图形库做一个低配版扫雷游戏

    游戏界面 由于这个游戏是我抱着玩一玩的心态做出来的,所以没有过多的去设计界面,也没有去找游戏的资源(图片.游戏音效等).仅使用了不同颜色的方块来表示游戏中方块的状态和种类.(绿色为初始状态(未翻转的状态),黄色为翻转后的背景颜色,蓝色表示已插旗的方块,红色代表地雷) 图1 游戏主菜单界面 图二 模式一的游戏界面(20*20 40个雷) 图三 模式二的游戏界面(10*10 20个雷) 图四 游戏成功界面 图五 游戏失败界面 2.全部代码 #include<graphics.h> #include

  • C++扫雷游戏的简单制作

    本文实例为大家分享了C++实现扫雷游戏的具体代码,供大家参考,具体内容如下 #ifndef SAOLEI_H #define SAOLEI_H class Block { friend class Saoleigame; public: Block(); bool isShown(); void setnum(int); int getnum(); bool isbomb(); protected: int num; bool flag_show; int x; int y; }; class

  • C++实现简单扫雷游戏

    扫雷是一个经典的电脑小游戏,用C++来编一下,效果自己试一下 #include<stdio.h> #include<Windows.h> #define YELLOW FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY #define CYAN FOREGROUND_BLUE | FOREGROUND_GREEN | FOREGROUND_INTENSITY #define ORANGE FOREGROUND_RED |

  • C++学习心得之扫雷游戏

    本文实例为大家分享了C++实现扫雷游戏的具体代码,供大家参考,具体内容如下 一.序言 创建一个9*9有10个雷的扫雷游戏 文章的顺序是按照我当时的编程顺序写的,顺便写下我当初的一点思路,总的代码在文章最后,前面的都是分散的函数,有需要的朋友直接复制最后的 二.创建 创建一个头文件,一个放游戏的程序,一个放运行测试的程序 #define _CRT_SECURE_NO_WARNINGS 1 #include<stdlib.h>//生成随机数 #include<stdio.h> #inc

  • C++基于EasyX实现简单扫雷游戏

    本文实例为大家分享了C++ EasyX实现简单扫雷游戏的具体代码,供大家参考,具体内容如下 [实现代码] #include <cmath> #include <time.h> #include <easyx.h> #include <conio.h> using namespace std; #define Size 500 //定义窗口大小 #define SquareSize 50 //定义格子大小 #define BackGroundColor LIG

  • C++实现扫雷游戏(控制台不闪屏版)

    之前写了一个C++ 的控制台扫雷小游戏,但由于过度使用system("cls")刷屏,导致闪屏,因此重写了一个改善的不闪屏版本,并把逻辑重新捋了一遍. map.h #ifndef MAP_H_ #define MAP_H_ #define MAX_WID 18 #define MAX_LEN 32 #define UP_EDGE 1 //上边界 #define LEFT_EDGE 1 //左边界 #define RIGHT_EDGE _len //右边界 #define DOWN_ED

  • C++实现扫雷经典小游戏

    用C++复现经典扫雷,供大家参考,具体内容如下 主要是dfs实现打开一片的操作,数字带有颜色,很真实. windows扫雷中鼠标左右键同时按也实现了,即试探. 先上图,详见下面代码: 代码中有详细注释,编译无任何错误警告. Ps.有bug请评论指出,谢谢啦~ 另外我觉得代码比较臃肿,有什么可以优化的也请提出~ #include<cstdio> #include<cstring> #include<algorithm> #include<conio.h> #i

  • C++实现一个扫雷小游戏

    本文实例为大家分享了C++实现扫雷小游戏的具体代码,供大家参考,具体内容如下 目前的版本是0.98版本,可以提出增加新功能意见哦 代码如下: #include<bits/stdc++.h> #include<windows.h> using namespace std; long long int c,dev,m,k,cnt,d,e,jie=10,z,abc,n,b[1000][1000],a[1000][1000],cc,cd,ce,def; //c是随机行,k是随机列 bool

  • C++实现简单的扫雷游戏(控制台版)

    C++新手的代码,请各位多包涵. 用C++写的一个简单的控制台版扫雷游戏.玩家通过输入方块的坐标来翻开方块. 只是一个雏形,能够让玩家执行翻开方块的操作并且判断输赢,还未添加标记方块.游戏菜单.记录游戏时间.重新开一局等等的功能. 玩家输入坐标的方式来翻开方块只适用于小型的"雷区",若"雷区"大了,用坐标会变得很不方便. 代码片段扫雷V1.1 #include<stdio.h> #include<Windows.h> #define YELL

随机推荐