Advertisement
stanevplamen

03.02.05.LifeGame

Jul 26th, 2013
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.72 KB | None | 0 0
  1. using System;
  2.  
  3. class LifeGame
  4. {
  5.     public static int[,] lifeMatrix;
  6.  
  7.     static void Main()
  8.     {
  9.         int numberOfCycles = int.Parse(Console.ReadLine());
  10.         string input = Console.ReadLine();
  11.         string[] splitedInput = input.Split();
  12.  
  13.         int allRows = int.Parse(splitedInput[0]);
  14.         int allCols = int.Parse(splitedInput[1]);
  15.  
  16.         lifeMatrix = new int[allRows + 2, allCols + 2];
  17.  
  18.         for (int rows = 1; rows < allRows+1; rows++)
  19.         {
  20.             string tempInput = Console.ReadLine();
  21.             string[] splitedTempInput = tempInput.Split();
  22.             for (int cols = 1; cols < allCols + 1; cols++)
  23.             {
  24.                 int currentNumber = int.Parse(splitedTempInput[cols - 1]);
  25.                 if (currentNumber == 1)
  26.                 {
  27.                     lifeMatrix[rows, cols] = 1;
  28.                 }
  29.                 else
  30.                 {
  31.                     lifeMatrix[rows, cols] = 2;
  32.                 }
  33.             }
  34.         }
  35.         // 0 neutral, 1 life, 2 dead (round of neutrals)
  36.         ImplementLifeCycle(numberOfCycles);
  37.     }
  38.  
  39.     private static void ImplementLifeCycle(int numberOfCycles)
  40.     {
  41.         int cycleLiveCells = 0;
  42.         bool[,] toChange = new bool[lifeMatrix.GetLength(0), lifeMatrix.GetLength(1)];
  43.         for (int i = 1; i <= numberOfCycles; i++)
  44.         {          
  45.             cycleLiveCells = 0;
  46.             for (int row = 1; row < lifeMatrix.GetLength(0) - 1; row++)
  47.             {
  48.                 for (int col = 1; col < lifeMatrix.GetLength(1) - 1; col++)
  49.                 {
  50.                     int lifeNeighbors = 0;
  51.  
  52.                     if (lifeMatrix[row-1, col-1] == 1) { lifeNeighbors++; }
  53.                     if (lifeMatrix[row-1, col] == 1) { lifeNeighbors++; }
  54.                     if (lifeMatrix[row-1, col+1] == 1) { lifeNeighbors++; }
  55.                     if (lifeMatrix[row, col-1] == 1) { lifeNeighbors++; }
  56.                     if (lifeMatrix[row, col+1] == 1) { lifeNeighbors++; }
  57.                     if (lifeMatrix[row+1, col-1] == 1) { lifeNeighbors++; }
  58.                     if (lifeMatrix[row+1, col] == 1) { lifeNeighbors++; }
  59.                     if (lifeMatrix[row+1, col+1]  == 1) { lifeNeighbors++; }
  60.  
  61.                     if (lifeMatrix[row, col] == 2 && lifeNeighbors != 3)
  62.                     {
  63.                         continue;
  64.                     }
  65.                     else if (lifeMatrix[row, col] == 2 && lifeNeighbors == 3)
  66.                     {
  67.                         //lifeMatrix[row, col] = 1;
  68.                         toChange[row, col] = true;
  69.                     }
  70.                     else if (lifeMatrix[row, col] == 1 && (lifeNeighbors == 2 || lifeNeighbors == 3))
  71.                     {
  72.                         cycleLiveCells++;
  73.                     }
  74.                     else
  75.                     {
  76.                         toChange[row, col] = true;
  77.                     }                  
  78.                 }
  79.             }
  80.  
  81.             for (int row = 1; row < lifeMatrix.GetLength(0) - 1; row++)
  82.             {
  83.                 for (int col = 1; col < lifeMatrix.GetLength(1) - 1; col++)
  84.                 {
  85.                     if (toChange[row, col] == true)
  86.                     {
  87.                         if (lifeMatrix[row, col] == 1)
  88.                         {
  89.                             lifeMatrix[row, col] = 2;
  90.                         }
  91.                         else
  92.                         {
  93.                             lifeMatrix[row, col] = 1;
  94.                             cycleLiveCells++;
  95.                         }
  96.                         toChange[row, col] = false;
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.         Console.WriteLine(cycleLiveCells);
  102.     }
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement