踩地雷

一、公用變數宣告

//定義地雷陣列: 1表示埋有地雷、0則無。

int arr[10][10]={

{0,0,0,0,0,0,0,0,0,0},

{1,0,1,0,1,0,1,0,1,1},

{0,0,0,0,0,0,0,0,0,0},

{0,0,1,0,1,0,1,0,1,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0}

};

int* ap=arr[0];//取得二維陣列arr開頭位址 

//定義周圍地雷數量: -1表示未選擇,>=0數字代表周圍地雷數、9代表本身是地雷。

int num[10][10]={

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1},

{-1,-1,-1,-1,-1,-1,-1,-1,-1,-1}

};

int* ap1=num[0];//取得二維陣列num開頭位址 

//定義已選位置: 0未選取、1已選取。

int mark[10][10]={

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

{0,0,0,0,0,0,0,0,0,0},

};

int* ap2=mark[0];//取得二維陣列mark開頭位址 

//

COORD cursorPos;

int s, row, col; //s:動作選擇、row:列座標、col:欄座標 

bool hit = false;

二、自訂函式預告

void init();//初始化

void clearScreen();//清除螢幕(全部填入空白)

void Check_Show(int*,int*); //檢查並顯示地雷區 

void input(); //輸入動作與座標 

void showArr(int*); //顯示陣列(測試用)

int  calMine(int*,int,int); //計算周圍地雷數

三、自訂游標相關操作函式

//獲取目前游標位置 

COORD getCurrentCursorPosition() {

    CONSOLE_SCREEN_BUFFER_INFO csbi;

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);


    if (!GetConsoleScreenBufferInfo(hConsole, &csbi)) {

        // 獲取失敗返回 (-1, -1)

        COORD invalidCoord = { -1, -1 };

        return invalidCoord;

    }


    return csbi.dwCursorPosition;

}

//移動游標至指定位置 

void moveCursorToPosition(int x, int y) {

    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

    COORD position = { static_cast<SHORT>(x), static_cast<SHORT>(y) };

    SetConsoleCursorPosition(hConsole, position);

}

//設定文字顏色 

void SetColor(int color = 7){

  HANDLE hConsole;

  hConsole = GetStdHandle(STD_OUTPUT_HANDLE);

  SetConsoleTextAttribute(hConsole,color);

}


四、主程式

int main() {

init(); //初始化 

while (not hit){

input(); //會先移動游標 

Check_Show(ap1,ap);

}


return 0;

}

五、自訂函式