Advertisement
Guest User

Game of Life

a guest
Jan 18th, 2011
862
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.59 KB | None | 0 0
  1. int width = 600; // Ancho
  2. int height = 400; // Alto
  3. int dotsize = 10; // Tamanho de los cuadrados
  4. boolean field[]; // Campo
  5. int dwidth; // Cuadrados por linea
  6. boolean stop = true; // ΒΏEsta parado?
  7. int fsize; // Tamanho del array de campo
  8.  
  9. /* Dibuja una cuadrcula */
  10. void gen_background() {
  11.   stroke( 0 );
  12.   for ( int x = 0 ; x < width ; x += dotsize ) {
  13.     line( x, 0, x, height );
  14.   }
  15.   for ( int y = 0 ; y < height ; y += dotsize ) {
  16.     line( 0, y, width, y );
  17.   }
  18. }
  19.  
  20. /* Prepara el escenario */
  21. void setup() {
  22.   size( width, height );
  23.   dwidth = width / dotsize;
  24.   fsize = (height / dotsize) * dwidth;
  25.   field = new boolean[ ( width * height ) / ( dotsize * dotsize) ];
  26. }
  27.  
  28. /* Cuenta los "vecinos" */
  29. int count_elements( boolean f[], int p ) {
  30.   int i = 0 ;
  31.   if (p + 1 >= dwidth) {
  32.     if (f[ (p + 1 - dwidth) ]) {
  33.       i++;
  34.     }
  35.   }
  36.  
  37.   if (p >= dwidth)
  38.     if (f[ (p - dwidth) ]) {
  39.       i++;
  40.     }
  41.   if (p - 1 >= dwidth ) {
  42.     if (f[ (p - 1 - dwidth) ]) {
  43.       i++;
  44.     }
  45.   }
  46.   if (p > 0) {
  47.     if (f[ (p - 1) ]) {
  48.       i++;
  49.     }
  50.   }
  51.   if (p + 1 < fsize) {
  52.     if (f[ (p + 1) ]) {
  53.       i++;
  54.     }
  55.   }
  56.   if (p + 1 < fsize - dwidth ) {
  57.     if (f[ (p + 1 + dwidth) ]) {
  58.       i++;
  59.     }
  60.   }
  61.   if (p - 1 < fsize - dwidth ) {
  62.     if (f[ (p - 1 + dwidth) ]) {
  63.       i++;
  64.     }
  65.   }
  66.   if (p < fsize - dwidth ) {
  67.  
  68.     if (f[ (p + dwidth) ]) {
  69.       i++;
  70.     }
  71.   }
  72.   return i;
  73. }
  74.  
  75. /* Obtiene el siguiente array */
  76. void getNextArr( boolean f[], boolean o[] ) {
  77.   int last = ( width * height ) / ( dotsize * dotsize ) ;
  78.   boolean flipFlop = !o[0];
  79.   for ( int i = 0 ; i < last ; i++ ) {
  80.     int v = count_elements(o, i);
  81.     f [ i ] = ((o[ i ] && v > 1 && v < 4) ||
  82.       ( v == 3 ) );
  83.   }
  84. }
  85.  
  86. /* Muestra el campo */
  87. void show_field( boolean f[] ) {
  88.   fill( 0 );
  89.   int i = 0;
  90.   for( int x = 0 ; x < width ; x += dotsize ) {
  91.     int j = 0;
  92.     for ( int y = 0 ; y < height ; y += dotsize ) {
  93.       if ( f[ ( dwidth * j) + i ] ) {
  94.         rect( x, y, dotsize, dotsize );
  95.       }
  96.       j++;
  97.     }
  98.     i++;
  99.   }
  100. }
  101.  
  102. /* Funcion por defecto */
  103. void draw() {
  104.   background( 255, 255, 255 );
  105.   if (!stop) {
  106.  
  107.     boolean old[] = field;
  108.     field = new boolean[ (width * height) / ( dotsize * dotsize) ];
  109.     getNextArr( field, old );
  110.   }
  111.   gen_background();
  112.   show_field( field );
  113. }
  114.  
  115. /* Activa/Desactiva una casilla con el raton */
  116. void mousePressed() {
  117.   stop = true;
  118.   int pos = ( mouseY / dotsize * dwidth ) + ( mouseX / dotsize );
  119.   field[ pos ] = !field[ pos ];
  120. }
  121.  
  122. /* Para/Reanuda con el teclado */
  123. void keyPressed() {
  124.   stop = !stop;
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement