Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <conio.h>
- #include <windows.h>
- using namespace std;
- class map {
- public:
- int size;
- char **map_line;
- int gen_i;
- int n_alive;
- char** createMap(int size_t) {
- char **map_t = new char* [size_t];
- for (int i = 0; i < size_t; i++)
- map_t[i] = new char[size_t];
- return map_t;
- }
- map() {
- gen_i = 1;
- n_alive = 0;
- cout << "-->> Game of Life <<--\n";
- cout << "Enter size of the Universe -> ";
- cin >> size;
- size += 2;
- map_line = createMap(size);
- for (int i = 0; i < size; i++)
- for (int j = 0; j < size; j++)
- map_line[i][j] = ' ';
- for (int i = 0; i < size; i++)
- map_line[0][i] = '.';
- for (int i = 0; i < size; i++)
- map_line[i][size - 1] = '.';
- for (int i = 0; i < size; i++)
- map_line[size - 1][size - 1 - i] = '.';
- for (int i = 0; i < size; i++)
- map_line[size - 1 - i][0] = '.';
- }
- void print() {
- system("cls");
- cout << "-->> Game of Life <<--\n";
- cout << "Generation: " << gen_i << endl;
- cout << "Number of alive cells: " << n_alive << endl;
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++)
- cout << map_line[i][j];
- cout << endl;
- }
- }
- bool input() {
- int x, y;
- print();
- cout << "The arrangement of alive cells\n";
- cout << "Enter [0] to complete.\n";
- cout << "Enter the coordinates in the format [x, y] -> ";
- cin >> x;
- if (x == 0)
- return 1;
- cin >> y;
- if (x < 1 || x > size - 2 || y < 1 || y > size - 2)
- return 0;
- y = size - 1 - y;
- if (map_line[y][x] == '*')
- return 0;
- map_line[y][x] = '*';
- n_alive++;
- return 0;
- }
- int neighbors(char q, int i, int j) {
- int tmp_i = 0;
- if (map_line[i + 1][j] == q)
- tmp_i++;
- if (map_line[i + 1][j + 1] == q)
- tmp_i++;
- if (map_line[i][j + 1] == q)
- tmp_i++;
- if (map_line[i - 1][j + 1] == q)
- tmp_i++;
- if (map_line[i - 1][j] == q)
- tmp_i++;
- if (map_line[i - 1][j - 1] == q)
- tmp_i++;
- if (map_line[i][j - 1] == q)
- tmp_i++;
- if (map_line[i + 1][j - 1] == q)
- tmp_i++;
- return tmp_i;
- }
- bool run() {
- gen_i++;
- bool f_b = 1;
- char** tmp_map_line = createMap(size);
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- if (map_line[i][j] == '.')
- tmp_map_line[i][j] = '.';
- else if (map_line[i][j] == ' ') {
- if (neighbors('*', i, j) == 3) {
- tmp_map_line[i][j] = '*';
- n_alive++;
- } else
- tmp_map_line[i][j] = ' ';
- } else {
- if (neighbors('*', i, j) > 3 || neighbors('*', i, j) < 2) {
- tmp_map_line[i][j] = ' ';
- n_alive--;
- } else
- tmp_map_line[i][j] = '*';
- }
- }
- }
- for (int i = 0; i < size; i++) {
- for (int j = 0; j < size; j++) {
- if (map_line[i][j] != tmp_map_line[i][j])
- f_b = 0;
- map_line[i][j] = tmp_map_line[i][j];
- }
- }
- return f_b;
- }
- };
- int main() {
- map map1;
- map1.print();
- while (1){
- if (map1.input() == 0)
- continue;
- else
- break;
- }
- cout << "Start the game [Y]/[N] ? ";
- char tmp;
- cin >> tmp;
- if (tmp == 'N')
- return 0;
- while (1) {
- if (map1.run() == 0) {
- map1.print();
- Sleep(300);
- }
- else
- break;
- }
- cout << "\nThe End.\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement