ally
ally

Reputation: 11

Conway's game of life: How to show the next generation

I am trying to program conway's game of life. I want to print out the next generation with information showing which cells are underpopulated, over populated and juse right.

However, when the user enters that they want to see the next generation, the program outputs the same information.

Does anyone know how I can get the program to output the new information?

#include<iostream>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <limits>
#include <map>
#include <string>
#include <vector>

using namespace std;

void print_vector(vector<vector<int>> v, int, int);

int main() {
    int row;
    int col;
    int alive;
    int rowcalc, colcalc;
    string cell = "True";
    int max_value;

    // Input rows & columns
    cout << "Enter number of rows: ";
    cin >> row;
    cout << "Enter number of columns: ";
    cin >> col;

    vector<vector<int>> v1(row, vector<int>(col, 0));
    int count = 1;
    for (int i = 0; i < v1.size(); i++) {
        for (int j = 0; j < v1[i].size(); j++) {
            v1[i][j] = count;
            count = count + 1;
        }
    }
    print_vector(v1, row, col);
    max_value = row * col;

    while (cell == "True") {
        while (1) {
            cout << "which cell would you like to make alive(0 to stop) ";
            std::cin >> alive;
            if (!std::cin.fail() &&
                (std::cin.peek() == EOF || std::cin.peek() == '\n') &&
                col > 0 && col <= 1000000000) {
                break;
            } else {
                std::cin.clear();
                std::cin.ignore(256, '\n');
                std::cout << "try inputting something within the bounds. "
                          << std::endl;
            }
        }

        if (alive == 0) cell = "False";
        if (cell == "True") {
            if (alive > 0 && alive <= max_value) {
                rowcalc = floor((alive) / col);
                colcalc = alive % col;
                colcalc = colcalc - 1;
                if (alive % col == 0) {
                    rowcalc = rowcalc - 1;
                    colcalc = colcalc + col;
                }

                if (v1[rowcalc][colcalc] == 0)
                    cout << "already alive. " << endl;
                else
                    v1[rowcalc][colcalc] = 0;
                for (int i = 0; i < v1.size(); i++) {
                    for (int j = 0; j < v1[i].size(); j++)
                        cout << v1[i][j] << setw(3);
                    cout << setw(1);
                    cout << endl;
                }
            }
        }
    }

    string run = "y";
    vector<vector<int>> v2(row, vector<int>(col, 0));
    while (run == "y") {
        int count = 0;
        int count2 = 0;
        vector<int> under;
        vector<int> over;
        vector<int> breeding;
        vector<int> right;

        for (int i = 0; i < row; i++) {
            for (int j = 0; j < col; j++) {
                count = 0;
                if (i - 1 >= 0 && j - 1 >= 0) {
                    if (v1[i - 1][j - 1] == 0) {
                        count++;
                    }
                }
                if (i - 1 >= 0) {
                    if (v1[i - 1][j] == 0) {
                        count++;
                    }
                }
                if (i - 1 >= 0 && j + 1 < col) {
                    if (v1[i - 1][j + 1] == 0) {
                        count++;
                    }
                }
                if (j + 1 < col) {
                    if (v1[i][j + 1] == 0) {
                        count++;
                    }
                }
                if (j - 1 >= 0) {
                    if (v1[i][j - 1] == 0) {
                        count++;
                    }
                }
                if (i + 1 < row) {
                    if (v1[i + 1][j] == 0) {
                        count++;
                    }
                }
                if (i + 1 < row && j - 1 >= 0) {
                    if (v1[i + 1][j - 1] == 0) {
                        count++;
                    }
                }
                if (i + 1 < row && j + 1 < col) {
                    if (v1[i + 1][j + 1] == 0) {
                        count++;
                    }
                }
                count2++;
                if (v1[i][j] == 0) {
                    if (count < 2) {
                        under.push_back(count2);
                    }
                    if (count == 2 or count == 3) {
                        v2[i][j] = 0;
                        right.push_back(count2);
                    }
                    if (count > 3) {
                        over.push_back(count2);
                    }
                } else {
                    if (count == 3) {
                        breeding.push_back(count2);
                        v2[i][j] = 0;
                    }
                }
            }
        }
        for (int i = 0; i < breeding.size(); i++)
            cout << "breedinging" << breeding.at(i) << ' ';
        cout << endl;
        for (int i = 0; i < over.size(); i++)
            cout << "overpop" << over.at(i) << ' ';
        cout << endl;
        for (int i = 0; i < under.size(); i++)
            cout << "underpop" << under.at(i) << ' ';
        cout << endl;
        cout << "just right";
        for (int i = 0; i < right.size(); i++) {
            cout << right.at(i) << ' ';
        }
        cout << endl;

        cout << "do you want to see the next generation?: ";
        cin >> run;

        if (run == "y") {
            count2 = 0;
            count = 0;

            continue;
        } else {
            break;
        }
    }

    return 0;
}

void print_vector(vector<vector<int>> v, int row, int col) {
    int count;
    count = 1;
    // Input vector's elements
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            v[i][j] = count;
            count++;
        }
    }
    for (int i = 0; i < row; i++) {
        for (int j = 0; j < col; j++) {
            cout << v[i][j] << setw(3);
        }
        cout << setw(1);
        cout << endl;
    }
}

Upvotes: 1

Views: 102

Answers (1)

rturrado
rturrado

Reputation: 8074

I've done some changes to your code:

  • First of all, tidy up the output a little bit.
  • Also, which I think it was your main question, print the grid after each iteration.
    Alongside this, I have removed v2. I understand that you have a starting grid, over which you iterate, modifying it with each iteration. I presume you don't need a copy of the original grid.
  • Finally, apply some coding best practices:
    • initialize local variables.
    • move declaration of variables near to its first use.
    • use braces even for one-line blocks.
    • use descriptive variable names: grid instead of v1, alive_neighbours instead of count, current_cell instead of count2...
    • avoid the use of floor for integer division.
    • remove some unnecessary headers.
    • simplify some constructs.
    • remove the using namespace std.
    • ...

Check here if the program is functioning properly now. If not, you will have at least a base point to start debugging it.

Upvotes: 2

Related Questions