Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. import java.io.File;
  2. import java.util.Scanner;
  3.  
  4. public class LifeKNematiPeriod3 {
  5.  
  6. int[][] board;
  7. int x = 0;
  8. int y = 0;
  9. int[] horizontal = { -1, 0, 1, -1, 1, -1, 0, 1 };
  10. int[] vertical = { -1, -1, -1, 0, 0, 0, 1, 1, 1 };
  11.  
  12. public static void main(String[] args){
  13. LifeKNematiPeriod3 k = new LifeKNematiPeriod3("life100.txt");
  14. k.runLife(2);
  15. k.printBoard();
  16. }
  17.  
  18. public LifeKNematiPeriod3(String fileName) {
  19. File file = new File(fileName);
  20. try {
  21. Scanner input = new Scanner(file);
  22. x = input.nextInt();
  23. y = input.nextInt();
  24. board = new int[x][y];
  25. while (input.hasNextLine()) {
  26. board[input.nextInt()][input.nextInt()] = 1;
  27. }
  28.  
  29. } catch (Exception e) {
  30. System.out.println(e.getMessage());
  31. System.exit(0);
  32. }
  33.  
  34. }
  35.  
  36. public void runLife(int numGenerations) {
  37. // complete this as many times as the generation you are trying to get
  38. for (int i = 1; i < numGenerations; i++) {
  39. int[][] temp = new int[x][y];
  40. // go through the entire board
  41. for (int row = 0; row < x; row++) {
  42. for (int col = 0; col < y; col++) {
  43. boolean living;
  44. int alive = 0;
  45. // check if the spot is living or not
  46. if (board[row][col] == 1) {
  47. living = true;
  48. } else {
  49. living = false;
  50. }
  51. // for each spot, go through each of the neighbors, and
  52. // count how many are living
  53. for (int j = 0; j < 8; j++) {
  54. // if the position is not out of bounds
  55. if ((row + horizontal[j]) >= 0
  56. && (row + horizontal[j]) < x
  57. && (col + vertical[j]) >= 0
  58. && (col + vertical[j]) < y) {
  59. // if the position is living
  60. if (board[row + horizontal[j]][col + vertical[j]] == 1) {
  61. alive++;
  62. }
  63. }
  64. }
  65. // decide whether you should make it alive in the new array
  66. if (living == true) {
  67. if (alive == 2 || alive == 3) {
  68. temp[row][col] = 1;
  69. }
  70. } else {
  71. if (alive == 3) {
  72. temp[row][col] = 1;
  73. }
  74. }
  75. }
  76. }
  77. board = temp;
  78. printBoard();
  79. }
  80. }
  81.  
  82. public int rowCount(int row) {
  83. if (row > x || row < 0) {
  84. return -1;
  85. }
  86. int count = 0;
  87. for (int i = 0; i < board.length; i++) {
  88. for (int j = 0; j < board[i].length; j++) {
  89. if (board[row][j] == 1) {
  90. if (row == i) {
  91. count++;
  92. }
  93. }
  94. }
  95. }
  96. return count;
  97. }
  98.  
  99. public int colCount(int col) {
  100. if (col > y || col < 0) {
  101. return -1;
  102. }
  103. int count = 0;
  104. for (int row = 0; row < board.length; row++) {
  105. for (int j = 0; j < board[row].length; j++) {
  106. if (board[row][j] == 1) {
  107. if (col == j) {
  108. count++;
  109. }
  110. }
  111. }
  112. }
  113. return count;
  114. }
  115.  
  116. public int totalCount() {
  117. int count = 0;
  118. for (int row = 0; row < board.length; row++) {
  119. for (int col = 0; col < board[row].length; col++) {
  120. if (board[row][col] == 1) {
  121. count++;
  122. }
  123. }
  124. }
  125. return count;
  126. }
  127.  
  128. public void printBoard() {
  129. System.out.print(" ");
  130. for (int i = 0; i <= x; i++) {
  131. System.out.printf("%4d", i);
  132. }
  133. System.out.println();
  134. for (int row = 0; row < board.length; row++) {
  135. System.out.printf("%4d", row);
  136. for (int col = 0; col < board[row].length; col++) {
  137. if (board[row][col] == 1) {
  138. System.out.printf("%4s", "*");
  139. } else {
  140. System.out.printf("%4s", " ");
  141. }
  142. }
  143.  
  144. System.out.println();
  145. }
  146. }
  147.  
  148. public void nextGeneration() {
  149. runLife(1);
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement