-1

我实际上在编写国际象棋游戏。我用箱子做了棋盘,我可以得到坐标。

我的问题是:如何(通过示例)为我的游戏制作具有属性(颜色等)的Pawn类。

谢谢大家!

我的代码实际上是:

    package coordboutons;
import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class CoordBoutons extends JFrame {

CoordBoutons() {
    super("GridLayout");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    Container contenant = getContentPane();
    contenant.setLayout(new GridLayout(8, 8));

    for (int i = 0; i < 8; i++) {
        for (int j = 0; j < 8; j++) {
            contenant.add(new CaseEchiquier(i, j));
        }
    }

    pack();
    setVisible(true);
}

class CaseEchiquier extends JPanel {

    private int lin, col;
    private char column;

    CaseEchiquier(int i, int j) {
        lin = i;
        col = j;
        setPreferredSize(new Dimension(80, 75));
        setBackground((i + j) % 2 == 0 ? Color.WHITE : Color.GRAY);
        addMouseListener(new MouseAdapter() {


            @Override
            public void mousePressed(MouseEvent e){
                CaseEchiquier current =(CaseEchiquier)e.getSource(); // get the object that the user pressed
                int linX = current.getLin();
                int colY = current.getCol();
                System.out.println(lin+"   "+col);

            }



        });

    }
    public int getCol() {
        return col;
    }

    public int getLin() {
        return lin;
    }

}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JFrame.setDefaultLookAndFeelDecorated(true);
            CoordBoutons coordBoutons = new CoordBoutons();
        }
    });
}
}
4

2 回答 2

1

解决这个问题的方法可能是首先定义一个抽象的 Piece 类(它将定义像getColor(),getPosition()setPosition(x,y), 并需要实现getMovementOptions, 等等的函数)。然后你可以创建六个不同的类来扩展Piece:Pawn、Rook、Knight、Bishop、Queen 和 King。getMovementOptions然后可以适当地实施这些中的每一个。

使用这样的基类的优点是,当您编写代码以实际玩游戏时,您可以以相同的方式对待所有 Pieces:您选择 a Piece p,并将其移动到由调用定义的合法方格p.getMovementOptions()(或者你最终定义了这个方法;例如,它可能需要访问棋盘位置)。

public abstract class Piece {
    public Color getColor() {
        return this.color;
    }

    public void setColor(Color c) {
        this.color = c;
    }

    public Square getPosition() {
        return this.position;
    }

    public void setPosition(Square p) {
        this.position = p;
    }

    public List<Square> getMovementOptions(Board b);
}

public class Pawn extends Piece {
    public List<Square> getMovementOptions(Board b) {
        // forward zero, one, or two squares, or capture diagonally one square ahead!
        // The list is based on this.position and the given Board.
    }
}
于 2013-05-09T15:58:20.710 回答
0

只上课

public class ChessPiece
{
    Color color;
    ...
}

.

public class Pawn extends ChessPiece
{
    public Pawn(Color c)
    {
        this.color = c;
    }

    public Color getColor()
    {
        return this.color;
    }
    ...
}
于 2013-05-09T15:57:42.190 回答