/**
 * Bishop class show the Bishop's moves that moves diagonally, and can move
 * as far as a player wants it to unless another piece blocks it.
 *@author kimiadorani
 *@version 1.0
 *@since 2019-5-7
 */

public class Bishop extends Piece {

    public Bishop(Cell cell, PieceColor pieceColor)
    {
        super(cell, pieceColor) ;
    }

    /**
     * this method check that if the move for Bishop is possible or not !
     * @param c
     * @param board
     * @param pw
     * @param pb
     * @return
     */

    @Override
    public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
        if (super.isDeleted()) return false ;
        int sw=0 ;
        if (super.getPieceColor() == PieceColor.BLACK) {
            for (Piece p:pw) {
                if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
            }
        }
        else {
            for (Piece p:pb) {
                if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()) sw=1 ;
            }
        }
        if ( (
                (Math.abs(this.getCell().getCol()-c.getCol()) == Math.abs(this.getCell().getRow()-c.getRow()))
        ) && (c.isEmpty() || (!c.isEmpty() && sw==1))  ) {
            if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() < c.getRow()) {
                for (int i=-1 ; i>-8 ; --i) {
                    if (this.getCell().getRow()-i == c.getRow() && sw==1) break ;
                    if (!board[this.getCell().getRow()-i][this.getCell().getCol()-i].isEmpty()) return false;
                    if (this.getCell().getRow()-i == c.getRow()) break ;
                }
            }
            if (this.getCell().getCol() < c.getCol() && this.getCell().getRow() > c.getRow()) {
                for (int i=-1 ; i>-8 ; --i) {
                    if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
                    if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) return false ;
                    if (this.getCell().getRow()+i == c.getRow()) break ;
                }
            }
            if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() < c.getRow()) {
                for (int i=1 ; i<8 ; ++i) {
                    if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
                    if (!board[this.getCell().getRow()+i][this.getCell().getCol()-i].isEmpty()) return false;
                    if (this.getCell().getCol()-i == c.getCol()) break ;
                }
            }
            if (this.getCell().getCol() > c.getCol() && this.getCell().getRow() > c.getRow()) {
                for (int i=-1 ; i>-8 ; --i) {
                    if (this.getCell().getRow()+i == c.getRow() && sw==1) break ;
                    if (!board[this.getCell().getRow()+i][this.getCell().getCol()+i].isEmpty()) return false;
                    if (this.getCell().getCol()+i == c.getCol()) break ;
                }
            }
            return true ;
        }
        else {
            return false ;
        }
    }
    private PieceColor checkCellPieceColor(Cell c, Cell board[][], Piece pw[], Piece pb[]){
        for (Piece p : pw){
            if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()){
                return p.getPieceColor();
            }

        }
        for (Piece p : pb){
            if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()){
                return p.getPieceColor();
            }
        }
        return null;
    }
}