-
Notifications
You must be signed in to change notification settings - Fork 18
Add example of game-of-life simulation #16
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
Open
Athroniaeth
wants to merge
4
commits into
MathisHammel:main
Choose a base branch
from
Athroniaeth:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ec9d685
Add example of game-of-life simulation
Athroniaeth c7e20ed
Refactor docstring, delete PharmaScreen dependency with staticmethod
Athroniaeth c2cda99
Delete useless type hints docstring
Athroniaeth ceeeb4a
Patchfix of 'static' call of is_drawable method in snake
Athroniaeth 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,122 @@ | ||
| import itertools | ||
| import random | ||
| import sys | ||
| import time | ||
| from typing import List | ||
|
|
||
| import pygame | ||
|
|
||
| from pharmacontroller import SCREEN_SIZE, PharmaScreen | ||
|
|
||
| Grid = type(List[List[int]]) | ||
|
|
||
|
|
||
| def is_valid_index(screen: PharmaScreen, row: int, column: int) -> bool: | ||
| """ | ||
| Check if the given row and column are valid indices. | ||
|
|
||
| Args: | ||
| row (int): The row index. | ||
| column (int): The column index. | ||
|
|
||
| Returns: | ||
| bool: True if the indices are valid, False otherwise. | ||
| """ | ||
| conditions = ( | ||
| 0 <= row < SCREEN_SIZE, | ||
| 0 <= column < SCREEN_SIZE, | ||
| screen.is_drawable(row, column), # Todo: transform to static method | ||
| ) | ||
| return all(conditions) | ||
|
|
||
|
|
||
| def generate_grid() -> Grid: | ||
| """ | ||
| Generate a grid without any living cells. | ||
|
|
||
| Returns: | ||
| numpy.ndarray: The grid state. | ||
| """ | ||
| return [[0 for _ in range(SCREEN_SIZE)] for _ in range(SCREEN_SIZE)] | ||
|
|
||
|
|
||
| def get_neighbors(screen: PharmaScreen, grid: Grid, row: int, column: int) -> List[float]: | ||
| """ | ||
| Get the neighbors of the cell at the given row and column. | ||
|
|
||
| Args: | ||
| grid (numpy.ndarray): The grid state. | ||
| row (int): The row of the cell. | ||
| column (int): The column of the cell. | ||
|
|
||
| Returns: | ||
| List[int]: The list of neighbors. | ||
| """ | ||
| neighbors = [] | ||
|
|
||
| # Iterate over the 3x3 grid around the cell | ||
| for i, j in itertools.product(range(-1, 2), repeat=2): | ||
| # Ignore current cell | ||
| if i == 0 and j == 0: | ||
| continue | ||
|
|
||
| # Get the new row and column | ||
| new_row = row + i | ||
| new_column = column + j | ||
|
|
||
| # Ignore out of bounds cells | ||
| if not is_valid_index(screen, new_row, new_column): | ||
| continue | ||
|
|
||
| neighbors.append(grid[new_row][new_column]) | ||
|
|
||
| return neighbors | ||
|
|
||
|
|
||
| def next_generation(screen: PharmaScreen, grid: Grid) -> Grid: | ||
| """ | ||
| Generate the next generation of the grid. | ||
|
|
||
| Returns: | ||
| numpy.ndarray: The new grid state. | ||
| """ | ||
| new_grid = generate_grid() | ||
|
|
||
| for row, column in itertools.product(range(SCREEN_SIZE), range(SCREEN_SIZE)): | ||
| neighbors = get_neighbors(screen, grid, row, column) | ||
| alive_neighbors = sum(neighbors) | ||
| cell = grid[row][column] | ||
|
|
||
| if cell == 1: | ||
| if alive_neighbors < 2 or alive_neighbors > 3: | ||
| new_grid[row][column] = 0 # Cell dies of underpopulation or overpopulation | ||
| else: | ||
| new_grid[row][column] = 1 # Cell survives | ||
| else: | ||
| if alive_neighbors == 3: | ||
| new_grid[row][column] = 1 # Cell is born (good population) | ||
|
|
||
| return new_grid | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| pygame.init() | ||
| screen = PharmaScreen(color_scale=False) | ||
|
|
||
| grid = generate_grid() | ||
|
|
||
| for _ in range(500): | ||
| grid[random.randrange(SCREEN_SIZE)][random.randrange(SCREEN_SIZE)] = 1 | ||
|
|
||
| running = True | ||
| while running: | ||
| for event in pygame.event.get(): | ||
| if event.type == pygame.QUIT: | ||
| pygame.quit() | ||
| sys.exit() | ||
|
|
||
| grid = next_generation(screen, grid) | ||
| screen.set_image(grid) | ||
|
|
||
| # Leave time to contemplate life | ||
| time.sleep(0.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.