Skip to content

Instantly share code, notes, and snippets.

@xthexder
Last active March 25, 2023 05:34
Show Gist options
  • Save xthexder/4c7ee482f62e8eb28fd2651996408ab7 to your computer and use it in GitHub Desktop.
Save xthexder/4c7ee482f62e8eb28fd2651996408ab7 to your computer and use it in GitHub Desktop.
Conway's Game of Life Simulation - Generated by ChatGPT (GPT-3.5)
import argparse
import numpy as np
import time
# Define the initial states as strings
INITIAL_STATES = {
"glider": ".X\n"
"..X\n"
"XXX",
"blinker": "XXX",
"gun": "..........X.........................\n"
"..........XXXX......................\n"
"...........XXXX..........XX.........\n"
"XX.........X..X.........X.X.........\n"
"XX.........XXXX........XXX........XX\n"
"..........XXXX........XXX.........XX\n"
"..........X............XXX..........\n"
"........................X.X.........\n"
".........................XX.........",
"spaceship": ".X..X\n"
"X....\n"
"X...X\n"
"XXXX.",
"diamond": "............X............\n"
"...........X.X...........\n"
"..........X...X..........\n"
".........X.....X.........\n"
"........X.......X........\n"
".......X.........X.......\n"
"......X...........X......\n"
".....X.............X.....\n"
"....X...............X....\n"
"...X.................X...\n"
"....X...............X....\n"
".....X.............X.....\n"
"......X...........X......\n"
".......X.........X.......\n"
"........X.......X........\n"
".........X.....X.........\n"
"..........X...X..........\n"
"...........X.X...........\n"
"............X............",
}
# Convert the string representation of the initial state to a numpy array and insert it in the center of a 30x100 grid
for key, value in INITIAL_STATES.items():
rows = [list(line) for line in value.split("\n") if line.strip()]
num_rows = len(rows)
num_cols = len(rows[0])
padded_rows = [['.' for _ in range((100 - num_cols) // 2)] + row + ['.' for _ in range((100 - num_cols + 1) // 2)] for row in rows]
num_padded_rows = len(padded_rows)
num_padded_cols = len(padded_rows[0])
padded_array = np.zeros((30, 100))
start_i = (30 - num_padded_rows) // 2
start_j = (100 - num_padded_cols) // 2
for i in range(num_padded_rows):
for j in range(num_padded_cols):
if padded_rows[i][j] == 'X':
padded_array[start_i + i, start_j + j] = 1
INITIAL_STATES[key] = padded_array
# Define a function to update the state of the board for each time step
def update_board(state):
# Create a copy of the current state to update
new_state = np.copy(state)
# Loop over every cell in the grid
for i in range(state.shape[0]):
for j in range(state.shape[1]):
# Calculate the indices of the neighbors of the current cell
i_top = (i - 1) % state.shape[0]
i_bottom = (i + 1) % state.shape[0]
j_left = (j - 1) % state.shape[1]
j_right = (j + 1) % state.shape[1]
# Calculate the sum of the values of the neighboring cells
num_neighbors = (state[i_top, j_left] + state[i_top, j] + state[i_top, j_right] +
state[i, j_left] + state[i, j_right] +
state[i_bottom, j_left] + state[i_bottom, j] + state[i_bottom, j_right])
# Apply the rules of the Game of Life
if state[i, j] == 1 and (num_neighbors < 2 or num_neighbors > 3):
new_state[i, j] = 0
elif state[i, j] == 0 and num_neighbors == 3:
new_state[i, j] = 1
# Return the updated state
return new_state
# Define a function to display the current state of the board in the terminal
def display_board(state):
# Move the cursor to the top-left corner of the terminal
print("\033[H", end="")
# Loop over every cell in the grid
for i in range(state.shape[0]):
for j in range(state.shape[1]):
# Move the cursor to the current cell
print("\033[{};{}H".format(i+1, j+1), end="")
# Print '#' for live cells and '.' for dead cells
print("#" if state[i, j] else ".", end="")
# Move to the next row of the grid
print()
# Parse command line arguments
parser = argparse.ArgumentParser(description="Conway's Game of Life")
parser.add_argument("--state", type=str, choices=["glider", "random", "blinker", "gun", "spaceship", "diamond"], default="glider", help="The initial state of the board")
parser.add_argument("--frames", type=int, default=0, help="Number of frames to show, 0=infinite")
args = parser.parse_args()
if args.state == "random":
# If the initial state is "random", populate the state with a random pattern
initial_state = np.random.randint(2, size=(30, 100))
else:
# Set the initial state of the board based on the command line argument
initial_state = INITIAL_STATES[args.state]
# Simulate the Game of Life and display the results in the terminal
state = initial_state
i = 0
while i == 0 or i + 1 <= args.frames:
display_board(state)
state = update_board(state)
time.sleep(0.05)
if args.frames > 0:
i += 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment