The implementation of the cult in chess

I want to implement chess with Java as an exercise. I am trying to implement some basic parts, such as a piece being moved , but I could not write this method in such a way that if you want to change yours you must know that there is one more part to move to a new place (we know that only a knight cannot jump over other parts).

And another problem is the implementation of moving the castle.

Relevant Code:

Slice:

 enum Color {BLACK , WHITE}
 enum TYPE {ROOK , BISHOP , KNIGHT , QUEEN , KING , PAWN}

public abstract class Piece {

private boolean available;
private char x;
private int y;
Color color;
TYPE type;

public Piece(boolean available, char x, int y, Color color, TYPE type) {
    this.available = available;
    this.x = x;
    this.y = y;
    this.color = color;
    this.type = type;
}

public boolean validateMove(ChessBoard board,char fromX, int fromY, char toX, int toY) {
    if(toX == fromX && toY == fromY)
        return false;

    if(getX() != fromX || toY != fromY)
        return false;

    if(toX - 'a' < 0 || toX - 'a' > 7 || toY < 0 || toY > 7 || fromX - 'a' < 0 || fromX - 'a' > 7 || fromY < 0 || fromY > 7)
        return false;
    protectingTheWay(board);
    return true;
}

(and all this is getter and setter).

Bishop:

public class Bishop extends Piece {


    public Bishop(boolean available, char x, int y, Color color, TYPE type) {
        super(available, x, y, color, type);
    }

    @Override
    public boolean validateMove(ChessBoard board,char fromX, int fromY, char toX, int toY) {
        if(super.validateMove(board,fromX, fromY, toX, toY) == false)
            return false;
        if( abs(toX - 'a' - (fromX - 'a')) == abs(toY - fromY) )
            return true;
        return false;
    }
}

Knight:

public class Knight  extends Piece {
    public Knight(boolean available, char x, int y, Color color, TYPE type) {
        super(available, x, y, color, type);
    }

    @Override
    public boolean validateMove(ChessBoard board,char fromX, int fromY, char toX, int toY) {
        if( super.validateMove(board,fromX, fromY, toX, toY) == false)
            return false;
        if(toX - 'a' != fromX - 'a' - 1 || toX - 'a' != fromX - 'a' + 1 || toX - 'a' != fromX - 'a' + 2 || toX - 'a' != fromX - 'a' - 2)
            if(toY != fromY - 2 || toY != fromY + 2 || toY != fromY - 1 || toY != fromY + 1)
            return false;
        return true;
    }
}

and other parts.

+4
source share
2 answers

, , , , . , SOLID, Long, - , , , .

, ( ) :

- , , , - . , , . ( ). . , , . , - ( , ), ( ), ( ... ).

, . - - , - ().

, . , , , . , , , .

- ? - , , , , , , ( != ).

+3

, Knight validateMove ? , .

, , validateMove Piece, , :

if(getX() != fromX || toY != fromY)
    return false;

, getX(), .

, , , IMO , - .

, , , Knight validateMove(). , , , 2 1 , ? , - , . , :

@Override
public boolean validateMove(ChessBoard board,char fromX, int fromY, char toX, int toY) {
    if (!super.validateMove(board,fromX, fromY, toX, toY))
        return false;

    int xIndex = Math.abs(toX - fromX);
    int yIndex = Math.abs(toY - fromY);

    if (!(xIndex == 2 && yIndex == 1 || xIndex == 1 && yIndex == 2)) {
        return false;
    }

    return true;
}

, .:)

+1

Source: https://habr.com/ru/post/1674011/


All Articles