G6 - Game of life

May 22, 2017

According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellular automaton devised by the British mathematician John Horton Conway in 1970."
Given a board with m by n cells, each cell has an initial state live (1) or dead (0). Each cell interacts with its eight neighbors (horizontal, vertical, diagonal) using the following four rules (taken from the above Wikipedia article):
Any live cell with fewer than two live neighbors dies, as if caused by under-population.
Any live cell with two or three live neighbors lives on to the next generation.
Any live cell with more than three live neighbors dies, as if by over-population..
Any dead cell with exactly three live neighbors becomes a live cell, as if by reproduction.
Write a function to compute the next state (after one update) of the board given its current state.

Solution :
code the next state at the same position by attaching the next state in the same number infront.

                
public class Solution {
    public void gameOfLife(int[][] board) {
        if(board == null || board.length == 0)
            return;
        int row = board.length;
        int col = board[0].length;

        for(int i=0; i<row; i++){
            for(int j=0; j<col; j++){
                int ln = getliveneighbours(board, i, j, row, col);
                if(board[i][j] == 1){
                    if(ln == 2 || ln == 3){
                        board[i][j] = 11;
                    }
                }else{
                    if(ln == 3){
                        board[i][j] = 10;
                    }
                }
            }
        }
        for(int i=0; i<row; i++){
            for(int j=0; j<col; j++){
                board[i][j] = board[i][j]/10;
            }
        }
    }

    public int getliveneighbours(int[][] board, int i, int j, int row, int col){
        int ln = 0;
        if(i-1 >= 0 && i-1 < row && j >= 0 && j < col)
            ln += board[i-1][j]%10;
        if(i+1 >= 0 && i+1 < row && j >= 0 && j < col)
            ln += board[i+1][j]%10;
        if(i >= 0 && i < row && j-1 >= 0 && j-1 < col)
            ln += board[i][j-1]%10;
        if(i >= 0 && i < row && j+1 >= 0 && j+1 < col)
            ln += board[i][j+1]%10;
        if(i-1 >= 0 && i-1 < row && j-1 >= 0 && j-1 < col)
            ln += board[i-1][j-1]%10;
        if(i+1 >= 0 && i+1 < row && j+1 >= 0 && j+1 < col)
            ln += board[i+1][j+1]%10;
        if(i-1 >= 0 && i-1 < row && j+1 >= 0 && j+1 < col)
            ln += board[i-1][j+1]%10;
        if(i+1 >= 0 && i+1 < row && j-1 >= 0 && j-1 < col)
            ln += board[i+1][j-1]%10;
        return ln;
    }
}