Game of life on python that works on terminal
- User
Game of life on python that works on terminal
- Assistant
Game of life on python that works on terminal
Certainly! Below is a Python implementation of Conway's Game of Life that runs in the terminal. This program uses the curses library to handle the terminal display, allowing for smooth updates of the game state directly in the console.
import curses
import time
import random
def main(stdscr):
# Initialize the screen
curses.curs_set(0) # Hide the cursor
stdscr.nodelay(True) # Don't block I/O calls
stdscr.timeout(100) # Refresh every 100 milliseconds
# Get screen dimensions
sh, sw = stdscr.getmaxyx()
# Create a grid of cells
grid = [[0 for _ in range(sw)] for _ in range(sh)]
# Initialize the grid with random cells
for y in range(sh):
for x in range(sw):
grid[y][x] = random.choice([0, 1])
while True:
next_grid = [[0 for _ in range(sw)] for _ in range(sh)]
for y in range(sh):
for x in range(sw):
# Count the number of living neighbors
neighbors = 0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if i == 0 and j == 0:
continue
ny = (y + i) % sh
nx = (x + j) % sw
neighbors += grid[ny][nx]
# Apply Conway's rules
if grid[y][x] == 1 and neighbors in [2, 3]:
next_grid[y][x] = 1
elif grid[y][x] == 0 and neighbors == 3:
next_grid[y][x] = 1
else:
next_grid[y][x] = 0
grid = next_grid
# Draw the grid
stdscr.clear()
for y in range(sh):
for x in range(sw):
if grid[y][x] == 1:
stdscr.addch(y, x, 'O')
stdscr.refresh()
# Exit on 'q' key press
try:
key = stdscr.getkey()
if key == 'q':
break
except:
pass
def run_game():
curses.wrapper(main)
if __name__ == "__main__":
run_game()
Instructions:
Save the code: Copy and paste the code into a file named game_of_life.py.
Install dependencies:
On Unix-based systems (Linux, macOS), the curses module is included with Python.
On Windows, install the windows-curses package:
pip install windows-curses
Run the script:
python game_of_life.py
Interact:
Explanation:
Modules Used:
curses: For handling terminal display and keyboard input.time: To control the refresh rate (though in this code, we use curses.timeout() instead).random: To initialize the grid with random alive or dead cells.Game Initialization:
0 (dead) or 1 (alive).Main Loop:
% is used to wrap around the edges, effectively creating a toroidal array (the grid wraps around).next_grid) replaces the current state (grid) after each iteration.Rendering:
'O' character for alive cells. Dead cells are left blank.stdscr.refresh() updates the display.User Input:
'q' will exit the simulation.Notes:
Performance Considerations: For very large terminal windows, updating every cell may cause the program to slow down. Adjust the terminal size or the stdscr.timeout() value if needed.
Extending the Program:
Example Modification:
To start with a specific pattern instead of a random grid, you can initialize the grid with your desired configuration. Here's how you might add a glider:
# Clear the grid
grid = [[0 for _ in range(sw)] for _ in range(sh)]
# Define a glider pattern
glider = [
(0, 1),
(1, 2),
(2, 0),
(2, 1),
(2, 2),
]
# Place the glider in the grid
for y, x in glider:
grid[y][x] = 1
Troubleshooting:
'O' with a simple character like '*'.windows-curses is properly installed.Feel free to customize and expand upon this code to explore the fascinating world of cellular automata. If you have any questions or need further assistance, don't hesitate to ask!
Not yet included in a published release.