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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import java.util.Arrays;
  2. public class Life implements ILife {
  3. static String[] board = new String[]{ "* ",
  4. "* ",
  5. "* ",
  6. " ",
  7. " " };
  8. public static void main(String[] args) {
  9. Life l = new Life(board);
  10. l = (Life) l.nextGeneration();
  11. }
  12. public Life() {
  13. nukeAll();
  14. }
  15. public Life(String[] setup) {
  16. this();
  17. for (int y = 0; y < setup.length; y++){
  18. for (int x = 0; x < setup[y].length(); x++){
  19. if (setup[y].charAt(x) != ' '){
  20. setAlive(x, y);
  21. }
  22. }
  23. //System.out.println(board[y]);
  24. }
  25. printTable(board);
  26. }
  27. private void printTable(String[] board) {
  28. for(int i = 0; i < board.length; i++) System.out.println(Arrays.toString(new String[]{board[i]}));
  29. System.out.println("________");
  30. }
  31. @Override
  32. public void nukeAll() { //works
  33. // TODO Auto-generated method stub
  34. for (int y = 0; y < board.length; y++){
  35. for (int x = 0; x < board[y].length(); x++) {
  36. setDead(x, y);
  37. }
  38. }
  39. }
  40. @Override
  41. public void setAlive(int x, int y) {
  42. // TODO Auto-generated method stub
  43. board[y] = board[y].substring(0, x) + '*' + board[y].substring(x+1);
  44. }
  45. @Override
  46. public void setDead(int x, int y) {
  47. // TODO Auto-generated method stub
  48. board[y] = board[y].substring(0, x) + ' ' + board[y].substring(x+1);
  49. }
  50. @Override
  51. public boolean isAlive(int x, int y) {
  52. if(board[y].charAt(x) == '*'){
  53. return true;
  54. }
  55. else{
  56. return false;
  57. }
  58. }
  59. @Override
  60. public ILife nextGeneration() {
  61. //Problem mehrere steps auf einmal da gesetzer * bei nachfolgendem durchlauf mitberücksichtigt wird
  62. int alive;
  63. String[] nextBoard = new String[]{ " ",
  64. " ",
  65. " ",
  66. " ",
  67. " " };
  68. for (int y = 0; y < board.length; y++){
  69. for (int x = 0; x < board[y].length(); x++){
  70. alive=aliveNeighbours(x,y);
  71. //A new Cell is born
  72. if(!isAlive(x,y) && alive == 3) {
  73. nextBoard[y] = nextBoard[y].substring(0, x) + '*' + nextBoard[y].substring(x+1);
  74. }
  75. //Cell is lonely and dies
  76. else if(isAlive(x,y) && alive < 2){
  77. nextBoard[y] = nextBoard[y].substring(0, x) + ' ' + nextBoard[y].substring(x+1);
  78. }
  79. else{
  80. nextBoard[y] = nextBoard[y].substring(0, x) + board[y].charAt(x) + nextBoard[y].substring(x+1);
  81. }
  82. }
  83. }
  84. board = nextBoard;
  85. System.out.println("next Generation");
  86. printTable(board);
  87. return null;
  88. }
  89. private int aliveNeighbours(int x, int y) {
  90. int neighbours = 0;
  91. if(x>0 && y>0){
  92. if(board[y-1].charAt(x-1) == '*'){
  93. neighbours++;
  94. }
  95. }
  96. if(x>0){
  97. if(board[y].charAt(x-1) == '*'){
  98. neighbours++;
  99. }
  100. if(y<4 ){
  101. if(board[y+1].charAt(x-1) == '*'){
  102. neighbours++;
  103. }
  104. }
  105. }
  106. if(y>0){
  107. if(board[y-1].charAt(x) == '*'){
  108. neighbours++;
  109. }
  110. if(x<4){
  111. if(board[y-1].charAt(x+1) == '*'){
  112. neighbours++;
  113. }
  114. }
  115. }
  116. if(x<4){
  117. if(board[y].charAt(x+1) == '*'){
  118. neighbours++;
  119. }
  120. }
  121. if(y<4){
  122. if(board[y+1].charAt(x) == '*'){
  123. neighbours++;
  124. }
  125. }
  126. if(x<4 && y<4){
  127. if(board[y+1].charAt(x+1) == '*'){
  128. neighbours++;
  129. }
  130. }
  131. return neighbours;
  132. }
  133. }