Conways Spiel des Lebens in Python

Dies ist mein erster Beitrag, in dem ich Ihnen etwas über den berühmtesten Mobilfunkautomaten "Game of Life" erzählen und ihn auch in Python mit Pygame-Grafiken schreiben möchte.





Conways Game of Life (auf Russisch "Game of Life") ist ein zellularer Automat, der 1970 von John Conway erfunden wurde.





Die Regeln sind sehr einfach, das ganze Spiel findet im 2D-Raum (Ebene) statt, auf dem es 2 Arten von Zellen geben kann: "Live" - ​​0 und "Empty" -1. Die Grundregeln für das Leben einer Zelle sind Birth3 Survive23, was bedeutet, dass eine Zelle mit drei Nachbarn geboren wird und mit zwei oder drei überlebt, andernfalls stirbt sie.





Die Bestimmung der Anzahl der Nachbarn erfolgt in der Nachbarschaft von Moore.





Eine kleine Hintergrundgeschichte aus Wikipedia.





John Conway interessierte sich für ein Problem, das in den 1940er Jahren vom berühmten Mathematiker John von Neumann vorgeschlagen wurde, der versuchte, eine hypothetische Maschine zu schaffen, die sich selbst reproduzieren konnte. John von Neumann gelang es, ein mathematisches Modell einer solchen Maschine mit sehr komplexen Regeln zu erstellen. Conway versuchte, die von Neumann vorgeschlagenen Ideen zu vereinfachen, und am Ende gelang es ihm, die Regeln zu erstellen, die zu den Regeln des Spiels "Leben" wurden.





(1970 ) Scientific American, « » (Martin Gardner)





, , Python/Pygame





Python, .





pygame "pip install pygame" "pip3 install pygame" ( "pip " , PATH Python)





,





# 
import pygame as p
from pygame.locals import *

#   RGB
BLACK = (0 , 0 , 0)
WHITE = (255 , 255 , 255)
#  
root = p.display.set_mode((1000 , 500))
#  
while 1:
    #    
    root.fill(WHITE)
    
    #  
    for i in range(0 , root.get_height() // 20):
        p.draw.line(root , BLACK , (0 , i * 20) , (root.get_width() , i * 20))
    for j in range(0 , root.get_width() // 20):
        p.draw.line(root , BLACK , (j * 20 , 0) , (j * 20 , root.get_height()))
    #        " "
    for i in p.event.get():
        if i.type==	QUIT:
          quit()
    p.display.update()
      
      



-





-
  1. system





  2. Erstellen Sie eine variable Zählerzahl





  3. Wir gehen jedes Element des Systems durch





  4. Wenn der Nachbar der Zelle "live" ist, erhöhen Sie den Zähler.





  5. Rückgabe der Zählung









# 2      
cells=[ [0 for j in range(root.get_width()//20)] for i in range(root.get_height()//20)]
cells2=cells
#   - 
def near(pos: list , system=[[-1 , -1] , [-1 , 0] , [-1 , 1] , [0 , -1] , [0 , 1] , [1 , -1] , [1 , 0] , [1 , 1]]):
    count = 0
    for i in system:
        if cells[(pos[0] + i[0]) % len(cells)][(pos[1] + i[1]) % len(cells[0])]:
            count += 1
    return count
      
      







Und jetzt machen wir die grundlegende Logik.





    #    
    for i in range(len(cells)):
        for j in range(len(cells[0])):
            #   
            if cells[i][j]:
                #     2  3 
                if near([i , j]) not in (2 , 3):
                    cells2[i][j] = 0
                    continue
                #   
                cells2[i][j] = 1
                continue
            #       3     
            if near([i , j]) == 3:
                cells2[i][j] = 1
                continue
            #       
            cells2[i][j] = 0
    cells = cells2
      
      



Vollständiger Code
# 
import time

import pygame as p
import random
from pygame.locals import *

#   RGB
BLACK = (0 , 0 , 0)
WHITE = (255 , 255 , 255)
#  
root = p.display.set_mode((1000 , 500))
# 2      
cells = [[random.choice([0 , 1]) for j in range(root.get_width() // 20)] for i in range(root.get_height() // 20)]


#   - 
def near(pos: list , system=[[-1 , -1] , [-1 , 0] , [-1 , 1] , [0 , -1] , [0 , 1] , [1 , -1] , [1 , 0] , [1 , 1]]):
    count = 0
    for i in system:
        if cells[(pos[0] + i[0]) % len(cells)][(pos[1] + i[1]) % len(cells[0])]:
            count += 1
    return count


#  
while 1:
    #    
    root.fill(WHITE)

    #  
    for i in range(0 , root.get_height() // 20):
        p.draw.line(root , BLACK , (0 , i * 20) , (root.get_width() , i * 20))
    for j in range(0 , root.get_width() // 20):
        p.draw.line(root , BLACK , (j * 20 , 0) , (j * 20 , root.get_height()))
   #        " "
    for i in p.event.get():
        if i.type == QUIT:
            quit()
    #    

    for i in range(0 , len(cells)):
        for j in range(0 , len(cells[i])):
            print(cells[i][j],i,j)
            p.draw.rect(root , (255 * cells[i][j] % 256 , 0 , 0) , [i * 20 , j * 20 , 20 , 20])
    #  
    p.display.update()
    cells2 = [[0 for j in range(len(cells[0]))] for i in range(len(cells))]
    for i in range(len(cells)):
        for j in range(len(cells[0])):
            if cells[i][j]:
                if near([i , j]) not in (2 , 3):
                    cells2[i][j] = 0
                    continue
                cells2[i][j] = 1
                continue
            if near([i , j]) == 3:
                cells2[i][j] = 1
                continue
            cells2[i][j] = 0
    cells = cells2
      
      







Codeprüfung
Codeprüfung

Alles hat geklappt, die Geschwindigkeit frustriert auch nicht.





In den nächsten Artikeln werden wir versuchen, Modifikationen des Spiels "Life" zu implementieren.












All Articles