需要帮助搞清楚基本国际象棋运动逻辑

时间:2012-03-01 18:23:45

标签: java eclipse model-view-controller chess

对不起文字墙,但目前很难过。我不是要求“勺子喂”,因为我对每一件都有基本的想法,只是我在如何完成确定我的移动逻辑上遇到了困难。请允许我进一步解释。我也会发布我的代码以便更好地参考。

我正在用MVC结构编码。我有一个Model,它是BoardSquares的2D数组。哪些BoardSquares跟踪它在棋盘上的位置,棋盘中的棋子(如果有的话),该棋子的颜色(如果有的话),以及它是否为空。这是代码:

public class BoardSquare {
// Private information stored in each BoardSquare
private boolean isSquareEmpty;
private int row;
private int col;
private String space;
private String pColor;

// Constructor to make an empty space on the board
public BoardSquare(int row, int col) {
    space = "";
    pColor = "";
    this.row = row;
    this.col = col;
    SquareEmpty();
}

//Constructor to get the information of a space with a piece
public BoardSquare(BoardPiece piece, int row, int col) {
    this.space = piece.getModelName();
    this.pColor = Character.toString((piece.getModelName().charAt(0))); 
    this.row = row;
    this.col = col;
    SquareNotEmpty();
}

public String getpColor() {
    String temp = getPieceColor(pColor);
    return temp;
}

//A bunch of auto generated getters/setters...

    // Gets the correct color label for the piece
public String getPieceColor(String info){
    String temp;
    if(info.equalsIgnoreCase("b")){
        temp = "Black";
    }
    else {
        temp = "White";
    }
    return temp;
}

目前我的所有Pieces都扩展了一个抽象的BoardPiece类,它也有几个抽象的方法,但是我的文章知道它在板上的位置,它的名称,它的颜色,以及我正在生成的方式的2D板阵列有效的举动。

我通过检查它们可以移动到的空间并查看它们是否为空并将它们添加到ArrayList并比较并查看该列表是否包含输入的目标来生成它们。

然而,我在思考如何停止检查Black Rook是否位于棋盘左侧并希望一直向右移动时遇到问题,但是在Rook右侧有一个黑棋子阻止它这样做。我试图通常想到它让我的生活更轻松,我也可以在其他的课程中实现它所以这里是Rook的代码:

public class Rook extends BoardPiece{
private String name = "Rook";
private String color;
private String modelName;

BoardSquare board[][];

// Both should remain 0 - 7
private int row;
private int col;

public Rook (String modelName, int row, int col, String color, BoardSquare[][] field) {
    this.modelName = modelName;
    this.row = row;
    this.col = col;
    this.color = color;
    this.board = field;
}

@Override
void move(BoardSquare target) {
    if(!getPossibleMove(board).contains(target)){
        //Sysout false move
    }

}

@Override
Collection<BoardSquare> getPossibleMove(BoardSquare[][] board) {
    ArrayList<BoardSquare> validMoves = new ArrayList<BoardSquare>();
    for(int i = 0; i < 8; i++){
        // Checks every column of the row that the piece is on
        if(moveValidator(board[row][i])){
            validMoves.add(board[row][i]);
        }
        // Checks every row of the column that the piece is on
        if(moveValidator(board[i][col])){
            validMoves.add(board[i][col]);
        }
    }
    return validMoves;
}

@Override
boolean moveValidator(BoardSquare space) {
    // Removes the current square the piece is on as a valid move
    if(space.getRow() == getRow() && space.getCol() == getCol()){
        return false;
    }
    //Checks if the space is not empty and the piece in the spot is the same color as the piece itself
    if(!space.isEmptySquare() && space.getpColor().equalsIgnoreCase(color)){
        return false;
    }
    return true;
}

就像我说任何帮助或向正确的方向推进都会受到赞赏。对于文本墙感到抱歉,但我尽可能地清楚,以便任何想要帮助的人都可以完全理解并看到我的逻辑。

编辑:忘了注意Rook也有自动生成的Getters和Setter。

1 个答案:

答案 0 :(得分:3)

我会以最蛮力和最直观的方式接近它:想想在轮到它的时候一次移动一个方块,看它是否与任何东西发生碰撞。也就是说,跳过每个方格,直到它到达所需的位置并检查它是否是每个方格的有效移动。

这是一个很好的方法,因为

  • 很容易理解
  • 直到你的国际象棋棋盘变得非常庞大,它并不昂贵,所以蛮力不是问题。
  • 它概括为所有部分:车辆必须“滑动”到目的地,但骑士可以“跳”或“传送”到其他部分到他们的新位置,所以不需要中间跃点。

这听起来很接近你当前的实现,但你需要在碰撞时停止追踪。也就是说,如果路上有黑色棋子,那个棋子后的列不再是有效空格,对吧?换句话说,将此行动视为车辆所采取的行动,而不是可能行动的独立状态空间,因为有些空间是合法移动,除非车辆先前已被阻挡。您需要知道车的来源以及确定有效移动的位置,而不仅仅是空间位于同一行。

所以,阅读你的代码(感谢发布上下文,顺便说一下,它非常有帮助)你真正需要做的就是不在每个方块上迭代 ,而是迭代移动每个方块,并在被阻止时停止迭代。