Skip to main content
2 of 7
edited body

Game of life(random generation)

I wrote a simple game on python, but i want to listen offers on account of "How I can improve it", will be useful any opinitions. I thought about optimization(program eat 20% resource of processor) too, don't know where to begin. Code of game:

import random
import time
import os

class LifeGame:
    def __init__(self):
        self.width = 20
        self.height = 20
        self.matrix = []

        row_counter = 0
        for y in range(self.height):
            self.matrix.append([])
            for x in range(self.width):
                self.matrix[row_counter].append(" ")
            row_counter += 1

    def print(self, iteration):
        life_cells = 0
        empty_cells = 0

        print("", "---"*self.width)
        for y in range(self.height):
            print("|", end='')
            for x in range(self.width):
                print(self.matrix[y][x], end='  ')
            print("|")
        print("", "---" * self.width)

        for y in range(self.height):
            for x in range(self.width):
                if self.matrix[y][x] == " ":
                    empty_cells += 1
                elif self.matrix[y][x] == "*":
                    life_cells += 1
        if empty_cells != 0:
            print(round((life_cells/100), 2)*(100/((self.width*self.height)/100)), "% of life")
        else:
            print("100 % of life")
        print(iteration)

    def spawn_life(self):
        rand_coords = []
        counter = 0
        for i in range(1):
            rand_coords.append([])
            rand_coords[counter].append(random.randint(0, self.width-1))
            rand_coords[counter].append(random.randint(0, self.height-1))
            counter += 1

        for i in range(len(rand_coords)):
            self.matrix[rand_coords[i][1]][rand_coords[i][0]] = "*"
        return rand_coords

    def get_available_coords(self, x, y):

        available_coords = [[x-1, y], [x+1, y], [x, y+1], [x, y-1]]
        coords = []
        for i in range(len(available_coords)):
            if available_coords[i][0] <= self.width-1 and available_coords[i][0] >= 0\
                and available_coords[i][1] <= self.height-1 and available_coords[i][1] >= 0\
            and self.matrix[available_coords[i][1]][available_coords[i][0]] == " ":
                coords.append([available_coords[i][0], available_coords[i][1]])

        return coords

    def add_life_to_matrix(self, coords):
        coords = sum(coords, [])
        for i in range(len(coords)):
            coord = coords[i]
            x, y = coord
            chance = random.randint(0, 100)
            if chance == 1:
                self.matrix[y][x] = '*'


lg = LifeGame()

lg.spawn_life()
iteration = 0
while True:
    coords = []

    for y in range(lg.width):
        for x in range(lg.height):
            if lg.matrix[y][x] == "*":
                coords.append(lg.get_available_coords(x, y))

    lg.add_life_to_matrix(coords)
    iteration += 1

    lg.print(iteration)
    time.sleep(0.01)
    os.system('cls')