• kimia's avatar
    GUI · 7bc51122
    kimia authored
    7bc51122
Knight.java 1.16 KB
/**
 * knight class show the knight's moves that is like L
 *@author kimiadorani
 *@version 1.0
 *@since 2019-5-7
 */

public class Knight extends Piece {


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

    @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 ;
            }
        }
        return ( (
                (Math.abs(this.getCell().getCol()-c.getCol()) + Math.abs(this.getCell().getRow() - c.getRow()) == 3) &&
                        (Math.abs(this.getCell().getCol()-c.getCol()) != 0 && Math.abs(this.getCell().getRow() - c.getRow()) != 0)
        )
                && (c.isEmpty() || (!c.isEmpty() && sw==1))
        ) ;
    }
}