Java编程 - 国际象棋移动(基本,没有AI)

Smo*_*tor 10 java chess

我在设计国际象棋游戏时需要帮助.我已经开始了,但还没有走远,因为我对Java很陌生,实际上是编程新手.

无论如何,我有我的抽象类Piece和各个部分作为子类.我有一个方法movePiece,在我的抽象类中,我想为所有子类定义.

它目前所做的就是将这件作品从一个方格移动到另一个方格.我有一个可以容纳Piece对象的Square类,该板由64x1 Square数组组成.

我知道碎片是如何移动的,但我如何实际编程呢?我想尝试应用MVC模式,但这是我第一次使用模式.

基本上我正在考虑使用Graphics2D为每个Square创建一个盒子.然后,当玩家点击一块时,移动后可用作目的地的方块将以某种颜色勾勒出轮廓.玩家点击其中一个方格后,我在movePiece方法中已有的代码将运行.

我想要做的是在Piece的每个子类中覆盖我的movePiece方法.问题是,代码如何在其中一种方法中看到?以Pawn子类为例.

我不是要求复制/粘贴代码,只是关于如何执行此操作的一些指示,最后是一些示例代码.

谢谢!

public class Game {


@SuppressWarnings("unused")
public static void main(String[] args){
    Board board = new Board();
} }

public class Board {

Square[] grid;

public Board(){
    grid = new Square[64];
}   
public Square getSquare(int i){
    return grid[i];
}   
public void setDefault(){

}   
public Boolean isMoveValid(){
    return null;    
} }

public class Square {

private Piece piece;

public void addPiece(Piece pieceType, String pieceColour, String pieceOwner) 
        throws ClassNotFoundException, InstantiationException, IllegalAccessException{

    PieceFactory factory = new PieceFactory();
    Piece piece = factory.createPiece(pieceType);

    piece.setColour(pieceColour);
    piece.setOwner(pieceOwner);

    this.piece = piece; 
}
public void addPiece(Piece pieceType){ 
    this.piece = pieceType; 
}
public void removePiece(){  
    piece = null;
}
public Piece getPiece(){
    return piece;       
}

class PieceFactory {     
     @SuppressWarnings("rawtypes")
     public Piece createPiece(Piece pieceType) 
            throws ClassNotFoundException, InstantiationException, IllegalAccessException{
         Class pieceClass = Class.forName(pieceType.toString());
         Piece piece = (Piece) pieceClass.newInstance();

         return piece;       
     } }

public void setColour(String colour){

} }

public abstract class Piece {

Board board;

public void setColour(String pieceColour) {
}

public void setOwner(String pieceOwner) {
}

public String getColour() {
    return "";
}

public String getOwner() {
    return "";      
}
public void movePiece(int oldIndex, int newIndex){
    board.getSquare(oldIndex).removePiece();
    board.getSquare(newIndex).addPiece(this);
}
public String toString(){
    return this.getClass().getSimpleName();
} }
Run Code Online (Sandbox Code Playgroud)

你想看看代码,我知道的非常基本.我会将[64]更改为[8] [8].我试图不让它变得更难.我可以将Color和Owner组合为一个属性,并使其成为枚举(BLACK或WHITE).

对不起格式化不好.

mer*_*ike 18

在设计软件时,我发现考虑如何使用方法,然后记下方法签名(如果你做测试驱动开发,单元测试),那么只考虑我将如何实现它.

在这里这样做,我发现了这个要求

然后,当玩家点击一块时,移动后可用作目的地的方块将以某种颜色勾勒出轮廓.

用一种方法是不可能满足的

void move(Square destination);
Run Code Online (Sandbox Code Playgroud)

因为没有实际制作它们就无法找到可能的动作.要突出显示正方形,如果我们有一个像这样的方法可能是最好的

Collection<Square> getPossibleMoves();
Run Code Online (Sandbox Code Playgroud)

然后我们就可以实施了

void move(Square destination) {
    if (!getPossibleMoves().contains(destination) {
        throw new IllegalMoveException();
    }

    this.location.occupyingPiece = null;
    this.location = destination;
    this.location.occupyingPiece = this;
}
Run Code Online (Sandbox Code Playgroud)

至于实现getPossibleMoves:

class Pawn extends Piece {
    @Override Collection<Square> getPossibleMoves() {
        List<Square> possibleMoves = new ArrayList<Square>();
        int dy = color == Color.white ? 1 : -1;
        Square ahead = location.neighbour(0, dy);
        if (ahead.occupyingPiece == null) {
            possibleMoves.add(ahead);
        }
        Square aheadLeft = location.neighbour(-1, dy);
        if (aheadLeft != null && aheadLeft.occupyingPiece != null && aheadLeft.occupyingPiece.color != color) {
            possibleMoves.add(aheadLeft);
        }
        Square aheadRight = location.neighbour(1, dy);
        if (aheadRight != null && aheadRight.occupyingPiece != null && aheadRight.occupyingPiece.color != color) {
            possibleMoves.add(aheadRight);
        }
        return possibleMoves;
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑:

class Knight extends Piece {
    @Override Collection<Square> getPossibleMoves() {
        List<Square> possibleMoves = new ArrayList<Square>();
        int[][] offsets = {
            {-2, 1},
            {-1, 2},
            {1, 2},
            {2, 1},
            {2, -1},
            {1, -2},
            {-1, -2},
            {-2, -1}
        };
        for (int[] o : offsets) {
            Square candidate = location.neighbour(o[0], o[1]);
            if (candidate != null && (candidate.occupyingPiece == null || candidate.occupyingPiece.color != color)) {
                possibleMoves.add(candidate);
            }
        }
        return possibleMoves;
    }
}
Run Code Online (Sandbox Code Playgroud)