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.4KB

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