create a simulation of conway's game of life
To upload files, please first save the app
import streamlit as st
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
st.title("Conway's Game of Life")
# Function to update the game state
def update_board(board):
# Count the number of neighbors for each cell
neighbors = (np.roll(board, 1, 0) + np.roll(board, -1, 0) +
np.roll(board, 1, 1) + np.roll(board, -1, 1) +
np.roll(np.roll(board, 1, 0), 1, 1) + np.roll(np.roll(board, 1, 0), -1, 1) +
np.roll(np.roll(board, -1, 0), 1, 1) + np.roll(np.roll(board, -1, 0), -1, 1))
# Apply Conway's rules
new_board = (neighbors == 3) | (board & (neighbors == 2))
return new_board
# Function to initialize the board
def initialize_board(size):
return np.random.choice([False, True], size=(size, size), p=[0.7, 0.3])
# Sidebar for user interaction
size = st.sidebar.slider("Grid Size", min_value=5, max_value=100, value=20)
iterations = st.sidebar.slider("Iterations", min_value=1, max_value=100, value=10)
# Initialize the board
board = initialize_board(size)
# Run the game for specified iterations
for _ in range(iterations):
board = update_board(board)
# Display the result using matplotlib
plt.imshow(board, cmap='binary')
plt.axis('off')
st.pyplot(plt)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?