Loading src/model/game/CheckRule.java +48 −9 Changes for src/model/game/CheckRule.java: 48 added lines, 9 removed lines. Original line number Diff line number Diff line Loading @@ -5,6 +5,9 @@ import model.dataType.ChessType; import model.dataType.GridPoint; import model.dataType.Site; import java.util.ArrayList; import java.util.List; public class CheckRule { /** Loading Loading @@ -34,6 +37,7 @@ public class CheckRule { Site src = handleChess.getLocation(); Site dst = dstSquare.getLocation(); GridPoint move = dst.subtract(src); Chessboard chessboard = Game.instance.chessboard; //是否都在主棋盘 if(src.plate != ChessColor.NONE || dst.plate != ChessColor.NONE) return false; Loading @@ -42,18 +46,18 @@ public class CheckRule { return Math.abs(move.getY()) + Math.abs(move.getX()) == 1; } else { //炮 if(!(Chessboard.getInstance().getSquareAtPoint(dst) instanceof Chess)) if(!(chessboard.getSquareAtPoint(dst) instanceof Chess)) return false; 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 Site(ChessColor.NONE, src.getY(), i)) instanceof Chess) { if (chessboard.getSquareAtPoint(new Site(ChessColor.NONE, 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 Site(ChessColor.NONE, i, src.getX())) instanceof Chess) { if (chessboard.getSquareAtPoint(new Site(ChessColor.NONE, i, src.getX())) instanceof Chess) { count++; } } Loading Loading @@ -82,11 +86,46 @@ public class CheckRule { } } // // public static boolean[] mobility(Chess chess){ // if (chess.chessType != ChessType.CANNON){ // chess.chessType // } // } public static List<GridPoint> searchDstList(Chess chess){ return searchDstList(Game.instance.chessboard, chess); } public static List<GridPoint> searchDstList(Chessboard chessboard, Chess chess){ List<GridPoint> dsts = new ArrayList<>(); if (chess.chessType != ChessType.CANNON){ if(chess.site.y > 0 && canMoveTo(chess, chessboard.getSquareAtPoint(chess.site.add(new GridPoint(-1,0))))) { dsts.add(chess.site.add(new GridPoint(-1, 0))); } if(chess.site.y < Chessboard.ROW_SIZE - 1 && canMoveTo(chess, chessboard.getSquareAtPoint(chess.site.add(new GridPoint(+1,0))))) { dsts.add(chess.site.add(new GridPoint(+1, 0))); } if(chess.site.x > 0 && canMoveTo(chess, chessboard.getSquareAtPoint(chess.site.add(new GridPoint(0,-1))))) { dsts.add(chess.site.add(new GridPoint(-1, 0))); } if(chess.site.x < Chessboard.COL_SIZE - 1 && canMoveTo(chess, chessboard.getSquareAtPoint(chess.site.add(new GridPoint(0,+1))))) { dsts.add(chess.site.add(new GridPoint(-1, 0))); } }else { Site dst; for (int i = 0; i < Chessboard.ROW_SIZE; i++) { dst = new Site(chess.site.plate, i, chess.site.x); if(i != chess.site.y && canMoveTo(chess, chessboard.getSquareAtPoint(dst))) { dsts.add(dst); } } for (int i = 0; i < Chessboard.COL_SIZE; i++) { dst = new Site(chess.site.plate, chess.site.y, i); if(i != chess.site.x && canMoveTo(chess, chessboard.getSquareAtPoint(dst))) { dsts.add(dst); } } } return dsts; } } Loading
src/model/game/CheckRule.java +48 −9 Changes for src/model/game/CheckRule.java: 48 added lines, 9 removed lines. Original line number Diff line number Diff line Loading @@ -5,6 +5,9 @@ import model.dataType.ChessType; import model.dataType.GridPoint; import model.dataType.Site; import java.util.ArrayList; import java.util.List; public class CheckRule { /** Loading Loading @@ -34,6 +37,7 @@ public class CheckRule { Site src = handleChess.getLocation(); Site dst = dstSquare.getLocation(); GridPoint move = dst.subtract(src); Chessboard chessboard = Game.instance.chessboard; //是否都在主棋盘 if(src.plate != ChessColor.NONE || dst.plate != ChessColor.NONE) return false; Loading @@ -42,18 +46,18 @@ public class CheckRule { return Math.abs(move.getY()) + Math.abs(move.getX()) == 1; } else { //炮 if(!(Chessboard.getInstance().getSquareAtPoint(dst) instanceof Chess)) if(!(chessboard.getSquareAtPoint(dst) instanceof Chess)) return false; 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 Site(ChessColor.NONE, src.getY(), i)) instanceof Chess) { if (chessboard.getSquareAtPoint(new Site(ChessColor.NONE, 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 Site(ChessColor.NONE, i, src.getX())) instanceof Chess) { if (chessboard.getSquareAtPoint(new Site(ChessColor.NONE, i, src.getX())) instanceof Chess) { count++; } } Loading Loading @@ -82,11 +86,46 @@ public class CheckRule { } } // // public static boolean[] mobility(Chess chess){ // if (chess.chessType != ChessType.CANNON){ // chess.chessType // } // } public static List<GridPoint> searchDstList(Chess chess){ return searchDstList(Game.instance.chessboard, chess); } public static List<GridPoint> searchDstList(Chessboard chessboard, Chess chess){ List<GridPoint> dsts = new ArrayList<>(); if (chess.chessType != ChessType.CANNON){ if(chess.site.y > 0 && canMoveTo(chess, chessboard.getSquareAtPoint(chess.site.add(new GridPoint(-1,0))))) { dsts.add(chess.site.add(new GridPoint(-1, 0))); } if(chess.site.y < Chessboard.ROW_SIZE - 1 && canMoveTo(chess, chessboard.getSquareAtPoint(chess.site.add(new GridPoint(+1,0))))) { dsts.add(chess.site.add(new GridPoint(+1, 0))); } if(chess.site.x > 0 && canMoveTo(chess, chessboard.getSquareAtPoint(chess.site.add(new GridPoint(0,-1))))) { dsts.add(chess.site.add(new GridPoint(-1, 0))); } if(chess.site.x < Chessboard.COL_SIZE - 1 && canMoveTo(chess, chessboard.getSquareAtPoint(chess.site.add(new GridPoint(0,+1))))) { dsts.add(chess.site.add(new GridPoint(-1, 0))); } }else { Site dst; for (int i = 0; i < Chessboard.ROW_SIZE; i++) { dst = new Site(chess.site.plate, i, chess.site.x); if(i != chess.site.y && canMoveTo(chess, chessboard.getSquareAtPoint(dst))) { dsts.add(dst); } } for (int i = 0; i < Chessboard.COL_SIZE; i++) { dst = new Site(chess.site.plate, chess.site.y, i); if(i != chess.site.x && canMoveTo(chess, chessboard.getSquareAtPoint(dst))) { dsts.add(dst); } } } return dsts; } }