0

I'm working on a version of Conway's Game of Life and I've created the method for creating a new Generation, where I copy a matrix and look for nearby neighbors. I would however like to split this method in to two seperate methods. A copy method of the matrix and then the original newGeneration would call on copy() for help. This is how my newGeneration method looks like right now.

public void newGeneration() {
     temp = new boolean[board.getRows()][board.getCols()];  
      for (int i = 0; i < board.getRows(); i++) {              
       for (int j = 0; j < board.getCols(); j++) {  
        if (board.get(i, j)==true) {       
         if (getNeighbours(board, i, j) > 3) {  
          temp[i][j] = false;
         } else if (getNeighbours(board, i, j) < 2) {
          temp[i][j] = false;
         } else{
          temp[i][j] = true;
         }
        } else if (board.get(i, j) == false) {
         if (getNeighbours(board, i, j) == 3) {
          temp[i][j] = true;
         }
        }
       }
      }
      for (int i = 0; i < board.getRows(); i++) {
       for (int j = 0; j < board.getCols(); j++) {
        board.put(i, j, temp[i][j]);
       }
      }

I want to split this in to two methods, newGeneration() and copy(). I've been working on it for a while now but I seem to screw up with the variables i and j because theyre locally set in the loops. Any help with splitting this method up in to two will be appreciated, thanks!

EDIT:

From some sage advice recommending me of this post, I made something like this

public void newGeneration() {
    boolean[][] tempCells = new boolean [board.getRows()][board.getCols()]; 

    for (int row = 0; row < board.getRows(); row++) {              
        for (int col = 0; col < board.getCols(); col++) { 
            int n = getNeighbours(board,row,col);

            if (n > 3  ||  n < 2)
                tempCells[row][col] = false;
            else if (n == 3)
                tempCells[row][col] = true;
            else
                tempCells[row][col] = temp[board.getRows()][board.getCols()];
        }
    }
}

But it doesn't seem to work properly.

Community
  • 1
  • 1
Michael
  • 644
  • 5
  • 14
  • 33
  • 2
    If you are using an IDE like eclipse, you can easily select the code segment and press Ctrl+Alt+M to extract method. IDE will take care of variables. – Vishal Nov 26 '14 at 15:05
  • What exactly do you want your two methods to do? – Eypros Nov 26 '14 at 15:06
  • It's not clear to me what you're asking. – Dave Newton Nov 26 '14 at 15:07
  • I want to create a method that copies my matrix (that I gather from the board class) and puts it in the a new matrix 'temp'. That would be done within the 'public void copy()' method. And then inside my 'newGeneration' class I would start by calling copy(); and then, I would look for as you see, "> 3", "< 2>, "==3" and so forth. – Michael Nov 26 '14 at 15:09
  • So create a method called `copy` that takes the current board and returns a new board--I don't see the issue. You *could* use instance properties directly, but I probably wouldn't. – Dave Newton Nov 26 '14 at 15:11
  • Yes, exactly. It's what I want to create but I haven't managed it yet. Having trouble copying it and returning a new board. – Michael Nov 26 '14 at 15:13
  • check http://stackoverflow.com/questions/4058292/please-help-with-my-basic-java-implementation-of-conways-game-of-life it might help you in better implementation. – cauchy Nov 26 '14 at 15:21
  • Thanks cauchy, Im very fond of that way of implementing it. I made an edit to my post with something similar, but I can't make it work properly. – Michael Nov 26 '14 at 15:33

1 Answers1

0

A simple way to get a copy of an array is to clone it. Since clone gives just a shallow copy, it requires explicit cloning for each additional dimension for multidimensional arrays:

public static boolean[][] copy(boolean[][] source) {
    boolean[][] copy = source.clone();
    for (int i=0; i<copy.length; ++i) {
        copy[i] = copy[i].clone();
    }
    return copy;
}
Durandal
  • 19,919
  • 4
  • 36
  • 70