Advertisement
Guest User

Untitled

a guest
Nov 18th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. import java.util.*;
  2. import java.lang.*;
  3.  
  4. public class Test {
  5. public static void main(String args[]) {
  6. int n = Integer.parseInt(args[0]);
  7. Cell Field[][] = new Cell[n][n];
  8. int counter=0;
  9.  
  10. for(int i=0; i<n;i++){
  11. for (int j=0; j<n;j++){
  12. Field[i][j]=new Cell(false, 0);
  13. }
  14. }
  15.  
  16. Cell Check = new Cell(false, 0);
  17. Initialization(n,Field);
  18.  
  19. do {
  20. Neighborhood(n,Field);
  21. Existence(n,Field);
  22. if( counter==0){System.out.println("First (Random) iteration: ");Draw(n,Field);}
  23. counter++;
  24. } while(!Check.CheckChanges());
  25. System.out.println(counter + " iterations soon..");
  26. Draw(n,Field);
  27.  
  28. }
  29. private static void Initialization(int n, Cell Field[][]){
  30. Random r = new Random(System.currentTimeMillis());
  31. for (int i=0;i<n;i++){
  32. for(int j=0; j<n;j++){
  33. if(r.nextBoolean()) Field[i][j].SetCell();
  34. }
  35. }
  36. }
  37. private static void Neighborhood(int n,Cell Field[][]){
  38. for (int i = 0;i < n;i++){
  39. for(int j = 0; j < n;j++){
  40. Field[i][j].NeighArrayReset();
  41. if ((j - 1 >= 0) && Field[i][j - 1].Flag) Field[i][j].NeighborhoodProc(1,0);
  42. if ((j + 1 < n) && Field[i][j + 1].Flag) Field[i][j].NeighborhoodProc(1,2);
  43. if ((i + 1 < n) && Field[i + 1][j].Flag) Field[i][j].NeighborhoodProc(2,1);
  44. if ((i - 1 >= 0) && Field[i - 1][j].Flag) Field[i][j].NeighborhoodProc(0,1);
  45. if ((j + 1 <n) && (i +1 <n) && Field[i + 1][j + 1].Flag) Field[i][j].NeighborhoodProc(2,2);
  46. if ((j - 1 >= 0) && (i -1 >=0) && Field[i - 1][j - 1].Flag) Field[i][j].NeighborhoodProc(0,0);
  47. if ((j - 1 >= 0) && (i + 1<n) && Field[i + 1][j - 1].Flag) Field[i][j].NeighborhoodProc(2,0);
  48. if ((i - 1 >= 0) && (j + 1<n) && Field[i - 1][j + 1].Flag) Field[i][j].NeighborhoodProc(0,2);
  49.  
  50. }
  51. }
  52. }
  53. private static void Existence(int n, Cell Field[][]){
  54. for (int i=0; i<n; i++){
  55. for (int j=0; j<n; j++){
  56. Field[i][j].CheckExistence();
  57. }
  58. }
  59. }
  60. private static void Draw(int n, Cell Field[][]){
  61. for (int i=0; i<n; i++){
  62. for (int j=0; j<n; j++){
  63. if(j+1==n) {
  64. if (Field[i][j].Flag) System.out.println(" O ");
  65. else System.out.println(" # ");
  66. } else {
  67. if (Field[i][j].Flag) System.out.print(" O ");
  68. else System.out.print(" # ");
  69. }
  70. }
  71. }
  72. }
  73. }
  74.  
  75. class Cell {
  76. private static long Counter=0;
  77. private static long Changes=0;
  78. private boolean CellFlag;
  79. private int Neighbors;
  80. boolean Flag;
  81. private int[][] NeighArray ={{0,0,0},{0,0,0},{0,0,0}};
  82.  
  83. Cell(boolean Cell, int n){
  84. CellFlag = Cell;
  85. Neighbors = n;
  86. Flag=CellFlag;
  87. }
  88. void SetCell(){
  89. CellFlag=true;
  90. Flag=true;
  91. Neighbors =0;
  92. }
  93. private void PopCell(){
  94. CellFlag=false;
  95. Flag = false;
  96. NeighArrayReset();
  97.  
  98. }
  99. void CheckExistence() {
  100. if(CellFlag) ResetExistence();
  101. else SetExistence();
  102. }
  103. private void ResetExistence(){
  104. if (Neighbors>3 || Neighbors<2){
  105. PopCell();
  106. Counter++;
  107. }
  108. }
  109. private void SetExistence(){
  110. if (Neighbors==3) {
  111. SetCell();
  112. Counter++;
  113. }
  114. }
  115. boolean CheckChanges(){
  116. if (Changes==Counter) return true;
  117. else Changes=Counter;
  118. return false;
  119. }
  120. void NeighborhoodProc(int i, int j){
  121. if(NeighArray[i][j]!=1){
  122. NeighArray[i][j]=1;
  123. Neighbors++;
  124. }
  125. }
  126. void NeighArrayReset(){
  127. for (int i=0; i<3; i++){
  128. for (int j=0; j<3; j++){
  129. NeighArray[i][j]=0;
  130. }
  131. }
  132. Neighbors=0;
  133. }
  134. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement