What's new

C & C++ Conway's game of life, help po

Tama bato lods?,
Gawin mo nalang example
C:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define GRID_SIZE 20
#define MAX_GENERATIONS 100

char grid[GRID_SIZE][GRID_SIZE];

void initializeGrid(char *config);
void printGrid();
void updateGrid();
int countNeighbors(int x, int y);

int main() {
    int generation = 0;
    int samePopulationCounter = 0;

    // Initialize the grid with the chosen configuration
    printf("Choose an initial configuration:\n");
    printf("1. Glider\n");
    printf("2. Small Exploder\n");
    printf("3. Exploder\n");
    printf("4. 10-cell row\n");
    printf("5. Lightweight Starship\n");
    printf("6. Tumbler\n");
    int configChoice;
    scanf("%d", &configChoice);
    switch (configChoice) {
        case 1:
            initializeGrid("Glider");
            break;
        case 2:
            initializeGrid("Small Exploder");
            break;
        case 3:
            initializeGrid("Exploder");
            break;
        case 4:
            initializeGrid("10-cell row");
            break;
        case 5:
            initializeGrid("Lightweight Starship");
            break;
        case 6:
            initializeGrid("Tumbler");
            break;
        default:
            printf("Invalid choice. Exiting program.\n");
            return 1;
    }

    // Generate successive generations until the maximum number of generations
    // is reached or the population has been stable for 10 generations
    while (generation < MAX_GENERATIONS) {
        printGrid();
        updateGrid();

        // Check if the population has been stable for 10 generations
        int i, j;
        int isStable = 1;
        char temp[GRID_SIZE][GRID_SIZE];
        for (i = 0; i < GRID_SIZE; i++) {
            for (j = 0; j < GRID_SIZE; j++) {
                temp[i][j] = grid[i][j];
            }
        }
        updateGrid();
        for (i = 0; i < GRID_SIZE; i++) {
            for (j = 0; j < GRID_SIZE; j++) {
                if (temp[i][j] != grid[i][j]) {
                    isStable = 0;
                    break;
                }
            }
        }
        if (isStable) {
            samePopulationCounter++;
            if (samePopulationCounter == 10) {
                break;
            }
        } else {
            samePopulationCounter = 0;
        }

        generation++;
        sleep(1);
        system("clear");
    }

    // Ask the user if they want to choose another configuration
    printf("Do you want to choose another configuration?\n");
    printf("1. Yes\n");
    printf("2. No\n");
    int anotherChoice;
    scanf("%d", &anotherChoice);
    if (anotherChoice == 1) {
 

Similar threads

Back
Top