Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.18 KB | None | 0 0
  1. // Algorytm Conwaya
  2. // wersja tekstowa, kolorowanie według wątków
  3. // Gra w życie
  4. #include <iostream>
  5. #include <thread>
  6. #include <chrono>
  7. #include <cstdlib>
  8. using namespace std;
  9.  
  10. const int height = 16; // wysokość planszy
  11. const int width = 32; // szerokość planszy
  12. int state[height][width]; // tablica z komórkami
  13. int sleep = 300; // czas pomiędzy kolejnymi stanami planszy
  14. int n = 50; // początkowa ilość żywych komórek
  15.  
  16. void init()
  17. {
  18.     // wypełnienie tablicy martwymi komórkami
  19.     for(int y=0; y<height; y++)
  20.         for(int x=0; x<width; x++)
  21.             state[y][x] = 0;
  22.     // wylosowanie położenia żywych komórek
  23.     srand(time(NULL));
  24.     for(int i=0; i<n; i++)
  25.         state[rand()%height][rand()%width] = 1;
  26. }
  27.  
  28. void print() // wypisuje stan planszy
  29. {
  30.     this_thread::sleep_for(chrono::milliseconds(sleep));
  31.     cout << "\033[2J\033[1;1H";
  32.     for(int y=0; y<height; y++)
  33.     {
  34.         for(int x=0; x<width; x++)
  35.             if(state[y][x] == 0) cout << ' ';
  36.             else cout << '*';
  37.         cout << endl;
  38.     }
  39. }
  40. // LICZENIE KOLEJNEGO STANU
  41. int neighbors(int y, int x) // liczy ilość sąsiadów dla danej komórki
  42. {
  43.     int i = 0;
  44.     if(y-1 >= 0 && x-1 >= 0) i += state[y-1][x-1];
  45.     if(y-1 >= 0) i += state[y-1][x];
  46.     if(y-1 >= 0 && x+1 < width) i += state[y-1][x+1];
  47.     if(x-1 >=0) i += state[y][x-1];
  48.     if(x+1 < width) i += state[y][x+1];
  49.     if(y+1 < height && x-1 >= 0) i += state[y+1][x-1];
  50.     if(y+1 < height) i += state[y+1][x];
  51.     if(y+1 < height && x+1 < width) i += state[y+1][x+1];
  52.     return i;
  53. }
  54.  
  55. int count() // oblicza kolejny stan
  56. {
  57.     int nextState[height][width]; // tablica tymczasowa
  58.     int end = 0; // flaga sprawdzająca czy symulacja została zakończona
  59.  
  60.     for(int y=0; y<height; y++)
  61.     {
  62.         for(int x=0; x<width; x++)
  63.         {
  64.             int nb = neighbors(y,x);
  65.             if(nb == 3 || (state[y][x] == 1 && nb == 2)) // zasady tworzenia nowego stanu
  66.             {
  67.                 nextState[y][x] = 1;
  68.                 end = 1;
  69.             }
  70.             else
  71.                 nextState[y][x] = 0;
  72.         }
  73.     }
  74.     // przepisanie stanów z tablicy tymczasowej
  75.     for(int y=0; y<height; y++)
  76.         for(int x=0; x<width; x++)
  77.             state[y][x] = nextState[y][x];
  78.     return end;
  79. }
  80.  
  81. int main()
  82. {
  83.     init();
  84.     while(count() != 0) print();
  85.     cout << endl << "Symulacja zakończona" << endl;
  86.     return 0;
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement