exit, 'q' ) # Set up keys for performing generation steps, either one-at-a-time or not. onkey ( erase, 'e' ) def makeRandom (): board. display () # Set up key bindings def erase (): board. penup () board = LifeBoard ( xsize // CELL_SIZE, 1 + ysize // CELL_SIZE ) # Set up mouse bindings def toggle ( x, y ): cell_x = x // CELL_SIZE cell_y = y // CELL_SIZE if board. setworldcoordinates ( 0, 0, xsize, ysize ) turtle. write ( s, font = ( 'sans-serif', 14, 'normal' )) y -= line_height def main (): display_help_window () scr = turtle. screensize () line_height = 20 y = height // 2 - 30 for s in ( "Click on cells to make them alive or dead.", "Keyboard commands:", " E)rase the board", " R)andom fill", " S)tep once or", " C)ontinuously - use 'S' to resume stepping", " Q)uit" ): help_t. speed ( 'fastest' ) width, height = help_screen. Canvas ( root, width = 300, height = 200, bg = "white" ) canvas. update () def display_help_window (): from turtle import TK root = TK. end_fill () def display ( self ): """Draw the whole board""" turtle. begin_fill () for i in range ( 4 ): turtle. setpos ( x * CELL_SIZE, y * CELL_SIZE ) turtle. state = d # Display-related methods # def draw ( self, x, y ): "Update the cell (x,y) on the display." turtle. add (( i, j )) elif live : # Death pass self. add (( i, j )) elif s = 2 and live : # Survival d. s -= live #print(d) #print(i, j, s, live) if s = 3 : # Birth d. state : s += 1 # Subtract the central cell's value it doesn't count. ysize, j + 2 ) ): for xp in x_range : if ( xp, yp ) in self. state ) for yp in range ( max ( 0, j - 1 ), min ( self. ysize ): s = 0 live = (( i, j ) in self. xsize ): x_range = range ( max ( 0, i - 1 ), min ( self. clear () def step ( self ): "Compute one generation, updating the display." d = set () for i in range ( self. add ( key ) def erase ( self ): """Clear the entire board.""" self. is_legal ( x, y ): raise ValueError ( "Coordinates ". set ( i, j ) def toggle ( self, x, y ): """Toggle a cell's state between live and dead.""" if not self. ysize = xsize, ysize def is_legal ( self, x, y ): "Returns true if the x,y coordinates are legal for this board." return ( 0 0.5 : self. scr - curses screen object to use for display char - character used to render live cells (default: '*') """ self. erase() - clear the entire board makeRandom() - fill the board randomly set(x,y) - set the given cell to Live doesn't refresh the screen toggle(x,y) - change the given cell from live to dead, or vice versa, and refresh the screen display """ def _init_ ( self, xsize, ysize ): """Create a new LifeBoard instance. Methods: display(update_board) - Display the state of the board on-screen. # An empty board will be displayed, and the following commands are available: # E : Erase the board # R : Fill the board randomly # S : Step for a single generation # C : Update continuously until a key is struck # Q : Quit # Cursor keys : Move the cursor around the board # Space or Enter : Toggle the contents of the cursor's position # import sys import turtle import random CELL_SIZE = 10 # Measured in pixels class LifeBoard : """Encapsulates a Life board Attributes: xsize, ysize : horizontal and vertical size of the board state : set containing (x,y) coordinates for live cells. #!/usr/bin/env python3 # life.py - A turtle-based version of Conway's Game of Life. Therefore the pattern moves through the grid. Return to their initial configuration but in a different position, and Patterns are stable and don’t change over time at all spaceships For example, oscillators go through aĬycle of states and return to their initial state still life Pattern, and this one is called an oscillator.ĭescribes many such terms. Researchers in Life have coined many terms for different types of This pattern ends up in a cycle that repeats itself endlessly. Occur after starting with five live cells in a row. (The general term for a simulationĬarried out on a grid of cells and following some simple rules is aĬellular automaton.) For example, here are some patterns that These rules are simple and readily understandable, but they lead to Death by overcrowding: if the cell alive and has more than three.Neighbours, the cell will become dead at the next step. Death by loneliness: if the cell has only zero or one live.Survival: if the cell has two or three live neighbours, the cell.Birth: if exactly three of its neighbours areĪlive, the cell will become alive at the next step.Let’s take the cell x in theĭiagram, whose neighbours have been numbered 1 through 8 in the The simulation is carried out at fixed time steps every time step,Īll the cells on the grid can switch from dead to alive, or alive toĭead, depending on four simple rules that only depend on a givenĬell’s eight immediate neighbours.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |