Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.48 KB | None | 0 0
  1. package life;
  2.  
  3. public class Life {
  4. private LifeBoard board;
  5.  
  6. /** Skapar ett Life-spel med spelplanen board */
  7. public Life(LifeBoard board) {
  8. this.board = board;
  9. }
  10.  
  11. /** Skapar en ny generation */
  12. public void newGeneration() {
  13.  
  14. /**int neighbours;
  15. LifeBoard newValues = new LifeBoard(board.getRows(), board.getCols());
  16.  
  17. for (int row = 0; row < board.getRows(); row++) {
  18. for (int col = 0; col < board.getCols(); col++) {
  19.  
  20. neighbours = numberOfNeighbours(row, col);
  21. if (board.get(row, col)) { // Om rutan är fylld.
  22. if (neighbours == 2 || neighbours == 3) {
  23. newValues.put(row, col, true);
  24. }
  25.  
  26. } else { // Om rutan är tom.
  27. if (neighbours == 3) {
  28. newValues.put(row, col, true);
  29. }
  30. }
  31. }
  32. }
  33. board = newValues;*/
  34. board.increaseGeneration();
  35.  
  36. }
  37.  
  38. /**
  39. * Ändrar innehåller i rutan med index row, col från individ till tom eller
  40. * tvärtom.
  41. */
  42. public void flip(int row, int col) {
  43.  
  44. if (board.get(row, col)) {
  45. board.put(row, col, false);
  46. } else {
  47. board.put(row, col, true);
  48. }
  49. }
  50.  
  51. /** Returnerar antal grannar till rutan med index row, col. */
  52. public int numberOfNeighbours(int row, int col) {
  53. int neighbours = 0;
  54. if (board.get(row, col)) {
  55. neighbours--;
  56. }
  57.  
  58. for (int r = 0 ; r <= 2 ; r++) {
  59. for (int c = 0 ; c <= 2 ; c++) {
  60. if (board.get(row + r - 1, col + c - 1)) {
  61. neighbours++;
  62. }
  63. }
  64. }
  65. return neighbours;
  66.  
  67. }
  68.  
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement