Advertisement
AvengersAssemble

GameOfLife

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