Advertisement
kuchuz

PBO-C EAS : Queen

Jan 11th, 2021
1,731
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.58 KB | None | 0 0
  1. package chessgui.pieces;
  2.  
  3. import chessgui.Board;
  4.  
  5. public class Queen extends Piece {
  6.  
  7.     public Queen(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 lurus, tidak boleh jalan
  22.         if(!((Math.abs(this.getX() - destination_x) == Math.abs(this.getY() - destination_y))
  23.           ||(this.getX() == destination_x || this.getY() == destination_y))) return false;
  24.  
  25.         // Mencari arah dari target
  26.         String arah = "";
  27.         if(destination_y > this.getY()) arah+= "atas";
  28.         if(destination_y < this.getY()) arah+= "bawah";
  29.         if(destination_x > this.getX()) arah+= "kanan";
  30.         if(destination_x < this.getX()) arah+= "kiri";
  31.  
  32.         // Cek jika ada buah catur lain disepanjang jalan menuju posisi target
  33.         // Jika ada, tidak boleh jalan
  34.                 if(arah.equals("atas")){
  35.             int jarak = Math.abs(destination_y - this.getY());
  36.             for(int i=1; i<jarak; i++){
  37.                 Piece jalur = board.getPiece(this.getX(),this.getY()+i);
  38.                 if(jalur != null) return false;
  39.             }
  40.         }else if(arah.equals("bawah")){
  41.             int jarak = Math.abs(destination_y - this.getY());
  42.             for(int i=1; i<jarak; i++){
  43.                 Piece jalur = board.getPiece(this.getX(),this.getY()-i);
  44.                 if(jalur != null) return false;
  45.             }
  46.         }else if(arah.equals("kanan")){
  47.             int jarak = Math.abs(destination_x - this.getX());
  48.             for(int i=1; i<jarak; i++){
  49.                 Piece jalur = board.getPiece(this.getX()+i,this.getY());
  50.                 if(jalur != null) return false;
  51.             }
  52.         }else if(arah.equals("kiri")){
  53.             int jarak = Math.abs(destination_x - this.getX());
  54.             for(int i=1; i<jarak; i++){
  55.                 Piece jalur = board.getPiece(this.getX()-i,this.getY());
  56.                 if(jalur != null) return false;
  57.             }
  58.         }else if(arah.equals("ataskanan")){
  59.             int jarak = Math.abs(destination_y - this.getY());
  60.             for(int i=1; i<jarak; i++){
  61.                 Piece jalur = board.getPiece(this.getX()+i,this.getY()+i);
  62.                 if(jalur != null) return false;
  63.             }
  64.         }else if(arah.equals("ataskiri")){
  65.             int jarak = Math.abs(destination_y - this.getY());
  66.             for(int i=1; i<jarak; i++){
  67.                 Piece jalur = board.getPiece(this.getX()-i,this.getY()+i);
  68.                 if(jalur != null) return false;
  69.             }
  70.         }else if(arah.equals("bawahkanan")){
  71.             int jarak = Math.abs(destination_y - this.getY());
  72.             for(int i=1; i<jarak; i++){
  73.                 Piece jalur = board.getPiece(this.getX()+i,this.getY()-i);
  74.                 if(jalur != null) return false;
  75.             }
  76.         }else if(arah.equals("bawahkiri")){
  77.             int jarak = Math.abs(destination_y - this.getY());
  78.             for(int i=1; i<jarak; i++){
  79.                 Piece jalur = board.getPiece(this.getX()-i,this.getY()-i);
  80.                 if(jalur != null) return false;
  81.             }
  82.         }
  83.         return true;
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement