SWEME Praktikum
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.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import java.util.Arrays;
  2. public class Life implements ILife {
  3. String[] lifeArray;
  4. public static void main(String[] args) {
  5. Life l = new Life(new String[] { " ",
  6. " ",
  7. " *** ",
  8. " ",
  9. " " });
  10. l = (Life) l.nextGeneration();
  11. }
  12. public Life() { nukeAll(); }
  13. public Life(String[] setup) {
  14. this();
  15. for (int y = 0; y < setup.length; y++)
  16. for (int x = 0; x < setup[y].length(); x++)
  17. if (setup[y].charAt(x) != ' ')
  18. setAlive(x, y);
  19. }
  20. @Override
  21. public void nukeAll() {
  22. // TODO Auto-generated method stub
  23. lifeArray = new String[] { " ",
  24. " ",
  25. " ",
  26. " ",
  27. " "};
  28. }
  29. @Override
  30. public void setAlive(int x, int y) {
  31. // TODO Auto-generated method stub
  32. String newString = "";
  33. newString = lifeArray[y].substring(0, x) + "*" + lifeArray[y].substring(x);
  34. lifeArray[y] = newString;
  35. }
  36. @Override
  37. public void setDead(int x, int y) {
  38. // TODO Auto-generated method stub
  39. String newString = "";
  40. newString = lifeArray[y].substring(0, x) + " " + lifeArray[y].substring(x);
  41. lifeArray[y] = newString;
  42. }
  43. @Override
  44. public boolean isAlive(int x, int y) {
  45. // TODO Auto-generated method stub
  46. if(lifeArray[y].charAt(x) == '*')
  47. return true;
  48. return false;
  49. }
  50. @Override
  51. public ILife nextGeneration() {
  52. // TODO Auto-generated method stub
  53. Life newLife = new Life();
  54. for(int y = 0; y < lifeArray.length; y++) {
  55. for(int x = 0; x < lifeArray[y].length(); x++) {
  56. int neighbors = countNeighbors(x, y);
  57. writeNewGen(neighbors, newLife, x, y);
  58. }
  59. }
  60. return newLife;
  61. }
  62. private void writeNewGen(int neighbors, Life newLife, int x, int y) {
  63. //if the space is alive...
  64. if(isAlive(x, y)) {
  65. //...and has more than three or less than two neighbors, it dies
  66. if (neighbors > 3 || neighbors < 2)
  67. newLife.setDead(x, y);
  68. //...and has 2 to 3 neighbors, it survives
  69. if (neighbors == 3 || neighbors == 2)
  70. newLife.setAlive(x, y);
  71. }
  72. //if the space is dead...
  73. if(!isAlive(x, y)) {
  74. //and has 3 neighbors, it comes to life
  75. if (neighbors == 3) {
  76. newLife.setAlive(x, y);
  77. } else {
  78. //and has anything but 3 neighbors, it stays dead
  79. newLife.setDead(x, y);
  80. }
  81. }
  82. }
  83. private int countNeighbors(int x, int y) {
  84. int counter = 0;
  85. //count three above
  86. counter += countAbove(x, y);
  87. //count three below
  88. counter += countBelow(x, y);
  89. //count two sides
  90. counter += countSides(x, y);
  91. return counter;
  92. }
  93. private int countSides(int x, int y) {
  94. int counter = 0;
  95. //is the space left us within the playing field and alive?
  96. if(x-1 >= 0 && isAlive(x-1, y))
  97. counter++;
  98. //is the space right us within the playing field and alive?
  99. if(x+1 < lifeArray[y].length() && isAlive(x+1, y))
  100. counter++;
  101. return counter;
  102. }
  103. private int countBelow(int x, int y) {
  104. int counter = 0;
  105. //is the line below us within the playing field?
  106. if(y+1 < lifeArray.length) {
  107. for (int i = x-1; i <= x+1; i++) {
  108. //are the spaces on our left, middle and right bottom within the playing field alive?
  109. if (i >= 0 && i < lifeArray[y+1].length() && isAlive(i, y + 1))
  110. counter++;
  111. }
  112. }
  113. return counter;
  114. }
  115. private int countAbove(int x, int y) {
  116. int counter = 0;
  117. //is the line above us within the playing field?
  118. if(y-1 >= 0) {
  119. for (int i = x-1; i <= x+1; i++) {
  120. //are the spaces on our left, middle and right top within the playing field alive?
  121. if (i >= 0 && i < lifeArray[y-1].length() && isAlive(i, y - 1))
  122. counter++;
  123. }
  124. }
  125. return counter;
  126. }
  127. public void showBoard() {
  128. for(int y = 0; y < lifeArray.length-1; y++)
  129. System.out.println(Arrays.toString(new String[] {lifeArray[y]}));
  130. }
  131. }