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

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

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

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

1.問題描述

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

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

2.設(shè)計思路

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

其他詳細內(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' << "選項" << '\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]++;
?? ??? ??? ?}
?? ??? ?}
?? ?}
?? ??? ?
}
//生成數(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.運行結(jié)果部分截圖

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

相關(guān)文章

最新評論

吐鲁番市| 绥中县| 山东省| 凤庆县| 隆昌县| 高邮市| 陇川县| 淮北市| 探索| 新郑市| 通州市| 革吉县| 张掖市| 和龙市| 遵义县| 翼城县| 五莲县| 油尖旺区| 湖州市| 临夏市| 清苑县| 海宁市| 甘肃省| 梅河口市| 临武县| 江北区| 从江县| 曲周县| 胶州市| 新乡市| 龙州县| 宣威市| 遂宁市| 宝兴县| 永州市| 淮滨县| 兴业县| 汨罗市| 东明县| 喀喇沁旗| 鱼台县|