Guest User

Plansza.cpp

a guest
Mar 23rd, 2017
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.69 KB | None | 0 0
  1. #include "Plansza.h"
  2. #include <cstdlib>
  3. #include <ctime>
  4. #include <iostream>
  5.  
  6. Plansza::Plansza(int wysokosc, int szeroksc) : koniec_gry(false) {
  7.  
  8.     srand(time(0));
  9.  
  10.     if(wysokosc < 1 || szeroksc < 1) {
  11.         M = 10;
  12.         N = 10;
  13.     }
  14.  
  15.     M = wysokosc;
  16.     N = szeroksc;
  17.  
  18.     plansza = new Pole*[M];
  19.  
  20.     for(int i = 0; i < M; i++) {
  21.         plansza[i] = new Pole[N];
  22.     }
  23. }
  24.  
  25. Plansza::~Plansza() {
  26.     for(int i = 0; i < M; i++) {
  27.         delete[] plansza[i];
  28.     }
  29.  
  30.     delete[] plansza;
  31. }
  32.  
  33. void Plansza::deployMines(int n, bool random) {
  34.     int ostatnie_x = 0;
  35.     int ostatnie_y = 0;
  36.     int rand_x = rand() % N;
  37.     int rand_y = rand() % M;
  38.  
  39.     // losowe
  40.     if(random) {
  41.         for(int i = 0; i < n; i++) {
  42.              do {
  43.                 rand_x = rand() % N;
  44.                 rand_y = rand() % M;
  45.             } while ((rand_x == ostatnie_x) && (rand_y == ostatnie_y));
  46.             plansza[rand_y][rand_x].ustawMina(true);
  47.             ostatnie_x = rand_x;
  48.             ostatnie_y = rand_y;
  49.         }
  50.     } else { // nielosowe
  51.         for(int i = 0; i < N; i++) {
  52.             plansza[0][i].ustawMina(true);
  53.         }
  54.  
  55.         for(int i = 0; i < M; i++) {
  56.             for(int j = 0; j < N; j++) {
  57.                 plansza[i][i].ustawMina(true);
  58.             }
  59.         }
  60.     }
  61. }
  62.  
  63. void Plansza::debug_display() {
  64.     for(int i = 0; i < M; i++) {
  65.         for(int j = 0; j < N; j++) {
  66.             plansza[i][j].info();
  67.         }
  68.         std::cout << std::endl;
  69.     }
  70. }
  71.  
  72. bool Plansza::hasMine(int x, int y) {
  73.     if(x >= 0 && x < N && y >= 0 && y < M)
  74.         return plansza[y][x].odczytajMina();
  75.     else {
  76.         //std::cout << "Niepoprawna wspolrzedna: x: " << x << ", y:" << y << std::endl;
  77.         false;
  78.     }
  79. }
  80.  
  81. int Plansza::countMines(int x, int y) {
  82.     int licznik = 0;
  83.  
  84.     for (int i = y - 1; i <= y + 1; i++) {
  85.         for (int j = x - 1; j <= x + 1; j++) {
  86.             //std::cout << "X: " << j << "\tY: " << i << std::endl;
  87.             if (hasMine(j, i)) {
  88.                 //std::cout << "X: " << j << ", Y: " << i << " has mine\n";
  89.                 std::cout.flush();
  90.                 licznik++;
  91.                 if (i == y && j == x)
  92.                     licznik--;
  93.             }
  94.         }
  95.     }
  96.     //std::cout << "Licznik: " << licznik << std::endl;
  97.  
  98.     return licznik;
  99. }
  100.  
  101. void Plansza::display() {
  102.     int min_w_sasiedztwie = 0;
  103.     for(int i = 0; i < M; i++) {
  104.  
  105.         for(int j = 0; j < N; j++) {
  106.             min_w_sasiedztwie = countMines(j, i);
  107.             //std::cout << "X: " << j << ", Y: " << i << " min w sasiedztwie: " << min_w_sasiedztwie << std::endl;
  108.             if(!plansza[i][j].odczytajFlaga() && plansza[i][j].odczytajUkryte()) {
  109.                 std::cout << "[.]";
  110.             }
  111.             else if(plansza[i][j].odczytajFlaga() && plansza[i][j].odczytajUkryte()) {
  112.                 std::cout << "[?]";
  113.             }
  114.             else if(!plansza[i][j].odczytajUkryte() && hasMine(j, i)) {
  115.                 std::cout << "[x]";
  116.             }
  117.             else if(!plansza[i][j].odczytajUkryte()) {
  118.                 if(countMines(j, i) == 0)
  119.                     std::cout << "[ ]";
  120.                 else
  121.                     std::cout << "[" << min_w_sasiedztwie << "]";
  122.             }
  123.  
  124.             //std::cout << j;
  125.             min_w_sasiedztwie = 0;
  126.         }
  127.         std::cout << std::endl;
  128.  
  129.     }
  130. }
  131.  
  132. */
  133. bool Plansza::reveal(int x, int y) {
  134.     if(koniec_gry) {
  135.         std::cout << "Przegrales" << std::endl;
  136.         return false;
  137.     }
  138.     plansza[y][x].ustawUkryte(false);
  139.     if(plansza[y][x].odczytajMina()) {
  140.         koniec_gry = true;
  141.         return false;
  142.     }
  143.     return true;
  144. }
Add Comment
Please, Sign In to add comment