Advertisement
Guest User

Untitled

a guest
Aug 26th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.79 KB | None | 0 0
  1. public class ConwayLife
  2. {
  3.     /* Please note that the htmlize function for C# currently isn't working
  4.         properly. I tested it on rextester.com and the code worked as expected,
  5.         but for some reason on codewars it isn't. When I find a solution to
  6.         the issue I will update the function. */
  7.     public static int[,] GetGeneration(int[,] cells, int generation)
  8.     {
  9.         for (int i = 0; i < generation; i++)
  10.         {
  11.             cells = progressConwayLife(enlargeCanvas(cells));
  12.             cells = adjustframe(cells);
  13.         }
  14.  
  15.         return cells;
  16.     }
  17.  
  18.     private static int[,] enlargeCanvas(int[,] cells)
  19.     {
  20.         int[,] frame = new int[cells.GetUpperBound(0) + 3, cells.GetUpperBound(1) + 3];
  21.         for (int i = 0; i <= cells.GetUpperBound(0); i++)
  22.         {
  23.             for (int j = 0; j <= cells.GetUpperBound(1); j++)
  24.             {
  25.                 frame[i + 1, j + 1] = cells[i, j];
  26.             }
  27.         }
  28.  
  29.         return frame;
  30.     }
  31.    
  32.     private static int[,] adjustframe(int[,] cells)
  33.     {
  34.         int firstHorizontal = 0;
  35.         int firstVertical = cells.GetUpperBound(1);
  36.         bool firstSet = false;
  37.         for (int i = 0; i <= cells.GetUpperBound(0); i++)
  38.         {
  39.             for (int j = 0; j <= cells.GetUpperBound(1); j++)
  40.             {
  41.                 if (cells[i, j] > 0)
  42.                 {
  43.                     if (!firstSet)
  44.                         firstHorizontal = i;
  45.                     firstSet = true;
  46.                     if (j < firstVertical)
  47.                     {
  48.                         firstVertical = j;
  49.                     }
  50.                 }
  51.             }
  52.         }
  53.  
  54.         if(!firstSet)
  55.             return new int[0,0];
  56.  
  57.  
  58.         firstSet = false;
  59.         int lastHorizontal = cells.GetUpperBound(0);
  60.         int lastVertical = 0;
  61.         for (int i = cells.GetUpperBound(0); i >= 0; i--)
  62.         {
  63.             for (int j = cells.GetUpperBound(1); j >= 0; j--)
  64.             {
  65.                 if (cells[i, j] > 0)
  66.                 {
  67.                     if (!firstSet)
  68.                         lastHorizontal = i;
  69.                     firstSet = true;
  70.                     if (j > lastVertical)
  71.                         lastVertical = j;
  72.                 }
  73.             }
  74.         }
  75.  
  76.  
  77.  
  78.         int[,] frame = new int[(lastHorizontal - firstHorizontal)+1, (lastVertical - firstVertical)+1];
  79.         for (int i = firstHorizontal; i <= lastHorizontal; i++)
  80.         {
  81.             for (int j = firstVertical; j <= lastVertical; j++)
  82.             {
  83.                 frame[i-firstHorizontal, j-firstVertical] = cells[i, j];
  84.             }
  85.         }
  86.  
  87.         return frame;
  88.     }
  89.  
  90.     private static int[,] progressConwayLife(int[,] cells)
  91.     {
  92.         int[,] result = new int[cells.GetUpperBound(0)+1, cells.GetUpperBound(1)+1];
  93.         for (int i = 0; i <= cells.GetUpperBound(0); i++)
  94.         {
  95.             for (int j = 0; j <= cells.GetUpperBound(1); j++)
  96.             {
  97.                 result[i, j] = getNewAlive(cells, i, j);
  98.             }
  99.         }
  100.  
  101.         return result;
  102.     }
  103.  
  104.     private static int getNewAlive(int[,] cells, int k, int l)
  105.     {
  106.         int value = 0;
  107.         for (int i = -1; i < 2; i++)
  108.         {
  109.             for (int j = -1; j < 2; j++)
  110.             {
  111.                 if (!(i == 0 && j == 0))
  112.                 {
  113.                     if (i + k >= 0 && l + j >= 0 && i+k <= cells.GetUpperBound(0) && l+j <= cells.GetUpperBound(1))
  114.                     {
  115.                         value += cells[i + k, j + l];
  116.                     }
  117.                 }
  118.             }
  119.         }
  120.  
  121.         if (value < 2 || value > 3)
  122.             return 0;
  123.         else if ((cells[k, l] != 0) || (cells[k,l] == 0 && value == 3))
  124.             return 1;
  125.         else
  126.             return 0;
  127.     }
  128.    
  129.    
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement