public class Pawn extends Piece{
    private boolean once, end ;

    public Pawn(Cell cell, PieceColor pieceColor) {
        super(cell, pieceColor) ;
        this.once = true ;
        this.end = false ;
    }

    public void setOnce(boolean once) {
        this.once = once;
    }

    public void setEnd(boolean end) {
        this.end = end;
    }

    @Override
    public boolean isValidMove(Cell c, Cell board[][], Piece pw[], Piece pb[]) {
        if (super.isDeleted()) return false ;
        if (super.getPieceColor()==PieceColor.BLACK && super.getCell().getRow()==0) end = true ;
        if (super.getPieceColor()==PieceColor.WHITE && super.getCell().getRow()==7) end = true ;

        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()
                        && c.getRow()-this.getCell().getRow()==-1
                        && Math.abs(this.getCell().getCol()-c.getCol())==1) sw=1 ;
            }
        }
        else {
            for (Piece p:pb) {
                if (!p.isDeleted() && p.getCell().getRow()==c.getRow() && p.getCell().getCol()==c.getCol()
                        && c.getRow()-this.getCell().getRow()==1
                        && Math.abs(this.getCell().getCol()-c.getCol())==1) sw=1 ;
            }
        }

        if (once) {
            //once = false ;
            //System.out.println("row: " + this.getCell().getRow()) ;
            //System.out.println("col: " + this.getCell().getCol()) ;
            if ((this.getCell().getCol() == c.getCol()) &&
                    (Math.abs(this.getCell().getRow()-c.getRow())==1 || Math.abs(this.getCell().getRow()-c.getRow())==2)
                    && c.isEmpty()){
                if(getCell().getRow() < c.getRow() && getPieceColor()==PieceColor.WHITE) {
                    return true;
                }else if(getCell().getRow() > c.getRow() && getPieceColor()==PieceColor.BLACK){
                    return true;
                }else {
                    return false;
                }
            }
            else return false ;
        }
        else {
            if (end) {
                if(
                        ((this.getCell().getCol() == c.getCol()) &&
                                (Math.abs(this.getCell().getRow()-c.getRow())==1)) ||
                                (Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
                                        Math.abs(this.getCell().getCol()-c.getCol())==1 && sw==1)
                        ) return true ;
                else return false ;
            }
            else {
                if(
                        (this.getCell().getCol() == c.getCol() && c.isEmpty() &&
                                ((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()==PieceColor.BLACK) ||
                                        (this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()==PieceColor.WHITE)) )
                                ||
                                (Math.abs(this.getCell().getCol()-c.getCol())==Math.abs(this.getCell().getRow()-c.getRow()) &&
                                        ((this.getCell().getRow()-c.getRow()==1 && super.getPieceColor()==PieceColor.BLACK && sw==1) ||
                                                (this.getCell().getRow()-c.getRow()==-1 && super.getPieceColor()==PieceColor.WHITE && sw==1)) )
                        ) return true ;
                else return false ;
            }
        }
    }
}