Aufgabe auf dem dritten 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.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //Bearbeitet von: Marie Trexler
  2. import java.util.Arrays;
  3. public class Life implements ILife {
  4. String[] gameWorld;
  5. public static void main(String[] args) {
  6. Life l = new Life( new String[] { " ",
  7. " ",
  8. " *** ",
  9. " ",
  10. " "});
  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. @Override
  24. public void nukeAll() {
  25. // TODO Auto-generated method stub
  26. gameWorld = new String[]{" ",
  27. " ",
  28. " ",
  29. " ",
  30. " "};
  31. }
  32. @Override
  33. public void setAlive(int x, int y) {
  34. // TODO Auto-generated method stub
  35. String newString ="";
  36. for(int i=0; i< gameWorld[y].length(); i++){
  37. if(i==x)
  38. newString +='*';
  39. else
  40. newString += gameWorld[y].charAt(i);
  41. }
  42. gameWorld[y]=newString;
  43. }
  44. @Override
  45. public void setDead(int x, int y) {
  46. // TODO Auto-generated method stub
  47. String newString ="";
  48. for(int i=0; i< gameWorld[y].length(); i++){
  49. if(i==x)
  50. newString +=' ';
  51. else
  52. newString += gameWorld[y].charAt(i);
  53. }
  54. gameWorld[y]=newString;
  55. }
  56. @Override
  57. public boolean isAlive(int x, int y) {
  58. // TODO Auto-generated method stub
  59. if(gameWorld[y].charAt(x) =='*'){
  60. return true;}
  61. else{
  62. return false;
  63. }}
  64. @Override
  65. public ILife nextGeneration() {
  66. // TODO Auto-generated method stub
  67. Life newWorld = new Life();
  68. for(int y=0; y< gameWorld.length;y++){
  69. for(int x= 0; x <gameWorld[y].length(); x++){
  70. int neighbor = countNeighbor(x, y);
  71. if(isAlive(x,y)) {
  72. if (neighbor == 3 || neighbor == 2) {
  73. newWorld.setAlive(x, y);
  74. }
  75. if (neighbor < 2 || neighbor > 3) {
  76. newWorld.setDead(x, y);
  77. }
  78. }
  79. if(!isAlive(x,y)) {
  80. if (neighbor == 3) {
  81. newWorld.setAlive(x, y);
  82. } else {
  83. newWorld.setDead(x, y);
  84. }
  85. }
  86. }
  87. }
  88. newWorld.showBoard();
  89. return newWorld;
  90. }
  91. public int countNeighbor(int x, int y) {
  92. int counter=0;
  93. //counts top row
  94. if(y-1 >=0){
  95. for(int i =x-1; i<= x+1;i++){
  96. if(i>=0 && i< gameWorld[y-1].length()){
  97. if(isAlive(i,y-1)){
  98. counter++;
  99. }
  100. }
  101. }
  102. }
  103. //counts bottom row
  104. if(y+1 <gameWorld.length){
  105. for(int i =x-1; i<= x+1;i++){
  106. if(i>=0 && i< gameWorld[y+1].length()){
  107. if(isAlive(i,y+1)){
  108. counter++;
  109. }
  110. }
  111. }
  112. }
  113. //counts sides
  114. if(x-1>=0){
  115. if(isAlive(x-1,y)){
  116. counter++;
  117. }
  118. }
  119. if( x+1< gameWorld[y].length()){
  120. if(isAlive(x+1,y)){
  121. counter++;
  122. }
  123. }
  124. return counter;
  125. }
  126. public void showBoard() {
  127. for (int i = 0; i < gameWorld.length; i++)
  128. System.out.println(Arrays.toString(new String[]{gameWorld[i]}));
  129. }
  130. }