Thursday, December 3, 2015

Make all diagonal to 0 when encountered a 0

Given a 2D array, write a method MakeDiagonalZeroWhenEncounteredZero() to convert all diagonal to zero when encountered a zero.

import java.util.ArrayList;

public class ChessBoard {
    public int[][] Board;
    public ChessBoard(int fill) { //Pass 1 for fill
        this.Board = new int[8][8];
        for(int i=0; i<8; i++)
            for(int j=0; j<8; j++)
                this.Board[i][j]=fill;
    }
    public void FillZero(int x, int y)    {
        if(x<8 && x>-1 && y<8 && y>-1)
            Board[x][y]=0;
        else
            System.out.println("Error: Possition Not Valid");
    }
    public void MakeDiagonalZeroWhenEncounteredZero()    {
        ArrayList<Coordinates> zeros = getZeroCordinates();
        for(Coordinates c: zeros)    {
            int x=c.getX();
            int y=c.getY();
            while(x<7 && y<7)    {
                x++; y++;
                Board[x][y]=0;
            }
            x=c.getX();    y=c.getY();
            while(x>0 && y>0)    {
                x--; y--;
                Board[x][y]=0;
            }
            x=c.getX();    y=c.getY();
            while(x<7 && y>0)    {
                x++; y--;
                Board[x][y]=0;
            }
            x=c.getX();    y=c.getY();
            while(x>0 && y<7)    {
                x--; y++;
                Board[x][y]=0;
            }
        }
    }
    private ArrayList<Coordinates> getZeroCordinates()    {
        ArrayList<Coordinates> zeros = new ArrayList<Coordinates>();
        for(int i=0; i<8; i++)
            for(int j=0; j<8; j++)    {
                if(Board[i][j]==0)
                    zeros.add(new Coordinates(i,j));
            }
        return zeros;
    }
    public void PrintBoard()    {
        for(int i=0; i<8; i++)    {
            StringBuffer sb = new StringBuffer();
            for(int j=0; j<8; j++)    {
                sb.append(Board[i][j]).append(", ");
            }
            System.out.println(sb.toString());
        }
    }
}
class Coordinates    {
    private int x, y;
    public Coordinates(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getX() {
        return x;
    }
    public int getY() {
        return y;
    }
}





Main Method:
        ChessBoard cb = new ChessBoard(1);
        cb.FillZero(4, 5);
        cb.FillZero(7, 2);
        System.out.println("Input: ");
        cb.PrintBoard();
        cb.MakeDiagonalZeroWhenEncounteredZero();
        System.out.println("Output: ");
        cb.PrintBoard();

   
Input:
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 0, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1,
Output:
1, 0, 1, 1, 1, 1, 1, 1,
1, 1, 0, 1, 1, 1, 1, 1,
1, 1, 1, 0, 1, 1, 1, 0,
1, 1, 1, 1, 0, 1, 0, 1,
1, 1, 1, 1, 1, 0, 1, 1,
0, 1, 1, 1, 0, 1, 0, 1,
1, 0, 1, 0, 1, 1, 1, 0,
1, 1, 0, 1, 1, 1, 1, 1,

No comments:

Post a Comment

UA-39217154-2