canMove

a
 avatar
unknown
abap
3 years ago
3.0 kB
4
Indexable
 @Override
    public boolean canMove(Board board, int x, int y) {
        boolean flag = false;
        if (board.validate(x, y)) {
            if (this.getCoordinatesX() != x && this.getCoordinatesY() != y) {
                return false;
            }
            //Search on road to pos
            if (this.getCoordinatesX() != x) {
                int currentPosX = this.getCoordinatesX();
                if (currentPosX > x) {
                    for (int i = currentPosX - 1; i >= x; i--) {
                        Piece piece = board.getAt(i, y);
                        if (piece == null) {
                            flag = true;
                        } else if (!this.getColor().equals(piece.getColor())) {
                            flag = true;
                        } else if (this.getColor().equals(piece.getColor())) {
                            flag = false;
                            break;
                        }
                    }
                } else if (currentPosX < x) {
                    for (int i = currentPosX + 1; i <= x; i++) {
                        Piece piece = board.getAt(i, y);
                        if (piece == null) {
                            flag = true;
                        } else if (!this.getColor().equals(piece.getColor())) {
                            flag = true;
                        } else if (this.getColor().equals(piece.getColor())) {
                            flag = false;
                            break;
                        }
                    }
                }

            } else if (this.getCoordinatesY() != y) {
                int currentPosY = this.getCoordinatesY();
                if (currentPosY > y) {
                    for (int i = currentPosY - 1; i >= y; i--) {
                        Piece piece = board.getAt(x, i);
                        if (piece == null) {
                            flag = true;
                        } else if (!this.getColor().equals(piece.getColor())) {
                            flag = true;
                        } else if (this.getColor().equals(piece.getColor())) {
                            flag = false;
                            break;
                        }
                    }
                } else if (currentPosY < y) {
                    for (int i = currentPosY + 1; i <= y; i++) {
                        Piece piece = board.getAt(x, i);
                        if (piece == null) {
                            flag = true;
                        } else if (!this.getColor().equals(piece.getColor())) {
                            flag = true;
                        } else if (this.getColor().equals(piece.getColor())) {
                            flag = false;
                            break;
                        }
                    }
                }
            }
            return flag;
        }
        return false;
    }
Editor is loading...