Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chessgui.pieces;
- import chessgui.Board;
- public class Queen extends Piece {
- public Queen(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 miring / tidak lurus, tidak boleh jalan
- if(!((Math.abs(this.getX() - destination_x) == Math.abs(this.getY() - destination_y))
- ||(this.getX() == destination_x || this.getY() == destination_y))) return false;
- // Mencari arah dari target
- String arah = "";
- if(destination_y > this.getY()) arah+= "atas";
- if(destination_y < this.getY()) arah+= "bawah";
- if(destination_x > this.getX()) arah+= "kanan";
- if(destination_x < this.getX()) arah+= "kiri";
- // Cek jika ada buah catur lain disepanjang jalan menuju posisi target
- // Jika ada, tidak boleh jalan
- if(arah.equals("atas")){
- int jarak = Math.abs(destination_y - this.getY());
- for(int i=1; i<jarak; i++){
- Piece jalur = board.getPiece(this.getX(),this.getY()+i);
- if(jalur != null) return false;
- }
- }else if(arah.equals("bawah")){
- int jarak = Math.abs(destination_y - this.getY());
- for(int i=1; i<jarak; i++){
- Piece jalur = board.getPiece(this.getX(),this.getY()-i);
- if(jalur != null) return false;
- }
- }else if(arah.equals("kanan")){
- int jarak = Math.abs(destination_x - this.getX());
- for(int i=1; i<jarak; i++){
- Piece jalur = board.getPiece(this.getX()+i,this.getY());
- if(jalur != null) return false;
- }
- }else if(arah.equals("kiri")){
- int jarak = Math.abs(destination_x - this.getX());
- for(int i=1; i<jarak; i++){
- Piece jalur = board.getPiece(this.getX()-i,this.getY());
- if(jalur != null) return false;
- }
- }else if(arah.equals("ataskanan")){
- int jarak = Math.abs(destination_y - this.getY());
- for(int i=1; i<jarak; i++){
- Piece jalur = board.getPiece(this.getX()+i,this.getY()+i);
- if(jalur != null) return false;
- }
- }else if(arah.equals("ataskiri")){
- int jarak = Math.abs(destination_y - this.getY());
- for(int i=1; i<jarak; i++){
- Piece jalur = board.getPiece(this.getX()-i,this.getY()+i);
- if(jalur != null) return false;
- }
- }else if(arah.equals("bawahkanan")){
- int jarak = Math.abs(destination_y - this.getY());
- for(int i=1; i<jarak; i++){
- Piece jalur = board.getPiece(this.getX()+i,this.getY()-i);
- if(jalur != null) return false;
- }
- }else if(arah.equals("bawahkiri")){
- int jarak = Math.abs(destination_y - this.getY());
- for(int i=1; i<jarak; i++){
- Piece jalur = board.getPiece(this.getX()-i,this.getY()-i);
- if(jalur != null) return false;
- }
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement