PythonでのConwaysGame of Life

これは私の最初の投稿で、最も有名なセルラーオートマトン「Gameof Life」について説明し、Pygameグラフィックを使用してPythonで記述したいと思います。





Conways Game of life(ロシア語で「Gameof life」)は、1970年にJohnConwayによって発明されたセルラーオートマトンです。





ルールは非常に単純で、ゲーム全体が2D空間(平面)で行われ、その上に「ライブ」の2種類のセル(0と「空」-1)が存在する可能性があります。セルの基本的なルールはBirth3Survive23です。これは、セルが3つの隣接セルで生まれ、2つまたは3つで存続することを意味します。





隣人の数の決定はムーア地区で行われます。





ウィキペディアからの少しの背景の歴史。





ジョン・コンウェイは、1940年代に有名な数学者ジョン・フォン・ノイマンによって提案された問題に興味を持ちました。ジョン・フォン・ノイマンは、それ自体を再現できる架空の機械を作成しようとしていました。John von Neumannは、非常に複雑なルールを使用して、このようなマシンの数学モデルを作成することができました。コンウェイはノイマンによって提案されたアイデアを単純化しようとしました、そして結局彼は人生のゲームのルールになるルールを作成することに成功しました。





このゲームの説明は、Scientific Americanの10月(1970)号に、MartinGardnerによる「MathGames」という見出しで最初に公開されました。





あなたはこの理論すべてにうんざりしていると確信しています。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. count









  3. "", counter.





  4. count









# 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
      
      







, .





    #    
    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
      
      



# 
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
      
      







コードチェック

すべてがうまくいった、速度もイライラしません。





次の記事では、ゲーム「Life」の変更を実装しようとします。












All Articles