JustAnotherCatgirl

Game of life finished

Apr 21st, 2023
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.75 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. struct point
  5. {
  6.     int first;
  7.     int second;
  8. };
  9.  
  10. class game
  11. {
  12.     typedef std::vector<std::vector<bool>> field_t;
  13.     int height, width;
  14.     field_t field;
  15.     mutable field_t mask;
  16.    
  17.     int count_neighbours(int posx, int posy) const
  18.     {
  19.         int count = 0;
  20.        
  21.         for(int i = -1; i<=1; ++i)
  22.         for(int j = -1; j<=1; ++j)
  23.         {
  24.             if(!i && !j ) continue;
  25.             try
  26.             {
  27.                 count += field.at(posx+i).at(posy+j);
  28.             }
  29.             catch(std::out_of_range) {}
  30.         }
  31.        
  32.         return count;
  33.     }
  34.    
  35.     void construct_mask() const
  36.     {
  37.         for(int i = 0; i<height; ++i)
  38.         for(int j = 0; j<width ; ++j)
  39.         {
  40.             int count = count_neighbours(i, j);
  41.             mask[i][j] = (field[i][j] && (count < 2 || count > 3))
  42.                          || (!field[i][j] && count == 3); //uhum uhum
  43.         }
  44.     }
  45.    
  46.     void apply_mask()
  47.     {
  48.         for(int i = 0; i<height; ++i)
  49.         for(int j = 0; j<width ; ++j)
  50.         {
  51.             field[i][j] = field[i][j] ^ mask[i][j];
  52.             // field[i][j] ^= mask[i][j] does not work
  53.         }
  54.     }
  55.    
  56. public:
  57.    
  58.     game(int h, int w): height{h}, width{w}
  59.     {
  60.         field = std::vector<std::vector<bool>>(height);
  61.         for(auto &x: field)
  62.         {
  63.             x = std::vector<bool>(width);
  64.         }
  65.         mask = field;
  66.     }
  67.    
  68.     void set_alive(std::vector<point> &points)
  69.     {
  70.         for(auto [i, j]: points)
  71.             field.at(i).at(j) = true;
  72.     }
  73.    
  74.     inline void next_iteration()
  75.     {
  76.         construct_mask();
  77.         apply_mask();
  78.     }
  79.    
  80.     void print() const
  81.     {
  82.         for(auto &x: field)
  83.         {
  84.             for(auto y: x)
  85.                 std::cout << (y ? '#' : '`') << ' ';
  86.             std::cout << '\n';
  87.         }
  88.         std::cout << '\n';
  89.     }
  90.    
  91. };
  92.  
  93. #include <thread>
  94. int main()
  95. {
  96.     int h, w;
  97.     std::cout << "enter height, width\n";
  98.     std::cin >> h >> w;
  99.     game G(h, w);
  100.     G.print();
  101.  
  102.        
  103.     setup:
  104.     std::cout << "\nenter amount of live cells\n";
  105.     int amount; std::cin >> amount;
  106.     std::cout << "\nenter points\n";
  107.     std::vector<point> points(amount);
  108.     for(auto &x: points)
  109.         std::cin >> x.first >> x.second;
  110.     try
  111.     {
  112.         G.set_alive(points);
  113.     }
  114.     catch(std::out_of_range &e)
  115.     {
  116.         std::cout << "\npoint out of bounds!\n";
  117.         goto setup;
  118.     }
  119.     std::cout << "\nsetup complete!\n";
  120.    
  121.    
  122.    
  123.     G.print();
  124.     for(int i = 0; i<100; ++i)
  125.     {
  126.         G.next_iteration();
  127.         G.print();
  128.         std::this_thread::sleep_for(std::chrono::milliseconds(100));
  129.     }
  130. }
  131.  
Advertisement
Add Comment
Please, Sign In to add comment