Advertisement
Tony041010

MineSweeper(Old)

Jun 5th, 2021 (edited)
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <conio.h>
  2. #include <iostream>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <time.h>
  6. #include <windows.h>
  7. using namespace std;
  8. char k; int p=13; string z;
  9. vector<string> map(144, "");
  10. vector<string> bign{"0","1","2","3","4","5","6","7","8"};
  11. void init() {
  12.     for( int i = 0 ; i < 144; i++ ){
  13.         if( i <=11 || i >=131 || i %12 ==0 || i %12 == 11 ){
  14.             map[i] = "W";
  15.         }
  16.     }
  17.     int t = 0;
  18.     int x = rand()%24;
  19.     x++;
  20.     while( t < x+1 ){
  21.         int y = rand()%144;
  22.         if( map[y] != "W" && y != p ){
  23.             map[y] ="B";
  24.             t++;
  25.         }
  26.     }
  27. }
  28.  
  29. //□■B
  30. void draw() {
  31.     system( "cls" );
  32.     for( int i = 0 ; i < 144 ; i++ ){
  33.         if( map[i] == "W" ){
  34.             cout << "";
  35.         }
  36.         else if(i == p){
  37.             cout << "□";
  38.         }
  39.         else if( map[i] == "B" && i == p ){
  40.             cout << "□";
  41.         }
  42.         else if( map[i] == "B" ){
  43.             cout << "■";
  44.         }
  45.         else if( map[i] == "b" ){
  46.             cout << "B";
  47.         }
  48.         else if( map[i] == "M" || map[i] == "m" ){
  49.             cout << "M";
  50.         }
  51.         else if( map[i] >= bign[0] && map[i] <= bign[8] ){
  52.             cout << map[i];
  53.         }
  54.         else if( i != p ){
  55.             cout << "■";
  56.         }
  57.         if(i % 12 == 11){
  58.             cout<<'\n';
  59.         }
  60.     }
  61. }
  62.  
  63.  
  64. bool end(){
  65.     for( int i = 0 ; i < 144 ; i++ ){
  66.         if( map[i] == "B" ){
  67.             return false;
  68.         }
  69.     }
  70.     return true;
  71. }
  72.  
  73. int counter( int p ){
  74.     int a = 0;
  75.     if( map[p-13] == "B" || map[p-13] == "m" ){
  76.         a++;
  77.     }
  78.     if( map[p-12] == "B" || map[p-12] == "m" ){
  79.         a++;
  80.     }
  81.     if( map[p-11] == "B" || map[p-11] == "m" ){
  82.         a++;
  83.     }
  84.     if( map[p-1] == "B" || map[p-1] == "m" ){
  85.         a++;
  86.     }
  87.     if( map[p+1] == "B" || map[p+1] == "m" ){
  88.         a++;
  89.     }
  90.     if( map[p+11] == "B" || map[p+11] == "m" ){
  91.         a++;
  92.     }
  93.     if( map[p+12] == "B" || map[p+12] == "m" ){
  94.         a++;
  95.     }
  96.     if( map[p+13] == "B" || map[p+13] == "m" ){
  97.         a++;
  98.     }
  99.    
  100.     map[p] = bign[a];
  101.     return a;
  102.                
  103. }
  104.  
  105. void dis( int p ){
  106.     int count[8] = { -13 , -12 , -11 , -1 , 1 , 11 , 12 , 13 };
  107.     if( counter( p ) == 0 ){
  108.         for(int i = 0 ; i < 8 ; i++ ){
  109.             if( map[p+count[i]] == "" ){
  110.                 if( counter( p+count[i] ) !=0 ){
  111.                     map[p+count[i]] = bign[counter(p+count[i])];
  112.                 }
  113.                 else{
  114.                     dis( p+count[i] );
  115.                 }
  116.             }
  117.         }
  118.     }
  119. }
  120.  
  121. int main() {
  122.     string a,b;  //string資料型態可包含文數字組合
  123.    
  124.     srand( time(NULL) );
  125.     init();
  126.     draw();
  127.     while (1) {
  128.         if( kbhit() ){
  129.             k = _getch();
  130.             if( k == 'q' ){
  131.                 break;
  132.             }
  133.             else if( k == 'w' && map[p-12] != "W" ){
  134.                 p -= 12;
  135.             }
  136.             else if( k == 's' && map[p+12] != "W" ){
  137.                 p += 12;
  138.             }
  139.             else if( k == 'a' && map[p-1] != "W" ){
  140.                 p -= 1;
  141.             }
  142.             else if( k == 'd' && map[p+1] != "W" ){
  143.                 p +=1;
  144.             }
  145.             else if( k =='c' ){
  146.                 if( map[p] == "B" ){
  147.                     for( int i = 0 ; i < 144 ; i++ ){
  148.                         if( map[i] == "B" ){
  149.                             map[i] = "b";
  150.                         }
  151.                     }
  152.                     draw();
  153.                     cout << "boom!!!\nGame Over!";
  154.                     break;
  155.                 }
  156.                 dis( p );
  157.             }
  158.            
  159.             else if( k == 'm' ){
  160.                 if( map[p] == "M" ){
  161.                     map[p] = "";
  162.                 }
  163.                 else if( map[p] == "B" ){
  164.                     map[p] = "m";
  165.                 }
  166.                 else if( map[p] =="m" ){
  167.                     map[p] = "B";
  168.                 }
  169.                 else{
  170.                     map[p] = "M";
  171.                 }
  172.             }
  173.         }
  174.         draw();
  175.         Sleep(300);
  176.         if( end() ){
  177.             draw();
  178.             cout<<"Congratulations!!!\nYou Win!";
  179.             break;
  180.         }
  181.     }
  182.     return 0;
  183. }
  184.  
  185.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement