Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Minesweeper.h:
- #include <iostream>
- using namespace std;
- class Minesweeper {
- private:
- int n;
- int m;
- bool** a;
- public:
- Minesweeper();
- Minesweeper(int n, int m);
- ~Minesweeper();
- int brVrsta() {
- return n;
- }
- int brKolona() {
- return m;
- }
- void ucitajMinesweeper();
- void ispisiMinesweeper();
- int opasnostPolja(int brV, int brK);
- void operator++(int);
- void operator--(int);
- void operator++();
- void operator--();
- friend istream& operator>>(istream& str, Minesweeper& m);
- friend ostream& operator<<(ostream& str, Minesweeper& m);
- };
- // Minesweeper.cpp:
- #include "Minesweeper.h"
- #include <iostream>
- using namespace std;
- Minesweeper::Minesweeper(int n, int m) {
- this->n = n;
- this->m = m;
- a = new bool*[n];
- for (int i = 0; i < n; i++) {
- a[i] = new bool[m];
- }
- }
- Minesweeper::Minesweeper() {
- n = 10;
- m = 10;
- }
- Minesweeper::~Minesweeper() {
- if (a) {
- for (int i = 0; i < n; i++) {
- delete [] a[i];
- }
- delete [] a;
- }
- }
- int Minesweeper::opasnostPolja(int x, int y) {
- int i,j,br = 0;
- if (a[x - 1][y - 1] == true) {
- return -1;
- }
- else {
- for (i = 0; i < 3; i++) {
- for (j = 0; j < 3; j++) {
- if (x - i >= 0 && x - i < n && y - j >= 0 && y - j < m) {
- if (a[x - i][y - j] == true) {
- br++;
- }
- }
- }
- }
- }
- return br;
- }
- void Minesweeper::ucitajMinesweeper() {
- cout << "Unesite matricu " << n << " x " << m << endl;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cin >> a[i][j];
- }
- }
- }
- void Minesweeper::ispisiMinesweeper() {
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < m; j++) {
- cout << a[i][j];
- }
- cout << endl;
- }
- }
- void Minesweeper::operator++(int) {
- int pom;
- for (int i = 0; i < n; i++) {
- pom = a[i][m - 1];
- for (int j = m - 1; j > 0; j--) {
- a[i][j] = a[i][j - 1];
- }
- a[i][0] = pom;
- }
- }
- void Minesweeper::operator--(int) {
- int pom;
- for (int i = 0; i < n; i++) {
- pom = a[i][0];
- for (int j = 0; j < m - 1; j++) {
- a[i][j] = a[i][j + 1];
- }
- a[i][m - 1] = pom;
- }
- }
- void Minesweeper::operator++() {
- int pom;
- for (int j = 0; j < m; j++) {
- pom = a[n - 1][j];
- for (int i = n - 1; i > 0; i--) {
- a[i][j] = a[i - 1][j];
- }
- a[0][j] = pom;
- }
- }
- void Minesweeper::operator--() {
- int pom;
- for (int j = 0; j < m; j++) {
- pom = a[0][j];
- for (int i = 0; i < n - 1; i++) {
- a[i][j] = a[i + 1][j];
- }
- a[n - 1][j] = pom;
- }
- }
- istream& operator>>(istream& str, Minesweeper& m) {
- for (int i = 0; i < m.n; i++) {
- for (int j = 0; j < m.m; j++) {
- str >> m.a[i][j];
- }
- }
- return str;
- }
- ostream& operator<<(ostream& str, Minesweeper& m) {
- for (int i = 0; i < m.n; i++) {
- for (int j = 0; j < m.m; j++) {
- str << m.a[i][j];
- }
- str << "\n";
- }
- return str;
- }
- // main.cpp:
- #include "Minesweeper.h"
- #include <iostream>
- using namespace std;
- void main() {
- Minesweeper mine(5,5);
- int x, y;
- cin >> mine;
- cout << "Unesite vrstu i kolonu polja koje zelite da proverite \n";
- cin >> x >> y;
- if (mine.opasnostPolja(x,y) < 0) {
- cout << mine;
- }
- else {
- cout << "Polje je okruzeno sa " << mine.opasnostPolja(x,y) << " mina\n";
- }
- cout << endl;
- mine++;
- cout << mine;
- cout << endl;
- mine--;
- cout << mine;
- cout << endl;
- ++mine;
- cout << mine;
- cout << endl;
- --mine;
- cout << mine;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement