欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C++實(shí)現(xiàn)掃雷小游戲(控制臺)

 更新時間:2022年05月07日 11:45:34   作者:張小桐  
這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)掃雷小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了C++實(shí)現(xiàn)掃雷小游戲的具體代碼,供大家參考,具體內(nèi)容如下

1.問題描述

用c++寫一個掃雷小游戲,掃雷大家都玩過吧,先任意點(diǎn)一個方格,沒有爆炸時,會出現(xiàn)一個數(shù)字,這個數(shù)字是以它為中心的9個格子內(nèi)所有雷的個數(shù)。一般圍在一堆數(shù)字中間的有可能是雷,你在你認(rèn)為是雷的那里右擊,就可以把它設(shè)定為雷,然后在數(shù)字區(qū)用鼠標(biāo)左右鍵雙擊,可以打開非雷區(qū),所有雷被標(biāo)記后,就贏了。
今天我們寫的程序需要能實(shí)現(xiàn)以下幾個功能

(1).輸入坐標(biāo)打開一個格子,此格子若是雷則游戲結(jié)束,若不是則顯示周圍雷的個數(shù)。
(2).輸入坐標(biāo)為格子插上棋子和取消插旗子。

2.設(shè)計(jì)思路

(1)創(chuàng)建兩個數(shù)組,一個是開發(fā)者數(shù)組,一個是玩家數(shù)組。生成兩個界面,開發(fā)者界面顯示雷和數(shù)字,玩家界面顯示問號和數(shù)字。
(2)初始化兩個雷陣,然后用隨機(jī)數(shù)布雷。
(3)開始游戲,點(diǎn)到不是雷的地方將周圍無雷的地方展開,如果點(diǎn)到雷游戲結(jié)束。

其他詳細(xì)內(nèi)容見代碼

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;

}
//開發(fā)者界面
void first()//初始化
{
?? ?for (int i = 0; i < 12; i++)
?? ?{
?? ??? ?for (int j = 0; j < 12; j++)
?? ??? ?{
?? ??? ??? ?shuzu1[i][j] = 0;//開發(fā)者數(shù)組
?? ??? ??? ?
?? ??? ?}
?? ?}
?? ?for (int i = 0; i < 12; i++)
?? ?{
?? ??? ?for (int j = 0; j <12; j++)?
?? ??? ?{
?? ??? ??? ?show[i][j] = '?';//玩家數(shù)組
?? ??? ?}
?? ?}
}
//初始化兩個雷陣
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' << "選項(xiàng)" << '\n' << "提示:輸入橫坐標(biāo)后回車再輸入縱坐標(biāo)\n" << "1-點(diǎn)擊(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]++;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ??? ?
}
//生成數(shù)字
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);
?? ?}
?? ?
?? ?
}
//結(jié)束游戲

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.運(yùn)行結(jié)果部分截圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

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

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

    由于本人打算使用Assimp來加載模型,這里記錄一下tinyobjloader庫的使用。之前也研究過fbxsdk,除了骨骼動畫暫未讀取外,代碼自認(rèn)為還算可靠
    2022-09-09
  • 詳細(xì)分析C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象

    詳細(xì)分析C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象

    這篇文章主要介紹了C++ 數(shù)據(jù)封裝和數(shù)據(jù)抽象的的相關(guān)資料,文中代碼非常詳細(xì),幫助大家更好的理解和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • C++鏈表類的封裝詳情介紹

    C++鏈表類的封裝詳情介紹

    這篇文章主要介紹了C++鏈表類的封裝,文章基于C++的相關(guān)資料展開主題的詳細(xì)內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-04-04
  • C++實(shí)現(xiàn)航空訂票程序

    C++實(shí)現(xiàn)航空訂票程序

    這篇文章主要為大家詳細(xì)介紹了C++實(shí)現(xiàn)航空訂票程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Linux C 時間函數(shù)應(yīng)用

    Linux C 時間函數(shù)應(yīng)用

    本文是關(guān)于Linux C時間函數(shù) time_t struct tm 進(jìn)行了詳細(xì)的分析介紹并有應(yīng)用實(shí)例,希望能幫到有需要的同學(xué)
    2016-07-07
  • 基于C++自動化編譯工具的使用詳解

    基于C++自動化編譯工具的使用詳解

    本篇文章是對C++中自動化編譯工具的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
    2013-05-05
  • c++連接mysql5.6的出錯問題總結(jié)

    c++連接mysql5.6的出錯問題總結(jié)

    下面小編就為大家?guī)硪黄猚++連接mysql5.6的出錯問題總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,祝大家游戲愉快哦
    2016-12-12
  • C++設(shè)計(jì)模式之迭代器模式

    C++設(shè)計(jì)模式之迭代器模式

    這篇文章主要介紹了C++設(shè)計(jì)模式之迭代器模式,本文講解了什么是迭代器模式、迭代器模式的代碼實(shí)例等內(nèi)容,需要的朋友可以參考下
    2014-10-10
  • 關(guān)于C語言操作符的那些事(超級全)

    關(guān)于C語言操作符的那些事(超級全)

    這篇文章主要給大家介紹了關(guān)于C語言操作符的那些事兒,c語言的操作符有很多,包括算術(shù)操作符、移位操作符、位操作符、賦值操作符、單目操作符、關(guān)系操作符、邏輯操作符、條件操作符、逗號表達(dá)式、下標(biāo)引用、函數(shù)調(diào)用和結(jié)構(gòu)成員,需要的朋友可以參考下
    2021-08-08
  • c++ 入門——淺析構(gòu)造函數(shù)和析構(gòu)函數(shù)

    c++ 入門——淺析構(gòu)造函數(shù)和析構(gòu)函數(shù)

    這篇文章主要介紹了c++ 淺析構(gòu)造函數(shù)和析構(gòu)函數(shù)的相關(guān)資料,幫助大家入門c++ 編程,感興趣的朋友可以了解下
    2020-08-08

最新評論