Java

int sum_adjacent_values(int r, int c) { int sum = - position_array[r][c]; for (int i = -1; i <= 1; ++i) { for (int j = -1; j <= 1; ++j) { int row = r+i; int col = c+j; if ( (row >= 0 && row < Rows) && ( col >= 0 && col < Cols)) { sum += position_array[row][col]; } } } return sum; } void compute_state_array() { for (int row = 0; row < Rows; ++row) { for (int col = 0; col < Cols; ++col) { if ( position_array[row][col] == 3) { state_array[row][col] = false; } else { int sum_of_adjacent_values = sum_adjacent_values(row, col); if ( sum_of_adjacent_values == 15) state_array[row][col] = false; else state_array[row][col] = true; } } } } public final boolean [][] get_state_array(){ return state_array; } public Bg(int [][] a) { Rows = a.length; Cols = a[0].length; position_array = new int[Rows][Cols]; state_array = new boolean[Rows][Cols]; for (int row = 0; row < Rows; ++row) for (int col = 0; col < Cols; ++col) position_array[row][col] = a[row][col]; compute_state_array(); } public static void show( int [][] a) { for (int r = 0; r < a.length; ++r) { for (int c = 0; c < a[0].length; ++c) System.out.print(String.format("%6d", a[r][c])); System.out.println(); } } public static void show( boolean [][] a) { for (int r = 0; r < a.length; ++r) { for (int c = 0; c < a[0].length; ++c) System.out.print(String.format("%6b", a[r][c])); System.out.println(); } } public static void main(String[] args) { int [][] position_array = { {1,1,2,3,3}, {2,1,1,2,3}, {3,2,2,1,2}, {3,3,3,3,3} }; Bg bg = new Bg( position_array); boolean [][] state_array = bg.get_state_array(); show( position_array); show( state_array); } }