最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

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

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

1.問(wèn)題描述

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

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

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

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

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

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ù)組
?? ??? ?}
?? ?}
}
//初始化兩個(gè)雷陣
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個(gè)雷
?? ?{
?? ??? ?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';
?? ??? ?}
?? ?}
?? ??? ?
} ? ?
//無(wú)雷展開
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 << "錯(cuò)誤";
?? ?}
}
//取消插旗子
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 << "錯(cuò)誤"; break;
?? ??? ??? ??? ??? ?
?? ??? ??? ??? ?}
?? ??? ?
?? ??? ??? ?}break;
?? ??? ??? ?case 2:
?? ??? ??? ?{
?? ??? ??? ??? ?cin >> x >> y;
?? ??? ??? ??? ?if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?flag(x, y);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cout << "錯(cuò)誤";
?? ??? ??? ??? ?}
?? ??? ??? ?}break;
?? ??? ??? ?case 3:
?? ??? ??? ?{
?? ??? ??? ??? ?cin >> x >> y;
?? ??? ??? ??? ?if (x >= 1 && x <= 10 && y >= 1 && y <= 10)
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?unflag(x, y);
?? ??? ??? ??? ?}
?? ??? ??? ??? ?else
?? ??? ??? ??? ?{
?? ??? ??? ??? ??? ?cout << "錯(cuò)誤";
?? ??? ??? ??? ?}
?? ??? ??? ?}break;
?? ??? ??? ?case 4:
?? ??? ??? ?{
?? ??? ??? ??? ?exit(0);

?? ??? ??? ?}break;
?? ??? ?}
?? ??? ?end();
?? ?}

}

4.運(yùn)行結(jié)果部分截圖

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

相關(guān)文章

最新評(píng)論

湄潭县| 嵊州市| 洛扎县| 绥化市| 天全县| 天津市| 漳浦县| 安宁市| 贵德县| 西昌市| 阳东县| 信丰县| 平远县| 鄂州市| 定南县| 唐海县| 河北区| 永川市| 北海市| 苏尼特左旗| 福海县| 池州市| 库伦旗| 西宁市| 抚州市| 桃江县| 娄烦县| 天峻县| 筠连县| 库伦旗| 泌阳县| 玉树县| 罗田县| 调兵山市| 循化| 朝阳区| 密云县| 偃师市| 玉龙| 勐海县| 永城市|