ETikhonov

Simple game of life

Jul 30th, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.08 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>
  4. #include <algorithm>
  5. #include <unistd.h>
  6. class GameOfLife{
  7.     private:
  8.         int x_size;
  9.         int y_size;
  10.         std::vector<int> field;
  11.     public:
  12.         GameOfLife(int x = 50, int y = 80);
  13.         void print();
  14.         void set(int x, int y);
  15.         int proxima(int x, int y);
  16.         void evolve();
  17.        
  18. };
  19.  
  20. void GameOfLife::print(){
  21.     for (int i = 0; i < y_size+2; ++i)
  22.         std::cout<<"=";
  23.     std::cout<<std::endl;
  24.     std::string buf;
  25.     for (int i = 0; i < x_size; ++i){
  26.             std::cout<<"|";
  27.         for (int j = 0; j < y_size; ++j){
  28.             if(field[i*y_size+j] != 0){
  29.                 buf = "#";
  30.             }
  31.             else
  32.                 buf = " ";
  33.             std::cout<<buf;
  34.         }
  35.     std::cout<<"|"<<std::endl;
  36.     }
  37.     for (int i = 0; i < y_size+2; ++i)
  38.         std::cout<<"=";
  39.     std::cout<<std::endl;
  40.     usleep(80000);
  41. }
  42.  
  43. void GameOfLife::set(int x, int y){
  44.     field[x*y_size+y] = 1;
  45. }
  46.  
  47. int GameOfLife::proxima(int x, int y){
  48.     int sum = 0;
  49.     if (field[((x+1+x_size)%x_size)*y_size+(y+y_size)%y_size] == 1)
  50.         sum+=1;
  51.     if (field[((x+1+x_size)%x_size)*y_size+(y+1+y_size)%y_size] == 1)
  52.         sum+=1;
  53.     if (field[((x+x_size)%x_size)*y_size+(y+1+y_size)%y_size] == 1)
  54.         sum+=1;
  55.     if (field[((x-1+x_size)%x_size)*y_size+(y+1+y_size)%y_size] == 1)
  56.         sum+=1;
  57.     if (field[((x-1+x_size)%x_size)*y_size+(y+y_size)%y_size] == 1)
  58.         sum+=1;
  59.     if (field[((x-1+x_size)%x_size)*y_size+(y-1+y_size)%y_size] == 1)
  60.         sum+=1;
  61.     if (field[((x+x_size)%x_size)*y_size+(y-1+y_size)%y_size] == 1)
  62.         sum+=1;
  63.     if (field[((x+1+x_size)%x_size)*y_size+(y-1+y_size)%y_size] == 1)
  64.         sum+=1;
  65.     return sum;
  66. }
  67.  
  68. void GameOfLife::evolve(){
  69.     std::vector<int> next = field;
  70.     std::copy(field.begin(), field.end(), next.begin());
  71.     for (int i = 0; i < x_size; ++i){
  72.         for (int j = 0; j < y_size; ++j){
  73.             if (field[i*y_size+j] == 0)
  74.                 if (GameOfLife::proxima(i,j) == 3)
  75.                     next[i*y_size+j] = 1;
  76.             if (field[i*y_size+j] == 1)
  77.                 if (GameOfLife::proxima(i,j) <= 1 ||  GameOfLife::proxima(i,j) >= 4)
  78.                     next[i*y_size+j] = 0;
  79.         }
  80.     }
  81.     std::copy(next.begin(), next.end(), field.begin());
  82.     GameOfLife::print();
  83. }
  84.  
  85. GameOfLife::GameOfLife(int x, int y) : x_size(x), y_size(y), field(x_size*y_size,0){
  86.         x_size = x;
  87.         y_size = y;
  88.         std::vector<int> field(x_size*y_size);
  89. }
  90. int main(int argc, char* argv[]){
  91.     GameOfLife GOL(60,100);
  92.  
  93.     GOL.set(30,2);
  94.     GOL.set(30,3);
  95.     GOL.set(31,2);
  96.     GOL.set(31,3);
  97.     GOL.set(30,12);
  98.     GOL.set(31,12);
  99.     GOL.set(32,12);
  100.     GOL.set(29,13);
  101.     GOL.set(33,13);
  102.     GOL.set(28,14);
  103.     GOL.set(34,14);
  104.     GOL.set(28,15);
  105.     GOL.set(34,15);
  106.     GOL.set(31,16);
  107.     GOL.set(29,17);
  108.     GOL.set(33,17);
  109.     GOL.set(30,18);
  110.     GOL.set(31,18);
  111.     GOL.set(32,18);
  112.     GOL.set(31,19);
  113.     GOL.set(28,22);
  114.     GOL.set(29,22);
  115.     GOL.set(30,22);
  116.     GOL.set(28,23);
  117.     GOL.set(29,23);
  118.     GOL.set(30,23);
  119.     GOL.set(27,24);
  120.     GOL.set(31,24);
  121.     GOL.set(26,26);
  122.     GOL.set(27,26);
  123.     GOL.set(31,26);
  124.     GOL.set(32,26);
  125.     GOL.set(28,36);
  126.     GOL.set(28,37);
  127.     GOL.set(29,36);
  128.     GOL.set(29,37);
  129.    
  130.    
  131.     GOL.set(30,60+20);
  132.     GOL.set(30,61+20);
  133.     GOL.set(31,60+20);
  134.     GOL.set(31,62+20);
  135.     GOL.set(32,62+20);
  136.     GOL.set(33,62+20);
  137.     GOL.set(33,63+20);
  138.  
  139.     GOL.print();
  140.     while (1)
  141.         GOL.evolve();
  142.     return 0;
  143. }
Advertisement
Add Comment
Please, Sign In to add comment