Skip to content

Commit

Permalink
Completed basic Game of Life functionality
Browse files Browse the repository at this point in the history
Major issues - Game of Life doesn't operate as intended. Cause currently
unknown.
  • Loading branch information
talhaahussain committed May 30, 2024
1 parent 5bee5e3 commit 6f72d62
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
.venv/
out.txt
53 changes: 38 additions & 15 deletions src/gameoflife.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def __init__(self, alive, neighbours, x, y):
self.next_alive = None
self.colour = "#00ff00" if self.alive else "#000000"
self.neighbours = neighbours
self.alive_neighbours = None
self.living_neighbours = 0
self.height = 10
self.width = 10
self.x = x
Expand All @@ -16,26 +16,39 @@ def __init__(self, alive, neighbours, x, y):
def draw(self, screen):
pygame.draw.rect(screen, self.colour, pygame.Rect((self.x, self.y, self.width-1, self.height-1)))

def tick():
if (self.alive == True) and (len(self.alive_neighbours) < 2):
def tick(self):
if self.alive == True and self.living_neighbours < 2:
self.next_alive = False
return
if (self.alive == True) and (len(self.alive_neighbours) in [2, 3]):
elif self.alive == True and self.living_neighbours == 2:
self.next_alive = True
return
if (self.alive == True) and (len(self.alive_neighbours) > 3):
elif self.alive == True and self.living_neighbours == 3:
self.next_alive = True
return
elif self.alive == True and self.living_neighbours > 3:
self.next_alive = False
return
if (self.alive == False) and (len(self.alive_neighbours) == 3):
elif self.alive == False and self.living_neighbours == 3:
self.next_alive = True
return

def update():
else:
self.next_alive = False
"""
print(self.living_neighbours < 2)
print(self.alive)
print(self.next_alive)
print("FATAL")
exit()
"""
def update(self):
self.alive = self.next_alive
if self.alive == True:
self.colour = "#00ff00"
else:
elif self.alive == False:
self.colour = "#000000"
else:
print("FATAL ERROR!")


def get_neighbours(x, y):
Expand All @@ -48,12 +61,10 @@ def cells_init():
matrix = []
for i in range(0, 500, 10):
for j in range(0, 500, 10):
row.append(Cell(bool(random.getrandbits(1)), get_neighbours(i, j), i, j))
print(get_neighbours(i, j))
print("\n")
row.append(Cell(True, get_neighbours(i, j), i, j))
matrix.append(row)
row = []
# bool(random.getrandbits(3))),
return matrix

def main():
Expand All @@ -70,12 +81,24 @@ def main():
if event.type == pygame.QUIT:
running = False
screen.fill("white")

for i in range(50):
for j in range(50):
cells[i][j].draw(screen)

cells[i][j].living_neighbours = 0
for neighbour in cells[i][j].neighbours:
if cells[neighbour[0]][neighbour[1]].alive == True:
cells[i][j].living_neighbours += 1
print("I have... " + str(cells[i][j].living_neighbours))
cells[i][j].tick()


for i in range(50):
for j in range(50):
cells[i][j].update()

pygame.display.flip()
clock.tick()
clock.tick(0.5)


pygame.quit()
Expand Down
5 changes: 5 additions & 0 deletions src/get_neighbours.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
def get_neighbours(x, y):
neighbours = [(i, j) for i, j in ((x-1, y-1), (x, y-1), (x+1, y-1), (x, y-1), (x, y+1), (x+1, y-1), (x+1, y), (x+1, y+1)) if 0 <=i<50 and 0<=j<50]
return neighbours

print(get_neighbours(50, 50))

0 comments on commit 6f72d62

Please sign in to comment.