Conways Game of life dengan Python

Ini adalah posting pertama saya di mana saya ingin memberi tahu Anda tentang "Game of life" otomat seluler paling terkenal, dan juga menulisnya dengan Python menggunakan grafik Pygame.





Conways Game of life (dalam 'Game of life' Rusia) adalah robot seluler yang ditemukan oleh John Conway pada tahun 1970.





Aturannya sangat sederhana, seluruh permainan berlangsung dalam ruang 2D (bidang) di mana bisa ada 2 jenis sel "Hidup" - 0 dan "Kosong" -1. Aturan dasar kehidupan sel adalah Birth3 Survive23, yang berarti sel dilahirkan dengan tiga tetangga dan bertahan dengan dua atau tiga, jika tidak maka akan mati.





Penentuan jumlah tetangga terjadi di lingkungan Moore.





Sedikit latar belakang sejarah dari Wikipedia.





John Conway tertarik pada masalah yang diajukan pada tahun 1940-an oleh matematikawan terkenal John von Neumann, yang mencoba membuat mesin hipotetis yang dapat mereproduksi dirinya sendiri. John von Neumann berhasil membuat model matematis dari mesin semacam itu dengan aturan yang sangat kompleks. Conway mencoba menyederhanakan ide-ide yang diajukan oleh Neumann, dan pada akhirnya ia berhasil membuat aturan yang menjadi aturan main "Life".





(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. Buat penghitung variabel





  3. Kami melewati setiap elemen sistem





  4. Jika tetangga sel "hidup", tambah penghitung.





  5. Menghitung kembali









# 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
      
      







Jadi, sekarang mari kita lakukan logika dasar.





    #    
    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
      
      



Kode lengkap
# 
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
      
      







Pemeriksaan kode
Pemeriksaan kode

Semuanya berhasil, kecepatannya juga tidak membuat frustrasi.





Pada artikel berikutnya kami akan mencoba menerapkan modifikasi dari game "Life".












All Articles