The Game of life in C

The Game of life in C

Inspiration

I have been reading C Programming Language by Brian W. Kernighan and Dennis M. Ritchie (1978 edition). After reading the 0th chapter where Kernighan gives us the background and reasons why he had to develop new programming languages, and what the issues faced by programmers during that time, I realized 1978 was an important point for computer science. The institutes like AT&T had huge and expensive computers but at the same time, small, cheap but not that powerful computers were made available in the market. So they tried to build an OS that could work on every machine but it was a daunting task because they would have to write it in Assembly Language and since the structure of every machine was different it was almost impossible to write a common program for all these machines. C tried to solve this problem. Before C languages like FORTRAN, BCPL, and PL/I didn't consider bits but words. Because at that era computers were used for mathematical computation but AT&T was a telecommunication company. All it had to do was to manage phone lines and generate and maintain bills.

About a week ago I watched a YouTube video of Conway's Game of Life where he said that the game was first compiled on PDP-8, so O decided to test my C learnings by writing a program to simulate Conway's Game of Life

Code and output

In the previous blog, we implemented the Game of Life in Python, we will be using the same logic here. While writing the program I decided that we will be printing a 2D array on the terminal where 1 (ON) will be represented by '*' and 0(OFF) by ' '. To create a random array we used rand() and srand(time(NULL)) to set the seed. Now I wanted my program to halt for a few seconds before reiterating so I used Sleep(1000). After doing the computation we will copy the new_matrix to the matrix and will clear the terminal. This will be iterated until a Keyboard Interruption. Below is the code and the output.

#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <windows.h>
#include <string.h>
void print_matrix(int matrix[10][10]);
int sum_neighbors(int matrix[10][10], int i, int j);
int main()
{
    srand(time(NULL));
    int r, state, sum;
    int i, j;
    int matrix[10][10];
    int new_matrix[10][10];
    for (i = 0; i < 10; i++)
    {
        for (j = 0; j < 10; j++)
        {
            r = rand() % 2 + 0;
            matrix[i][j] = r;
        }
    }
    while (1)
    {
        Sleep(1000);

        for (i = 0; i < 10; i++)
        {
            for (j = 0; j < 10; j++)
            {
                state = matrix[i][j];
                // if (i == 0 || j == 0 || i == 9 || j == 9)
                // {
                //     new_matrix[i][j] = state;
                // }
                // else
                // {
                sum = sum_neighbors(matrix, i, j);
                if ((sum < 2 || sum > 3) && state == 1)
                {
                    new_matrix[i][j] = 0;
                }
                else if (sum == 3 && state == 0)
                {
                    new_matrix[i][j] = 1;
                }
                else
                {
                    new_matrix[i][j] = state;
                }
                // }
            }
        }

        // print_matrix(matrix);
        system("cls");
        print_matrix(new_matrix);
        memcpy(matrix, new_matrix, sizeof new_matrix);
    }
}

int sum_neighbors(int matrix[10][10], int i, int j)
{
    int x, y, sum;
    sum = 0;
    for (x = -1; x <= 1; x++)
    {
        for (y = -1; y <= 1; y++)
        {
            sum = sum + matrix[(x + i + 10) % 10][(y + j + 10) % 10];
        }
    }
    sum = sum - matrix[i][j];
    return sum;
}

void print_matrix(int matrix[10][10])
{
    int i, j, state;
    for (i = 0; i < 10; i++)
    {
        printf("\n");
        printf("\t");
        for (j = 0; j < 10; j++)
        {
            state = matrix[i][j];
            if (state == 1)
                printf("%s ", "*");
            else
                printf("%s ", " ");
        }
    }
    printf("\n");
}

What's next?

Until now our initial configuration was completely random, in the upcoming blog we will customize our initial configuration and its size so that the user can choose if he wants a glider gun, a toad, or any other "body" from the Game of Life. We will be writing the code for the next program in Rust.