Advertisement
Kaelygon

Conway's game of life but it's ascii and on torus

Apr 30th, 2023 (edited)
1,225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #include <ncurses.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <locale.h>
  5. #include <unistd.h>
  6. #include <math.h>
  7. #include <ctime>
  8.  
  9. int main()
  10. {
  11.  
  12.     // Initialize ncurses
  13.     setlocale(LC_ALL, "");
  14.     initscr();
  15.     cbreak();
  16.     noecho();
  17.     nodelay(stdscr, TRUE);
  18.  
  19.     float gps = 20; //generations per second
  20.  
  21.     uint sheight = 20;
  22.     uint swidth = 30;
  23.  
  24.     //cell margin
  25.     int mrg=1;
  26.     int mrg2=mrg*2;
  27.  
  28.     int swm2 = swidth+mrg2;
  29.     int shm2 = sheight+mrg2;
  30.  
  31.     // Declare the 2D array pointer
  32.     bool** screen;
  33.     bool** screenbuf;
  34.  
  35.     // Allocate the first dimension of the array
  36.     screen = new bool*[swm2];
  37.     screenbuf = new bool*[swm2];
  38.  
  39.     // Allocate the second dimension of the array for each row
  40.     for (int i = 0; i < swm2; ++i) {
  41.         screen[i] = new bool[shm2];
  42.         screenbuf[i] = new bool[shm2];
  43.     }
  44.  
  45.     int exit=0;
  46.  
  47.     //set zero
  48.     for (int yi = 0; yi < shm2; yi += 1) {
  49.         for (int xi = 0; xi < swm2; xi += 1) {
  50.             screen   [xi][yi]=0;
  51.             screenbuf[xi][yi]=0;
  52.         }
  53.     }
  54.  
  55.     //flyers and a rods
  56.     for(int ofs=0;ofs<4;ofs++){
  57.         int m=4*ofs;
  58.         screen[(1+m)%swm2][(0+m)%shm2]=1;
  59.         screen[(2+m)%swm2][(1+m)%shm2]=1;
  60.         screen[(0+m)%swm2][(2+m)%shm2]=1;
  61.         screen[(1+m)%swm2][(2+m)%shm2]=1;
  62.         screen[(2+m)%swm2][(2+m)%shm2]=1;
  63.         if(ofs%2==1){
  64.             screen[(10+m)%swm2][(0+m)%shm2]=1;
  65.             screen[(10+m)%swm2][(1+m)%shm2]=1;
  66.             screen[(10+m)%swm2][(2+m)%shm2]=1;
  67.         }
  68.     }
  69.  
  70.     //preview
  71.     clear();
  72.     for (int yi = 0; yi < sheight; yi += 1) {
  73.         for (int xi = 0; xi < swidth; xi += 1) {
  74.             int xb = (xi + swm2 ) % swm2;
  75.             int yb = (yi + shm2 ) % shm2;
  76.             screen[xb][yb] ? printw("██") : printw("░░");
  77.         }
  78.         printw("\n");
  79.     }
  80.     refresh();
  81.  
  82.     getchar();
  83.  
  84.     clock_t stclock=0;
  85.     clock_t ndclock,tdelta=0;
  86.     uint long gen = 0;
  87.  
  88.     //generation loop
  89.     do{
  90.         clear();
  91.         for (int yi = 0; yi < sheight; yi += 1) {
  92.             for (int xi = 0; xi < swidth; xi += 1) {
  93.                 int xb = (xi + swm2 ) % swm2;
  94.                 int yb = (yi + shm2 ) % shm2;
  95.                 screen[xb][yb] ? printw("██") : printw("░░");
  96.             }
  97.             printw("\n");
  98.         }
  99.         printw("gen:%llu    gps:%f\n",gen,1000000.0/tdelta);
  100.  
  101.         clock();
  102.        
  103.         //copy to buffer
  104.         for (int yi = 0; yi < shm2; yi += 1) {
  105.             for (int xi = 0; xi < swm2; xi += 1) {
  106.                 screenbuf[xi][yi]=screen[xi][yi];
  107.             }
  108.         }
  109.  
  110.         //compute cell rules
  111.         for (int yi = 0; yi < shm2; yi += 1) {
  112.             for (int xi = 0; xi < swm2; xi += 1) {
  113.                
  114.                 int xp = (xi + 1        ) % swm2;
  115.                 int xn = (xi - 1 + swm2 ) % swm2;
  116.                 int yp = (yi + 1        ) % shm2;
  117.                 int yn = (yi - 1 + shm2 ) % shm2;
  118.  
  119.                 bool neig[8]={
  120.                     screenbuf[xn][yp], //NE
  121.                     screenbuf[xp][yi], //N
  122.                     screenbuf[xp][yp], //NW
  123.                     screenbuf[xn][yi], //W
  124.                     screenbuf[xp][yn], //SW
  125.                     screenbuf[xi][yp], //S
  126.                     screenbuf[xn][yn], //SE
  127.                     screenbuf[xi][yn]  //E
  128.                 };
  129.                
  130.                 int neigsum=0;
  131.                 for(int i=0; i<sizeof(neig);i++){
  132.                     neigsum+=neig[i];
  133.                 }
  134.  
  135.                 if(neigsum==3){
  136.                     screen[xi][yi]=1;
  137.                 }else if(neigsum==2){
  138.  
  139.                 }else{
  140.                     screen[xi][yi]=0;
  141.                 }
  142.  
  143.             }
  144.         }
  145.         gen++;
  146.  
  147.         //read key
  148.         do{
  149.             ndclock=clock();
  150.             tdelta = ndclock-stclock;
  151.  
  152.             int keypress=getch();
  153.             if (keypress != ERR) {
  154.                 switch (keypress) {
  155.                     case 'q':   //quit
  156.                         exit = 1;
  157.                         break;
  158.                     case '1':   //slow down
  159.                         gps/=2;
  160.                         break;
  161.                     case '2':   //gps rst
  162.                         gps=10;
  163.                         break;
  164.                     case '3':   //speed up
  165.                         gps*=2;
  166.                         break;
  167.                 }
  168.             }
  169.         }while(tdelta<1000000/gps);
  170.         usleep(1000000/gps/2);
  171.         stclock=clock();
  172.        
  173.     }while(!exit);
  174.  
  175.     endwin();
  176.  
  177.     //delete
  178.     for (int i = 0; i < swm2; ++i) {
  179.         delete[] screen[i];
  180.     }
  181.     delete[] screen;
  182.  
  183.     for (int i = 0; i < swm2; ++i) {
  184.         delete[] screenbuf[i];
  185.     }
  186.     delete[] screenbuf;
  187.  
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement