Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package life;
- public class Life {
- private LifeBoard board;
- /** Skapar ett Life-spel med spelplanen board */
- public Life(LifeBoard board) {
- this.board = board;
- }
- /** Skapar en ny generation */
- public void newGeneration() {
- /**int neighbours;
- LifeBoard newValues = new LifeBoard(board.getRows(), board.getCols());
- for (int row = 0; row < board.getRows(); row++) {
- for (int col = 0; col < board.getCols(); col++) {
- neighbours = numberOfNeighbours(row, col);
- if (board.get(row, col)) { // Om rutan är fylld.
- if (neighbours == 2 || neighbours == 3) {
- newValues.put(row, col, true);
- }
- } else { // Om rutan är tom.
- if (neighbours == 3) {
- newValues.put(row, col, true);
- }
- }
- }
- }
- board = newValues;*/
- board.increaseGeneration();
- }
- /**
- * Ändrar innehåller i rutan med index row, col från individ till tom eller
- * tvärtom.
- */
- public void flip(int row, int col) {
- if (board.get(row, col)) {
- board.put(row, col, false);
- } else {
- board.put(row, col, true);
- }
- }
- /** Returnerar antal grannar till rutan med index row, col. */
- public int numberOfNeighbours(int row, int col) {
- int neighbours = 0;
- if (board.get(row, col)) {
- neighbours--;
- }
- for (int r = 0 ; r <= 2 ; r++) {
- for (int c = 0 ; c <= 2 ; c++) {
- if (board.get(row + r - 1, col + c - 1)) {
- neighbours++;
- }
- }
- }
- return neighbours;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement