I got the program to run, but the output is not the way it should be and I don't know where I went wrong.

The output is supposed to look like this:
Sample Input
6 8
7
3 8
3 8
Sample Output






              • -
    • @ @ @ - - - -
    • @ @ @ @ - @ -


        • @ @ - @ -
    • @ - @ - @ @ -





                • -





                • -
    • @ @ @ @ - - -
    • @ @ @ @ @ - -
    • @ @ @ @ @ @ -

      • @ @ @ @ @ -





                • -





                • -
    • @ @ @ @ @ - -
    • @ - - @ @ @ -
    • @ @ - - @ @ -
    • @ @ @ @ @ @ -





                • -





                • -
    • @ @ @ @ @ @ -
    • @ @ @ @ @ @ -
    • @ @ @ @ @ @ -
    • @ @ @ @ @ @ -





                • -





                • -
    • @ @ @ @ @ @ -
    • @ - - - - @ -
    • @ - - - - @ -
    • @ @ @ @ @ @ -





                • -





                • -
    • @ @ @ @ @ @ -
    • @ @ @ @ @ @ -
    • @ @ @ @ @ @ -
    • @ @ @ @ @ @ -







    import java.util.Random;
    import java.util.Scanner;

    public static void main(String[] args) {
    
        Scanner console = new Scanner(System.in); 
        System.out.println(" ");
        int rows = console.nextInt(); 
    
        System.out.println(" ");
        int columns = console.nextInt();
    
        System.out.println(" ");
        long seed = console.nextLong(); 
    
        int birthLow = console.nextInt(); 
    
        int birthHigh = console.nextInt(); 
    
        int liveLow = console.nextInt();
    
        int liveHigh = console.nextInt(); 
    
        boolean myMatrix[][] = new boolean[rows][columns]; 
        array(myMatrix, rows, columns, seed); 
        printArray(myMatrix, rows, columns); 
    
        System.out.println();
    
        for(int i=0; i<4; i++) {
            alterMatrix(myMatrix, rows, columns, birthLow, birthHigh, liveLow, liveHigh);
            printArray(myMatrix, rows, columns);
            System.out.println();
        }
    }
    

    //------------------------------------------------------------------------------------------
    public static void array(boolean[][] Matrix, int rows, int columns,
    long seed) {

        Random generator = new Random(seed); 
        for (int i = 1; i < rows - 1; i++) {
            for (int j = 1; j < columns - 1; j++) {
    
                boolean x = generator.nextBoolean();
                if (!x) {
                    Matrix[i][j] = false;
                } else {
                    Matrix[i][j] = true;
                }
            }
        }
    }
    

    //-----------------------------------------------------------------------------------------------
    // Prints the array
    public static void printArray(boolean[][] Matrix, int rows, int columns) {

        for (int k = 0; k < rows; k++) {
            for (int m = 0; m < columns; m++) {
                if (!Matrix[k][m]) {
                    System.out.print("- ");
                } else {
                    System.out.print("@ ");
                }
            }
            System.out.println();
        }
    }
    

    //---------------------------------------------------------------------------------------------------------------------
    public static void alterMatrix(boolean[][] myMatrix, int rows,
    int columns, int birthLow, int birthHigh, int liveLow, int liveHigh) {

        boolean[][] myNewMatrix = myMatrix.clone();
        for (int row = 0; row < myMatrix.length; row++) {
            myNewMatrix[row] = myNewMatrix[row].clone();
    
            for (int i = 1; i < rows - 1; i++) {
                for (int j = 1; j < columns - 1; j++) {
    
    
                    if (!myMatrix[i][j]) {
                        int counter = 0;
                        if (myMatrix[i - 1][j - 1] == true) {
                            counter = counter + 1;
                        } if (myMatrix[i - 1][j] == true) {
                            counter = counter + 1;
                        } if (myMatrix[i - 1][j + 1] == true) {
                            counter = counter + 1;
                        } if (myMatrix[i][j - 1] == true) {
                            counter = counter + 1;
                        } if (myMatrix[i][j + 1] == true) {
                            counter = counter + 1;
                        } if (myMatrix[i + 1][j + 1] == true) {
                            counter = counter + 1;
                        } if (myMatrix[i + 1][j - 1] == true) {
                            counter = counter + 1;
                        } if (myMatrix[i + 1][j] == true) {
                            counter = counter + 1;
                        } else {
    
                        } if (counter >= birthLow && counter <= birthHigh) {
                            myNewMatrix[i][j] = true;
                        }
    
                    } else {
    
                        int counter2 = 0;
    
                        if (myMatrix[i - 1][j - 1] == true) {
                            counter2 = counter2 + 1;
                        } if (myMatrix[i - 1][j] == true) {
                            counter2 = counter2 + 1;
                        } if (myMatrix[i - 1][j + 1] == true) {
                            counter2 = counter2 + 1;
                        } if (myMatrix[i][j - 1] == true) {
                            counter2 = counter2 + 1;
                        } if (myMatrix[i][j + 1] == true) {
                            counter2 = counter2 + 1;
                        } if (myMatrix[i + 1][j + 1] == true) {
                            counter2 = counter2 + 1;
                        } if (myMatrix[i + 1][j - 1] == true) {
                            counter2 = counter2 + 1;
                        } if (myMatrix[i + 1][j] == true) {
                            counter2 = counter2 + 1;
                        }
    
                        if (counter2 >= liveHigh || counter2 <= liveLow) {
                            myNewMatrix[i][j] = false;
                        } else {
    
                        }
                    }
                }
            }
        }
    }
    

    }

Edited 10 Months Ago by Zac_3

<edit> - removed incorrect comment </edit>

In alterMatrix you create and populate myNewMatrix but you never do anything with it.

Edited 10 Months Ago by JamesCherrill

Can't you refactor this bit?

if (myMatrix[i - 1][j - 1] == true) { counter2 = counter2 + 1;
} if (myMatrix[i - 1][j] == true) {counter2 = counter2 + 1;
} if (myMatrix[i - 1][j + 1] == true) { counter2 = counter2 + 1;
} if (myMatrix[i][j - 1] == true) { counter2 = counter2 + 1;
 } if (myMatrix[i][j + 1] == true) {counter2 = counter2 + 1;
 } if (myMatrix[i + 1][j + 1] == true) {counter2 = counter2 + 1;
 } if (myMatrix[i + 1][j - 1] == true) {counter2 = counter2 + 1;
 } if (myMatrix[i + 1][j] == true) {counter2 = counter2 + 1;
 }

First refactoring: remove all the == true parts. checking if 'true == true' is almost as efficient as just checking if true it does, however, increase the chance for typo's.
if ( myMatrix[i][j] = true) for instance, would give wrong results if the original value for myMatrix[i][j] was false.

So, this would lead to:

if (myMatrix[i - 1][j - 1] ) { counter2 = counter2 + 1;
} if (myMatrix[i - 1][j] ) {counter2 = counter2 + 1;
} if (myMatrix[i - 1][j + 1] ) { counter2 = counter2 + 1;
} if (myMatrix[i][j - 1] ) { counter2 = counter2 + 1;
 } if (myMatrix[i][j + 1] ) {counter2 = counter2 + 1;
 } if (myMatrix[i + 1][j + 1] ) {counter2 = counter2 + 1;
 } if (myMatrix[i + 1][j - 1] ) {counter2 = counter2 + 1;
 } if (myMatrix[i + 1][j]) {counter2 = counter2 + 1;
 }

next recommendation: you are repeating a lot of code, why not just using an iteration?

for ( int a = i - 1; a <= i+1; i++){
  if ( myMatrix[a][j-1] || myMatrix[a][j+1] || 
      (a == i && myMatrix[a][j])){ 
          counter2 += 1; 
  }
}

In the end, this will be a lot easier to maintain comparing to all the if blocks.

Edited 10 Months Ago by stultuske

Using an iteration:
Life is very compute intensive when running interesting-sized simulations, so the alterMatrix logic can be time-critical.
When I did this some years ago (!) I tried it both ways

   private int getNeighbourCount(int x, int y) {
      int count = 0;
      if ((grid[x - 1][y - 1])) count++;
      if ((grid[x - 1][y])) count++;
      if ((grid[x - 1][y + 1])) count++;
      if ((grid[x][y - 1])) count++;
      if ((grid[x][y + 1])) count++;
      if ((grid[x + 1][y - 1])) count++;
      if ((grid[x + 1][y])) count++;
      if ((grid[x + 1][y + 1])) count++;
      return count;
   }

   private int getNeighbourCount2(int x, int y) {
      int count = 0;
      for (int xx = x - 1; xx <= x + 1; xx++) {
         for (int yy = y - 1; yy <= y + 1; yy++) {
            if (grid[xx][yy]) count++;
         }
      }
      if (grid[x][y]) count--; // don't count the target cell
      return count;
   }

Against my expectations the first version was 2 1/2 times faster than the second.

Edited 10 Months Ago by JamesCherrill

This article has been dead for over six months. Start a new discussion instead.