You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Life.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import java.util.Arrays;
  2. public class Life implements ILife {
  3. Life nextworld;
  4. String[] board;
  5. public static void main(String[] args) {
  6. Life l = new Life(new String[]{" ",
  7. " ",
  8. " *** ",
  9. " ",
  10. " "});
  11. l = (Life) l.nextGeneration();
  12. }
  13. public Life() {
  14. nukeAll();
  15. }
  16. public Life(String[] setup) {
  17. this();
  18. for (int y = 0; y < setup.length; y++)
  19. for (int x = 0; x < setup[y].length(); x++)
  20. if (setup[y].charAt(x) != ' ')
  21. setAlive(x, y);
  22. }
  23. @Override
  24. public void nukeAll() {
  25. // TODO Auto-generated method stub
  26. board = new String[]{" ",
  27. " ",
  28. " ",
  29. " ",
  30. " "};
  31. }
  32. @Override
  33. public void setAlive(int x, int y) {
  34. // TODO Auto-generated method stub
  35. String line = "";
  36. for (int i = 0; i < board[y].length(); i++) {
  37. if (i == x)
  38. line += '*';
  39. else
  40. line += board[y].charAt(i);
  41. }
  42. board[y] = line;
  43. }
  44. @Override
  45. public void setDead(int x, int y) {
  46. // TODO Auto-generated method stub
  47. String line = "";
  48. for (int i = 0; i < board[y].length(); i++) {
  49. if (i == x)
  50. line += ' ';
  51. else
  52. line += board[y].charAt(i);
  53. }
  54. board[y] = line;
  55. }
  56. @Override
  57. public boolean isAlive(int x, int y) {
  58. // TODO Auto-generated method stub
  59. if (board[y].charAt(x) == '*')
  60. return true;
  61. else
  62. return false;
  63. }
  64. @Override
  65. public ILife nextGeneration() {
  66. // TODO Auto-generated method stub
  67. Life nextWorld = new Life();
  68. for (int y = 0; y < board.length; y++) {
  69. for (int x = 0; x < board[y].length(); x++) {
  70. int neighbourCells = CountNeighbourCells(x, y);
  71. if (isAlive(x, y))
  72. if (neighbourCells == 3 || neighbourCells == 2)
  73. nextWorld.setAlive(x, y);
  74. if (neighbourCells < 2 || neighbourCells > 3)
  75. nextWorld.setDead(x, y);
  76. if (!isAlive(x, y))
  77. if (neighbourCells == 3)
  78. nextWorld.setAlive(x, y);
  79. else
  80. nextWorld.setDead(x, y);
  81. }
  82. }
  83. nextWorld.drawWorld();
  84. return nextWorld;
  85. }
  86. public int CountNeighbourCells(int x, int y) {
  87. int pre_y = y - 1;
  88. int pre_x = x - 1;
  89. int after_y = y + 1;
  90. int after_x = x + 1;
  91. int counter = 0;
  92. //UpperSide
  93. if (pre_y >= 0) {
  94. for (int i = pre_x; i <= after_x; i++) {
  95. if (i >= 0 && i < board[pre_y].length()&& isAlive(i,pre_y)){
  96. counter++;
  97. }
  98. }
  99. }
  100. //DownSide
  101. if (after_y < board.length) {
  102. for (int i = pre_x; i <= after_x; i++) {
  103. if (i >= 0 && i < board[after_y].length()&&isAlive(i,after_y)) {
  104. counter++;
  105. }
  106. }
  107. }
  108. //leftSide
  109. if (pre_x >= 0&& isAlive(pre_x,y)) {
  110. counter++;
  111. }
  112. //rightSide
  113. if (after_x < board[y].length()&&isAlive(after_x,y)) {
  114. counter++;
  115. }
  116. return counter;
  117. }
  118. public void drawWorld() {
  119. for (int i = 0; i < board.length; i++) {
  120. System.out.println(Arrays.toString(new String[]{
  121. board[i]
  122. }));
  123. }
  124. }
  125. }