Advertisement
Guest User

Untitled

a guest
Jan 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.28 KB | None | 0 0
  1. static bool[,] NewWorld(bool[,] currGen, int N)
  2. {
  3. Random TrueOrFalse = new Random();
  4. int RandomAns;
  5. for (int C = 1; C <= N; C++)
  6. {
  7. for(int R = 1; R <= N; R++)
  8. {
  9. RandomAns = TrueOrFalse.Next(2);
  10. if (RandomAns == 0)
  11. {
  12. currGen[C, R] = false;
  13. }else
  14. {
  15. currGen[C, R] = true;
  16. }
  17. }
  18. }
  19. return currGen;
  20. }
  21.  
  22. static int NCount(bool[,] currGen, int C, int R)
  23. {
  24. int counter = 0;
  25. counter += Convert.ToInt32(currGen[C - 1, R]);
  26. counter += Convert.ToInt32(currGen[C + 1, R]);
  27. counter += Convert.ToInt32(currGen[C, R - 1]);
  28. counter += Convert.ToInt32(currGen[C, R + 1]);
  29. counter += Convert.ToInt32(currGen[C - 1, R - 1]);
  30. counter += Convert.ToInt32(currGen[C + 1, R + 1]);
  31. counter += Convert.ToInt32(currGen[C - 1, R + 1]);
  32. counter += Convert.ToInt32(currGen[C + 1, R - 1]);
  33. return counter;
  34. }
  35.  
  36. static bool[,] NextGenMaker(bool[,] currGen, bool[,] nextGen, int N)
  37. {
  38. for(int C = 1; C <= N; C++)
  39. {
  40. for (int R = 1; R <= N; R++)
  41. {
  42. if (NCount(currGen, C, R) > 5 || NCount(currGen, C, R) < 3)
  43. {
  44. nextGen[C, R] = false;
  45. }else
  46. {
  47. nextGen[C, R] = true;
  48. }
  49.  
  50.  
  51. }
  52. }
  53. return nextGen;
  54. }
  55.  
  56. static void PrintWorld(bool[,] currGen, int N)
  57. {
  58. for(int C = 1; C <= N; C++)
  59. {
  60. for(int R = 1; R <= N; R++)
  61. {
  62. Console.Write(Convert.ToInt32(currGen[C, R]) + " ");
  63. }
  64. Console.WriteLine();
  65. }
  66. }
  67.  
  68.  
  69. static void Main(string[] args)
  70. {
  71. Console.WriteLine("Please Enter The Value Of N");
  72. int N = int.Parse(Console.ReadLine());
  73. Console.WriteLine("Please Enter The Amount Of Generations To Simulate");
  74. int FutureGens = int.Parse(Console.ReadLine());
  75. bool[,] currGen = new bool[N + 2, N + 2];
  76. bool[,] nextGen = new bool[N + 2, N + 2];
  77. currGen = NewWorld(currGen, N);
  78. for (int Gens = 1; Gens <= FutureGens; Gens++)
  79. {
  80. Console.WriteLine("Simulation Of Generation Number {0}:", (Gens));
  81. PrintWorld(currGen, N);
  82. if (Gens != FutureGens)
  83. {
  84. nextGen = NextGenMaker(currGen, nextGen, N);
  85. for (int C = 1; C <= N; C++)
  86. {
  87. for(int R = 1; R <= N; R++)
  88. {
  89.  
  90. currGen[C, R] = nextGen[C, R];
  91. }
  92. }
  93.  
  94. }
  95. }
  96. }
  97. }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement