From 88a183d432519a7d000ac8fc6233124ee480b154 Mon Sep 17 00:00:00 2001 From: IlIllII <78166995+IlIllII@users.noreply.github.com> Date: Mon, 23 Oct 2023 15:59:39 -0500 Subject: [PATCH] Add random symbol generator --- plane-game/main.py | 33 +++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/plane-game/main.py b/plane-game/main.py index e31657f..94fbdcf 100644 --- a/plane-game/main.py +++ b/plane-game/main.py @@ -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)] @@ -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 @@ -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 @@ -72,6 +82,7 @@ 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 @@ -79,14 +90,12 @@ def get_terminal_dims() -> Tuple[int, int]: 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)