Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package chessgui.pieces;
- import chessgui.Board;
- public class Pawn extends Piece {
- private boolean has_moved;
- public Pawn(int x, int y, boolean is_white, String file_path, Board board)
- {
- super(x,y,is_white,file_path, board);
- has_moved = false;
- }
- public void setHasMoved(boolean has_moved)
- {
- this.has_moved = has_moved;
- }
- public boolean getHasMoved()
- {
- return has_moved;
- }
- @Override
- public boolean canMove(int destination_x, int destination_y)
- {
- // deklarasi posisi target, dan jarak
- Piece target = board.getPiece(destination_x, destination_y);
- int jarak_x = Math.abs(this.getX()-destination_x);
- int jarak_y = Math.abs(this.getY()-destination_y);
- //Untuk mempermudah perhitungan di baris kode bawah
- int kalimin = 1;
- if(this.isBlack()) kalimin=-1;
- // Cek buah catur sudah pernah jalan atau belum
- if(this.getHasMoved()){
- //jika sudah pernah jalan, bidak tidak boleh jalan vertikal lebih dari 1 petak
- if(jarak_y>1) return false;
- }else{
- //jika sudah pernah jalan, bidak tidak boleh jalan vertikal lebih dari 2 petak
- if(jarak_y>2) return false;
- // Cek jika ada buah catur lain disepanjang jalan menuju posisi target
- // Jika ada, tidak boleh jalan
- for(int i=1; i<jarak_y; i++){
- Piece jalur = board.getPiece(this.getX(),this.getY()+(i*kalimin));
- if(jalur != null) return false;
- }
- }
- // Bidak tidak boleh mundur
- if((this.getY()-destination_y)*kalimin >= 0) return false;
- // Jika posisi target tidak kosong, bidak hanya bisa jalan miring 1 petak
- // Jika posisi kosong, bidak hanya bisa jalan lurus vertikal
- if(target!=null){
- if(jarak_x != 1 || jarak_y != 1) return false;
- if(this.isWhite()){
- if(target.isWhite()) return false;
- }else{
- if(target.isBlack()) return false;
- }
- }else{
- if(jarak_x != 0) return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement