BIRLA INSTITUTE OF
TECHNOLOGY , MESRA RANCHI
Programming for Problem
Solving Assignment
Submitted to : Department of
Computer Science & Engineering
Professor Abhijeet Mustafi
Submitted by :
Diwakar Chaudhary:
Btech/10426/24
Aakash Mahatha:
Btech/10402/24
Branch : CSE
Sec : F
Assignment 1
Write a program to simulate the game of life on a 48 x 48 sized grid. The initial “alive squares”
are to be read in from a text file containing a comma separated set of x and y co-ordinates, one
cell per line e.g.
3, 4
6, 8
12, 6
….
The rules of the game demand that cells continue to live on die or are freshly born in the next
iteration depending on the status of a cell in the previous iteration. Thus only cells with two live
neighbours in the current generation can spawn or continue to live on in the next generation.
Your task is to print the boar after k iterations starting with the initial population of live cells.
Topic : Conway's Game of Life 48x48 Grid
1. What is Conway's Game of Life?
[Link] by John Conway in 1970.
b. A zero-player cellular automation.
[Link] rules lead to complex behaviors.
[Link] evolution and pattern growth.
Pseudocode Overview:
1. Create a 48 x 48 grid initialized with 0s (all cells dead)
2. Read initial live cell coordinates from a file
- For each (x, y), set grid[x][y] = 1
3. Ask user for:
- k = number of iterations
4. Repeat the following k times:
a. For each cell (i, j) in the grid:
i. Count the number of live neighbors (up to 8 directions)
ii. If count == 2, the cell becomes alive in next grid
Else, the cell becomes dead
b. Copy next generation grid back into the original grid
5. Print the final grid:
- Show 'O' for alive, '.' for dead
complete code
#include <stdio.h> // Count number of live neighbors
int countNeighbors(int x, int y) {
#include <stdlib.h> int count = 0;
int dx[] = {-1, -1, -1, 0, 0, 1, 1, 1};
#define SIZE 48 int dy[] = {-1, 0, 1, -1, 1,-1, 0, 1};
for (int d = 0; d < 8; d++) {
// Global grids int nx = x + dx[d];
int grid[SIZE][SIZE] = {0}; int ny = y + dy[d];
int next[SIZE][SIZE] = {0};
if (isValid(nx, ny) && grid[nx][ny] ==
1)
// Check if cell is within grid bounds count++;
int isValid(int x, int y) { }
return (x >= 0 && x < SIZE && y >= 0 return count;
}
&& y < SIZE);
}
for (int j = 0; j < SIZE; j++)
// Update the board according to rules grid[i][j] = next[i][j];
void updateGrid() { }
for (int i = 0; i < SIZE; i++) {
for (int j = 0; j < SIZE; j++) { // Print the grid
int neighbors = countNeighbors(i, j); void printGrid() {
for (int i = 0; i < SIZE; i++) {
// Only 2 neighbors rule for (int j = 0; j < SIZE; j++) {
if (neighbors == 2) if (grid[i][j])
next[i][j] = 1; printf("O ");
else else
next[i][j] = 0; printf(". ");
} }
} printf("\n");
}
// Copy next to grid printf("\n");
for (int i = 0; i < SIZE; i++) }
int main() { // Ask user how many iterations to
FILE *file; simulate
int x, y, k; printf("Enter number of iterations (k): ");
scanf("%d", &k);
// Open file with coordinates
file = fopen("[Link]", "r"); // Simulate k generations
if (file == NULL) { for (int gen = 0; gen < k; gen++) {
printf("Error opening file.\n"); updateGrid();
return 1; }
}
// Print final board
// Read coordinates of live cells printf("\nFinal state after %d iterations:\
while (fscanf(file, "%d, %d", &x, &y) == n", k);
2) { printGrid();
if (isValid(x, y))
grid[x][y] = 1; return 0;
} }
Simplified Rules Used:
[Link] 8 neighboring cells
2. A cell becomes alive in next gen if it has exactly 2 live neighbors
3. All other cells die or remain dead
Input Format:
File contains coordinates of initial live cells:
3, 3
3, 4
3, 5
4, 3
4, 5
5, 3
5, 4
5, 5
6, 4
Input via terminal:
Enter number of iterations (k): 1
n (number of initial live cells)
Sample Output (after k iterations):
.........
: Simplified Rules Used
....O....
Check 8 neighboring cells
..O...O..
A cell becomes alive in next
.[Link].O. gen if it has exactly 2 live
neighbors
..O...O..
All other cells die
...OOO...or remain dead
.........
O = alive cell
. = dead cell
Pseudocode Overview:
1. Create a 48 x 48 grid initialized with 0s (all cells dead)
2. Read initial live cell coordinates from a file
- For each (x, y), set grid[x][y] = 1
3. Ask user for:
- k = number of iterations
4. Repeat the following k times:
a. For each cell (i, j) in the grid:
i. Count the number of live neighbors (up to 8 directions)
ii. If count == 2, the cell becomes alive in next grid
Else, the cell becomes dead
b. Copy next generation grid back into the original grid
5. Print the final grid:
- Show 'O' for alive, '.' for dead
Core Code Logic (C Language)
Neighbor count logic:
int count(int x, int y) {
int c = 0;
// check all 8 neighbors
return c;
}
Update loop:
next[i][j] = (count(i,j) == 2);
Discussion on Unhandled Cases:
[Link] File Format: If the input file has incorrect formatting (e.g., missing
commas or non-integer values), the program may behave unexpectedly.
[Link] Out of Bounds: Coordinates outside the 0–47 range are ignored.
The program does not flag them as errors.
[Link] k Value: If the number of iterations k is very high, execution might be
slow due to no optimization for stable states.
[Link] Optimization: The program uses two full-size 2D arrays, which is fine
for 48x48 but could be inefficient for larger grids.
Limitations & Improvements
Only supports 2-neighbor rule (not standard Game of Life)
Fixed 48x48 grid (not dynamic)
No visual animation or GUI
Improvements:
Dynamic grid
Original rules support
Visual display with SDL or graphics.h
References
Conway, J. H., 1970
Wikipedia: Conway’s Game of Life
GeeksforGeeks: C Programming Basics
MIT OCW: Automata Theory
Thank you.