from random import randint from celle import Celle import os class Rutenett: #Konstrukt?r def __init__(self, rader, kolonner): self._ant_rader = rader self._ant_kolonner = kolonner self._rutenett = [] """initialisernede metoder:""" self._lag_tomt_rutenett() def _lag_tomt_rutenett(self): for indeks in range(self._ant_rader): self._rutenett.append(self._lag_tom_rad()) def _lag_tom_rad(self): rad = [] for indeks in range(self._ant_kolonner): rad.append(None) return rad def fyll_med_tilfeldige_celler(self): for x in range(len(self._rutenett)): for y in range(len(self._rutenett[x])): self.lag_celle(x, y) if randint(1, 3) == 1: self._rutenett[x][y].sett_levende() def lag_celle(self, rad, kol): self._rutenett[rad][kol] = Celle() def hent_celle(self, rad, kol): if (rad > self._ant_rader - 1 or rad < 0) or (kol > self._ant_kolonner - 1 or kol < 0): return None else: return self._rutenett[rad][kol] def tegn_rutenett(self): print("\n" * 100) for rad in self._rutenett: for objekt in rad: print(objekt.hent_status_tegn(),end="") print() def _sett_naboer(self, rad, kol): for x in range(-1, 2): for y in range(-1, 2): if not (x == 0 and y == 0) and self.hent_celle(rad + x, kol + y) != None: self.hent_celle(rad, kol).legg_til_nabo(self._rutenett[rad + x][kol + y]) def koble_celler(self): for x in range(len(self._rutenett)): for y in range(len(self._rutenett[x])): self._sett_naboer(x, y) def hent_alle_celler(self): liste = [] for rad in self._rutenett: for objekt in rad: liste.append(objekt) return liste def antall_levende(self): teller = 0 for rad in self._rutenett: for objekt in rad: if objekt.er_levende(): teller += 1 return teller """ def lag_manuell(self): for x in range(len(self._rutenett)): for y in range(len(self._rutenett[x])): self.lag_celle(x,y) print(x, y) svar = input("levende j/n?") if svar.lower() == "j": self._rutenett[x][y].sett_levende()"""