c++趣味入門——掃雷遊戲

陣列中的地雷、數字、空白格與標記格設計

用一個二維矩陣代表陣列:

元素值統一初始化爲-1;
隨機產生10組座標,每組座標對應地雷元素,共10個地雷,並均賦值爲9,即地雷的元素值爲9;
元素值爲n(-1<n<9)代表周圍有n個地雷(遍歷元素周圍8個元素如果檢測是地雷counter++,遍歷完後返回counter);
元素值爲0代表周圍沒有地雷,挖到該元素時自動顯示周圍8個元素,如果周圍又有個0值元素,遞歸調用自動顯示周圍8個元素的方法;
如果確認某一元素是地雷,則對其標記並將元素值設爲-2,這樣挖掘某一元素時判斷是地雷的條件爲(元素值爲9或元素值爲-2),當然沒有考慮誤標記的情況,但一般人不會將標記了的元素再挖掘一次,在本程序中如果你挖掘標記爲地雷的元素,遊戲就結束了,你得爲自己的智商付出代價;
當然標記也可以取消,這樣就把這個元素值初始化爲-1。

流程設計

1.初始化陣列。
2.輸入座標點。
3.選擇:挖掘,標記,取消標記,重啓,退出遊戲。
如果選了挖掘,判斷座標點是地雷則遊戲結束,是數字則顯示數字並回到2,是空格則顯示周圍8個元素值並直到連帶的空格顯示完了回到2;
如果選了標記,將該點的元素值設爲-2並回到2;
如果選了取消標記,初始化該點,回到2;
如果選了重啓,則初始化陣列,回到2;
如果選了退出遊戲,則exit。
4.挖掘完所有非地雷點後,遊戲勝利,選擇是否再來一局,是則回到1,否則exit

面向對象設計思想

創建一個bombsweep類,存儲幾個方法:

calculate:統計以(x,y)爲中心周圍8個點的地雷數目。

game:模擬遊戲過程。

print:打印陣列。

check:檢查是否滿足勝利條件。

在main函數中,在需要的時候根據bombsweep類創建bs對象,調用bs裏面的相關方法。

程序代碼

參考了點擊打開鏈接 的代碼並進行了優化,謝謝原作者分享。

<span style="font-weight: normal;"><span style="font-size:14px;">#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()
</span></span>


發佈了39 篇原創文章 · 獲贊 17 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章