SHARE
TWEET

Untitled

a guest Oct 21st, 2019 66 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. namespace GameOfLife
  2. {
  3. public class Game
  4. {
  5. public static bool[,] NextStep(bool[,] field)
  6. {
  7. var a = new bool[field.GetLength(0), field.GetLength(1)];
  8. for (var i = 0; i < field.GetLength(0) - 1; i++)
  9. for (var j = 0; j < field.GetLength(1) - 1; j++)
  10. a[i, j] = field[i, j];
  11.  
  12. for (var i = 0; i < field.GetLength(0) - 1; i++)
  13. for (var j = 0; j < field.GetLength(1) - 1; j++)
  14. {
  15. var temp = new bool[3, 3];
  16.  
  17. if (i == 0 || j == 0 || i == field.GetLength(0) - 1 || j == field.GetLength(1) - 1)
  18. temp = GetUnic(i, j, field);
  19.  
  20. else
  21. temp = new bool[,]{{field[i - 1 , j - 1], field[i - 1, j], field[i - 1, j + 1]},
  22. {field[i, j - 1], field[i , j], field[i, j + 1]},
  23. {field[i + 1 , j - 1], field[i + 1, j], field[i + 1, j + 1]}
  24. };
  25. a[i, j] = Check(temp);
  26. }
  27.  
  28. return a;
  29. }
  30.  
  31. private static bool[,] GetUnic(int i, int j, bool[,] field)
  32. {
  33. if (i == 0 && j == 0)
  34. return new bool[,]{{false, false, false},
  35. {false, field[i , j], field[i, j + 1]},
  36. {false, field[i + 1, j], field[i + 1, j + 1]}
  37. };
  38. if (i == 0 && j == field.GetLength(1) - 1)
  39. return new bool[,]{{false, false, false},
  40. {field[i, j - 1], field[i , j], false},
  41. {field[i + 1 , j - 1], field[i + 1, j], false}
  42. };
  43. if (i == field.GetLength(0) - 1 && j == 0)
  44. return new bool[,]{{false, field[i - 1, j], field[i - 1, j + 1]},
  45. {false, field[i , j], field[i, j + 1]},
  46. {false, false, false}
  47. };
  48. if (i == field.GetLength(0) - 1 && j == field.GetLength(1) - 1)
  49. return new bool[,]{{field[i - 1 , j - 1], field[i - 1, j], false},
  50. {field[i, j - 1], field[i , j],false},
  51. {false, false, false}
  52. };
  53. if (i == field.GetLength(0) - 1)
  54. return new bool[,]{{field[i - 1 , j - 1], field[i - 1, j], false},
  55. {field[i, j - 1], field[i , j], false},
  56. {field[i + 1 , j - 1], field[i + 1, j], false}
  57. };
  58. if (i == 0)
  59. return new bool[,]{{false, false, false},
  60. {field[i, j - 1], field[i , j], field[i, j + 1]},
  61. {field[i + 1 , j - 1], field[i + 1, j], field[i + 1, j + 1]}
  62. };
  63. if (j == 0)
  64. return new bool[,]{{false, field[i - 1 , j], field[i , j + 1]},
  65. {false, field[i , j], field[i, j + 1]},
  66. {false, field[i + 1, j], field[i + 1, j + 1]}
  67. };
  68. if (j == field.GetLength(1) - 1)
  69. return new bool[,]{{field[i - 1 , j - 1], field[i - 1, j], false},
  70. {field[i, j - 1], field[i , j], false},
  71. {field[i + 1, j - 1], field[i + 1, j], false}
  72. };
  73. return null;
  74. }
  75. public static bool Check(bool[,] field)
  76. {
  77. var result = 0;
  78. for (var i = 0; i < 3; i++)
  79. for (var j = 0; j < 3; j++)
  80. if (field[i, j] && !(i == 1 && j == 1))
  81. result += 1;
  82. if (result == 2)
  83. return field[1, 1];
  84. if (result == 3)
  85. return true;
  86. return false;
  87. }
  88. }
  89. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top