C++實(shí)現(xiàn)趣味掃雷游戲
本文實(shí)例為大家分享了C++實(shí)現(xiàn)趣味掃雷游戲的具體代碼,供大家參考,具體內(nèi)容如下
流程設(shè)計(jì)
1.初始化陣列。
2.輸入坐標(biāo)點(diǎn)。
3.選擇:挖掘,標(biāo)記,取消標(biāo)記,重啟,退出游戲。
如果選了挖掘,判斷坐標(biāo)點(diǎn)是地雷則游戲結(jié)束,是數(shù)字則顯示數(shù)字并回到2,是空格則顯示周?chē)?個(gè)元素值并直到連帶的空格顯示完了回到2;
如果選了標(biāo)記,將該點(diǎn)的元素值設(shè)為-2并回到2;
如果選了取消標(biāo)記,初始化該點(diǎn),回到2;
如果選了重啟,則初始化陣列,回到2;
如果選了退出游戲,則exit。
4.挖掘完所有非地雷點(diǎn)后,游戲勝利,選擇是否再來(lái)一局,是則回到1,否則exit
面向?qū)ο笤O(shè)計(jì)思想
創(chuàng)建一個(gè)bombsweep類(lèi),存儲(chǔ)幾個(gè)方法:
calculate:統(tǒng)計(jì)以(x,y)為中心周?chē)?個(gè)點(diǎn)的地雷數(shù)目。
game:模擬游戲過(guò)程。
print:打印陣列。
check:檢查是否滿(mǎn)足勝利條件。
在main函數(shù)中,在需要的時(shí)候根據(jù)bombsweep類(lèi)創(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()
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
新手向超詳細(xì)的C語(yǔ)言實(shí)現(xiàn)動(dòng)態(tài)順序表
本文主要介紹了C語(yǔ)言實(shí)現(xiàn)動(dòng)態(tài)順序表,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-09-09
C和C++如何實(shí)現(xiàn)互相調(diào)用詳解
在學(xué)習(xí)c++中用到一些古老的c語(yǔ)言庫(kù)時(shí),在工作中我們經(jīng)常要使用C和C++混合編程,下面這篇文章主要給大家介紹了關(guān)于C和C++如何實(shí)現(xiàn)互相調(diào)用的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-01-01
C++實(shí)現(xiàn)對(duì)RGB圖片進(jìn)行編碼的示例代碼
這篇文章主要為大家詳細(xì)介紹了如何利用得到的RGB信息重新對(duì)RGB圖片進(jìn)行編碼,以及對(duì)其他圖片如BMP所得到的RGB信息進(jìn)行編碼從而得到*.jpg文件,感興趣的可以了解一下2023-05-05
Qt中QTextEdit和QPlainTextEdit控件的實(shí)現(xiàn)
本文主要介紹了Qt中QTextEdit和QPlainTextEdit控件的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2025-04-04
Matlab實(shí)現(xiàn)四種HSV色輪圖繪制的示例代碼
色輪圖就是色彩相位圖,它完整表現(xiàn)了色相環(huán)360度的全部顏色。本文將利用Matlab語(yǔ)言繪制四種不同的HSV色輪圖,感興趣的可以動(dòng)手嘗試一下2022-07-07
C 與 C++ 接口函數(shù)相互調(diào)用的實(shí)現(xiàn)
這篇文章主要介紹了C 與 C++ 接口函數(shù)相互調(diào)用的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-03-03
c語(yǔ)言實(shí)現(xiàn)冒泡排序、希爾排序等多種算法示例
c語(yǔ)言實(shí)現(xiàn)插入排序、冒泡排序、選擇排序、快速排序、堆排序、歸并排序、希爾排序示例,需要的朋友可以參考下2014-04-04

