Skip to content

Commit

Permalink
Add random symbol generator
Browse files Browse the repository at this point in the history
  • Loading branch information
IlIllII committed Oct 23, 2023
1 parent 3824c62 commit 88a183d
Showing 1 changed file with 21 additions and 12 deletions.
33 changes: 21 additions & 12 deletions plane-game/main.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import time
import os
from typing import List, Tuple
from typing import Tuple

OFF_CELL = " "
ON_CELL = "O"


def symbol_generator():
symbols = ["!", "@", "#", "$", "%", "^", "&", "*", "+", "="]
index = 0
while True:
yield symbols[index]
index = (index + 1) % len(symbols)

symbols = symbol_generator()

class Board:
def __init__(self, width: int, height: int) -> None:
self.cells = [[OFF_CELL for x in range(width)] for y in range(height)]
Expand All @@ -27,7 +37,7 @@ def num_surrounding(self, x: int, y: int) -> int:
for j in [x - 1, x, x + 1]:
if i == y and j == x:
continue
elif ON_CELL in self.get(j, i):
elif self.get(j, i) != OFF_CELL:
count += 1
return count

Expand All @@ -37,19 +47,19 @@ def process_step(self) -> None:
new_cells.append(row.copy())
for y in range(len(new_cells)):
for x in range(len(new_cells[y])):
if ON_CELL in self.get(x, y):
if self.get(x, y) == OFF_CELL:
surrounding = self.num_surrounding(x, y)
if surrounding in [3]:
new_cells[y][x] = "\033[91m" + next(symbols) + "\033[0m"
else:
new_cells[y][x] = OFF_CELL
else:
surrounding = self.num_surrounding(x, y)
valid = [2, 3]
if surrounding not in valid:
new_cells[y][x] = OFF_CELL
else:
new_cells[y][x] = "\033[94m" + ON_CELL + "\033[0m"
if self.get(x, y) == OFF_CELL:
surrounding = self.num_surrounding(x, y)
if surrounding in [3]:
new_cells[y][x] = "\033[91m" + ON_CELL + "\033[0m"
else:
new_cells[y][x] = OFF_CELL

self.cells = new_cells

Expand All @@ -72,21 +82,20 @@ def __repr__(self) -> str:


def get_terminal_dims() -> Tuple[int, int]:
"""Return terminal (width, height)."""
h, w = os.popen("stty size", "r").read().split()
width = int(w) - 1
height = int(h) - 2
return width, height


if __name__ == "__main__":

board = Board(*get_terminal_dims())
board.process_step()
os.system("clear")

for i in range(500):
while True:
board.process_step()
print("\033[H")
print(board, end="", flush=True)
time.sleep(0.1)
print(board)

0 comments on commit 88a183d

Please sign in to comment.