import itertools class Board: def __init__(self, width, height, quotient_x, quotient_y): self.width = width self.height = height self.quotient_x = quotient_x self.quotient_y = quotient_y self.lives = set() def toggle(self, x, y): if (x, y) in self.lives: self.lives.remove((x, y)) else: self.lives.add((x, y)) def evaluate(self): new_lives = self.try_evaluate() self.lives = new_lives def try_evaluate(self) -> set[(int, int)]: new_lives = set() for (x, y) in itertools.product(range(self.width), range(self.height)): neighbor_count = 0 for (dx, dy) in itertools.product([-1, 0, 1], [-1, 0, 1]): if dx == 0 and dy == 0: continue nx = x + dx ny = y + dy if self.quotient_x: nx %= self.width if self.quotient_y: ny %= self.height if (nx, ny) in self.lives: neighbor_count += 1 if (x, y) in self.lives and neighbor_count in [2, 3]: new_lives.add((x, y)) if (x, y) not in self.lives and neighbor_count == 3: new_lives.add((x, y)) return new_lives def __str__(self): res = "" for y in range(self.height): for x in range(self.width): if (x, y) in self.lives: res += "o" else: res += " " res += "\n" return res def __repr__(self): return self.__str__()