這篇“C++如何實(shí)現(xiàn)趣味掃雷游戲”文章的知識(shí)點(diǎn)大部分人都不太理解,所以小編給大家總結(jié)了以下內(nèi)容,內(nèi)容詳細(xì),步驟清晰,具有一定的借鑒價(jià)值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來(lái)看看這篇“C++如何實(shí)現(xiàn)趣味掃雷游戲”文章吧。

創(chuàng)新互聯(lián)建站2013年開(kāi)創(chuàng)至今,先為磁縣等服務(wù)建站,磁縣等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為磁縣企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問(wèn)題。
1.初始化陣列。
2.輸入坐標(biāo)點(diǎn)。
3.選擇:挖掘,標(biāo)記,取消標(biāo)記,重啟,退出游戲。
如果選了挖掘,判斷坐標(biāo)點(diǎn)是地雷則游戲結(jié)束,是數(shù)字則顯示數(shù)字并回到2,是空格則顯示周圍8個(gè)元素值并直到連帶的空格顯示完了回到2;
如果選了標(biāo)記,將該點(diǎn)的元素值設(shè)為-2并回到2;
如果選了取消標(biāo)記,初始化該點(diǎn),回到2;
如果選了重啟,則初始化陣列,回到2;
如果選了退出游戲,則exit。
4.挖掘完所有非地雷點(diǎn)后,游戲勝利,選擇是否再來(lái)一局,是則回到1,否則exit
創(chuàng)建一個(gè)bombsweep類,存儲(chǔ)幾個(gè)方法:
calculate:統(tǒng)計(jì)以(x,y)為中心周圍8個(gè)點(diǎn)的地雷數(shù)目。
game:模擬游戲過(guò)程。
print:打印陣列。
check:檢查是否滿足勝利條件。
在main函數(shù)中,在需要的時(shí)候根據(jù)bombsweep類創(chuàng)建bs對(duì)象,調(diào)用bs里面的相關(guān)方法。
程序代碼
#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()以上就是關(guān)于“C++如何實(shí)現(xiàn)趣味掃雷游戲”這篇文章的內(nèi)容,相信大家都有了一定的了解,希望小編分享的內(nèi)容對(duì)大家有幫助,若想了解更多相關(guān)的知識(shí)內(nèi)容,請(qǐng)關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞標(biāo)題:C++如何實(shí)現(xiàn)趣味掃雷游戲
當(dāng)前地址:http://www.chinadenli.net/article8/gcscip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動(dòng)網(wǎng)站建設(shè)、網(wǎng)站收錄、服務(wù)器托管、用戶體驗(yàn)、Google、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)