Advertisement
MeehoweCK

Untitled

Jun 18th, 2019
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. class Plansza
  7. {
  8.     int rozmiar;
  9.     bool** plansza;
  10.     int ilosc;
  11. public:
  12.     Plansza(int r);
  13.     ~Plansza();
  14.     void umiesc(int x, int y);
  15.     int ile();
  16.     int sasiedzi(int x, int y);
  17.     void usun();
  18.     void usun2();
  19. };
  20.  
  21. struct wspolrzedne
  22. {
  23.     int x;
  24.     int y;
  25. };
  26.  
  27. Plansza::Plansza(int r) : rozmiar(r), ilosc(0)
  28. {
  29.     plansza = new bool*[r];
  30.     for(int i = 0; i < r; ++i)
  31.         plansza[i] = new bool[r];
  32.     for(int i = 0; i < r; ++i)
  33.         for(int j = 0; j < r; ++j)
  34.             plansza[i][j] = false;
  35. }
  36.  
  37. Plansza::~Plansza()
  38. {
  39.     for(int i = 0; i < rozmiar; ++i)
  40.         delete[] plansza[i];
  41.     delete[] plansza;
  42. }
  43.  
  44. void Plansza::umiesc(int x, int y)
  45. {
  46.     plansza[x][y] = true;
  47.     ++ilosc;
  48. }
  49.  
  50. int Plansza::ile()
  51. {
  52.     return ilosc;
  53. }
  54.  
  55. int Plansza::sasiedzi(int x, int y)
  56. {
  57.     int licznik = 0;
  58.     if(x != 0 && y != 0)            // lewy górny
  59.         if(plansza[x-1][y-1])
  60.             ++licznik;
  61.     if(x != 0)                      // lewy
  62.         if(plansza[x-1][y])
  63.             ++licznik;
  64.     if(x != 0 && y < (rozmiar - 1)) // lewy dolny
  65.         if(plansza[x-1][y+1])
  66.             ++licznik;
  67.     if(y < (rozmiar - 1))           // dolny
  68.         if(plansza[x][y+1])
  69.             ++licznik;
  70.     if(x < (rozmiar - 1) && y < (rozmiar - 1))  // prawy dolny
  71.         if(plansza[x+1][y+1])
  72.             ++licznik;
  73.     if(x < (rozmiar-1))             // prawy
  74.         if(plansza[x+1][y])
  75.             ++licznik;
  76.     if(x < (rozmiar-1) && y > 0)    // prawy gorny
  77.         if(plansza[x+1][y-1])
  78.             ++licznik;
  79.     if(y > 0)                       // gorny
  80.         if(plansza[x][y-1])
  81.             ++licznik;
  82.     return licznik;
  83. }
  84.  
  85. void Plansza::usun()
  86. {
  87.     // usuwa pionki z tych wszystkich pol, ktore maja mniej niz dwoch sasiadow
  88.     int** tablica = new int*[rozmiar];
  89.     for(int i = 0; i < rozmiar; ++i)
  90.         tablica[i] = new int[rozmiar];
  91.     for(int i = 0; i < rozmiar; ++i)
  92.         for(int j = 0; j < rozmiar; ++j)
  93.             tablica[i][j] = sasiedzi(i, j);
  94.     for(int i = 0; i < rozmiar; ++i)
  95.         for(int j = 0; j < rozmiar; ++j)
  96.             if(tablica[i][j] < 2 && plansza[i][j])
  97.             {
  98.                 plansza[i][j] = false;
  99.                 --ilosc;
  100.             }
  101.     for(int i = 0; i < rozmiar; ++i)
  102.         delete[] tablica[i];
  103.     delete[] tablica;
  104. }
  105.  
  106. void Plansza::usun2()
  107. {
  108.     // usuwa pionki z tych wszystkich pol, ktore maja mniej niz dwoch sasiadow
  109.     wspolrzedne wsp;
  110.     vector<wspolrzedne> do_usuniecia;
  111.     do_usuniecia.clear();
  112.     for(int i = 0; i < rozmiar; ++i)
  113.         for(int j = 0; j < rozmiar; ++j)
  114.             if(sasiedzi(i, j) < 2 && plansza[i][j])
  115.             {
  116.                 wsp.x = i;
  117.                 wsp.y = j;
  118.                 do_usuniecia.push_back(wsp);
  119.             }
  120.     int x, y;
  121.     for(unsigned i = 0; i < do_usuniecia.size(); ++i)
  122.     {
  123.         x = do_usuniecia[i].x;
  124.         y = do_usuniecia[i].y;
  125.         plansza[x][y] = false;
  126.         --ilosc;
  127.     }
  128. }
  129.  
  130. int main()
  131. {
  132.     Plansza nowa(4);
  133.     nowa.umiesc(2, 1);
  134.     nowa.umiesc(2, 2);
  135.     nowa.umiesc(2, 3);
  136.     cout << nowa.ile() << endl;
  137.     nowa.usun2();
  138.     cout << nowa.ile() << endl;
  139.     return 0;
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement