控制台版掃雷程式

來源:互聯網
上載者:User

測試平台: WIN7

工具: VC6.0 , VS2008都能編譯得過。

花了兩天時間寫的,裡面涉及的演算法大都是自己想的,所以可能有些BUG。

如果出現錯誤請提醒,鞠躬,謝謝!

#include <iostream>#include <time.h>#include <windows.h>using namespace std;#pragma comment (linker,"/subsystem:console")#define BLACK 0//空白#define MINE 100//地雷#define NOSWEEP 0//未掃過#define SWEEP 1//掃雷#define FLAG 2//標記class WinMine{public:WinMine(int iRow, int iColumn);~WinMine();public:void InitMine(int iMineMax);void SetColor();void StatisticsMine();void Map();bool OpenWhites(int x, int y, int status);void GameStart(int iMineMax);void GameOver();bool GoodOver();private:int **m_ppMine;int **m_ppSweep;bool m_bMineflag;bool m_bSweepflag;int m_row;int m_column;int m_minenum;};int main(void){system("color 0d");while (true){int level;WinMine *Mine;cout << "請輸入遊戲等級:" <<endl;cout << "1.初級" <<endl;cout << "2.中級" <<endl;cout << "3.進階" <<endl;cout << "4.退出" <<endl;cin >> level;if (level == 1){Mine = new WinMine(9,9);Mine->GameStart(10);}else if (level == 2){Mine = new WinMine(16,16);Mine->GameStart(40);}else if (level == 3){Mine = new WinMine(16,30);Mine->GameStart(70);}else if (level == 4){return 0;}else {cout << "輸入錯誤!" <<endl;continue;}delete Mine;}return 0;}WinMine::WinMine(int iRow, int iColumn){int i;//儲雷狀態m_ppMine = (int **) new int[iRow]; if (!m_ppMine) return;m_ppMine[0] = new int[iRow * iColumn]; if (!*m_ppMine) { delete[] m_ppMine; m_ppMine = NULL; return;}m_bMineflag = true;//掃雷狀態m_ppSweep = (int **) new int[iRow]; if (!m_ppSweep) return;m_ppSweep[0] = new int[iRow * iColumn]; if (!*m_ppSweep) { delete[] m_ppSweep; m_ppSweep = NULL; return;}m_bSweepflag = true;m_row = iRow; m_column = iColumn;for (i = 1; i < iRow; i++){m_ppMine[i] = m_ppMine[0] + i * iRow;}for (i = 1; i < iRow; i++){m_ppSweep[i] = m_ppSweep[0] + i * iRow;}memset(m_ppSweep[0], 0, iRow * iColumn * sizeof(int));memset(m_ppMine[0], 0, iRow * iColumn * sizeof(int));}WinMine::~WinMine(){if (m_bMineflag){if (m_ppMine[0]) delete[] m_ppMine[0];if (m_ppMine) delete[] m_ppMine;}if (m_bSweepflag){if (m_ppSweep[0]) delete[] m_ppSweep[0];if (m_ppSweep) delete[] m_ppSweep;}}//設定顏色void WinMine::SetColor(){system("color 0a");}//初始化雷數void WinMine::InitMine(int iMineMax){int x, y,num = 0;srand( (unsigned)time(NULL) );for (int i = 0; num != iMineMax; i++){x = rand()%m_row;y = rand()%m_column;if (MINE != m_ppMine[x][y]){m_ppMine[x][y] = MINE;num++;}}m_minenum = num;}//統計雷數void WinMine::StatisticsMine(){int i, j;int n, m;int num = 0; //儲存雷數//中間for ( i = 1; i < m_row - 1; i++){for ( j = 1; j < m_column - 1; j++){if ( m_ppMine[i][j] == BLACK){for (n = i - 1; n <= i + 1; n++){for (m = j - 1; m <= j + 1; m++){if ( m_ppMine[n][m] == MINE )num++;}}m_ppMine[i][j] = num;num = 0;}}}//頂邊for ( i = 1; i < m_column - 1; i++){if (m_ppMine[0][i] == BLACK){for (n = 0; n < 2; n++){for (m = i - 1; m <= i + 1; m++){if (m_ppMine[n][m] == MINE)num++;}}m_ppMine[0][i] = num;num = 0;}}//下邊for ( i = 1; i < m_column - 1; i++){if (m_ppMine[m_row - 1][i] == BLACK){for (n = m_row - 2; n < m_row; n++){for (m = i - 1; m <= i + 1; m++){if (m_ppMine[n][m] == MINE)num++;}}m_ppMine[m_row - 1][i] = num;num = 0;}}//左邊for ( i = 1; i < m_row - 1; i++ ){if (m_ppMine[i][0] == BLACK){for (n = i - 1; n <= i + 1; n++){for (m = 0; m < 2; m++)if (m_ppMine[n][m] == MINE)num++;}m_ppMine[i][0] = num;num = 0;}}//右邊for ( i = 1; i < m_row - 1; i++ ){if (m_ppMine[i][m_column - 1] == BLACK){for (n = i - 1; n <= i + 1; n++){for (m = m_column - 2; m < m_column; m++){if (m_ppMine[n][m] == MINE)num++;}}m_ppMine[i][m_column - 1] = num;num = 0;}}//左上方if (m_ppMine[0][0] != MINE){if (m_ppMine[0][1] == MINE)num++;if (m_ppMine[1][1] == MINE)num++;if (m_ppMine[1][0] == MINE)num++;m_ppMine[0][0] = num;num = 0;}//左下角if (m_ppMine[m_row - 1][0] != MINE){if (m_ppMine[m_row - 2][0] == MINE)num++;if (m_ppMine[m_row - 2][1] == MINE)num++;if (m_ppMine[m_row - 1][1] == MINE)num++;m_ppMine[m_row - 1][0] = num;num = 0;}//右上方if (m_ppMine[0][m_column - 1] != MINE){if (m_ppMine[1][m_column - 1] == MINE)num++;if (m_ppMine[1][m_column - 2] == MINE)num++;if (m_ppMine[0][m_column - 2] == MINE)num++;m_ppMine[0][m_column - 1] = num;num = 0;}//右下角if (m_ppMine[m_row - 1][m_column - 1] != MINE){if (m_ppMine[m_row - 2][m_column - 1] == MINE)num++;if (m_ppMine[m_row - 2][m_column - 2] == MINE)num++;if (m_ppMine[m_row - 1][m_column - 2] == MINE)num++;m_ppMine[m_row - 1][m_column - 1] = num;num = 0;}}//展開空白bool WinMine::OpenWhites(int row, int column, int status){if (row < 0 || row > (m_row - 1) || column < 0 || column > (m_column - 1))return true;if (status == SWEEP &&  m_ppMine[row][column] == MINE){return false;}if (status == FLAG){m_ppSweep[row][column] = FLAG;return true;}if (m_ppSweep[row][column] == NOSWEEP && m_ppMine[row][column] != MINE){if (m_ppMine[row][column] > 0){m_ppSweep[row][column] = SWEEP; return true;}else{m_ppSweep[row][column] = SWEEP;OpenWhites(row-1, column, status);OpenWhites(row-1, column-1, status);OpenWhites(row-1, column+1, status);OpenWhites(row, column-1, status);OpenWhites(row, column+1, status);OpenWhites(row+1, column, status);OpenWhites(row+1, column-1, status);OpenWhites(row+1, column+1, status);}}return true;}//地圖void WinMine::Map(){SetColor();int i, j;for ( i = 0; i < m_row; i++){for (j = 0; j < m_column; j++){if (m_ppSweep[i][j] == SWEEP){if (m_ppMine[i][j] == 0){cout << "□";}else if (m_ppMine[i][j] == 1){cout << "①";}else if (m_ppMine[i][j] == 2){cout << "②";}else if (m_ppMine[i][j] == 3){cout << "③";}else if (m_ppMine[i][j] == 4){cout << "④";}else if (m_ppMine[i][j] == 5){cout << "⑤";}else if (m_ppMine[i][j] == 6){cout << "⑥";}else if (m_ppMine[i][j] == 7){cout << "⑦";}else if (m_ppMine[i][j] == 8){cout << "⑧";}}else if (m_ppSweep[i][j] == FLAG){cout << "⊙";}elsecout << "▇";}cout << endl;}}//遊戲結束void WinMine::GameOver(){int i, j;for ( i = 0; i < m_row; i++){for (j = 0; j < m_column; j++){if (m_ppMine[i][j] == MINE)cout << "★";else cout << "□";}cout << endl;}}//檢查是否雷標記正確。bool WinMine::GoodOver(){int i, j;int num = 0;for (i = 0; i < m_row; i++){for (j = 0; j < m_column; j++){if (m_ppSweep[i][j] == FLAG && m_ppMine[i][j] == MINE)num++;}}if (num == m_minenum)return true;elsereturn false;}//開始遊戲void WinMine::GameStart(int iMineMax){int x, y;int flag;begin:memset(m_ppSweep[0], 0, m_row * m_column * sizeof(int));memset(m_ppMine[0], 0, m_row * m_column * sizeof(int));InitMine(iMineMax);StatisticsMine();while (true){system("cls");Map();cout << "請輸入要掃的地區的座標:" <<endl;cout << "請輸入縱座標:";cin>>x;cout << "請輸入橫座標:";cin>>y;if (x <= 0 || x > m_row || y <= 0 || y > m_column){cout <<"輸入錯誤請重新輸入" <<endl;Sleep(1000);continue;}cout << "請輸入是要做標記還是掃雷,掃雷請按1,標記請按2:" <<endl;cin >> flag;if ( false == OpenWhites(x-1, y-1, flag) ){int i;system("cls");GameOver();cout << "遊戲結束!" <<endl;cout << "繼續遊戲請按1,離開遊戲請按0:"<<endl;cin >> i;if (i == 1)goto begin;else goto end;}else{if (GoodOver() == true){int i;cout << "掃雷成功!" <<endl;cout << "繼續遊戲請按1,離開遊戲請按0:"<<endl;cin >> i;if (i == 1)goto begin;else goto end;}}}end:return;}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.