Suzi_Cell_War.html
001| import numpy as np
002| import matplotlib.pyplot as plt
003| import matplotlib.animation as animation
004| from matplotlib.patches import RegularPolygon
005| import random
006|
007| grid_size = 20
008| num_generations = 500
009| mode = 'test'
010|
011| grid = np.random.rand(grid_size, grid_size, 3)
012| generations = [grid.copy()]
013|
014| neighbor_offsets_even = [(-1, 0), (-1, +1), (0, -1), (0, +1), (+1, 0), (+1, +1)]
015| neighbor_offsets_odd = [(-1, -1), (-1, 0), (0, -1), (0, +1), (+1, -1), (+1, 0)]
016|
017| def get_neighbors(grid, x, y):
018| offsets = neighbor_offsets_even if x % 2 == 0 else neighbor_offsets_odd
019| neighbors = []
020| for dx, dy in offsets:
021| nx, ny = x + dx, y + dy
022| if 0 <= nx < grid_size and 0 <= ny < grid_size:
023| neighbors.append(grid[nx, ny])
024| return neighbors
025|
026| def update_cell_color(grid, x, y, mode):
027| neighbors = get_neighbors(grid, x, y)
028| if not neighbors:
029| return grid[x, y]
030| if mode == 'average':
031| return np.mean([grid[x, y]] + neighbors, axis=0)
032| elif mode == 'random':
033| return random.choice(neighbors)
034| elif mode == 'test':
035| indices = [0,1,2,3,4,5,6,0,3,6]
036| selected_neighbors = [neighbors[i] for i in indices if i < len(neighbors)]
037| return random.choice(selected_neighbors)
038| elif mode == 'top-right':
039| offsets = neighbor_offsets_even if x % 2 == 0 else neighbor_offsets_odd
040| nx, ny = x + offsets[1][0], y + offsets[1][1]
041| if 0 <= nx < grid_size and 0 <= ny < grid_size:
042| return grid[nx, ny]
043| else:
044| return grid[x, y]
045| else:
046| raise ValueError("Unknown mode.")
047|
048|
049| for _ in range(num_generations):
050| new_grid = grid.copy()
051| for i in range(grid_size):
052| for j in range(grid_size):
053| new_grid[i, j] = update_cell_color(grid, i, j, mode)
054| generations.append(new_grid.copy())
055| grid = new_grid
056|
057|
058| fig, ax = plt.subplots()
059| fig.patch.set_facecolor('black')
060| ax.set_facecolor('black')
061| ax.set_aspect('equal')
062| ax.axis('off')
063|
064|
065| hex_radius = 1
066| hex_centers = []
067| for row in range(grid_size):
068| for col in range(grid_size):
069| x_offset = col * np.sqrt(3) * hex_radius
070| y_offset = row * 1.5 * hex_radius
071| if row % 2 == 1:
072| x_offset += np.sqrt(3) * hex_radius / 2
073| hex_centers.append((x_offset, y_offset))
074|
075|
076| patches = []
077| for (x, y) in hex_centers:
078| hex_patch = RegularPolygon((x, y), numVertices=6, radius=hex_radius, orientation=np.radians(30))
079| ax.add_patch(hex_patch)
080| patches.append(hex_patch)
081|
082|
083| ax.set_xlim(-1, np.sqrt(3) * grid_size)
084| ax.set_ylim(-1, 1.5 * grid_size)
085|
086| def update(frame):
087| data = generations[frame]
088| for i, patch in enumerate(patches):
089| row = i // grid_size
090| col = i % grid_size
091| patch.set_facecolor(data[row, col])
092| return patches
093|
094| ani = animation.FuncAnimation(fig, update, frames=len(generations), interval=50, blit=True)
095| ani.save('hex_visual.gif', writer='pillow')
096|
097| plt.show()
098|