Advertisement
Guest User

Untitled

a guest
Dec 11th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3.  
  4.  
  5. public class Life {
  6. private LifeBoard board;
  7. private int neighbours;
  8. private int rows, cols;
  9.  
  10.  
  11. // Skapar ett Life-spel med spelplanen board
  12. public Life(LifeBoard board) {
  13. this.board = board;
  14. }
  15.  
  16. // Skapar en ny generation
  17. public void newGeneration() {
  18.  
  19. rows = board.getRows();
  20. cols = board.getCols();
  21. LifeBoard tempBoard = new LifeBoard(rows, cols);
  22.  
  23.  
  24. for (int i=0; i < rows; i++ ) {
  25. for (int k=0; k< cols; k++) {
  26.  
  27. getNeighbours(board, i, k);
  28.  
  29.  
  30. if (neighbours < 2 || neighbours > 3) {
  31. tempBoard.put(i, k, false);
  32. }
  33. if ( neighbours == 2 && (board.get(i, k) == true)){
  34. tempBoard.put(i, k, true);
  35.  
  36. }
  37. if (neighbours == 3) {
  38. tempBoard.put(i, k, true);
  39. }
  40.  
  41. }
  42.  
  43. }
  44.  
  45. for (int y=0; y < rows; y++ ) {
  46. for (int x=0; x< cols; x++) {
  47. board.put(y, x, tempBoard.get(y, x));
  48. }
  49.  
  50. }
  51. board.increaseGeneration();
  52. }
  53.  
  54. // Ändrar innehållet i rutan med index row, col från individ till tom eller
  55. // tvärtom
  56. public void flip(int row, int col) {
  57.  
  58. if (board.get(row, col) == true) {
  59. board.put(row, col, false);
  60. } else
  61. board.put(row, col, true);
  62.  
  63. }
  64.  
  65. private int getNeighbours(LifeBoard board, int row, int col) {
  66.  
  67. neighbours = 0;
  68.  
  69. //for (int i=-1;i <=1; i++) {
  70. //for(int k = -1; k<=1; k++) {
  71. //if ((board.get(row + i, col + k)) && (i !=0 && k!=0)) {
  72. //neighbours++;
  73. //}
  74. //}
  75. //}
  76.  
  77. if (board.get(row -1, col)) {//N
  78. neighbours++;
  79. }
  80. if (board.get(row -1, col+1)) {//NE
  81. neighbours++;
  82. }
  83. if (board.get(row , col+1)) {//E
  84. neighbours++;
  85. }
  86. if (board.get(row +1, col+1)) {//SE
  87. neighbours++;
  88. }
  89. if (board.get(row +1, col)) {//S
  90. neighbours++;
  91. }
  92. if (board.get(row +1, col-1)) {//SW
  93. neighbours++;
  94. }
  95. if (board.get(row , col-1)) {//W
  96. neighbours++;
  97. }
  98. if (board.get(row -1, col-1)) {//NW
  99. neighbours++;
  100. }
  101.  
  102.  
  103. return neighbours;
  104.  
  105.  
  106. }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement