|
1 | 1 | Contains an implementation of [Conway's Game of Life](https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life) cellular automation. |
| 2 | + |
| 3 | +```mojo |
| 4 | +import random |
| 5 | +
|
| 6 | +
|
| 7 | +# Grid is a 2D structure holding cell states (0: dead, 1: alive) |
| 8 | +# It supports string conversion, output writing, and cell access/update |
| 9 | +@value |
| 10 | +struct Grid(Stringable, Writable): |
| 11 | + var data: List[List[Int, True]] # 2D grid of integers (1 = alive, 0 = dead) |
| 12 | +
|
| 13 | + # Constructor to initialize the grid with given data |
| 14 | + fn __init__(out self, data: List[List[Int, True]]): |
| 15 | + self.data = data |
| 16 | +
|
| 17 | + # Get the number of rows in the grid |
| 18 | + fn row_count(self) -> Int: |
| 19 | + if self.data: |
| 20 | + return len(self.data) |
| 21 | + else: |
| 22 | + return 0 |
| 23 | +
|
| 24 | + # Get the number of columns in the grid |
| 25 | + fn col_count(self) -> Int: |
| 26 | + if self.data[0]: |
| 27 | + return len(self.data[0]) |
| 28 | + else: |
| 29 | + return 0 |
| 30 | +
|
| 31 | + # Convert the grid to a string for pretty-printing |
| 32 | + fn __str__(self) -> String: |
| 33 | + capacity = self.row_count() * self.col_count() |
| 34 | + if capacity == 0: |
| 35 | + return String() |
| 36 | + s = String(capacity=capacity) |
| 37 | + row_index = 0 |
| 38 | + for row in self.data: |
| 39 | + for col in row[]: |
| 40 | + if col[] == 1: |
| 41 | + s += "*" # Alive cell represented by '*' |
| 42 | + else: |
| 43 | + s += " " # Dead cell is blank |
| 44 | + if row_index != self.row_count() - 1: |
| 45 | + s += "\n" # Line break between rows |
| 46 | + row_index += 1 |
| 47 | + return s |
| 48 | +
|
| 49 | + # Allow writing the grid to any output writer |
| 50 | + fn write_to[W: Writer](self, mut writer: W) -> None: |
| 51 | + writer.write(self.__str__()) |
| 52 | +
|
| 53 | + # Access cell at (row, col) |
| 54 | + fn __getitem__(self, row: Int, col: Int) -> Int: |
| 55 | + return self.data[row][col] |
| 56 | +
|
| 57 | + # Update cell at (row, col) |
| 58 | + fn __setitem__(mut self, row: Int, col: Int, value: Int) -> None: |
| 59 | + self.data[row][col] = value |
| 60 | +
|
| 61 | + # Static method to create a random grid with specified size |
| 62 | + @staticmethod |
| 63 | + fn new(rows: Int, cols: Int) -> Self: |
| 64 | + random.seed() |
| 65 | + data = List[List[Int, True]](capacity=rows) |
| 66 | + for row in range(rows): |
| 67 | + record = List[Int, True](capacity=cols) |
| 68 | + for col in range(cols): |
| 69 | + # Initialize each cell randomly to 0 or 1 |
| 70 | + record.append(Int(random.random_si64(0, 1))) |
| 71 | + data.append(record) |
| 72 | + return Self(data) |
| 73 | +
|
| 74 | + # Perform one step of mutation (Game of Life rules) |
| 75 | + fn mutate(mut self): |
| 76 | + rows = self.row_count() |
| 77 | + cols = self.col_count() |
| 78 | + for row in range(rows): |
| 79 | + above = (row - 1) % rows |
| 80 | + below = (row + 1) % rows |
| 81 | + for col in range(cols): |
| 82 | + left = (col - 1) % cols |
| 83 | + right = (col + 1) % cols |
| 84 | +
|
| 85 | + # Count live neighbors using 8-connected grid |
| 86 | + alive_neighbours = ( |
| 87 | + self[above, left] |
| 88 | + + self[above, col] |
| 89 | + + self[above, right] |
| 90 | + + self[row, right] |
| 91 | + + self[below, right] |
| 92 | + + self[below, col] |
| 93 | + + self[below, left] |
| 94 | + + self[row, left] |
| 95 | + ) |
| 96 | +
|
| 97 | + # Apply Conway's Game of Life rules: |
| 98 | + # Rule 1 & 2: Any live cell with 2 or 3 live neighbors survives |
| 99 | + if self[row, col] == 1 and ( |
| 100 | + alive_neighbours == 2 or alive_neighbours == 3 |
| 101 | + ): |
| 102 | + continue # Keep alive |
| 103 | +
|
| 104 | + # Rule 3: All other live cells die |
| 105 | + else: |
| 106 | + self[row, col] = 0 |
| 107 | +
|
| 108 | + # Rule 4: Any dead cell with exactly 3 live neighbors becomes alive |
| 109 | + if self[row, col] == 0 and alive_neighbours == 3: |
| 110 | + self[row, col] = 1 |
| 111 | +``` |
0 commit comments