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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. import java.util.Arrays;
  2. //Gruppe: @FrederikHagen, @DenushanJegatheeswaran, @NathanGebre-Michael, @Justus Görgens
  3. public class Life implements ILife {
  4. static String[] welt = new String[]{
  5. " ",
  6. " ",
  7. " *** ",
  8. " ",
  9. " "};
  10. public static void main(String[] args) {
  11. Life l = new Life(welt);
  12. l = (Life) l.nextGeneration();
  13. }
  14. public Life() {
  15. nukeAll();
  16. }
  17. public Life(String[] setup) {
  18. this();
  19. for (int y = 0; y < setup.length; y++)
  20. for (int x = 0; x < setup[y].length(); x++)
  21. if (setup[y].charAt(x) != ' ')
  22. setAlive(x, y);
  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 boolean isDead(int x, int y) {
  55. // TODO Auto-generated method stub
  56. if (welt[y].charAt(x) == ' ')
  57. return true;
  58. else
  59. return false;
  60. }
  61. @Override
  62. public ILife nextGeneration() {
  63. // TODO Auto-generated method stub
  64. String[] neueWelt = new String[]{
  65. " ",
  66. " ",
  67. " ",
  68. " ",
  69. " "};
  70. //Zellen einzeln betrachten
  71. for (int y = 0; y < welt.length; y++)
  72. for (int x = 0; x < welt[y].length(); x++) {
  73. //anzahl der lebenden nachbarn
  74. int nachbarn = lebendeNachbarn(y, x);
  75. //neue Zelle
  76. //TODO fehler wenn x = 0
  77. if (nachbarn == 3) {
  78. if (x > 0 && x < welt[0].length() - 1) {
  79. neueWelt[y] = neueWelt[y].substring(0, x) + '*' + neueWelt[y].substring(x + 1);
  80. } else if (x == 0) {
  81. neueWelt[y] = '*' + neueWelt[y].substring(x + 1);
  82. } else {
  83. neueWelt[y] = neueWelt[y].substring(0, x) + '*';
  84. }
  85. }
  86. //eine Zelle stirbt an überpopulation oder einsamkeit
  87. //TODO fehler wenn x = 0
  88. if (nachbarn < 2 || nachbarn > 3) {
  89. if (x > 0 && x < welt[0].length() - 1) {
  90. neueWelt[y] = neueWelt[y].substring(0, x) + ' ' + neueWelt[y].substring(x + 1);
  91. } else if (x == 0) {
  92. neueWelt[y] = ' ' + neueWelt[y].substring(x + 1);
  93. } else {
  94. neueWelt[y] = neueWelt[y].substring(0, x) + ' ';
  95. }
  96. }
  97. //eine Zelle bleibt am Leben
  98. //TODO fehler wenn x = 0
  99. if ((nachbarn == 2) && (welt[y].charAt(x) == '*')) {
  100. if (x > 0 && x < welt[0].length() - 1) {
  101. neueWelt[y] = neueWelt[y].substring(0, x) + '*' + neueWelt[y].substring(x + 1);
  102. } else if (x == 0) {
  103. neueWelt[y] = '*' + neueWelt[y].substring(x + 1);
  104. } else {
  105. neueWelt[y] = neueWelt[y].substring(0, x) + '*';
  106. }
  107. }
  108. }
  109. return new Life(neueWelt);
  110. }
  111. public int lebendeNachbarn(int y, int x) { // 1, 1
  112. int nachbarn = 0;
  113. //linke nachbars-spalte
  114. if (x > 0) {
  115. if (y > 0) {
  116. if (welt[y - 1].charAt(x - 1) == '*') {
  117. nachbarn++;
  118. }
  119. }
  120. if (welt[y].charAt(x - 1) == '*') {
  121. nachbarn++;
  122. }
  123. if (y < welt.length - 1) {
  124. if (welt[y + 1].charAt(x - 1) == '*') {
  125. nachbarn++;
  126. }
  127. }
  128. }
  129. //rechte nachbars-spalte
  130. if (x < welt[0].length() - 1) {
  131. if (y > 0) {
  132. if (welt[y - 1].charAt(x + 1) == '*') {
  133. nachbarn++;
  134. }
  135. }
  136. if (welt[y].charAt(x + 1) == '*') {
  137. nachbarn++;
  138. }
  139. if (y < welt.length - 1) {
  140. if (welt[y + 1].charAt(x + 1) == '*') {
  141. nachbarn++;
  142. }
  143. }
  144. }
  145. // mittlere Spalte
  146. if (y > 0) {
  147. if (welt[y - 1].charAt(x) == '*') {
  148. nachbarn++;
  149. }
  150. }
  151. if (y < welt.length - 1) {
  152. if (welt[y + 1].charAt(x) == '*') {
  153. nachbarn++;
  154. }
  155. }
  156. return nachbarn;
  157. }
  158. @Override
  159. public String toString() {
  160. return super.toString();
  161. }
  162. }