Advertisement
SilentSpirit

Console Sapper

May 15th, 2019
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.76 KB | None | 0 0
  1. #include "pch.h"
  2. #include <iostream>
  3. #include <chrono>
  4. #include <random>
  5.  
  6. int random(int num)
  7. {
  8.     int seed1 = (int)(std::chrono::system_clock::now().time_since_epoch().count());
  9.  
  10.     std::minstd_rand0 random(seed1);
  11.     int randomResult = random() % num;
  12.     return randomResult;
  13. }
  14.  
  15. class Location
  16. {
  17. public:
  18.     bool bomb = false;
  19.     char icon = 'O';
  20.  
  21.     void SetBomb()
  22.     {
  23.             bomb = true;
  24.     }
  25.     bool GetBomb()
  26.     {
  27.         return this->bomb;
  28.     }
  29.  
  30. };
  31.  
  32. void printMap(std::vector< std::vector<Location> > &map, int row, int col)
  33. {
  34.     std::cout << "-----------------------" << std::endl;
  35.     std::cout << "    0 1 2 3 4 5 6 7 8 9" << std::endl;
  36.  
  37.     for (int y = 0; y < row; ++y)
  38.     {
  39.         std::cout << y << "   ";
  40.         for (int x = 0; x < col; ++x)
  41.             std::cout << map[x][y].icon << " ";
  42.         std::cout << std::endl;
  43.     }
  44.     std::cout << "    0 1 2 3 4 5 6 7 8 9" << std::endl;
  45.     std::cout << "-----------------------" << std::endl;
  46. }
  47.  
  48. void printBomb(std::vector< std::vector<Location> > &map, int row, int col)
  49. {
  50.     std::cout << "-----------------------" << std::endl;
  51.     std::cout << "    0 1 2 3 4 5 6 7 8 9" << std::endl;
  52.  
  53.     for (int y = 0; y < row; ++y)
  54.     {
  55.         std::cout << y << " - ";
  56.         for (int x = 0; x < col; ++x)
  57.             if(map[x][y].bomb)
  58.                 std::cout << "B ";
  59.             else
  60.                 std::cout << map[x][y].icon << " ";
  61.         std::cout << std::endl;
  62.     }
  63.     std::cout << "    0 1 2 3 4 5 6 7 8 9" << std::endl;
  64.     std::cout << "-----------------------" << std::endl;
  65.     std::cout << "BOOM BITCH!!!" << std::endl;
  66. }
  67.  
  68. void bombMap(std::vector< std::vector<Location> > &map, int row, int col, int &bombs)
  69. {
  70.     while (bombs > 80 || bombs < 5)
  71.     {
  72.         std::cout << "Enter amount of bombs from 5 to 80- ";
  73.         std::cin >> bombs;
  74.     }
  75.  
  76.     int x, y, count = 0;
  77.     do
  78.     {
  79.         x = random(row - 1);
  80.         y = random(col - 1);
  81.         if (!map[x][y].GetBomb())
  82.         {
  83.             map[x][y].SetBomb();
  84.             count += 1;
  85.         }
  86.        
  87.     } while (count != bombs);
  88. }
  89.  
  90. int countBombs(std::vector< std::vector<Location> > &map, int row, int col)
  91. {
  92.     int count = 0;
  93.     for (int y = 0; y < row; ++y)
  94.         for (int x = 0; x < col; ++x)
  95.             if (map[y][x].GetBomb())
  96.             count += 1;
  97.     return count;
  98. }
  99.  
  100. bool playerSelection(std::vector< std::vector<Location> > &map, int row, int col, int &userX, int &userY, bool &wining)
  101. {
  102.     wining = true;
  103.  
  104.     do
  105.     {
  106.         std::cout << "Enter X - ";
  107.         std::cin >> userX;
  108.     } while (userX < 0 || userX > row);
  109.  
  110.     do
  111.     {
  112.         std::cout << "Enter Y - ";
  113.         std::cin >> userY;
  114.     } while (userY < 0 || userY > col);
  115.  
  116.     if (map[userX][userY].bomb)
  117.         wining = false;
  118.  
  119.     return wining;
  120. }
  121.  
  122. void checkNearBombs(std::vector< std::vector<Location> > &map, int row, int col, int userX, int userY)
  123. {
  124.     int nearBombs = 0;
  125.     for (int i = (userX - 1 > 0) ? userX - 1 : 0; i <= ((userX + 1 < row - 1 ) ? userX + 1 : row - 1); i++)
  126.         for (int j = (userY - 1 > 0) ? userY - 1 : 0; j <= ((userY + 1 < col - 1 ) ? userY + 1 : col - 1); j++)
  127.             if (map[i][j].bomb)
  128.                 nearBombs++;
  129.  
  130.     map[userX][userY].icon = nearBombs + '0';
  131. }
  132.  
  133. int main()
  134. {
  135.     int row = 10, col = 10, bombs = 0, userX, userY, count, save;
  136.     bool wining;
  137.  
  138.     std::vector< std::vector<Location> >map(col, std::vector<Location>(row));
  139.  
  140.     bombMap(map, row, col, bombs);
  141.     save = row * col - bombs;
  142.  
  143.     std::cout << "You have 10 x 10 map" << std::endl;
  144.     std::cout << countBombs(map, row, col) << " bombs planted" << std::endl;
  145.  
  146.     printMap(map, row, col);
  147.  
  148.     std::cout << save << " save zones. Good Luck!" << std::endl;
  149.  
  150.     while (playerSelection(map, row, col, userX, userY, wining))
  151.     {
  152.         checkNearBombs(map, row, col, userX, userY);
  153.         printMap(map, row, col);
  154.         --save;
  155.         std::cout << save << " save zones left" << std::endl;
  156.         if (save == 0)
  157.             break;
  158.     }
  159.     if (save == 0)
  160.         std::cout << save << "AMAZING!!! YOU WIN!!!" << std::endl;
  161.     else
  162.         printBomb(map, row, col);
  163.  
  164.     system("pause");
  165.     return 0;
  166. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement