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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. }
  24. printTable(board);
  25. }
  26. private void printTable(String[] board) {
  27. for(int i = 0; i < board.length; i++) System.out.println(Arrays.toString(new String[]{board[i]}));
  28. System.out.println("________");
  29. }
  30. @Override
  31. public void nukeAll() { //works
  32. // TODO Auto-generated method stub
  33. for (int y = 0; y < board.length; y++){
  34. for (int x = 0; x < board[y].length(); x++) {
  35. setDead(x, y);
  36. }
  37. }
  38. }
  39. @Override
  40. public void setAlive(int x, int y) {
  41. // TODO Auto-generated method stub
  42. board[y] = board[y].substring(0, x) + '*' + board[y].substring(x+1);
  43. }
  44. @Override
  45. public void setDead(int x, int y) {
  46. // TODO Auto-generated method stub
  47. board[y] = board[y].substring(0, x) + ' ' + board[y].substring(x+1);
  48. }
  49. @Override
  50. public boolean isAlive(int x, int y) {
  51. if(board[y].charAt(x) == '*'){
  52. return true;
  53. }
  54. else{
  55. return false;
  56. }
  57. }
  58. @Override
  59. public ILife nextGeneration() {
  60. int alive;
  61. String[] nextBoard = new String[]{ " ",
  62. " ",
  63. " ",
  64. " ",
  65. " " };
  66. for (int y = 0; y < board.length; y++){
  67. for (int x = 0; x < board[y].length(); x++){
  68. alive=aliveNeighbours(x,y);
  69. //A new Cell is born
  70. if(!isAlive(x,y) && alive == 3) {
  71. nextBoard[y] = nextBoard[y].substring(0, x) + '*' + nextBoard[y].substring(x+1);
  72. }
  73. //Cell is lonely and dies or cell is overpopulated and dies
  74. else if((isAlive(x,y) && (alive < 2)) || (alive > 3)){
  75. nextBoard[y] = nextBoard[y].substring(0, x) + ' ' + nextBoard[y].substring(x+1);
  76. }
  77. //Cell with two or three living neighbours lives on
  78. else if(isAlive(x,y) && (alive == 2 || alive == 3)){
  79. nextBoard[y] = nextBoard[y].substring(0, x) + board[y].charAt(x) + nextBoard[y].substring(x+1);
  80. }
  81. }
  82. }
  83. board = nextBoard;
  84. System.out.println("next Generation");
  85. printTable(board);
  86. return null;
  87. }
  88. private int aliveNeighbours(int x, int y) {
  89. int neighbours = 0;
  90. if(x>0 && y>0){
  91. if(board[y-1].charAt(x-1) == '*'){
  92. neighbours++;
  93. }
  94. }
  95. if(x>0){
  96. if(board[y].charAt(x-1) == '*'){
  97. neighbours++;
  98. }
  99. if(y<4 ){
  100. if(board[y+1].charAt(x-1) == '*'){
  101. neighbours++;
  102. }
  103. }
  104. }
  105. if(y>0){
  106. if(board[y-1].charAt(x) == '*'){
  107. neighbours++;
  108. }
  109. if(x<4){
  110. if(board[y-1].charAt(x+1) == '*'){
  111. neighbours++;
  112. }
  113. }
  114. }
  115. if(x<4){
  116. if(board[y].charAt(x+1) == '*'){
  117. neighbours++;
  118. }
  119. }
  120. if(y<4){
  121. if(board[y+1].charAt(x) == '*'){
  122. neighbours++;
  123. }
  124. }
  125. if(x<4 && y<4){
  126. if(board[y+1].charAt(x+1) == '*'){
  127. neighbours++;
  128. }
  129. }
  130. return neighbours;
  131. }
  132. }