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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. public class Life implements ILife {
  2. private final int x_value = 10;
  3. private final int y_value = 10;
  4. public enum CellValue {Dead, Alive}
  5. private final CellValue[][] cells = new CellValue[x_value][y_value];
  6. public static void main(String[] args) {
  7. Life l = new Life(new String[] { " ",
  8. " ",
  9. " *** ",
  10. " ",
  11. " " }
  12. );
  13. l.print();
  14. l = (Life) l.nextGeneration();
  15. l.print();
  16. }
  17. public Life() {
  18. nukeAll();
  19. }
  20. public Life(String[] setup) {
  21. this();
  22. for (int y = 0; y < y_value; y ++)
  23. for (int x = 0; x < x_value; x ++)
  24. if (setup[y].charAt(x) != ' ')
  25. setAlive(x, y);
  26. }
  27. @Override
  28. public void nukeAll() {
  29. // TODO Auto-generated method stub
  30. for( int y = 0; y < y_value; y ++){
  31. for( int x = 0; x < x_value; x ++){
  32. setDead(x,y);
  33. }
  34. }
  35. }
  36. @Override
  37. public void setAlive(int x, int y) {
  38. // TODO Auto-generated method stub
  39. cells[x][y] = CellValue.Alive;
  40. }
  41. @Override
  42. public void setDead(int x, int y) {
  43. // TODO Auto-generated method stub
  44. cells[x][y] = CellValue.Dead;
  45. }
  46. @Override
  47. public boolean isAlive(int x, int y) {
  48. // TODO Auto-generated method stub
  49. return cells[x][y] == CellValue.Alive;
  50. }
  51. @Override
  52. public boolean isDead(int x, int y) {
  53. return cells[x][y] == CellValue.Dead;
  54. }
  55. @Override
  56. public ILife nextGeneration() {
  57. // TODO Auto-generated method stub
  58. Life newGeneration = new Life();
  59. for (int y = 0; y < y_value; y ++) {
  60. for(int x = 0; x < x_value; x ++) {
  61. int neighborsAlive = calculateNeighborsAlive(x,y);
  62. if(neighborsAlive == 3) {
  63. newGeneration.setAlive(x,y);
  64. }
  65. if(neighborsAlive < 2){
  66. newGeneration.setDead(x,y);
  67. }
  68. if(isAlive(x,y) && (neighborsAlive == 2 || neighborsAlive == 3)){
  69. newGeneration.setAlive(x,y);
  70. }
  71. if(isAlive(x,y) && neighborsAlive > 3) {
  72. newGeneration.setDead(x,y);
  73. }
  74. }
  75. }
  76. return newGeneration;
  77. }
  78. public void print() {
  79. for(int x= 0; x < x_value; x ++) {
  80. for(int y = 0; y < y_value; y ++) {
  81. if(cells[x][y] == CellValue.Alive) {
  82. System.out.print("*");
  83. }else{
  84. System.out.print(" ");
  85. }
  86. }
  87. System.out.print("\n");
  88. }
  89. }
  90. public int calculateNeighborsAlive( int x, int y) {
  91. int min_X = x - 1;
  92. int max_X = x + 1;
  93. int min_Y = y - 1;
  94. int max_Y = y + 1;
  95. if( min_X < 0) min_X=0;
  96. if( max_X >= x_value) max_X=x_value - 1;
  97. if( min_Y < 0) min_Y=0;
  98. if( max_Y >= x_value) max_Y=y_value - 1;
  99. int sum = 0;
  100. for(int dx = min_X; dx <= max_X; dx++){
  101. for(int dy = min_Y; dy <= max_Y; dy++){
  102. if(dx != x || dy != y) {
  103. if(cells[dx][dy] == CellValue.Alive ){
  104. sum++;
  105. }
  106. }
  107. }
  108. }
  109. return sum;
  110. }
  111. }