Advertisement
Guest User

Untitled

a guest
Jun 29th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. import java.util.*;//for Scanner
  2. import java.io.*;//for reading a file
  3. //has neighbor() and isBlank() methods.
  4. public class g
  5. {
  6. /*
  7. *
  8. */ final static int M =75, N = 25;
  9. final static char DOT = '.';
  10.  
  11. public static void init(char[][] grid)
  12. //initializes the array by placing dots in it
  13. {
  14. for(int r = 0; r <= N + 1 ; r++)
  15. {
  16. for(int c = 0; c <= M + 1; c++)
  17. {
  18. grid[r][c] = DOT;
  19. }
  20. }
  21. }
  22.  
  23. public static void read(char[][] grid)
  24. //reads the file
  25. {
  26. Scanner kbd = null;;
  27. File f = new File("C:\\Users\\Usiel\\Desktop\\life1.dat");
  28. String s;
  29. try
  30. {
  31. kbd = new Scanner(f);//checked ekception
  32. }catch (Exception e){}
  33. for(int r = 1; r <= N; r++)
  34. {
  35. s = kbd.next();
  36. for(int c = 1; c <= M; c++)
  37. {
  38. grid[r][c] =s.charAt(c-1);
  39. }
  40. }
  41. }
  42. public static char[][] newgrid(char[][] grid)
  43. {
  44. char[][] grid2 = new char[N+2][M + 2];
  45. for(int row = 1; row <= N; row++)
  46. {
  47. for (int col = 1; col <= M; col++)
  48. {
  49. if (grid[row][col] == 'X' && neighbor(grid, row, col)==2)
  50. {
  51. grid2[row][col] = 'X';
  52. }
  53. else if (grid[row][col] == 'X' && neighbor(grid, row, col)==3)
  54. {
  55. grid2[row][col] = 'X';
  56. }
  57. else if (grid[row][col] == '.' && neighbor(grid, row, col)==3)
  58. {
  59. grid2[row][col] = 'X';
  60. }
  61. else
  62. {
  63. grid2[row][col] = '.';
  64. }
  65. }
  66. }
  67. return grid2;
  68. }
  69.  
  70. public static int neighbor(char[][] grid, int row, int col)
  71. //counts the number of nearest neighbors
  72. {
  73. int count = 0;
  74. for(int r = row -1; r <= row+ 1; r++)
  75. {
  76. for( int c = col-1; c<= col+1; c++)
  77. {
  78. if( grid[r][c] == 'X' )
  79. {
  80. count++;
  81. }
  82. }
  83. }
  84. if(grid[row][col] =='X') count = count -1;//compensate for middle cell having an 'X'
  85. return count;
  86. }
  87.  
  88. public static boolean isBlank(char[][] grid )
  89. //checks if world is all blanks
  90. {
  91.  
  92. for(int r = 1; r <= N; r++)
  93. {
  94. for( int c = 1; c<= M; c++)
  95. {
  96. if( grid[r][c] == 'X' )
  97. {
  98. return false;
  99. }
  100. }
  101. }
  102. return true;
  103. }
  104.  
  105. public static void print(char[][] grid)
  106. //prints the array
  107. {
  108. for(int r = 1; r <= N ; r++)
  109. {
  110. for(int c = 1; c <= M; c++)
  111. {
  112. System.out.print( grid[r][c]);
  113. }
  114. System.out.println();
  115. }
  116. }
  117. public static void pause()
  118. {
  119. System.out.println("1 to continue");
  120. int temp = 0;
  121. Scanner wait = new Scanner(System.in);
  122. temp = wait.nextInt();
  123. while (temp != 1);
  124. }
  125.  
  126. public static void main(String[] args)
  127. {
  128. char[][] grid = new char[N+2][M + 2];
  129. init(grid);
  130. read(grid);
  131. print(grid);
  132. System.out.println();
  133. pause();
  134. char[][] grid2 = new char[N+2][M + 2];
  135. grid2 = newgrid(grid);
  136. print(grid2);
  137. }
  138. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement