Verteiltesystheme/Code/GameLogic/Rules.py

40 lines
1.7 KiB
Python
Raw Normal View History

2022-03-20 09:47:49 +01:00
from Code.UI.Field import Field
from Code.UI.Square import Square
from Code.helpers import index_2d
class Rules:
def __init__(self):
pass
def _get_neighborhood(self, target: Square, field_of_squares: list)->list:
mid_y, mid_x = index_2d(field_of_squares, target)
#TODO i did not consider the frame so every pixel on the edge will couse an error i am not sure how we gonna do it with the conection between the frames
top = field_of_squares[mid_y - 1][mid_x - 1], field_of_squares[mid_y - 1][mid_x],field_of_squares[mid_y - 1][mid_x + 1]
middle = field_of_squares[mid_y][mid_x - 1],field_of_squares[mid_y][mid_x + 1]
bottom = field_of_squares[mid_y + 1][mid_x - 1], field_of_squares[mid_y + 1][mid_x],field_of_squares[mid_y + 1][mid_x + 1]
neighborhood = [top, middle, bottom]
return neighborhood
def _number_of_alive_nigbohrs(self,neighborhood:list)->int:
count=0
for square in neighborhood:
if square.active:
count+=1
return count
def use_ruels_on_square(self,target: Square, field_of_squares: list):
neighborhood=self._get_neighborhood(target,field_of_squares)
number_of_alive_nigbohrs=self._get_neighborhood(neighborhood)
if not target.active:
if number_of_alive_nigbohrs==3:
target.update(True)
else:
if number_of_alive_nigbohrs<2:
target.update(False)
if number_of_alive_nigbohrs==2 or number_of_alive_nigbohrs==3:
target.update(True)# eigentlich unnötig da es einfach den zustand beibehalten kann
if number_of_alive_nigbohrs>3:
target.update(False)