Commit 0e123c7b authored by 刘家荣's avatar 刘家荣 💬
Browse files

fix(CheckChessLogic -> CheckRule): adopted code & add linear calculations of GridPoint

parent c6a775eb
Loading
Loading
Loading
Loading
+0 −64
Original line number Diff line number Diff line
package controller;

import model.chess.Chess;
import model.chess.EmptySlot;
import model.chess.Square;
import model.ChessType;

public class CheckChessLogic  {

    /* path 和 军衔 判定分两个method
     * 则需要两个return同时为true*/

    // 对吃棋时的 path 判定
    // todo : optimize codes below
    public static boolean checkPath(Chess handleChess, Square destination) {
        boolean check = false;
        if (handleChess.getChessType() != ChessType.CANNON) {
            check = ((Math.abs(handleChess.getX() - destination.getX()) == 1 && Math.abs(handleChess.getY() - destination.getY()) == 0) ||
                    (Math.abs(handleChess.getX() - destination.getX()) == 0 && Math.abs(handleChess.getY() - destination.getY()) == 1));
        } else {
            if (handleChess.getY() == destination.getY()) {
                int count = 0;
                for (int i = Math.min(handleChess.getX(), destination.getX()); i < Math.max(handleChess.getX(), destination.getX()); i++) {
                    if (!(chessboard[i][handleChess.getY()] instanceof EmptySlot)) {
                        count++;
                    }
                }
                check = count == 0;
            } else if (handleChess.getX() == destination.getX()) {
                int count = 0;
                for (int i = Math.min(handleChess.getY(), destination.getY()); i < Math.max(handleChess.getY(), destination.getY()); i++) {
                    if (!(chessboard[handleChess.getX()][i] instanceof EmptySlot)) {
                        count++;
                    }
                }
                check = count == 0;
            }
        }
        return check;
        return true;
    }

    /* handleChess 是所执棋子
     * destinationChess 是第二个选中棋子,即被吃的棋子
     * 此处利用 enum 索引,以 <= 判断吃棋是否合规 */
    public static boolean checkRank(Chess handleChess, Chess destinationChess) {
        boolean check = false;
        //已翻转的情况
        //todo: check and delete '!handleChess.isReversal()'
        if ( !destinationChess.isReversal()) {
            // handleChess == SOLDIER && destinationChess == CANNON
            if (handleChess.getChessType() == ChessType.SOLDIER && destinationChess.getChessType() == ChessType.CANNON) {
                check = true;
            } else {
//                check = handleChess.getChessType().ordinal() <= destinationChess.getChessType().ordinal();
                check = (destinationChess.getChessType().compareTo(handleChess.getChessType()) >= 0);
            }
        } else if (destinationChess.isReversal() && handleChess.getChessType() == ChessType.CANNON) {
            check = true;
        }
        return check;
    }

}
+63 −0
Original line number Diff line number Diff line
package controller;

import model.ChessType;
import model.Chessboard;
import model.GridPoint;
import model.chess.Chess;
import model.chess.Square;

public class CheckRule {

    /* path 和 军衔 判定分两个method
     * 则需要两个return同时为true*/

    /**
     * 对吃棋时的 path 判定
     *
     * @param handleChess 所执棋子
     * @param dstSquare   第二个选中棋子,即被吃的棋子
     */
    public static boolean checkPath(Chess handleChess, Square dstSquare) {
        GridPoint src = handleChess.getGridPoint();
        GridPoint dst = dstSquare.getGridPoint();
        GridPoint move = dst.subtract(src);
        if (handleChess.getChessType() != ChessType.CANNON) {
            return Math.abs(move.getY()) + Math.abs(move.getX()) == 1;
        } else {
            int count = 0;
            if (move.getY() == 0) {
                for (int i = Math.min(src.getX(), dst.getX()) + 1; i < Math.max(src.getX(), dst.getX()); i++) {
                    if (Chessboard.getInstance().getSquareAtPoint(new GridPoint(src.getY(), i)) instanceof Chess) {
                        count++;
                    }
                }
            } else if (move.getX() == 0) {
                for (int i = Math.min(src.getY(), dst.getY()) + 1; i < Math.max(src.getY(), dst.getY()); i++) {
                    if (Chessboard.getInstance().getSquareAtPoint(new GridPoint(i, src.getX())) instanceof Chess) {
                        count++;
                    }
                }
            }
            return count == 1;
        }
    }

    /**
     * @param handleChess 所执棋子
     * @param dstChess    第二个选中棋子,即被吃的棋子
     *                    此处利用 enum 的 compareTo 方法 判断吃棋是否合规
     */
    public static boolean checkRank(Chess handleChess, Chess dstChess) {
        //已翻转的情况
        if (dstChess.isReversal()) {
            return handleChess.getChessColor() != dstChess.getChessColor() && (
                    (handleChess.getChessType() == ChessType.SOLDIER &&
                            dstChess.getChessType() == ChessType.CANNON) ||
                            dstChess.getChessType().compareTo(handleChess.getChessType()) >= 0
            );
        } else {
            return (handleChess.getChessType() == ChessType.CANNON);
        }
    }

}

src/controller/ChessLogic.java

deleted100644 → 0
+0 −20
Original line number Diff line number Diff line
package controller;

import model.chess.Chess;

public class ChessLogic  {
        private final int step = 1;

        /* path 和 军衔 判定分两个method
         * 则需要两个return同时为true*/

    public boolean checkStep (int Chess1X, int Chess1Y, int Chess2X, int Chess2Y) {
        boolean check = (Math.abs(Chess1X - Chess2X) == step && Math.abs(Chess1Y - Chess2Y) == 0) ||
                    (Math.abs(Chess1X - Chess2X) == 0 && Math.abs(Chess1Y - Chess2Y) == 1);
            //非cannon的path判定
        return check;
    }
    public boolean checkLaw(Chess chessComponent1, Chess chessComponent2){
        return true;
    }
}
+13 −1
Original line number Diff line number Diff line
@@ -24,6 +24,18 @@ public class GridPoint {
        return x;
    }

    public GridPoint subtract(GridPoint rhs){
        return new GridPoint(this.getY() - rhs.getY(), this.getX() - rhs.getX());
    }

    public GridPoint add(GridPoint rhs){
        return new GridPoint(this.getY() + rhs.getY(), this.getX() + rhs.getX());
    }

    public GridPoint multiply(int k){
        return new GridPoint(k * this.getY(), k * this.getX());
    }

    @Override
    public String toString() {
        return String.format("GridPoint{y: %d, x: %d}", y, x);
+7 −9
Original line number Diff line number Diff line
package model.chess;

import controller.CheckChessLogic;
import controller.CheckRule;
import model.ChessColor;
import model.ChessType;
import model.Chessboard;
@@ -62,19 +62,17 @@ public class Chess extends Square {

    /**
     * @param handleChess 当前选择的棋子
     * @param destination 目标位置,如(0, 0), (0, 1)等等
     * @param dst 目标位置,如(0, 0), (0, 1)等等
     * @return this棋子对象的移动规则和当前位置(chessboardPoint)能否到达目标位置
     * <br>
     * 这个方法主要是检查移动的合法性,如果合法就返回true,反之是false。
     */
    public static boolean canMoveTo(Chess handleChess, GridPoint destination) {
        Square destinationSquareChess = Chessboard.getInstance().getSquareAtPoint(destination);
        // downcast
        //TODO: Obviously there may cause a bug. Any downcast should be checked whether it can be down-casted.
        Chess destinationChess = (Chess) destinationSquareChess;
    public static boolean canMoveTo(Chess handleChess, GridPoint dst) {
        Square dstSquare = Chessboard.getInstance().getSquareAtPoint(dst);
        //此处判断 handleChess 是否翻转
        return handleChess.isReversal &&
                CheckChessLogic.checkPath(handleChess,destinationSquareChess) &&
                CheckChessLogic.checkRank(handleChess,destinationChess);
                CheckRule.checkPath(handleChess,dstSquare) &&
                (!(dstSquare instanceof Chess dstChess) ||
                        CheckRule.checkRank(handleChess,dstChess));
    }
}