conway.py

Created by pikube

Created on October 26, 2019

2.14 KB

ATTENTION: CE PROGRAMME EXIGE TOUTE LA MEMOIRE DE LA CALCULATRICE (donc supprimer tous les autres programmes python présents avant de l’injecter).

Le jeu de la vie de Conway. Essayez gameoflife(). Pour changer la configuration initiale, allez dans #initialsetup est choisissez les cellules en vie avec live(x,y). Noter que 0,0 est le coin haut gauche et que une cellule représente 20 pixels. live(20,0) rend vivante la deuxième cellule en partant du haut gauche et en comptant de gauche à droite.


from math import *
import kandinsky
import time
global tsize
global screen
tsize=10
white=kandinsky.color(255,255,255)
dark=kandinsky.color(0,0,0)
screen=[]
#upperleftcorner 0,0
def die(x,y):
  for cx in range(tsize):
    for cy in range(tsize):
      kandinsky.set_pixel(cx+x,cy+y,dark)
def live(x,y):
  for cx in range(tsize):
    for cy in range(tsize):
      kandinsky.set_pixel(cx+x,cy+y,white)
def isalive(x,y):
  if kandinsky.get_pixel(x,y)==dark:
    return 0
  else:
    return 1
def resetscreen():
  for loopx in range(320):
    for loopy in range(201):
      kandinsky.set_pixel(loopx,loopy,dark)
def surround(x,y):
  s=0
  #surroundofacell 
  if isalive(x-tsize,y+tsize)==1:
    s+=1
  if isalive(x,y+tsize)==1:
    s+=1
  if isalive(x+tsize,y+tsize)==1:
    s+=1
  if isalive(x+tsize,y)==1:
    s+=1
  if isalive(x+tsize,y-tsize)==1:
    s+=1
  if isalive(x,y-tsize)==1:
    s+=1
  if isalive(x-tsize,y-tsize)==1:
    s+=1
  if isalive(x-tsize,y)==1:
    s+=1
  return s
#rules
def compute():
  for cx in range(320/tsize):
    for cy in range(200/tsize):
      sur=surround(cx*tsize,cy*tsize)
      if isalive(cx*tsize,cy*tsize)==1:
        if sur>=2 and sur<=3:
          screen[cy][cx]=1
        if sur<2 or sur>3:
          screen[cy][cx]=0
      if isalive(cx*tsize,cy*tsize)==0:
        if sur==3:
          screen[cy][cx]=1
        if sur!=3:
          screen[cy][cx]=0
def show():
  for cx in range(320/tsize):
    for cy in range(200/tsize):
      if screen[cy][cx]==1:
        live(cx*tsize,cy*tsize)
      if screen[cy][cx]==0:
        die(cx*tsize,cy*tsize)    
def gameoflife(t=0):
  global screen
  screen=[]
  resetscreen()
  #initialsetup
  live(0,10)
  live(10,20)
  live(20,20)
  live(20,10)
  live(20,0)
  #endsetup
  gen=0
  #creatingscreenmatrix
  #changeoftsizeimplies
  #changingthisstupidbug
  for cy in range(200/tsize):
    screen.append([0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0])
  while 1: 
    kandinsky.draw_string("gen:%s"%gen,130,201,)
    time.sleep(t)
    kandinsky.draw_string("processing",220,206)
    compute()
    show()
    kandinsky.draw_string("      done",220,206)
    gen+=1
  screen=[]

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.