Commit abd06e14 authored by lynn's avatar lynn
Browse files

Merge remote-tracking branch 'origin/main'

parents 4a7480a5 c49902bf
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -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()];

+10 −31
Original line number Diff line number Diff line
@@ -2,7 +2,6 @@ package controller;


import chessComponent.ChessComponent;
import chessComponent.EmptySlotComponent;
import chessComponent.SquareComponent;
import model.ChessColor;
import view.ChessGameFrame;
@@ -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;
@@ -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();
@@ -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;
@@ -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()));

src/model/History.java

0 → 100644
+4 −0
Original line number Diff line number Diff line
package model;

public class History {
}