POV-Ray : Newsgroups : povray.text.scene-files : Game of Life implementation : Game of Life implementation Server Time
27 Jun 2024 17:19:01 EDT (-0400)
  Game of Life implementation  
From: SeeSchloss
Date: 6 Sep 2004 16:27:22
Message: <pan.2004.09.06.20.27.28.246600@seeschloss.org>
If anybody is interested, here's an implementation of a cellular
automaton, Conway's Game of Life, with POV's preprocessing language.

If special_pattern is true, the original pattern is taken from the file
specified by INIT (which must be the same size as the world), with white
pixels as dead cells and black pixels as living cells. If special_pattern
is false, then the initial cells are placed randomly (using seed SEED).

That's all, I hope Pan won't wrap my code, and if you have any question,
ask  :)

////////////////////////////

#declare cell_thickness = 0.125;

#declare HEIGHT = 30;
#declare WIDTH  = 30;

#declare SEED   = 1792;
#declare special_pattern = yes;

#declare STEPS  = 100;

#declare INIT="init.png"

////////////////////////////

#declare cell = box
                  {
                  <-0.5, 0.0, -0.5>
                  < 0.5, cell_thickness, 0.5>
                  texture
                    {
                    pigment
                      {
                      colour Yellow
                      }
                    }
                  }

#declare init_pattern = function
                          {
                          pattern
                            {
                            image_pattern
                              {
                              png INIT
                              }
                            }
                          }

////////////////////////////

#macro rnd (d)
  #declare dec = d - int (d);
  #if (dec < 0.5)
    #declare ret = int (d);
  #else
    #declare ret = int (d) + 1;
  #end
  ret;
#end

#macro evolve ()  // computes the next state of each cell
  #declare new_world = world;
  #declare i=1;
  #while (i <= HEIGHT)
    #declare j=1;
    #while (j <= WIDTH)
      #declare somme =         world[i  ][j+1];
      #declare somme = somme + world[i  ][j-1];
      #declare somme = somme + world[i+1][j-1];
      #declare somme = somme + world[i+1][j  ];
      #declare somme = somme + world[i+1][j+1];
      #declare somme = somme + world[i-1][j-1];
      #declare somme = somme + world[i-1][j  ];
      #declare somme = somme + world[i-1][j+1];
      #if (world[i][j] = 0)
        #if (somme = 3)
          #declare new_world[i][j] = 1;
        #end
      #else
        #if (somme != 2 & somme != 3)
          #declare new_world[i][j] = 0;
        #end
      #end
      #declare j=j+1;
    #end
  #declare i=i+1;
  #end
  #declare world = new_world;
#end

#macro show (level)
  #declare i=0;
  #while (i <= HEIGHT)
    #declare j=0;
    #while (j <= WIDTH)
      #if (world[i][j])
        object
          {
          cell
          translate <i-(HEIGHT/2), level*cell_thickness, j-(WIDTH/2)>
          texture
            {
            pigment
              {
              #if (odd (STEP))
                rgbft <STEP/STEPS/2, 1, STEP/STEPS, 0.0, 0.0>
              #else
                rgbft <1, STEP/STEPS/2, STEP/STEPS, 0.0, 0.0>
              #end
              }
            normal
              {
              bozo
              }
            finish
              {
              reflection 0.25
              }
            }
          }
      #end
      #declare j=j+1;
    #end
    #declare i=i+1;
  #end
#end

#macro init_world (empty)  // initialises the world, if (empty != 0), no cell is
placed
  #declare world  = array[HEIGHT+2];
  #declare i=0;         
  #while (i <= HEIGHT+1)
    #declare j=0;
    #declare world[i] = array[WIDTH+2];
    #while (j <= WIDTH+1)
      #if (i != 0 & i != HEIGHT+1)
        #if (j != 0 & j != WIDTH+1)
          #if (empty)
            #declare world[i][j] = 0;
          #else
            #declare world[i][j] = rnd (rand (germe) - 0.33);
            #end
        #else
          #declare world[i][j] = 0;
        #end
      #else
        #declare world[i][j] = 0;
      #end
      #declare j=j+1;
    #end
    #declare i=i+1;
  #end
#end

////////////////////////////

#declare germe = seed (SEED);

init_world (special_pattern)

#if (special_pattern)
  #declare i = 0;
  #while (i < HEIGHT)
    #declare j = 0;
    #while (j < WIDTH)
      #declare world[i][j] = -init_pattern ((i+0.5)/HEIGHT, (j+0.5)/WIDTH, 0) + 1;
      #declare j = j + 1;
    #end
    #declare i = i + 1;
  #end
#end

#declare STEP=0;
#while (STEP < STEPS)
  #debug "Placing level "
  #debug str (STEP+1, -(log (STEPS) + 1), 0)
  #debug "....... "
  union
    {
    show (STEP)
    rotate <0, 130, 0>
    }
  #debug "finished\nComputing next state... " evolve () #debug
  "finished\n"
  #declare STEP=STEP+1;
#end

////////////////////////////


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.