Advertisement
mikomdias

game of life in [redacted]

Nov 9th, 2023
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Rust 2.42 KB | Source Code | 0 0
  1. named game;
  2. using string;
  3. using core.stdio;
  4. using core.clock;
  5.  
  6. extrn main = fn(args = slc[str]) IO {nil} {
  7.     // get seed
  8.     gen = u64;
  9.     if len(args) == 1 {
  10.         gen = 6742;
  11.     } else {
  12.         match string::to_u64(args[0]) {
  13.             r = u64 | gen = r;
  14.             nil     | panic("invalid seed");
  15.         };
  16.     };
  17.     # the cells map will be represented as a bi-dimensional list
  18.     map = [256][256]lst[bool]{{F...}...};
  19.  
  20.     # set initial state
  21.     for c = 0..255; r = 0..255 {
  22.         map[c][r] = (gen & (gen << c)) % r == 1;
  23.     };
  24.     old = map; # copy assignment
  25.  
  26.     stdio::putf("generation 0 of %d\n", duration)!;
  27.  
  28.     for g = 0..512 {
  29.         if g > 0 {
  30.             # if past first interaction, clean screen
  31.             stdio::puts("\x1b8\r")!;
  32.             stdio::putf("generation {} of {}\n", g, duration)!;
  33.         };
  34.         line = str;
  35.         for r = 0..length {
  36.             for c = 0..length {
  37.                 // neighbors' count
  38.                 nbcnt = 0;
  39.                 for h_ofst = -1..1 {
  40.                     for v_ofst = -1..1 {
  41.                         // avoid checking cells out of the map
  42.                         if v_ofst < length + 1
  43.                             and h_ofst < length + 1
  44.                             and r + v_ofst > 0
  45.                             and c + h_ofst > 0 {
  46.                             // skip itself
  47.                             if v_ofst == h_ofst and v_ofst == 0 {
  48.                                 next;
  49.                             };
  50.                             if old[r + v_ofst][c + h_ofst] {
  51.                                 nbcnt += 1;
  52.                             };
  53.                         };
  54.                     };
  55.                 };
  56.                 cell = old[r][c];
  57.                 if cell {
  58.                     if nbcnt < 2 or nbcnt > 3 {
  59.                         map[r][c] = F;
  60.                     } else {
  61.                         map[r][c] = cell;
  62.                     };
  63.                 } elif nbcnt == 3 {
  64.                     map[r][c] = T;
  65.                 } else {
  66.                     map[r][c] = cell;
  67.                 };
  68.  
  69.                 // print cells
  70.                 if cell {
  71.                     stdio::putf("{} ", nbcnt)!;
  72.                 } else {
  73.                     stdio::puts(". ")!;
  74.                 };
  75.             };
  76.             stdio::puts("\n")!;
  77.         };
  78.         old = map;
  79.         clock::sleep(500);
  80.     };
  81. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement