Advertisement
AvengersAssemble

GameOfLifeAnnotated

Feb 6th, 2014
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.34 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace GameOfLife
  7. {
  8.     class Program
  9.     {
  10.         /// <summary>
  11.         /// Function recieves board and a copy of the board and starts a new cycle of the game.
  12.         /// </summary>
  13.         /// <param name="arr">The board</param>
  14.         /// <param name="copy">The copy of the board</param>
  15.         public static void UpdateBoard(int[,] arr, int[,] copy)
  16.         {
  17.             for (int i = 1; i < arr.GetLength(0) - 1; i++)
  18.             {
  19.                 for (int k = 1; k < arr.GetLength(1) - 1; k++)
  20.                     UpdateCell(arr, i, k, copy);
  21.             }
  22.         }
  23.         /// <summary>
  24.         /// Function
  25.         /// </summary>
  26.         /// <param name="arr"></param>
  27.         /// <param name="a"></param>
  28.         /// <param name="b"></param>
  29.         /// <param name="copy"></param>
  30.         public static void UpdateCell(int[,] arr, int a, int b, int[,] copy)
  31.         {
  32.             int neihbors = CountNeighbors(arr, a, b);
  33.             switch (neihbors)
  34.             {
  35.                 case 0:
  36.                 case 1:
  37.                     copy[a, b] = 0;
  38.                     break;
  39.                 case 2:
  40.                     break;
  41.                 case 3:
  42.                     if (arr[a, b] == 0)
  43.                         copy[a, b] = 1;
  44.                     break;
  45.                 default:
  46.                     copy[a, b] = 0;
  47.                     break;
  48.             }
  49.         }
  50.         public static int CountNeighbors(int[,] arr, int a, int b)
  51.         {
  52.             int count = 0;
  53.             if (arr[a, b] == 1)
  54.                 count--;
  55.             for (int i = a - 1; i < a + 2; i++)
  56.             {
  57.                 for (int k = b - 1; k < b + 2; k++)
  58.                 {
  59.                     count += (arr[i, k] + Math.Abs(arr[i, k])) / 2;
  60.                 }
  61.             }
  62.             return count;
  63.         }
  64.         public static void InitializeArray(int[,] arr)
  65.         {
  66.             for (int i = 0; i < arr.GetLength(0); i += arr.GetLength(0) - 1)
  67.                 for (int k = 0; k < arr.GetLength(1); k++)
  68.                     arr[i, k] = -1;
  69.             for (int i = 0; i < arr.GetLength(1); i += arr.GetLength(1) - 1)
  70.                 for (int k = 0; k < arr.GetLength(0); k++)
  71.                     arr[k, i] = -1;
  72.             for (int i = 1; i < arr.GetLength(0) - 1; i++)
  73.             {
  74.                 for (int k = 1; k < arr.GetLength(1) - 1; k++)
  75.                     arr[i, k] = int.Parse(Console.ReadLine());
  76.             }
  77.         }
  78.         public static void RandomSetup(int[,] arr)
  79.         {
  80.             Random rnd = new Random();
  81.             for (int i = 0; i < arr.GetLength(0); i += arr.GetLength(0) - 1)
  82.                 for (int k = 0; k < arr.GetLength(1); k++)
  83.                     arr[i, k] = -1;
  84.             for (int i = 0; i < arr.GetLength(1); i += arr.GetLength(1) - 1)
  85.                 for (int k = 0; k < arr.GetLength(0); k++)
  86.                     arr[k, i] = -1;
  87.             for (int i = 1; i < arr.GetLength(0) - 1; i++)
  88.             {
  89.                 for (int k = 1; k < arr.GetLength(1) - 1; k++)
  90.                 {
  91.                     arr[i, k] = rnd.Next(0, 2);
  92.                     for (int m = 0; m < rnd.Next(1, 3); m++)
  93.                     {
  94.                         if (arr[i, k] == 1)
  95.                             arr[i, k] = rnd.Next(0, 2);
  96.                     }
  97.                 }
  98.             }
  99.         }
  100.         public static void CopyBoard(int[,] original, int[,] copy)
  101.         {
  102.             for (int i = 0; i < original.GetLength(0); i++)
  103.             {
  104.                 for (int k = 0; k < original.GetLength(1); k++)
  105.                     copy[i, k] = original[i, k];
  106.             }
  107.         }
  108.         public static void Print(int[,] arr)
  109.         {
  110.             for (int i = 0; i < arr.GetLength(0); i++)
  111.             {
  112.                 for (int k = 0; k < arr.GetLength(1); k++)
  113.                 {
  114.                     switch (arr[i, k])
  115.                     {
  116.                         case 1:
  117.                             //Console.Write("▀");
  118.                             Console.Write("♦");
  119.                             break;
  120.                         case 0:
  121.                             Console.Write(" ");
  122.                             break;
  123.                         default:
  124.                             Console.Write(" ");
  125.                             break;
  126.                     }
  127.                 }
  128.                 Console.WriteLine();
  129.             }
  130.         }
  131.         public static bool GameOver(int[,] board)
  132.         {
  133.             for (int i = 0; i < board.GetLength(0); i++)
  134.             {
  135.                 for (int k = 0; k < board.GetLength(1); k++)
  136.                     if (board[i, k] == 1)
  137.                         return false;
  138.             }
  139.             return true;
  140.         }
  141.         public static bool StandStill(int[,] board, int[,] lastBoard)
  142.         {
  143.             for (int i = 0; i < board.GetLength(0); i++)
  144.             {
  145.                 for (int k = 0; k < board.GetLength(1); k++)
  146.                 {
  147.                     if (board[i, k] != lastBoard[i, k])
  148.                         return false;
  149.                 }
  150.             }
  151.             return true;
  152.         }
  153.         static void Main(string[] args)
  154.         {
  155.             char playAgain = 'y';
  156.             int[,] gameBoard = new int[15, 65];
  157.             int[,] boardCopy = new int[gameBoard.GetLength(0), gameBoard.GetLength(1)];
  158.             while (playAgain.ToString().ToLower().Equals("y"))
  159.             {
  160.                 RandomSetup(gameBoard);
  161.                 CopyBoard(gameBoard, boardCopy);
  162.                 do
  163.                 {
  164.                     CopyBoard(boardCopy, gameBoard);
  165.                     Print(gameBoard);
  166.                     UpdateBoard(gameBoard, boardCopy);
  167.                     System.Threading.Thread.Sleep(400);
  168.                     Console.Clear();
  169.                 }
  170.                 while (!GameOver(gameBoard) && !StandStill(boardCopy, gameBoard));
  171.                 if (GameOver(gameBoard))
  172.                     Console.WriteLine("Congratulations, you won!");
  173.                 else
  174.                     Console.WriteLine("You lose!");
  175.                 Console.WriteLine("Play again? y/n");
  176.                 playAgain = char.Parse(Console.ReadLine());
  177.                 Console.Clear();
  178.             }
  179.         }
  180.     }
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement