EAS/Final Project - PBO C - Game Catur 2D Multiplayer

January 11, 2021 2

Pada kesempatan kali ini, saya akan mendemonstrasikan tentang game yang telah kami buat secara berkelompok. Saya Refaldyka Galuh Pratama dan teman saya Fajar Satria. Pembuatan game ini bertujuan untuk menyelesaikan final project dari mata kuliah pemrograman berbasis objek. Dasarnya game yang kami buat ini berjudul "Catur 2D Multiplayer". Game ini tidak ada AI atau BOT jadi hanya bisa dimainkan ketika ada dua pemain. Cara memainkannya sederhana cukup dengan bermain catur seperti biasa. Berikut ini adalah source code dari game yang telah kami buat.


  • Sourcecode ChessGUI.java:
  • package chessgui;
    
    public class ChessGUI {
        
        public BoardFrame boardframe;
        public static void main(String[] args) {
            ChessGUI gui = new ChessGUI();
            gui.boardframe = new BoardFrame();
            gui.boardframe.setVisible(true);
            
        }
    }
    

  • Sourcecode BoardFrame.java:
  • package chessgui;
    
    public class ChessGUI {
        
        public BoardFrame boardframe;
        public static void main(String[] args) {
            ChessGUI gui = new ChessGUI();
            gui.boardframe = new BoardFrame();
            gui.boardframe.setVisible(true);
            
        }
    }
    
  • Sourcecode Board.java:
  • package chessgui;
    
    import chessgui.pieces.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import java.awt.image.*;
    import java.io.*;
    import java.util.*;
    import javax.imageio.*;
    import javax.swing.*;
    
    
    @SuppressWarnings("serial")
    public class Board extends JComponent {
            
        public int turnCounter = 0;
        private static Image NULL_IMAGE = new BufferedImage(10, 10, BufferedImage.TYPE_INT_ARGB);
    
        private final int Square_Width = 65;
        public ArrayList<Piece> White_Pieces;
        public ArrayList<Piece> Black_Pieces;
        
    
        public ArrayList<DrawingShape> Static_Shapes;
        public ArrayList<DrawingShape> Piece_Graphics;
    
        public Piece Active_Piece;
    
        private final int rows = 8;
        private final int cols = 8;
        private Integer[][] BoardGrid;
        private String board_file_path = "images" + File.separator + "board.png";
        private String active_square_file_path = "images" + File.separator + "active_square.png";
    
        public void initGrid(){
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    BoardGrid[i][j] = 0;
                }
            }
     
    
            White_Pieces.add(new King(3,0,true,"King",this));
            White_Pieces.add(new Queen(4,0,true,"Queen",this));
            White_Pieces.add(new Bishop(2,0,true,"Bishop",this));
            White_Pieces.add(new Bishop(5,0,true,"Bishop",this));
            White_Pieces.add(new Knight(1,0,true,"Knight",this));
            White_Pieces.add(new Knight(6,0,true,"Knight",this));
            White_Pieces.add(new Rook(0,0,true,"Rook",this));
            White_Pieces.add(new Rook(7,0,true,"Rook",this));
            White_Pieces.add(new Pawn(0,1,true,"Pawn",this));
            White_Pieces.add(new Pawn(1,1,true,"Pawn",this));
            White_Pieces.add(new Pawn(2,1,true,"Pawn",this));
            White_Pieces.add(new Pawn(3,1,true,"Pawn",this));
            White_Pieces.add(new Pawn(4,1,true,"Pawn",this));
            White_Pieces.add(new Pawn(5,1,true,"Pawn",this));
            White_Pieces.add(new Pawn(6,1,true,"Pawn",this));
            White_Pieces.add(new Pawn(7,1,true,"Pawn",this));
    
            Black_Pieces.add(new King(3,7,false,"King",this));
            Black_Pieces.add(new Queen(4,7,false,"Queen",this));
            Black_Pieces.add(new Bishop(2,7,false,"Bishop",this));
            Black_Pieces.add(new Bishop(5,7,false,"Bishop",this));
            Black_Pieces.add(new Knight(1,7,false,"Knight",this));
            Black_Pieces.add(new Knight(6,7,false,"Knight",this));
            Black_Pieces.add(new Rook(0,7,false,"Rook",this));
            Black_Pieces.add(new Rook(7,7,false,"Rook",this));
            Black_Pieces.add(new Pawn(0,6,false,"Pawn",this));
            Black_Pieces.add(new Pawn(1,6,false,"Pawn",this));
            Black_Pieces.add(new Pawn(2,6,false,"Pawn",this));
            Black_Pieces.add(new Pawn(3,6,false,"Pawn",this));
            Black_Pieces.add(new Pawn(4,6,false,"Pawn",this));
            Black_Pieces.add(new Pawn(5,6,false,"Pawn",this));
            Black_Pieces.add(new Pawn(6,6,false,"Pawn",this));
            Black_Pieces.add(new Pawn(7,6,false,"Pawn",this));
    
        }
    
        public Board() {
    
            BoardGrid = new Integer[rows][cols];
            Static_Shapes = new ArrayList();
            Piece_Graphics = new ArrayList();
            White_Pieces = new ArrayList();
            Black_Pieces = new ArrayList();
    
            initGrid();
    
            
            this.setPreferredSize(new Dimension(520, 520));
    
            this.addMouseListener(mouseAdapter);
            //this.addComponentListener(componentAdapter);
            //this.addKeyListener(keyAdapter);
    
    
            
            this.setVisible(true);
            this.requestFocus();
            drawMenu();
        }
    
        private void drawWinner(String winner){
            //Reset semua bidak catur
            White_Pieces.removeAll(White_Pieces);
            Black_Pieces.removeAll(Black_Pieces);
            //Reset semua gambar bidak yg terload
            Piece_Graphics.clear();
            Static_Shapes.clear();
            Image board = loadImage("images" + File.separator + "win_"+winner+".png");
            Static_Shapes.add(new DrawingImage(board, new Rectangle2D.Double(0, 0, board.getWidth(null), board.getHeight(null))));
            this.repaint();
            initGrid();
        }
        private void drawMenu(){
            //Reset semua bidak catur
            White_Pieces.removeAll(White_Pieces);
            Black_Pieces.removeAll(Black_Pieces);
            //Reset semua gambar bidak yg terload
            Piece_Graphics.clear();
            Static_Shapes.clear();
            Image board = loadImage("images" + File.separator + "menu.png");
            Static_Shapes.add(new DrawingImage(board, new Rectangle2D.Double(0, 0, board.getWidth(null), board.getHeight(null))));
            this.repaint();
            initGrid();
        }
    
        private void drawBoard()
        {
            Piece_Graphics.clear();
            Static_Shapes.clear();
            String winner = "";
            Image board = loadImage(board_file_path);
            Static_Shapes.add(new DrawingImage(board, new Rectangle2D.Double(0, 0, board.getWidth(null), board.getHeight(null))));
            if (Active_Piece != null){
                Image active_square = loadImage("images" + File.separator + "active_square.png");
                Static_Shapes.add(new DrawingImage(active_square, new Rectangle2D.Double(Square_Width*Active_Piece.getX(),Square_Width*Active_Piece.getY(), active_square.getWidth(null), active_square.getHeight(null))));
            }
            boolean is_white_king_exist = false;
            for (int i=0;i<White_Pieces.size(); i++){
                if(White_Pieces.get(i).getFilePath().equals("King")) is_white_king_exist = true;
                int COL = White_Pieces.get(i).getX();
                int ROW = White_Pieces.get(i).getY();
                Image piece = loadImage("images" + File.separator + "white_pieces" + File.separator + White_Pieces.get(i).getFilePath() + ".png");
                Piece_Graphics.add(new DrawingImage(piece, new Rectangle2D.Double(Square_Width*COL,Square_Width*ROW, piece.getWidth(null), piece.getHeight(null))));
            }
            boolean is_black_king_exist = false;
            for (int i = 0; i < Black_Pieces.size(); i++){
                if(Black_Pieces.get(i).getFilePath().equals("King")) is_black_king_exist = true;
                int COL = Black_Pieces.get(i).getX();
                int ROW = Black_Pieces.get(i).getY();
                Image piece = loadImage("images" + File.separator + "black_pieces" + File.separator + Black_Pieces.get(i).getFilePath() + ".png");
                Piece_Graphics.add(new DrawingImage(piece, new Rectangle2D.Double(Square_Width*COL,Square_Width*ROW, piece.getWidth(null), piece.getHeight(null))));
            }
            if(!is_white_king_exist) drawWinner("black");
            if(!is_black_king_exist) drawWinner("white");
            this.repaint();
        }
    
        
        public Piece getPiece(int x, int y) {
            for (Piece p : White_Pieces){
                if (p.getX() == x && p.getY() == y){
                    return p;
                }
            }
            for (Piece p : Black_Pieces){
                if (p.getX() == x && p.getY() == y){
                    return p;
                }
            }
            return null;
        }
    
    
        private MouseAdapter mouseAdapter = new MouseAdapter() {
    
            @Override
            public void mouseClicked(MouseEvent e){
            }
            @Override
            public void mousePressed(MouseEvent e) {
                int d_X = e.getX();
                int d_Y = e.getY();  
                int Clicked_Row = d_Y / Square_Width;
                int Clicked_Column = d_X / Square_Width;
                boolean is_whites_turn = true;
                boolean king_killed = false;
                if (turnCounter%2 == 1){
                    is_whites_turn = false;
                }
                Piece clicked_piece = getPiece(Clicked_Column, Clicked_Row);
                if (Active_Piece == null && clicked_piece != null &&
                        ((is_whites_turn && clicked_piece.isWhite()) || (!is_whites_turn && clicked_piece.isBlack())))
                {
                    Active_Piece = clicked_piece;
                }
                else if (Active_Piece != null && Active_Piece.getX() == Clicked_Column && Active_Piece.getY() == Clicked_Row)
                {
                    Active_Piece = null;
                }
                else if (Active_Piece != null && Active_Piece.canMove(Clicked_Column, Clicked_Row) 
                        && ((is_whites_turn && Active_Piece.isWhite()) || (!is_whites_turn && Active_Piece.isBlack())))
                {
                    // Jika posisi target sudah ada bidak, maka diganti dengan bidak yg dijalankan
                    if (clicked_piece != null)
                    {
                        if (clicked_piece.isWhite())
                        {
                            White_Pieces.remove(clicked_piece);
                        }
                        else
                        {
                            Black_Pieces.remove(clicked_piece);
                        }
                    }
    
                    Active_Piece.setX(Clicked_Column);
                    Active_Piece.setY(Clicked_Row);
                    
                    // Jika bidak yg dijalankan adalah pion, maka ubah status setHasMoved 
                    if (Active_Piece.getClass().equals(Pawn.class))
                    {
                        Pawn castedPawn = (Pawn)(Active_Piece);
                        castedPawn.setHasMoved(true);
                    }
                    
                    
                    Active_Piece = null;
                    turnCounter++;
                }
                
                drawBoard();
            }    
        };
    
        private void adjustShapePositions(double dx, double dy) {
    
            Static_Shapes.get(0).adjustPosition(dx, dy);
            this.repaint();
    
        } 
            
            
          
        private Image loadImage(String imageFile) {
            try {
                return ImageIO.read(new File(imageFile));
            }
            catch (IOException e) {
                    return NULL_IMAGE;
            }
        }
    
        @Override
        protected void paintComponent(Graphics g) {
    
            super.paintComponent(g);
    
            Graphics2D g2 = (Graphics2D)g;
            drawBackground(g2);
            drawShapes(g2);
        }
    
        private void drawBackground(Graphics2D g2) {
            g2.setColor(getBackground());
            g2.fillRect(0,  0, getWidth(), getHeight());
        }
           
    
        private void drawShapes(Graphics2D g2) {
            for (DrawingShape shape : Static_Shapes) {
                shape.draw(g2);
            }    
            for (DrawingShape shape : Piece_Graphics) {
                shape.draw(g2);
            }
        }
    
        private ComponentAdapter componentAdapter = new ComponentAdapter() {
    
            @Override
            public void componentHidden(ComponentEvent e) {
    
            }
    
            @Override
            public void componentMoved(ComponentEvent e) {
    
            }
    
            @Override
            public void componentResized(ComponentEvent e) {
    
            }
    
            @Override
            public void componentShown(ComponentEvent e) {
    
            }    
        };
    
        private KeyAdapter keyAdapter = new KeyAdapter() {
    
            @Override
            public void keyPressed(KeyEvent e) {
    
            }
    
            @Override
            public void keyReleased(KeyEvent e) {
    
            }
    
            @Override
            public void keyTyped(KeyEvent e) {
    
            }    
        };
    
    }
    
    
    
    interface DrawingShape {
        boolean contains(Graphics2D g2, double x, double y);
        void adjustPosition(double dx, double dy);
        void draw(Graphics2D g2);
    }
    
    
    class DrawingImage implements DrawingShape {
    
        public Image image;
        public Rectangle2D rect;
    
        public DrawingImage(Image image, Rectangle2D rect) {
                this.image = image;
                this.rect = rect;
        }
    
        @Override
        public boolean contains(Graphics2D g2, double x, double y) {
                return rect.contains(x, y);
        }
    
        @Override
        public void adjustPosition(double dx, double dy) {
                rect.setRect(rect.getX() + dx, rect.getY() + dy, rect.getWidth(), rect.getHeight());    
        }
    
        @Override
        public void draw(Graphics2D g2) {
                Rectangle2D bounds = rect.getBounds2D();
                g2.drawImage(image, (int)bounds.getMinX(), (int)bounds.getMinY(), (int)bounds.getMaxX(), (int)bounds.getMaxY(),
                                                0, 0, image.getWidth(null), image.getHeight(null), null);
        }    
    }
    
Kemudian dibawah ini adalah sourcecode untuk menggerakan suatu piece dalam game catur yang kami buat.
  • Sourcecode Piece.java:
  • package chessgui.pieces;
    
    import chessgui.Board;
    
    public class Piece {
        private int x;
        private int y;
        final private boolean is_white;
        private String file_path;
        public Board board;
        
        public Piece(int x, int y, boolean is_white, String file_path, Board board)
        {
            this.is_white = is_white;
            this.x = x;
            this.y = y;
            this.file_path = file_path;
            this.board = board;
        }
        
        public String getFilePath()
        {
            return file_path;
        }
        
        public void setFilePath(String path)
        {
            this.file_path = path;
        }
        
        public boolean isWhite()
        {
            return is_white;
        }
        
        public boolean isBlack()
        {
            return !is_white;
        }
        
        public void setX(int x)
        {
            this.x = x;
        }
        
        public void setY(int y)
        {
            this.y = y;
        }
        
        public int getX()
        {
            return x;
        }
        
        public int getY()
        {
            return y;
        }
        
        public boolean canMove(int destination_x, int destination_y)
        {
            return false;
        }
    }
    
  • Sourcecode King.java:
  • package chessgui.pieces;
    
    import chessgui.Board;
    
    public class King extends Piece {
    
        public King(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 jarak lebih dari 1 petak, tidak boleh jalan
            if(Math.abs(this.getX() - destination_x)>1 || Math.abs(this.getY() - destination_y)>1) return false;
            return true;
        }
    }
    
  • Sourcecode Knight.java:
  • 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;
        }
    }
    
  • Sourcecode Pawn.java:
  • 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;
        }
    }
    
  • Sourcecode Bishop.java:
  • package chessgui.pieces;
    
    import chessgui.Board;
    
    public class Bishop extends Piece {
    
        public Bishop(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 boleh jalan
            if(Math.abs(this.getX() - destination_x) != Math.abs(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("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;
        }
    }
    
  • Sourcecode Queen.java:
  • 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;
        }
    }
    
  • Sourcecode Rook.java:
  • package chessgui.pieces;
    
    import chessgui.Board;
    
    public class Rook extends Piece {
    
        public Rook(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 lurus, tidak boleh jalan
            if(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;
                }
            }
            return true;
        }
    }
    

Dengan ini juga terdapat video demo program yang telah kami buat 


Bagi yang ingin mencoba memainkan game dari kami bisa didownload pada link ini. Untuk menjalankannya hanya tinggal double click ChessGUI.jar. Pastikan sudah menginstall java sebelum menjalankan game ini.

Terima kasih telah berkunjung diblog saya bila ada salah kata mohon maklum. 😄

2 Comments for "EAS/Final Project - PBO C - Game Catur 2D Multiplayer"