Skip to content

ParisaArbab/Conway-s-Game-of-Life

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 

Repository files navigation

Conway-s-Game-of-Life

implementing Conway’s Game of Life. This code implements Conway's Game of Life, a cellular automaton devised by the British mathematician John Horton Conway. Here's an explanation of each part of the code:

conway function:

This function generates a square two-dimensional NumPy array representing the Conway board. Parameters: s: Size of the board (s x s). p: Probability of a cell being alive initially (default is 0.1). It initializes an empty board filled with zeros and randomly selects a certain number of cells to be alive based on the given probability p. count_neighbors function:

This function counts the number of live neighbors for each cell on the board. Parameters: board: The Conway board. It defines a kernel for counting neighbors and iterates over each cell in the board to compute the number of live neighbors using convolution. The center cell value is subtracted from the total neighbor count to account for self-counting. advance function:

This function advances the Conway board t time steps according to the game rules. Parameters: b: The Conway board. t: Number of time steps. It iterates over t time steps, computes the number of live neighbors for each cell using the count_neighbors function, and applies the game rules to update the board accordingly. display_board function:

This function displays the Conway board. Parameter: board: The Conway board to display. It prints each row of the board with live cells represented by '1' and dead cells represented by '0'. calculate_live_cells_percentage function:

This function calculates the percentage of live cells in the given board. Parameter: board: The Conway board. It calculates the total number of cells and the number of live cells, then returns the percentage of live cells. Example usage:

It initializes an initial board using the conway function and displays it. It advances the board for 5 time steps, displaying the board after each step. It calculates and prints the percentage of live cells in the initial board. This code provides a comprehensive implementation of Conway's Game of Life, allowing you to generate and simulate the evolution of the board according to the defined rules.