home *** CD-ROM | disk | FTP | other *** search
/ Photo CD Demo 1 / Demo.bin / cellsim / v2_5 / cm2 / rule_src / cyclv256.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-02-26  |  2.1 KB  |  83 lines

  1. /*
  2.  * File: cycle.v256.c
  3.  *   By: Dave Hiebeler
  4.  *       hiebeler@turing.cs.rpi.edu
  5.  *       Feb. 1990
  6.  *
  7.  * Griffeath's cyclic CA rule, described by A.K. Dewdney in his
  8.  * "Computer Recreations" column in Scientific American, sometime in '89.
  9.  *
  10.  * parm1 is the number of states to use.
  11.  *
  12.  * This is the version which picks a random neighbor to compare itself to,
  13.  * rather than checking all neighbors.
  14.  *
  15.  * An interesting starting-pattern is a square of randomness in a clear
  16.  * background.
  17.  * For example, on a 256x256 array, with parm1 set to 16, make a 40x40
  18.  * square of cells with values between 0 and 15, out in the middle of
  19.  * an otherwise empty array.
  20.  */
  21.  
  22.  
  23. #include "CMnborhood.h"
  24.  
  25. byte cycle_rule(), cycle_exit();
  26. static CM_field_id_t direction, chosen_nbor;
  27.  
  28.  
  29. void
  30. init_function()
  31. {
  32.  
  33.     update_function = cycle_rule;
  34.     exit_function = cycle_exit;
  35.     parm1 = 16;
  36.     direction = CM_allocate_heap_field(2);
  37.     chosen_nbor = CM_allocate_heap_field(8);
  38. }
  39.  
  40.  
  41. byte
  42. cycle_rule()
  43. {
  44.     /* pick a random direction */
  45.     CM_u_random_1L(direction, 2, 4);
  46.     /* then get the appropriate neighbor to compare ourselves to */
  47.     CM_u_eq_constant_1L(direction, 0, 2);
  48.     CM_logand_context_with_test();
  49.     CM_u_move_1L(chosen_nbor, VNorth, 8);
  50.     CM_set_context();
  51.     CM_u_eq_constant_1L(direction, 1, 2);
  52.     CM_logand_context_with_test();
  53.     CM_u_move_1L(chosen_nbor, VSouth, 8);
  54.     CM_set_context();
  55.     CM_u_eq_constant_1L(direction, 2, 2);
  56.     CM_logand_context_with_test();
  57.     CM_u_move_1L(chosen_nbor, VEast, 8);
  58.     CM_set_context();
  59.     CM_u_eq_constant_1L(direction, 3, 2);
  60.     CM_logand_context_with_test();
  61.     CM_u_move_1L(chosen_nbor, VWest, 8);
  62.     
  63.     CM_set_context();
  64.     /* compare center+1 to chosen neighbor */
  65.     CM_u_add_constant_2_1L(VCenter, 1, 8);
  66.     CM_u_mod_constant_2_1L(VCenter, parm1, 8);
  67.     CM_u_eq_1L(chosen_nbor, VCenter, 8);
  68.     CM_logand_context_with_test();
  69.     /* if equal, set cell to new value, the value of chosen_nbor.
  70.      * otherwise, we won't change cell.
  71.      */
  72.     CM_u_move_1L(cell, chosen_nbor, 8);
  73.     CM_set_context();
  74. }
  75.  
  76.  
  77. byte
  78. cycle_exit()
  79. {
  80.     CM_deallocate_heap_field(direction);
  81.     CM_deallocate_heap_field(chosen_nbor);
  82. }
  83.