Advertisement
kuchuz

PBO-C EAS : Bishop

Jan 11th, 2021
1,267
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.38 KB | None | 0 0
  1. package chessgui.pieces;
  2.  
  3. import chessgui.Board;
  4.  
  5. public class Bishop extends Piece {
  6.  
  7.     public Bishop(int x, int y, boolean is_white, String file_path, Board board)
  8.     {
  9.         super(x,y,is_white,file_path, board);
  10.     }
  11.    
  12.     @Override
  13.     public boolean canMove(int destination_x, int destination_y){
  14.         // Jika posisi target sudah ada buah catur dan itu milik kita, tidak boleh jalan
  15.         Piece target = board.getPiece(destination_x, destination_y);
  16.         if(target != null){
  17.             if(target.isWhite() && this.isWhite()) return false;
  18.             if(target.isBlack() && this.isBlack()) return false;
  19.         }
  20.  
  21.         // Jika posisi target tidak miring, tidak boleh jalan
  22.         if(Math.abs(this.getX() - destination_x) != Math.abs(this.getY() - destination_y)) return false;
  23.  
  24.         // Mencari arah dari target
  25.         String arah = "";
  26.         if(destination_y > this.getY()) arah+= "atas";
  27.         if(destination_y < this.getY()) arah+= "bawah";
  28.         if(destination_x > this.getX()) arah+= "kanan";
  29.         if(destination_x < this.getX()) arah+= "kiri";
  30.  
  31.         // Cek jika ada buah catur lain disepanjang jalan menuju posisi target
  32.         // Jika ada, tidak boleh jalan
  33.         if(arah.equals("ataskanan")){
  34.             int jarak = Math.abs(destination_y - this.getY());
  35.             for(int i=1; i<jarak; i++){
  36.                 Piece jalur = board.getPiece(this.getX()+i,this.getY()+i);
  37.                 if(jalur != null) return false;
  38.             }
  39.         }else if(arah.equals("ataskiri")){
  40.             int jarak = Math.abs(destination_y - this.getY());
  41.             for(int i=1; i<jarak; i++){
  42.                 Piece jalur = board.getPiece(this.getX()-i,this.getY()+i);
  43.                 if(jalur != null) return false;
  44.             }
  45.         }else if(arah.equals("bawahkanan")){
  46.             int jarak = Math.abs(destination_y - this.getY());
  47.             for(int i=1; i<jarak; i++){
  48.                 Piece jalur = board.getPiece(this.getX()+i,this.getY()-i);
  49.                 if(jalur != null) return false;
  50.             }
  51.         }else if(arah.equals("bawahkiri")){
  52.             int jarak = Math.abs(destination_y - this.getY());
  53.             for(int i=1; i<jarak; i++){
  54.                 Piece jalur = board.getPiece(this.getX()-i,this.getY()-i);
  55.                 if(jalur != null) return false;
  56.             }
  57.         }
  58.         return true;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement