如何在国际象棋比赛中宣布国际象棋棋子?

时间:2013-05-09 15:54:31

标签: java

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

我的问题是:如何为我的游戏制作一个 Pawn 类及其属性( color 等...)。

谢谢大家!

我的代码实际上是:

    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();
        }
    });
}
}

2 个答案:

答案 0 :(得分:1)

可能的方法是首先定义一个抽象的Piece类(它将定义getColor()getPosition()setPosition(x,y)等函数,并且需要实现getMovementOptions , 等等)。然后你可以制作六个不同的类,扩展Piece:Pawn,Rook,Knight,Bishop,Queen和King。然后,每个人都可以酌情实施getMovementOptions

使用这样的基类的好处是,当你编写代码来实际玩游戏时,你可以用相同的方式处理所有的片段:你选择一个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.
    }
}

答案 1 :(得分:0)

只需上课

public class ChessPiece
{
    Color color;
    ...
}

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

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