-
-
Notifications
You must be signed in to change notification settings - Fork 46.9k
Hacktoberfest 2020: Conway's Game of Life #3070
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
743c0ca
Created conways_game_of_life.py
f6e2b38
Added new_generation(list[int[int]]) -> list[list[int]]
1aff5ad
Added glider example
e404734
Added comments and shortened glider example
64bfe92
Fixed index out of bounds error
7dbe709
Added test
0d8df7f
Added blinker example
c33f0ef
Added ability to generate images
2de3779
Moved image generating code into a separate function
f7586fc
Added comments
f758067
Comment
f209481
Reformatted file
cb7a4db
Formatting
30829c8
Removed glider test
93ae236
Update cellular_automata/conways_game_of_life.py
8660547
Update conways_game_of_life.py
poyea cff3f91
Update conways_game_of_life.py
poyea File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
""" | ||
Conway's Game of Life implemented in Python. | ||
https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
from PIL import Image | ||
|
||
# Define glider example | ||
GLIDER = [ | ||
[0, 1, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 1, 0, 0, 0, 0, 0], | ||
[1, 1, 1, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
[0, 0, 0, 0, 0, 0, 0, 0], | ||
] | ||
|
||
# Define blinker example | ||
BLINKER = [[0, 1, 0], [0, 1, 0], [0, 1, 0]] | ||
|
||
|
||
def new_generation(cells: list[list[int]]) -> list[list[int]]: | ||
""" | ||
Generates the next generation for a given state of Conway's Game of Life. | ||
>>> new_generation(BLINKER) | ||
[[0, 0, 0], [1, 1, 1], [0, 0, 0]] | ||
""" | ||
next_generation = [] | ||
for i in range(len(cells)): | ||
next_generation_row = [] | ||
for j in range(len(cells[i])): | ||
# Get the number of live neighbours | ||
neighbour_count = 0 | ||
if i > 0 and j > 0: | ||
neighbour_count += cells[i - 1][j - 1] | ||
if i > 0: | ||
neighbour_count += cells[i - 1][j] | ||
if i > 0 and j < len(cells[i]) - 1: | ||
neighbour_count += cells[i - 1][j + 1] | ||
if j > 0: | ||
neighbour_count += cells[i][j - 1] | ||
if j < len(cells[i]) - 1: | ||
neighbour_count += cells[i][j + 1] | ||
if i < len(cells) - 1 and j > 0: | ||
neighbour_count += cells[i + 1][j - 1] | ||
if i < len(cells) - 1: | ||
neighbour_count += cells[i + 1][j] | ||
if i < len(cells) - 1 and j < len(cells[i]) - 1: | ||
neighbour_count += cells[i + 1][j + 1] | ||
|
||
# Rules of the game of life (excerpt from Uncyclopedia): | ||
# 1. Any live cell with two or three live neighbours survives. | ||
# 2. Any dead cell with three live neighbours becomes a live cell. | ||
# 3. All other live cells die in the next generation. | ||
# Similarly, all other dead cells stay dead. | ||
alive = cells[i][j] == 1 | ||
if ( | ||
(alive and 2 <= neighbour_count <= 3) | ||
or not alive | ||
and neighbour_count == 3 | ||
): | ||
next_generation_row.append(1) | ||
else: | ||
next_generation_row.append(0) | ||
|
||
next_generation.append(next_generation_row) | ||
return next_generation | ||
|
||
|
||
def generate_images(cells: list[list[int]], frames) -> list[Image.Image]: | ||
""" | ||
Generates a list of images of subsequent Game of Life states. | ||
""" | ||
images = [] | ||
for _ in range(frames): | ||
# Create output image | ||
img = Image.new("RGB", (len(cells[0]), len(cells))) | ||
pixels = img.load() | ||
|
||
# Save cells to image | ||
for x in range(len(cells)): | ||
for y in range(len(cells[0])): | ||
colour = 255 - cells[y][x] * 255 | ||
pixels[x, y] = (colour, colour, colour) | ||
|
||
# Save image | ||
images.append(img) | ||
cells = new_generation(cells) | ||
return images | ||
|
||
|
||
if __name__ == "__main__": | ||
images = generate_images(GLIDER, 16) | ||
images[0].save("out.gif", save_all=True, append_images=images[1:]) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.