Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "pch.h"
- #include <iostream>
- #include <chrono>
- #include <random>
- int random(int num)
- {
- int seed1 = (int)(std::chrono::system_clock::now().time_since_epoch().count());
- std::minstd_rand0 random(seed1);
- int randomResult = random() % num;
- return randomResult;
- }
- class Location
- {
- public:
- bool bomb = false;
- char icon = 'O';
- void SetBomb()
- {
- bomb = true;
- }
- bool GetBomb()
- {
- return this->bomb;
- }
- };
- void printMap(std::vector< std::vector<Location> > &map, int row, int col)
- {
- std::cout << "-----------------------" << std::endl;
- std::cout << " 0 1 2 3 4 5 6 7 8 9" << std::endl;
- for (int y = 0; y < row; ++y)
- {
- std::cout << y << " ";
- for (int x = 0; x < col; ++x)
- std::cout << map[x][y].icon << " ";
- std::cout << std::endl;
- }
- std::cout << " 0 1 2 3 4 5 6 7 8 9" << std::endl;
- std::cout << "-----------------------" << std::endl;
- }
- void printBomb(std::vector< std::vector<Location> > &map, int row, int col)
- {
- std::cout << "-----------------------" << std::endl;
- std::cout << " 0 1 2 3 4 5 6 7 8 9" << std::endl;
- for (int y = 0; y < row; ++y)
- {
- std::cout << y << " - ";
- for (int x = 0; x < col; ++x)
- if(map[x][y].bomb)
- std::cout << "B ";
- else
- std::cout << map[x][y].icon << " ";
- std::cout << std::endl;
- }
- std::cout << " 0 1 2 3 4 5 6 7 8 9" << std::endl;
- std::cout << "-----------------------" << std::endl;
- std::cout << "BOOM BITCH!!!" << std::endl;
- }
- void bombMap(std::vector< std::vector<Location> > &map, int row, int col, int &bombs)
- {
- while (bombs > 80 || bombs < 5)
- {
- std::cout << "Enter amount of bombs from 5 to 80- ";
- std::cin >> bombs;
- }
- int x, y, count = 0;
- do
- {
- x = random(row - 1);
- y = random(col - 1);
- if (!map[x][y].GetBomb())
- {
- map[x][y].SetBomb();
- count += 1;
- }
- } while (count != bombs);
- }
- int countBombs(std::vector< std::vector<Location> > &map, int row, int col)
- {
- int count = 0;
- for (int y = 0; y < row; ++y)
- for (int x = 0; x < col; ++x)
- if (map[y][x].GetBomb())
- count += 1;
- return count;
- }
- bool playerSelection(std::vector< std::vector<Location> > &map, int row, int col, int &userX, int &userY, bool &wining)
- {
- wining = true;
- do
- {
- std::cout << "Enter X - ";
- std::cin >> userX;
- } while (userX < 0 || userX > row);
- do
- {
- std::cout << "Enter Y - ";
- std::cin >> userY;
- } while (userY < 0 || userY > col);
- if (map[userX][userY].bomb)
- wining = false;
- return wining;
- }
- void checkNearBombs(std::vector< std::vector<Location> > &map, int row, int col, int userX, int userY)
- {
- int nearBombs = 0;
- for (int i = (userX - 1 > 0) ? userX - 1 : 0; i <= ((userX + 1 < row - 1 ) ? userX + 1 : row - 1); i++)
- for (int j = (userY - 1 > 0) ? userY - 1 : 0; j <= ((userY + 1 < col - 1 ) ? userY + 1 : col - 1); j++)
- if (map[i][j].bomb)
- nearBombs++;
- map[userX][userY].icon = nearBombs + '0';
- }
- int main()
- {
- int row = 10, col = 10, bombs = 0, userX, userY, count, save;
- bool wining;
- std::vector< std::vector<Location> >map(col, std::vector<Location>(row));
- bombMap(map, row, col, bombs);
- save = row * col - bombs;
- std::cout << "You have 10 x 10 map" << std::endl;
- std::cout << countBombs(map, row, col) << " bombs planted" << std::endl;
- printMap(map, row, col);
- std::cout << save << " save zones. Good Luck!" << std::endl;
- while (playerSelection(map, row, col, userX, userY, wining))
- {
- checkNearBombs(map, row, col, userX, userY);
- printMap(map, row, col);
- --save;
- std::cout << save << " save zones left" << std::endl;
- if (save == 0)
- break;
- }
- if (save == 0)
- std::cout << save << "AMAZING!!! YOU WIN!!!" << std::endl;
- else
- printBomb(map, row, col);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement