Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <vector>
- #include <algorithm>
- #include <unistd.h>
- class GameOfLife{
- private:
- int x_size;
- int y_size;
- std::vector<int> field;
- public:
- GameOfLife(int x = 50, int y = 80);
- void print();
- void set(int x, int y);
- int proxima(int x, int y);
- void evolve();
- };
- void GameOfLife::print(){
- for (int i = 0; i < y_size+2; ++i)
- std::cout<<"=";
- std::cout<<std::endl;
- std::string buf;
- for (int i = 0; i < x_size; ++i){
- std::cout<<"|";
- for (int j = 0; j < y_size; ++j){
- if(field[i*y_size+j] != 0){
- buf = "#";
- }
- else
- buf = " ";
- std::cout<<buf;
- }
- std::cout<<"|"<<std::endl;
- }
- for (int i = 0; i < y_size+2; ++i)
- std::cout<<"=";
- std::cout<<std::endl;
- usleep(80000);
- }
- void GameOfLife::set(int x, int y){
- field[x*y_size+y] = 1;
- }
- int GameOfLife::proxima(int x, int y){
- int sum = 0;
- if (field[((x+1+x_size)%x_size)*y_size+(y+y_size)%y_size] == 1)
- sum+=1;
- if (field[((x+1+x_size)%x_size)*y_size+(y+1+y_size)%y_size] == 1)
- sum+=1;
- if (field[((x+x_size)%x_size)*y_size+(y+1+y_size)%y_size] == 1)
- sum+=1;
- if (field[((x-1+x_size)%x_size)*y_size+(y+1+y_size)%y_size] == 1)
- sum+=1;
- if (field[((x-1+x_size)%x_size)*y_size+(y+y_size)%y_size] == 1)
- sum+=1;
- if (field[((x-1+x_size)%x_size)*y_size+(y-1+y_size)%y_size] == 1)
- sum+=1;
- if (field[((x+x_size)%x_size)*y_size+(y-1+y_size)%y_size] == 1)
- sum+=1;
- if (field[((x+1+x_size)%x_size)*y_size+(y-1+y_size)%y_size] == 1)
- sum+=1;
- return sum;
- }
- void GameOfLife::evolve(){
- std::vector<int> next = field;
- std::copy(field.begin(), field.end(), next.begin());
- for (int i = 0; i < x_size; ++i){
- for (int j = 0; j < y_size; ++j){
- if (field[i*y_size+j] == 0)
- if (GameOfLife::proxima(i,j) == 3)
- next[i*y_size+j] = 1;
- if (field[i*y_size+j] == 1)
- if (GameOfLife::proxima(i,j) <= 1 || GameOfLife::proxima(i,j) >= 4)
- next[i*y_size+j] = 0;
- }
- }
- std::copy(next.begin(), next.end(), field.begin());
- GameOfLife::print();
- }
- GameOfLife::GameOfLife(int x, int y) : x_size(x), y_size(y), field(x_size*y_size,0){
- x_size = x;
- y_size = y;
- std::vector<int> field(x_size*y_size);
- }
- int main(int argc, char* argv[]){
- GameOfLife GOL(60,100);
- GOL.set(30,2);
- GOL.set(30,3);
- GOL.set(31,2);
- GOL.set(31,3);
- GOL.set(30,12);
- GOL.set(31,12);
- GOL.set(32,12);
- GOL.set(29,13);
- GOL.set(33,13);
- GOL.set(28,14);
- GOL.set(34,14);
- GOL.set(28,15);
- GOL.set(34,15);
- GOL.set(31,16);
- GOL.set(29,17);
- GOL.set(33,17);
- GOL.set(30,18);
- GOL.set(31,18);
- GOL.set(32,18);
- GOL.set(31,19);
- GOL.set(28,22);
- GOL.set(29,22);
- GOL.set(30,22);
- GOL.set(28,23);
- GOL.set(29,23);
- GOL.set(30,23);
- GOL.set(27,24);
- GOL.set(31,24);
- GOL.set(26,26);
- GOL.set(27,26);
- GOL.set(31,26);
- GOL.set(32,26);
- GOL.set(28,36);
- GOL.set(28,37);
- GOL.set(29,36);
- GOL.set(29,37);
- GOL.set(30,60+20);
- GOL.set(30,61+20);
- GOL.set(31,60+20);
- GOL.set(31,62+20);
- GOL.set(32,62+20);
- GOL.set(33,62+20);
- GOL.set(33,63+20);
- GOL.print();
- while (1)
- GOL.evolve();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment