Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chessgui.pieces;
- import chessgui.Board;
- public class Knight extends Piece {
- public Knight(int x, int y, boolean is_white, String file_path, Board board)
- {
- super(x,y,is_white,file_path, board);
- }
- @Override
- public boolean canMove(int destination_x, int destination_y){
- // Jika posisi target sudah ada buah catur dan itu milik kita, tidak boleh jalan
- Piece target = board.getPiece(destination_x, destination_y);
- if(target != null){
- if(target.isWhite() && this.isWhite()) return false;
- if(target.isBlack() && this.isBlack()) return false;
- }
- // Jika posisi target tidak L, tidak boleh jalan
- // Dalam artian hanya boleh jalan (2 petak horizontal dan 1 petak vertikal)
- // atau (1 petak horizontal dan 2 petak vertikal)
- int jarak_x = Math.abs(this.getX()-destination_x);
- int jarak_y = Math.abs(this.getY()-destination_y);
- if(!(((jarak_x == 2) && (jarak_y == 1)) ||
- ((jarak_x == 1) && (jarak_y == 2)))) return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement