import time
import os
import random
percent = int(input("What percent of cells would you like to start off alive?"))
rows = int(input("How large would you like the grid to be? (10-50)"))
prob = percent/100
grid = []
def makeGrid(grid):
for i in range(rows):
row = []
for j in range(rows):
if random.random() < prob:
row.append(1)
else:
row.append(0)
grid.append(row)
for row in grid:
print(''.join('██' if cell == 1 else ' ' for cell in row))
def printGrid(grid):
for row in grid:
print(''.join('██' if cell == 1 else ' ' for cell in row))
def count_neighbors(grid, x, y):
neighbors = 0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if i == 0 and j == 0:
continue
neighbor_x, neighbor_y = x + i, y + j
if 0 <= neighbor_x < rows and 0 <= neighbor_y < rows:
neighbors += grid[neighbor_x][neighbor_y]
return neighbors
def next_generation(grid):
new_grid = []
for x in range(rows):
new_row = []
for y in range(rows):
neighbors = count_neighbors(grid, x, y)
if grid[x][y] == 1:
if neighbors < 2 or neighbors > 3:
new_row.append(0)
else:
new_row.append(1)
else:
if neighbors == 3:
new_row.append(1)
else:
new_row.append(0)
new_grid.append(new_row)
return new_grid
makeGrid(grid)
while True:
os.system('clear')
printGrid(grid)
grid = next_generation(grid)
time.sleep(0.2)
Green Game of Life(+ other minor changes)
import time
import os
import random
class GameOfLife:
def __init__(self, rows, percent_alive, delay=1):
self.rows = rows
self.percent_alive = percent_alive
self.prob = percent_alive / 100
self.delay = delay
self.grid = self.make_grid()
def make_grid(self):
grid = []
for i in range(self.rows):
row = []
for j in range(self.rows):
if random.random() < self.prob:
row.append(1)
else:
row.append(0)
grid.append(row)
return grid
def print_grid(self):
for row in self.grid:
print(''.join('\033[42m \033[0m' if cell == 1 else ' ' for cell in row))
def count_neighbors(self, x, y):
neighbors = 0
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if i == 0 and j == 0:
continue
nx, ny = x + i, y + j
if 0 <= nx < self.rows and 0 <= ny < self.rows:
neighbors += self.grid[nx][ny]
return neighbors
def next_generation(self):
new_grid = []
for x in range(self.rows):
new_row = []
for y in range(self.rows):
neighbors = self.count_neighbors(x, y)
if self.grid[x][y] == 1:
if neighbors < 2 or neighbors > 3:
new_row.append(0)
else:
new_row.append(1)
else:
if neighbors == 3:
new_row.append(1)
else:
new_row.append(0)
new_grid.append(new_row)
self.grid = new_grid
def run(self):
while True:
os.system('clear')
self.print_grid()
self.next_generation()
time.sleep(self.delay)
if __name__ == "__main__":
percent = int(input("What percent of cells would you like to start off alive? "))
rows = int(input("How large would you like the grid to be? (10–50) "))
delay = float(input("How many seconds between updates? (e.g. 0.5, 1) "))
game = GameOfLife(rows, percent, delay)
game.run()
Rainbow of Life
import time
import os
import random
class GameOfLife:
def __init__(self, rows, percent_alive, delay=1):
self.rows = rows
self.percent_alive = percent_alive
self.prob = percent_alive / 100
self.delay = delay
self.grid = self.make_grid()
def random_color(self):
return random.choice([(255, 0, 0), (0, 255, 0), (0, 0, 255)])
def make_grid(self):
grid = []
for i in range(self.rows):
row = []
for j in range(self.rows):
if random.random() < self.prob:
row.append([1, self.random_color()]) # alive with random color
else:
row.append([0, (0, 0, 0)]) # dead, black
grid.append(row)
return grid
def print_grid(self):
for row in self.grid:
line = ''
for cell in row:
alive, color = cell
if alive:
r, g, b = color
line += f'\033[48;2;{r};{g};{b}m \033[0m
else:
line += ' '
print(line)
def count_neighbors(self, x, y):
neighbors = 0
color_sum = [0, 0, 0]
for i in [-1, 0, 1]:
for j in [-1, 0, 1]:
if i == 0 and j == 0:
continue
nx, ny = x + i, y + j
if 0 <= nx < self.rows and 0 <= ny < self.rows:
alive, color = self.grid[nx][ny]
if alive:
neighbors += 1
color_sum[0] += color[0]
color_sum[1] += color[1]
color_sum[2] += color[2]
if neighbors > 0:
avg_color = tuple(min(255, max(0, c // neighbors)) for c in color_sum)
else:
avg_color = (0, 0, 0)
return neighbors, avg_color
def next_generation(self):
new_grid = []
for x in range(self.rows):
new_row = []
for y in range(self.rows):
alive, color = self.grid[x][y]
neighbors, avg_color = self.count_neighbors(x, y)
if alive:
if neighbors < 2 or neighbors > 3:
new_row.append([0, (0, 0, 0)]) # dies
else:
new_row.append([1, color]) # stays alive, keep color
else:
if neighbors == 3:
new_row.append([1, avg_color]) # born with mixed color
else:
new_row.append([0, (0, 0, 0)]) # stays dead
new_grid.append(new_row)
self.grid = new_grid
def run(self):
while True:
os.system('clear')
self.print_grid()
self.next_generation()
time.sleep(self.delay)
# ---- Run the Game ----
if __name__ == "__main__":
percent = int(input("What percent of cells would you like to start off alive? "))
rows = int(input("How large would you like the grid to be? (10–50) "))
delay = float(input("How many seconds between updates? (e.g. 0.5, 1) "))
game = GameOfLife(rows, percent, delay)
game.run()