在国际象棋游戏中,棋子应该移动还是棋盘应该移动

时间:2019-11-21 16:42:54

标签: java

我正在使用Java构建国际象棋游戏(可能不会使用任何gui或任何简单的控制台游戏),但是我有ChessBoard类,如下所示:

public class ChessBoard {

        private BasePiece[][] board = new BasePiece[8][8];

        private int charToInt(char input){
            return (int)input - 97;
        }

        public void setPiece(String colour, String type, char x, int y){
            board[charToInt(x)][y-1] = new BasePiece(colour, type);
        }

        public void setPiece(String piece, char x, int y){
            board[charToInt(x)][y-1] = new BasePiece(piece);
        }

        public String getPiece(char x, int y){
            return board[charToInt(x)][y-1].getPiece();
        }

    }

这是目前非常简单的代码,能够将部件添加到板上,然后打印出任何给定位置的部件,但是我还有一个ChessPiece类,如下所示:

public class ChessPiece {
    private String colour;
    private String type;

    ChessPiece(String colour, String type){
        setColour(colour);
        setType(type);
    }

    ChessPiece(String piece){
        setColour(piece.toCharArray()[0]);
        setType(piece.toCharArray()[1]);
    }

    private void setColour(String colour){
        this.colour = colour.toLowerCase();
    }

    private void setColour(char colour){
        switch (colour) {
            case 'w':
                setColour("white");
                break;
            case 'b':
                setColour("black");
                break;
            default:
                setColour("invalid colour");
                break;
        }
    }

    private void setType(String type){
        this.type = type.toLowerCase();
    }

    private void setType(char type){
        switch (type) {
            case 'K':
                setType("king");
                break;
            case 'q':
                setType("queen");
                break;
            case 'r':
                setType("rook");
                break;
            case 'b':
                setType("bishop");
                break;
            case 'k':
                setType("knight");
                break;
            case 'p':
                setType("pawn");
                break;
            default:
                setType("invalid type");
                break;
        }
    }

    public String getColour(){
        return colour;
    }

    public String getType(){
        return type;
    }

    public String getPiece(){
        return getColour() + " " + getType();
    }
}

到目前为止一切正常,但是我想知道让棋子移动是更好的做法(所以移动应该在ChessPiece类中进行)还是仅具有如何在ChessPiece类中移动然后再给出将他们转移到ChessBoard类的实际工作 谢谢

2 个答案:

答案 0 :(得分:1)

正如我在评论中提到的那样,我认为您的OOP结构可能有点过于简单。我建议您考虑创建:

  • 枚举ChessColor:白色,黑色。最好使用枚举而不是字符串,因为后者容易出错,并且不允许进行方法参数和其他编译时类型检查
  • 抽象类AbstractChessPiece:片段从其延伸的抽象类。它具有ChessColor字段,以及所有组件都需要的任何其他抽象方法。可能实际上不需要这个。
  • 枚举ChessPiece扩展了AbstractChessPiece:也可以对此进行枚举,因为它们将是常量。如果此段允许移动,将包含includine public boolean moveAllowed(ChessSquare square)的几种方法,它们返回true。
  • ChessSquare类:它知道自己的具有getter和setter的等级和文件(int字段),并且具有ChessPiece字段,如果正方形为空,则为null;否则为ChessPiece。
  • 国际象棋棋盘:拥有一个国际象棋棋盘格
  • ChessPlayer类:代表两个玩家之一。它有一个ChessColor字段,一个List<ChessPiece> capturedPieces,用于进行移动,提议平局,投降的方法...
  • ChessGame类:该程序可以启动所有内容并控制游戏流程。进行此操作的玩家将向游戏对象提出要进行有效性检查的移动,如果有效,则进行该移动并检查该移动的游戏结果,包括捕获,检查,将死,...

答案 1 :(得分:0)

您应该使用computer style chess notation来回答这个正则表达式"^[a-h][1-8][a-h][1-8]$"所代表的四个字符的问题:例如,e1g1表示怀特城堡为白宫,{{1 }}表示黑城堡位于皇后区。

我们使用此e8c8是因为它易于从数字来回转换。

还要注意,computer style chess notation的动作需要en passant

相关问题