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 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. import java.util.Arrays;
  2. public class Life implements ILife {
  3. static String[] welt = new String[]{
  4. " ",
  5. " ",
  6. " *** ",
  7. " ",
  8. " "};
  9. public static void main(String[] args) {
  10. Life l = new Life(welt);
  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. public void printWelt() {
  24. for (int i = 0; i < welt.length; i++)
  25. System.out.println(Arrays.toString(new String[]{welt[i]}));
  26. }
  27. @Override
  28. public void nukeAll() {
  29. // TODO Auto-generated method stub
  30. for (int y = 0; y < welt.length; y++)
  31. for (int x = 0; x < welt[y].length(); x++)
  32. setDead(x, y);
  33. }
  34. @Override
  35. public void setAlive(int x, int y) {
  36. // TODO Auto-generated method stub
  37. welt[y] = welt[y].substring(0, x) + '*' + welt[y].substring(x + 1);
  38. }
  39. @Override
  40. public void setDead(int x, int y) {
  41. // TODO Auto-generated method stub
  42. welt[y] = welt[y].substring(0, x) + ' ' + welt[y].substring(x + 1);
  43. }
  44. @Override
  45. public boolean isAlive(int x, int y) {
  46. // TODO Auto-generated method stub
  47. if (welt[y].charAt(x) == '*')
  48. return true;
  49. else
  50. return false;
  51. }
  52. @Override
  53. public boolean isDead(int x, int y) {
  54. // TODO Auto-generated method stub
  55. if (welt[y].charAt(x) == ' ')
  56. return true;
  57. else
  58. return false;
  59. }
  60. @Override
  61. public ILife nextGeneration() {
  62. // TODO Auto-generated method stub
  63. String[] neueWelt = new String[]{
  64. " ",
  65. " ",
  66. " ",
  67. " ",
  68. " "};
  69. //Zellen einzeln betrachten
  70. for (int y = 0; y < welt.length; y++)
  71. for (int x = 0; x < welt[y].length(); x++) {
  72. //anzahl der lebenden nachbarn
  73. int nachbarn = lebendeNachbarn(y, x);
  74. //neue Zelle
  75. //TODO fehler wenn x = 0
  76. if (nachbarn == 3) {
  77. if (x > 0 && x < welt[0].length() - 1) {
  78. neueWelt[y] = neueWelt[y].substring(0, x) + '*' + neueWelt[y].substring(x + 1);
  79. } else if (x == 0) {
  80. neueWelt[y] = '*' + neueWelt[y].substring(x + 1);
  81. } else {
  82. neueWelt[y] = neueWelt[y].substring(0, x) + '*';
  83. }
  84. }
  85. //eine Zelle stirbt an überpopulation oder einsamkeit
  86. //TODO fehler wenn x = 0
  87. if (nachbarn < 2 || nachbarn > 3) {
  88. if (x > 0 && x < welt[0].length() - 1) {
  89. neueWelt[y] = neueWelt[y].substring(0, x) + ' ' + neueWelt[y].substring(x + 1);
  90. } else if (x == 0) {
  91. neueWelt[y] = ' ' + neueWelt[y].substring(x + 1);
  92. } else {
  93. neueWelt[y] = neueWelt[y].substring(0, x) + ' ';
  94. }
  95. }
  96. //eine Zelle bleibt am Leben
  97. //TODO fehler wenn x = 0
  98. if ((nachbarn == 2) && (welt[y].charAt(x) == '*')) {
  99. if (x > 0 && x < welt[0].length() - 1) {
  100. neueWelt[y] = neueWelt[y].substring(0, x) + '*' + neueWelt[y].substring(x + 1);
  101. } else if (x == 0) {
  102. neueWelt[y] = '*' + neueWelt[y].substring(x + 1);
  103. } else {
  104. neueWelt[y] = neueWelt[y].substring(0, x) + '*';
  105. }
  106. }
  107. }
  108. return new Life(neueWelt);
  109. }
  110. public int lebendeNachbarn(int y, int x) { // 1, 1
  111. int nachbarn = 0;
  112. //linke nachbars-spalte
  113. if (x > 0) {
  114. if (y > 0) {
  115. if (welt[y - 1].charAt(x - 1) == '*') {
  116. nachbarn++;
  117. }
  118. }
  119. if (welt[y].charAt(x - 1) == '*') {
  120. nachbarn++;
  121. }
  122. if (y < welt.length - 1) {
  123. if (welt[y + 1].charAt(x - 1) == '*') {
  124. nachbarn++;
  125. }
  126. }
  127. }
  128. //rechte nachbars-spalte
  129. if (x < welt[0].length() - 1) {
  130. if (y > 0) {
  131. if (welt[y - 1].charAt(x + 1) == '*') {
  132. nachbarn++;
  133. }
  134. }
  135. if (welt[y].charAt(x + 1) == '*') {
  136. nachbarn++;
  137. }
  138. if (y < welt.length - 1) {
  139. if (welt[y + 1].charAt(x + 1) == '*') {
  140. nachbarn++;
  141. }
  142. }
  143. }
  144. // mittlere Spalte
  145. if (y > 0) {
  146. if (welt[y - 1].charAt(x) == '*') {
  147. nachbarn++;
  148. }
  149. }
  150. if (y < welt.length - 1) {
  151. if (welt[y + 1].charAt(x) == '*') {
  152. nachbarn++;
  153. }
  154. }
  155. return nachbarn;
  156. }
  157. @Override
  158. public String toString() {
  159. return super.toString();
  160. }
  161. }