class Game(object):
    def __init__(self, state, infinite_board=True):
        self.state = state
        self.width = state.width
        self.height = state.height
        self.infinite_board = infinite_board

    def step(self, count=1):

        for generation in range(count):

            new_board = [[False] * self.width for row in range(self.height)]

            for y, row in enumerate(self.state.board):
                for x, cell in enumerate(row):
                    neighbours = self.neighbours(x, y)
                    previous_state = self.state.board[y][x]
                    should_live = neighbours == 3 or (
                        neighbours == 2 and previous_state
                    )
                    new_board[y][x] = should_live

            self.state.board = new_board

    def neighbours(self, x, y):

        count = 0

        for hor in [-1, 0, 1]:
            for ver in [-1, 0, 1]:
                if not hor == ver == 0 and (
                    self.infinite_board
                    or (0 <= x + hor < self.width and 0 <= y + ver < self.height)
                ):
                    count += self.state.board[(y + ver) % self.height][
                        (x + hor) % self.width
                    ]

        return count

    def display(self):
        return self.state.display()
