c++實現掃雷(座標)

來源:互聯網
上載者:User

標籤:for   自動   cin   oid   中心   ios   位置   統計   分享   

昨天在觀察貪食蛇的代碼時,看到了有如何?掃雷的c++代碼,覺得挺有趣,今天便又試了一下

#include <ctime>#include <cstdlib>#include <iostream>using namespace std;int map[12][12];    // 為避免邊界的特殊處理,故將二維數組四周邊界擴充1int derection[3] = { 0, 1, -1 };  //方向數組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)為中心的四周的雷數目 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 (){ for ( int i = 1; i < 10; i++ ) {  for ( int j = 1; j < 10; j++ )  {   if ( map[i][j] == -1 || map[i][j] == 9 )    cout << "#";   else    cout << map[i][j];  }  cout << endl; }}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) );  // 將map全部初始化為-1,以後用-1表示未涉及的地區   for ( i = 0; i < 10;  )  {   x = rand()%9 + 1;   y = rand()%9 + 1;   if ( map[x][y] != 9 )   {    map[x][y] = 9;    i++;   }  }   for ( i = 1; i < 10; i++ )  {   for ( j = 1; j < 10; j++ )    cout << "#";   cout << "\n";  }  cout << "\n";   cout << "Please enter a coordinate: ";  while ( cin >> x >> y )  {   if ( map[x][y] == 9 )   {    cout << "GAME OVER" << endl;    //點中雷之後遊戲結束,並且輸出雷的位置    for ( i = 1; i < 10; i++ )    {     for ( j = 1; j < 10; j++ )     {      if ( map[i][j] == 9 )       cout << "@";      else       cout << "#";     }     cout << endl;    }    break;   }   game(x,y);   print();    if ( check () )   {    cout << "YOU WIN" << endl;    break;   }   cout << "\n\n";  }   cout << "Do you want to play again, if true enter Y, or enter N" << endl;  cin >> ch;  cout << "\n\n"; } while ( ch == ‘Y‘ );  return 0;}  

程式:

介面有點簡陋,並是通過座標來排雷的,而且到現在還沒搞懂他這個程式到底該怎樣輸入座標,不過還是感謝作者吧

c++實現掃雷(座標)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.