Advertisement
DacCum

Game of Life(in dev...)

Sep 21st, 2021
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <conio.h>
  3. #include <windows.h>
  4.  
  5. using namespace std;
  6.  
  7. class map {
  8. public:
  9.     int size;
  10.     char **map_line;
  11.     int gen_i;
  12.     int n_alive;
  13.  
  14.     char** createMap(int size_t) {
  15.         char **map_t = new char* [size_t];
  16.         for (int i = 0; i < size_t; i++)
  17.             map_t[i] = new char[size_t];
  18.         return map_t;
  19.     }
  20.  
  21.     map() {
  22.         gen_i = 1;
  23.         n_alive = 0;
  24.         cout << "-->> Game of Life <<--\n";
  25.         cout << "Enter size of the Universe -> ";
  26.         cin >> size;
  27.         size += 2;
  28.         map_line = createMap(size);
  29.  
  30.         for (int i = 0; i < size; i++)
  31.             for (int j = 0; j < size; j++)
  32.                 map_line[i][j] = ' ';
  33.  
  34.         for (int i = 0; i < size; i++)
  35.             map_line[0][i] = '.';
  36.         for (int i = 0; i < size; i++)
  37.             map_line[i][size - 1] = '.';
  38.         for (int i = 0; i < size; i++)
  39.             map_line[size - 1][size - 1 - i] = '.';
  40.         for (int i = 0; i < size; i++)
  41.             map_line[size - 1 - i][0] = '.';
  42.     }
  43.  
  44.     void print() {
  45.         system("cls");
  46.         cout << "-->> Game of Life <<--\n";
  47.         cout << "Generation: " << gen_i << endl;
  48.         cout << "Number of alive cells: " << n_alive << endl;
  49.         for (int i = 0; i < size; i++) {
  50.             for (int j = 0; j < size; j++)
  51.                 cout << map_line[i][j];
  52.             cout << endl;
  53.         }
  54.     }
  55.  
  56.     bool input() {
  57.         int x, y;
  58.         print();
  59.         cout << "The arrangement of alive cells\n";
  60.         cout << "Enter [0] to complete.\n";
  61.         cout << "Enter the coordinates in the format [x, y] -> ";
  62.         cin >> x;
  63.         if (x == 0)
  64.             return 1;
  65.         cin >> y;
  66.         if (x < 1 || x > size - 2 || y < 1 || y > size - 2)
  67.             return 0;
  68.         y = size - 1 - y;
  69.         if (map_line[y][x] == '*')
  70.             return 0;
  71.         map_line[y][x] = '*';
  72.         n_alive++;
  73.         return 0;
  74.     }
  75.  
  76.     int neighbors(char q, int i, int j) {
  77.         int tmp_i = 0;
  78.         if (map_line[i + 1][j] == q)
  79.             tmp_i++;
  80.         if (map_line[i + 1][j + 1] == q)
  81.             tmp_i++;
  82.         if (map_line[i][j + 1] == q)
  83.             tmp_i++;
  84.         if (map_line[i - 1][j + 1] == q)
  85.             tmp_i++;
  86.         if (map_line[i - 1][j] == q)
  87.             tmp_i++;
  88.         if (map_line[i - 1][j - 1] == q)
  89.             tmp_i++;
  90.         if (map_line[i][j - 1] == q)
  91.             tmp_i++;
  92.         if (map_line[i + 1][j - 1] == q)
  93.             tmp_i++;
  94.         return tmp_i;
  95.     }
  96.  
  97.     bool run() {
  98.         gen_i++;
  99.         bool f_b = 1;
  100.  
  101.         char** tmp_map_line = createMap(size);
  102.  
  103.         for (int i = 0; i < size; i++) {
  104.             for (int j = 0; j < size; j++) {
  105.                 if (map_line[i][j] == '.')
  106.                     tmp_map_line[i][j] = '.';
  107.                 else if (map_line[i][j] == ' ') {
  108.                     if (neighbors('*', i, j) == 3) {
  109.                         tmp_map_line[i][j] = '*';
  110.                         n_alive++;
  111.                     } else
  112.                         tmp_map_line[i][j] = ' ';
  113.                 } else {
  114.                     if (neighbors('*', i, j) > 3 || neighbors('*', i, j) < 2) {
  115.                         tmp_map_line[i][j] = ' ';
  116.                         n_alive--;
  117.                     } else
  118.                         tmp_map_line[i][j] = '*';
  119.                 }
  120.             }
  121.         }
  122.         for (int i = 0; i < size; i++) {
  123.             for (int j = 0; j < size; j++) {
  124.                 if (map_line[i][j] != tmp_map_line[i][j])
  125.                     f_b = 0;
  126.                 map_line[i][j] = tmp_map_line[i][j];
  127.             }
  128.         }
  129.         return f_b;
  130.     }
  131.  
  132. };
  133.  
  134. int main() {
  135.     map map1;
  136.     map1.print();
  137.  
  138.     while (1){
  139.         if (map1.input() == 0)
  140.             continue;
  141.         else
  142.             break;     
  143.     }
  144.  
  145.     cout << "Start the game [Y]/[N] ? ";
  146.     char tmp;
  147.     cin >> tmp;
  148.     if (tmp == 'N')
  149.         return 0;
  150.     while (1) {
  151.         if (map1.run() == 0) {
  152.         map1.print();
  153.         Sleep(300);
  154.         }
  155.         else
  156.             break;
  157.     }
  158.     cout << "\nThe End.\n";
  159.  
  160.     return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement