forked from Mrinank-Bhowmick/python-beginner-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
114 lines (86 loc) · 2.82 KB
/
main.py
File metadata and controls
114 lines (86 loc) · 2.82 KB
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import os
import random
import sys
import time
from enum import Enum
from shutil import get_terminal_size
TICKS_PER_SECOND = 60
def clear() -> None:
"""Clear the terminal based on OS."""
if sys.platform == "nt":
os.system("cls")
else:
os.system("clear")
class Direction(Enum):
"""Mapping of direction to int."""
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
class Empty(str):
"""String subclass to return character representing empty space."""
CHAR = " "
def __new__(cls) -> str:
return str.__new__(cls, cls.CHAR)
class Neuron(str):
"""String subclass to return character representing a neuron."""
CHAR = "██"
CHANCE_OF_DEATH = 35
def __new__(cls) -> str:
return str.__new__(cls, cls.CHAR)
class Grid:
"""Class to store grid."""
def __init__(self, width, height) -> None:
"""Initialise the grid with empty cells."""
self._grid = [[Empty() for _ in range(width)] for _ in range(height)]
def __str__(self) -> str:
"""Return the string representation of the grid."""
return "\n".join(["".join(row) for row in self._grid])
@property
def neurons(self) -> tuple[tuple[Neuron, tuple[int, int]]]:
"""Return 2D tuple of Neurons."""
return tuple(
(element, (x, y))
for y, row in enumerate(self._grid)
for x, element in enumerate(row)
if isinstance(element, Neuron)
)
def set_neuron(self, x, y) -> None:
"""Set x, y to a Neuron."""
self._grid[y][x] = Neuron()
def tick(self) -> None:
"""Randomly kill or move all Neurons."""
for neuron_data in self.neurons:
neuron = neuron_data[0]
x, y = neuron_data[1]
if random.randint(1, 100) <= Neuron.CHANCE_OF_DEATH:
self._grid[y][x] = Empty()
continue
direction = random.choice(list(Direction))
if direction == Direction.UP:
y -= 1
elif direction == Direction.RIGHT:
x += 1
elif direction == Direction.DOWN:
y += 1
elif direction == Direction.LEFT:
x -= 1
try:
self._grid[y][x] = Neuron()
except IndexError:
pass
def main() -> None:
"""Run the program with two example Neurons."""
# Set the grid size proportional to the terminal
width, height = get_terminal_size()
grid = Grid(width // len(Neuron.CHAR), height)
grid.set_neuron(15, 15) # Example placement
grid.set_neuron(35, 35)
while grid.neurons:
clear()
print(grid) # Output the string representation of the grid
grid.tick()
time.sleep(1 / TICKS_PER_SECOND)
if __name__ == "__main__":
while True:
main()