Advertisement
Guest User

minesweeper2

a guest
Dec 11th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.39 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <conio.h>
  4. #include <windows.h>
  5. #include <ctime>
  6. #include <iomanip>
  7.  
  8. void game();
  9. void settings();
  10. void flagging();
  11. void click();
  12. void settings();
  13. void board();
  14. bool ifend();
  15. void counter(int lin, int col);
  16. struct square
  17. {
  18.     char view=char(254u);
  19.     bool flags=0;
  20.     bool bombs=0;
  21.     bool if_checked=0;
  22. };
  23. square tab[9][9];
  24. int flagsleft=10;
  25.  
  26. int main()
  27. {
  28.     std::cout << "====================" << std::endl;
  29.     std::cout << "Minesweeper v.2.0" << std::endl;
  30.     std::cout << "by Bartek Mekarski" << std::endl;
  31.     std::cout << "====================" << std::endl;
  32.     std::cout << "\n";
  33.     std::cout << "1. New game" << std::endl;
  34.     std::cout << "2. Exit" << std::endl;
  35.     std::cout << "Press the key to select an option" << std::endl;
  36.     char pick;
  37.     do
  38.     {
  39.         pick=getch();
  40.         switch (pick)
  41.         {
  42.         case '1': game();
  43.         break;
  44.         case '2': return 0;
  45.         break;
  46.         default:
  47.             {
  48.                 system("cls");
  49.                 std::cout << "Press the correct key!" << std::endl;
  50.                 std::cout << "1. New game" << std::endl;
  51.                 std::cout << "2. Exit" << std::endl;
  52.             }
  53.         }
  54.     }while (pick!='1'&&pick!='2');
  55.     return 0;
  56. }
  57.  
  58. void game()
  59. {
  60.     system ("cls");
  61.     srand(time(NULL));
  62.     int i=0;
  63.     while (i<10)
  64.     {
  65.         int wherebombx=rand()%9, wherebomby=rand()%9;
  66.         if (tab[wherebombx][wherebomby].bombs==0)
  67.         {
  68.            tab[wherebombx][wherebomby].bombs=1;
  69.            i++;
  70.         }
  71.     }
  72.     clock_t start = clock();
  73.     while (ifend()==0)
  74.     {
  75.         board();
  76.         std::cout << "\nTo put/remove a flag, press F!" << std::endl;
  77.         std::cout << "To 'click' on a square, press J!" << std::endl;
  78.         std::cout << "\n\nTime: " << (clock()-start)/CLOCKS_PER_SEC << " s" << std::endl;
  79.         std::cout << "Flags left: " << flagsleft << std::endl;
  80.         char ans;
  81.         ans = getch();
  82.         switch(ans)
  83.             {
  84.             case 70: flagging();
  85.             break;
  86.             case 102: flagging();
  87.             break;
  88.             case 74: click();
  89.             break;
  90.             case 106: click();
  91.             break;
  92.             default: std::cout << "Press the correct key!" << std::endl;
  93.             break;
  94.             }
  95.     }
  96.     board();
  97.     std::cout << "\nYOU WON!!!\n\n";
  98.     std::cout << "Your time was: " << (clock()-start)/CLOCKS_PER_SEC << " s" << std::endl;
  99.     std::cout << "Press any key to continue!" << std::endl;
  100.     getch();
  101. }
  102.  
  103. void board()
  104. {
  105.     system("cls");
  106.     std::cout << "1 2 3 4 5 6 7 8 9 " << std::endl;
  107.     std::cout << "------------------" << std::endl;
  108.     for (int i=0; i<9; i++)
  109.     {
  110.         for (int j=0; j<9; j++)
  111.             std::cout << tab[i][j].view << " ";
  112.         std::cout << "|" << i+1 << std::endl;
  113.     }
  114. }
  115.  
  116. bool ifend()
  117. {
  118.     int c=0;
  119.     for (int i=0; i<9; i++)
  120.         for (int j=0; j<9; j++)
  121.             {
  122.                 if (tab[i][j].view == char(254u) || tab[i][j].view == 'F')
  123.                     c++;
  124.             }
  125.     int d=0;
  126.     if (c==10)
  127.     {
  128.         for (int i=0; i<9; i++)
  129.             for (int j=0; j<9; j++)
  130.                 {
  131.                     if (tab[i][j].bombs == 1 && (tab[i][j].view == char(254u) || tab[i][j].view == 'F'))
  132.                         d++;
  133.                 }
  134.     }
  135.     if (d==10)
  136.         return 1;
  137.     else
  138.         return 0;
  139. }
  140.  
  141. void flagging()
  142. {
  143.     int lin,col;
  144.     std::cout << "Enter coordinates (line and column) of the flag!" << std::endl;
  145.     std::cin >> lin >> col;
  146.     lin--;
  147.     col--;
  148.     if (lin>=0 && lin<=8 && col>=0 && col<=8 && (tab[lin][col].view==char(254u) || tab[lin][col].view=='F'))
  149.     {
  150.         if (tab[lin][col].flags==0)
  151.         {
  152.             tab[lin][col].flags=1;
  153.             tab[lin][col].view='F';
  154.             flagsleft--;
  155.         }
  156.         else
  157.         {
  158.             tab[lin][col].flags=0;
  159.             tab[lin][col].view=char(254u);
  160.             flagsleft++;
  161.         }
  162.     }
  163.     else
  164.         std::cout << "Incorrect coordinates!" << std::endl;
  165. }
  166.  
  167. void click()
  168. {
  169.     int lin,col;
  170.     std::cout << "Enter coordinates (line and column) of the square!" << std::endl;
  171.     std::cin >> lin >> col;
  172.     lin--;
  173.     col--;
  174.     std::cout << "\n";
  175.     if (lin>=0 && lin<=8 && col>=0 && col<=8)
  176.     {
  177.         if (tab[lin][col].flags==1)
  178.             std::cout << "There is a flag on this square!" << std::endl;
  179.         else if (tab[lin][col].bombs==1)
  180.         {
  181.             system("cls");
  182.             tab[lin][col].view='X';
  183.             board();
  184.             //prints board with marked bombs
  185.             std::cout << "\nYOU LOST!!!\n\n";
  186.             std::cout << "1 2 3 4 5 6 7 8 9 " << std::endl;
  187.             std::cout << "------------------" << std::endl;
  188.             for (int i=0; i<9; i++)
  189.             {
  190.                 for (int j=0; j<9; j++)
  191.                     {
  192.                         if (tab[i][j].bombs==1)
  193.                             std::cout << "B ";
  194.                         else
  195.                             std::cout << char(254u) <<" ";
  196.                     }
  197.                 std::cout << "| " << i+1 << std::endl;
  198.             }
  199.             std::cout << "Press any key to continue!" << std::endl;
  200.             getch();
  201.             exit(0);
  202.         }
  203.         else
  204.             counter(lin,col);
  205.     }
  206.     else
  207.     {
  208.         std::cout << "Incorrect coordinates!" << std::endl;
  209.     }
  210. }
  211.  
  212. void counter (int lin, int col)
  213. {
  214.     if (lin>=0 && lin<=8 && col>=0 && col<=8)
  215.     {
  216.         tab[lin][col].if_checked = 1;
  217.         char c=48;
  218.         if (tab[lin-1][col-1].bombs==1 && lin-1>=0 && col-1>=0)
  219.             c++;
  220.         if (tab[lin-1][col].bombs==1 && lin-1>=0)
  221.             c++;
  222.         if (tab[lin-1][col+1].bombs==1 && lin-1>=0 && col+1<=8)
  223.             c++;
  224.         if (tab[lin][col+1].bombs==1 && col+1<=8)
  225.             c++;
  226.         if (tab[lin+1][col+1].bombs==1 && lin+1<=8 && col+1<=8)
  227.             c++;
  228.         if (tab[lin+1][col].bombs==1 && lin+1<=8)
  229.             c++;
  230.         if (tab[lin+1][col-1].bombs==1 && lin+1<=8 && col-1>=0)
  231.             c++;
  232.         if (tab[lin][col-1].bombs==1 && col-1>=0)
  233.             c++;
  234.         if (c==48)
  235.         {
  236.             tab[lin][col].view=32;//space
  237.             if (tab[lin-1][col-1].flags==0 && lin-1>=0 && col-1>=0 && tab[lin-1][col-1].if_checked==0)
  238.                 counter(lin-1,col-1);
  239.             if (tab[lin-1][col].flags==0 && lin-1>=0 && tab[lin-1][col].if_checked==0)
  240.                 counter(lin-1,col);
  241.             if (tab[lin-1][col+1].flags==0 && lin-1>=0 && col+1<=8 && tab[lin-1][col+1].if_checked==0)
  242.                 counter(lin-1,col+1);
  243.             if (tab[lin][col+1].flags==0 && col+1<=8 && tab[lin][col+1].if_checked==0)
  244.                 counter(lin,col+1);
  245.             if (tab[lin+1][col+1].flags==0 && lin+1<=8 && col+1<=8 && tab[lin+1][col+1].if_checked==0)
  246.                 counter(lin+1,col+1);
  247.             if (tab[lin+1][col].flags==0 && lin+1<=8 && tab[lin+1][col].if_checked==0)
  248.                 counter(lin+1,col);
  249.             if (tab[lin+1][col-1].flags==0 && lin+1<=8 && col-1>=0 && tab[lin+1][col-1].if_checked==0)
  250.                 counter(lin+1,col-1);
  251.             if (tab[lin][col-1].flags==0 && col-1>=0 && tab[lin][col-1].if_checked==0)
  252.                 counter(lin,col-1);
  253.         }
  254.     else
  255.         tab[lin][col].view = c;
  256.     }
  257. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement