VaKeR CYBER ARMY
Logo of a company Server : Apache/2.4.41 (Ubuntu)
System : Linux absol.cf 5.4.0-198-generic #218-Ubuntu SMP Fri Sep 27 20:18:53 UTC 2024 x86_64
User : www-data ( 33)
PHP Version : 7.4.33
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Directory :  /usr/share/emscripten/tests/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //usr/share/emscripten/tests/life.c
// From http://rosettacode.org/wiki/Conway%27s_Game_of_Life#C

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
 
#define for_x for (int x = 0; x < w; x++)
#define for_y for (int y = 0; y < h; y++)
#define for_xy for_x for_y

void show(void *u, int w, int h)
{
  int (*univ)[w] = u;
  for_x printf("-"); printf("\n");
  for_y {
    for_x printf(univ[y][x] ? "[]" : "  ");
    printf("\n");
  }
  for_x printf("-"); printf("\n");
  fflush(stdout);
}
 
void evolve(void *u, int w, int h)
{
  unsigned (*univ)[w] = u;
  unsigned new[h][w];
 
  for_y for_x {
    int n = 0;
    for (int y1 = y - 1; y1 <= y + 1; y1++)
      for (int x1 = x - 1; x1 <= x + 1; x1++)
        if (univ[(y1 + h) % h][(x1 + w) % w])
          n++;
 
    if (univ[y][x]) n--;
    new[y][x] = (n == 3 || (n == 2 && univ[y][x]));
  }
  for_y for_x univ[y][x] = new[y][x];
}

void nudge(void *u, int w, int h)
{
  unsigned (*univ)[w] = u;
  int sum = 0;
  for_xy sum += univ[y][x];
  while (sum < (w*h)/8) {
    int x = sum & (w-1);
    int y = (sum*sum) & (h-1);
    univ[y][x] = 1;
    sum++;
  }
}

void game(int w, int h, int i)
{
  unsigned univ[h][w];
  //for_xy univ[y][x] = rand() < RAND_MAX / 10 ? 1 : 0;
  int acc = 0; // nonrandom generation, for benchmarking
  for_xy {
    acc += (x*17) % (y*3 + 1);
    univ[y][x] = acc & 1;
  }
  while (i != 0) {
    //show(univ, w, h);
    evolve(univ, w, h);
    if (i > 0) {
      i--;
      nudge(univ, w, h); // keep it interesting for benchmark
    } else {
#if !__EMSCRIPTEN__
      usleep(20000);
#endif
      show(univ, w, h);
    }
  }
  show(univ, w, h);
}
 
int main(int argc, char **argv)
{
  int w, h, i;
  int arg = argc > 1 ? argv[1][0] - '0' : 3;
  switch(arg) {
    case 0: return 0; break;
    case 1: w = h = 32; i = 2500; break;
    case 2: w = h = 32; i = 13000; break;
    case 3: w = h = 32; i = 24000; break;
    case 4: w = h = 32; i = 5*24000; break;
    case 5: w = h = 32; i = 10*24000; break;
    default: printf("error: %d\\n", arg); return -1;
  }

  printf("life: %d,%d,%d,%d\n", arg, w, h, i);
  game(w, h, i);

  return 0;
}


VaKeR 2022