forked from MrMil/gameoflife-nn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgameoflife.py
95 lines (78 loc) · 2.71 KB
/
gameoflife.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import random
from copy import deepcopy
from typing import Optional
BOARD_T = list[list[int]]
def get_neighbors_sum(board: BOARD_T, y: int, x: int) -> int:
up = get_direction(board, "up", y, x)
down = get_direction(board, "down", y, x)
left = get_direction(board, "left", y, x)
right = get_direction(board, "right", y, x)
top_right_corner = get_direction(board, "right", *up)
top_left_corner = get_direction(board, "left", *up)
bottom_right_corner = get_direction(board, "right", *down)
bottom_left_corner = get_direction(board, "left", *down)
return sum(
[
board[i][j]
for i, j in [
up,
down,
left,
right,
top_right_corner,
bottom_right_corner,
top_left_corner,
bottom_left_corner,
]
]
)
def get_direction(board: BOARD_T, direction: str, y: int, x: int) -> tuple[int, int]:
if direction == "up":
return y - 1, x
elif direction == "right":
return (y, x + 1) if x < len(board[0]) - 1 else (y, 0)
elif direction == "down":
return (y + 1, x) if y < len(board) - 1 else (0, x)
elif direction == "left":
return y, x - 1
else:
raise ValueError("Invalid direction")
def next_generation(board: BOARD_T) -> BOARD_T:
"""
This function gets a game of life board and return the next generation
"""
new_board = deepcopy(board)
for i in range(len(board)):
for j in range(len(board[0])):
neighbors_sum = get_neighbors_sum(board, i, j)
if board[i][j]:
if neighbors_sum < 2 or neighbors_sum > 3:
new_board[i][j] = 0
else:
if neighbors_sum == 3:
new_board[i][j] = 1
return new_board
def add_glider(board: BOARD_T, y: int, x: int) -> None:
board[y][x + 1] = 1
board[y + 1][x + 2] = 1
board[y + 2][x] = 1
board[y + 2][x + 1] = 1
board[y + 2][x + 2] = 1
def add_cells_pattern(board: BOARD_T, cells_pattern: str, y: int, x: int) -> None:
data = cells_pattern.split("\n")
for line in data:
if line and line[0] in (".", "O"):
x_temp = x
for char in line:
if char == "O":
board[y][x_temp] = 1
x_temp += 1
y += 1
def generate_random_board(y: int, x: Optional[int] = None) -> BOARD_T:
if x is None:
x = y
return [[random.randint(0, 1) for _ in range(x)] for _ in range(y)]
def generate_empty_board(y: int, x: Optional[int] = None) -> BOARD_T:
if x is None:
x = y
return [[0 for _ in range(x)] for _ in range(y)]