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

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