Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.80 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. public class g
  4. {
  5.  
  6.  
  7. final static int M = 75, N = 25;
  8. final static char DOT = '.';
  9.  
  10. public static void init(char[][] grid)
  11. {
  12. for(int r = 0; r <= N + 1 ; r++)
  13. {
  14. for(int c = 0; c <= M + 1; c++)
  15. {
  16. grid[r][c] = DOT;
  17. }
  18. }
  19. }
  20.  
  21. public static void read(char[][] grid)
  22. {
  23. Scanner kbd = null;;
  24. File f = new File("C:\\Users\\Usiel\\Desktop\\life1.dat");
  25. String s;
  26. try
  27. {
  28. kbd = new Scanner(f);
  29. }catch (Exception e){}
  30. for(int r = 1; r <= N; r++)
  31. {
  32. s = kbd.next();
  33. for(int c = 1; c <= M; c++)
  34. {
  35. grid[r][c] =s.charAt(c-1);
  36. }
  37. }
  38. }
  39. public static char[][] newgrid(char[][] grid)
  40. {
  41. char[][] grid2 = new char[N+2][M + 2];
  42. for(int row = 1; row <= N; row++)
  43. {
  44. for (int col = 1; col <= M; col++)
  45. {
  46. if (grid[row][col] == 'X' && neighbor(grid, row, col)==2)
  47. {
  48. grid2[row][col] = 'X';
  49. }
  50. else if (grid[row][col] == 'X' && neighbor(grid, row, col)==3)
  51. {
  52. grid2[row][col] = 'X';
  53. }
  54. else if (grid[row][col] == '.' && neighbor(grid, row, col)==3)
  55. {
  56. grid2[row][col] = 'X';
  57. }
  58. else
  59. {
  60. grid2[row][col] = '.';
  61. }
  62. }
  63. }
  64. return grid2;
  65. }
  66.  
  67. public static int neighbor(char[][] grid, int row, int col)
  68. {
  69. int count = 0;
  70. for(int r = row -1; r <= row+ 1; r++)
  71. {
  72. for( int c = col-1; c<= col+1; c++)
  73. {
  74. if( grid[r][c] == 'X' )
  75. {
  76. count++;
  77. }
  78. }
  79. }
  80. if(grid[row][col] =='X') count = count -1;
  81. return count;
  82. }
  83.  
  84. public static boolean isBlank(char[][] grid )
  85. {
  86.  
  87. for(int r = 1; r <= N; r++)
  88. {
  89. for( int c = 1; c<= M; c++)
  90. {
  91. if( grid[r][c] == 'X' )
  92. {
  93. return false;
  94. }
  95. }
  96. }
  97. return true;
  98. }
  99.  
  100. public static boolean stable(char[][] grid, char[][] grid2)
  101. {
  102.  
  103. for(int r = 1; r <= N; r++)
  104. {
  105. for( int c = 1; c<= M; c++)
  106. {
  107. if( grid[r][c] != grid2[r][c] )
  108. {
  109. return false;
  110. }
  111. }
  112. }
  113. return true;
  114. }
  115.  
  116. public static void print(char[][] grid)
  117. {
  118. for(int r = 1; r <= N ; r++)
  119. {
  120. for(int c = 1; c <= M; c++)
  121. {
  122. System.out.print( grid[r][c]);
  123. }
  124. System.out.println();
  125. }
  126. }
  127. public static int pause()
  128. {
  129. System.out.println("1 to continue, 0 to exit");
  130. int temp = 2;
  131. Scanner wait = new Scanner(System.in);
  132. temp = wait.nextInt();
  133. while (temp != 1 && temp != 0);
  134. return temp;
  135. }
  136.  
  137. public static void main(String[] args)
  138. {
  139. int exit = 1;
  140. char[][] grid = new char[N+2][M + 2];
  141. init(grid);
  142. read(grid);
  143. print(grid);
  144. System.out.println();
  145. exit = pause();
  146. char[][] grid2 = new char[N+2][M + 2];
  147. grid2 = newgrid(grid);
  148. if (exit != 0 && !isBlank(grid))
  149. {
  150. print(grid2);
  151. System.out.println();
  152. exit = pause();
  153. while (exit != 0 && !isBlank(grid) && !isBlank(grid2) && !stable(grid, grid2))
  154. {
  155. grid = newgrid(grid2);
  156. print(grid);
  157. System.out.println();
  158. exit = pause();
  159. if (exit != 0 && !isBlank(grid) && !isBlank(grid2) && !stable(grid, grid2))
  160. {
  161. grid2 = newgrid(grid);
  162. print(grid2);
  163. System.out.println();
  164. exit = pause();
  165. }
  166. }
  167. }
  168. if (stable(grid, grid2))
  169. {
  170. System.out.println("The environment is stable and will not change.");
  171. }
  172. if (isBlank(grid) || isBlank(grid2))
  173. {
  174. System.out.println("The environment is empty.");
  175. }
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement