Loading src/chessComponent/SquareComponent.java +2 −0 Changes for src/chessComponent/SquareComponent.java: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -112,6 +112,8 @@ public abstract class SquareComponent extends JComponent { * 这个方法主要是检查移动的合法性,如果合法就返回true,反之是false。 */ //todo: Override this method for Cannon //todo: add color detect //todo: move it to ChessComponent public boolean canMoveTo(SquareComponent[][] chessboard, ChessboardPoint destination) { SquareComponent destinationChess = chessboard[destination.getX()][destination.getY()]; Loading src/controller/ClickController.java +10 −31 Changes for src/controller/ClickController.java: 10 added lines, 31 removed lines. Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ package controller; import chessComponent.ChessComponent; import chessComponent.EmptySlotComponent; import chessComponent.SquareComponent; import model.ChessColor; import view.ChessGameFrame; Loading @@ -10,7 +9,7 @@ import view.Chessboard; public class ClickController { private final Chessboard chessboard; private SquareComponent first; private ChessComponent first = null; public ClickController(Chessboard chessboard) { this.chessboard = chessboard; Loading @@ -19,18 +18,17 @@ public class ClickController { public void onClick(SquareComponent squareComponent) { //判断第一次点击 if (first == null) { if (handleFirst(squareComponent)) { squareComponent.setSelected(true); first = squareComponent; if (squareComponent instanceof ChessComponent chessComponent && handleFirst(chessComponent)) { chessComponent.setSelected(true); first = chessComponent; first.repaint(); } } else { if (first == squareComponent) { // 再次点击取消选取 squareComponent.setSelected(false); SquareComponent recordFirst = first; first.repaint(); first = null; recordFirst.repaint(); } else if (handleSecond(squareComponent)) { } else if (first.canMoveTo(chessboard.getChessComponents(), squareComponent.getChessboardPoint())) { //repaint in swap chess method. chessboard.swapChessComponents(first, squareComponent); chessboard.clickController.swapPlayer(); Loading @@ -43,17 +41,15 @@ public class ClickController { /** * @param squareComponent 目标选取的棋子 * @param chessComponent 目标选取的棋子 * @return 目标选取的棋子是否与棋盘记录的当前行棋方颜色相同 */ private boolean handleFirst(SquareComponent squareComponent) { if (!(squareComponent instanceof ChessComponent chessComponent)) { return false; } private boolean handleFirst(ChessComponent chessComponent) { if (!chessComponent.isReversal()) { chessComponent.setReversal(true); System.out.printf("onClick to reverse a chess [%d,%d]\n", chessComponent.getChessboardPoint().getX(), chessComponent.getChessboardPoint().getY()); System.out.printf("onClick to reverse a chess [%d,%d]\n", chessComponent.getChessboardPoint().getX(), chessComponent.getChessboardPoint().getY()); chessComponent.repaint(); chessboard.clickController.swapPlayer(); return false; Loading @@ -61,23 +57,6 @@ public class ClickController { return chessComponent.getChessColor() == chessboard.getCurrentColor(); } /** * @param squareComponent first棋子目标移动到的棋子second * @return first棋子是否能够移动到second棋子位置 */ private boolean handleSecond(SquareComponent squareComponent) { //没翻开或空棋子,进入if if (!squareComponent.isReversal()) { //没翻开且非空棋子不能走 if (!(squareComponent instanceof EmptySlotComponent)) { return false; } } return squareComponent.getChessColor() != chessboard.getCurrentColor() && first.canMoveTo(chessboard.getChessComponents(), squareComponent.getChessboardPoint()); } public void swapPlayer() { chessboard.setCurrentColor(chessboard.getCurrentColor() == ChessColor.BLACK ? ChessColor.RED : ChessColor.BLACK); ChessGameFrame.getStatusLabel().setText(String.format("%s's TURN", chessboard.getCurrentColor().getName())); Loading src/model/History.java 0 → 100644 +4 −0 Changes for src/model/History.java: 4 added lines, 0 removed lines. Original line number Diff line number Diff line package model; public class History { } Loading
src/chessComponent/SquareComponent.java +2 −0 Changes for src/chessComponent/SquareComponent.java: 2 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -112,6 +112,8 @@ public abstract class SquareComponent extends JComponent { * 这个方法主要是检查移动的合法性,如果合法就返回true,反之是false。 */ //todo: Override this method for Cannon //todo: add color detect //todo: move it to ChessComponent public boolean canMoveTo(SquareComponent[][] chessboard, ChessboardPoint destination) { SquareComponent destinationChess = chessboard[destination.getX()][destination.getY()]; Loading
src/controller/ClickController.java +10 −31 Changes for src/controller/ClickController.java: 10 added lines, 31 removed lines. Original line number Diff line number Diff line Loading @@ -2,7 +2,6 @@ package controller; import chessComponent.ChessComponent; import chessComponent.EmptySlotComponent; import chessComponent.SquareComponent; import model.ChessColor; import view.ChessGameFrame; Loading @@ -10,7 +9,7 @@ import view.Chessboard; public class ClickController { private final Chessboard chessboard; private SquareComponent first; private ChessComponent first = null; public ClickController(Chessboard chessboard) { this.chessboard = chessboard; Loading @@ -19,18 +18,17 @@ public class ClickController { public void onClick(SquareComponent squareComponent) { //判断第一次点击 if (first == null) { if (handleFirst(squareComponent)) { squareComponent.setSelected(true); first = squareComponent; if (squareComponent instanceof ChessComponent chessComponent && handleFirst(chessComponent)) { chessComponent.setSelected(true); first = chessComponent; first.repaint(); } } else { if (first == squareComponent) { // 再次点击取消选取 squareComponent.setSelected(false); SquareComponent recordFirst = first; first.repaint(); first = null; recordFirst.repaint(); } else if (handleSecond(squareComponent)) { } else if (first.canMoveTo(chessboard.getChessComponents(), squareComponent.getChessboardPoint())) { //repaint in swap chess method. chessboard.swapChessComponents(first, squareComponent); chessboard.clickController.swapPlayer(); Loading @@ -43,17 +41,15 @@ public class ClickController { /** * @param squareComponent 目标选取的棋子 * @param chessComponent 目标选取的棋子 * @return 目标选取的棋子是否与棋盘记录的当前行棋方颜色相同 */ private boolean handleFirst(SquareComponent squareComponent) { if (!(squareComponent instanceof ChessComponent chessComponent)) { return false; } private boolean handleFirst(ChessComponent chessComponent) { if (!chessComponent.isReversal()) { chessComponent.setReversal(true); System.out.printf("onClick to reverse a chess [%d,%d]\n", chessComponent.getChessboardPoint().getX(), chessComponent.getChessboardPoint().getY()); System.out.printf("onClick to reverse a chess [%d,%d]\n", chessComponent.getChessboardPoint().getX(), chessComponent.getChessboardPoint().getY()); chessComponent.repaint(); chessboard.clickController.swapPlayer(); return false; Loading @@ -61,23 +57,6 @@ public class ClickController { return chessComponent.getChessColor() == chessboard.getCurrentColor(); } /** * @param squareComponent first棋子目标移动到的棋子second * @return first棋子是否能够移动到second棋子位置 */ private boolean handleSecond(SquareComponent squareComponent) { //没翻开或空棋子,进入if if (!squareComponent.isReversal()) { //没翻开且非空棋子不能走 if (!(squareComponent instanceof EmptySlotComponent)) { return false; } } return squareComponent.getChessColor() != chessboard.getCurrentColor() && first.canMoveTo(chessboard.getChessComponents(), squareComponent.getChessboardPoint()); } public void swapPlayer() { chessboard.setCurrentColor(chessboard.getCurrentColor() == ChessColor.BLACK ? ChessColor.RED : ChessColor.BLACK); ChessGameFrame.getStatusLabel().setText(String.format("%s's TURN", chessboard.getCurrentColor().getName())); Loading
src/model/History.java 0 → 100644 +4 −0 Changes for src/model/History.java: 4 added lines, 0 removed lines. Original line number Diff line number Diff line package model; public class History { }