sdsdfdu

Conway's Game of Life in C# by Daniel G.

May 20th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.05 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Timers;
  4. //Daniel G. 3rd period AP Comp Sci
  5. class MainClass {//Conway's Game of Life
  6.     enum cell {dead, alive, tempD, tempA,};
  7.     static int gen = 0;
  8.     static bool iterate = false;
  9.     static void Life(List<List<int>> life)
  10.     {
  11.         int y = 1;
  12.         bool addTop = true;
  13.         bool addBot = true;
  14.         for(int i = 1; i < life.Count-1; i ++)//If any live cell is nearing the edge, the list will gain an index in both dimensions to remain square
  15.         {
  16.             for(int a = 1; a < life[0].Count-1; a ++)
  17.             {
  18.                 if(addTop)
  19.                 {
  20.                     if (i == 1 || a == 1)
  21.                     {
  22.                         if(life[i][a] == (int)cell.alive)
  23.                         {
  24.                             addTop = false;
  25.                             life.Insert(0,new List<int>());
  26.                             for(int w = 0; w < life[1].Count; w ++)
  27.                                 life[0].Add(0);
  28.                             for(int w = 0; w < life.Count; w ++)
  29.                                 life[w].Insert(0,0);
  30.                         }
  31.                     }
  32.                 }
  33.                 if(addBot)
  34.                 {
  35.                     if (i == life.Count-2 || a == life[0].Count-2)
  36.                     {
  37.                         if(life[i][a] == (int)cell.alive)
  38.                         {
  39.                             addBot = false;
  40.                             life.Add(new List<int>());
  41.                             for(int w = 0; w < life[1].Count; w ++)
  42.                                 life[life.Count-1].Add(0);
  43.                             for(int w = 0; w < life.Count; w ++)
  44.                                 life[w].Add(0);
  45.                         }
  46.                     }
  47.                 }
  48.                 int count = -1;
  49.                 for(int w = -1; w <= 1; w ++)
  50.                 {
  51.                     for(int z = -1; z <= 1; z ++)//Based on Conway's rules, the cells take up temporary states so as not to mess with the ongoing process
  52.                     {
  53.                         if(life[(i+w)][(a+z)] == (int)cell.alive || life[(i+w)][(a+z)] == (int)cell.tempD)
  54.                             ++count;
  55.                     }
  56.                 }
  57.                 if((count < 2) || (count > 3))//Cells are switched to either dead or alive based on temp states
  58.                 {
  59.                     if(life[i][a] != (int)cell.dead)
  60.                         life[i][a] = (int)cell.tempD;
  61.                 }
  62.                 else if(count == 2)
  63.                 {
  64.                     if(life[i][a] != (int)cell.alive)
  65.                         life[i][a] = (int)cell.tempA;
  66.                 }
  67.                 else if(count == 3)
  68.                 {
  69.                     if(life[i][a] != (int)cell.alive)
  70.                         life[i][a] = (int)cell.dead;
  71.                 }
  72.             }
  73.         }
  74.         for(int i = 0; i < life.Count-1; i ++)//The cells are converted into the proper format
  75.         {
  76.             for(int a = 0; a < life[0].Count-1; a ++)
  77.             {
  78.                 int x = life[i][a];
  79.                 if(x == (int)cell.tempD)
  80.                     x = (int)cell.dead;
  81.                 if(x == (int)cell.tempA)
  82.                     x = (int)cell.alive;
  83.                 life[i][a] = x;
  84.             }
  85.         }
  86.         Console.WriteLine("\n-----------GENERATION {0}------------------",gen);
  87.         Console.Write("\t");
  88.         foreach(List<int> x in life)
  89.         {
  90.             foreach(int i in x)
  91.             {
  92.                 Console.Write(i+" ");
  93.                 if(y++ % life[0].Count == 0 && life.IndexOf(x) != life.Count-1)
  94.                     Console.Write("\n\t");
  95.             }
  96.         }
  97.     }
  98.     static void Main()
  99.     {
  100.         Console.Write("Number of Generations: ");//How many iterations of the list will be made
  101.         int limit = Convert.ToInt32(Console.ReadLine());
  102.         Timer t = new Timer(2500);//Seperates iterations by 2.5 seconds so that it is easier to see them
  103.         int[,] b =    {{0,0,0,0,0,0,0,0,0,0},//The array is easier to visually manipulate values within the script itself
  104.                        {0,0,0,0,0,0,0,0,0,0},//The initial pattern is made here
  105.                        {0,0,0,0,0,0,0,0,0,0},
  106.                        {0,0,0,0,0,1,0,0,0,0},
  107.                        {0,0,0,0,0,0,1,0,0,0},
  108.                        {0,0,0,0,1,1,1,0,0,0},
  109.                        {0,0,0,0,0,0,0,0,0,0},
  110.                        {0,0,0,0,0,0,0,0,0,0},
  111.                        {0,0,0,0,0,0,0,0,0,0},
  112.                        {0,0,0,0,0,0,0,0,0,0}};
  113.         List<List<int>> life = new List<List<int>>();//The array is converted into a list to be used hereafter
  114.         for(int i = 0; i < b.GetLength(0); i ++)
  115.         {
  116.             life.Add(new List<int>());
  117.         }
  118.        
  119.         int y = 1;
  120.         foreach(int i in b)
  121.         {
  122.             life[(y-1)].Add(i);
  123.             if(y++ % b.GetLength(0) == 0)
  124.             {
  125.                 y = 1;
  126.             }
  127.         }
  128.         y=1;
  129.         Console.WriteLine("\n-----------GENERATION {0}------------------",gen);
  130.         Console.Write("\t");
  131.         foreach(List<int> i in life)
  132.         {
  133.             foreach(int c in i)
  134.             {
  135.                 Console.Write(c+" ");
  136.                 if(y++ % life[0].Count == 0 && life.IndexOf(i) != life.Count-1)
  137.                     Console.Write("\n\t");
  138.             }
  139.         }
  140.         t.Elapsed += new ElapsedEventHandler(timer_Tick);
  141.         t.Start();
  142.         while(gen < limit+1)//stops the timer method when the number of desired generations is reached
  143.         {
  144.             if(iterate == true)
  145.             {
  146.                 Life(life);
  147.                 iterate = false;
  148.             }
  149.             if(gen == limit)
  150.                 t.Stop();
  151.         }
  152.     }
  153.     static void timer_Tick(object sender, EventArgs e)
  154.     {
  155.       iterate = true;
  156.       ++gen;
  157.     }
  158. }
  159. /*
  160. Oh, I'm a goofy goober, yeah!
  161. You're a goofy goober, yeah!
  162. We're all goofy goobers, yeah!
  163. Goofy, goofy, goober, goober, YEAH!
  164. */
Advertisement
Add Comment
Please, Sign In to add comment