Commit 48b502fe authored by lynn's avatar lynn
Browse files

partly completed and moved canMoveTo.

parent abd06e14
Loading
Loading
Loading
Loading
+1 −4
Original line number Diff line number Diff line
package chessComponent;

import controller.ClickController;
import model.ChessColor;
import model.ChessboardPoint;

import javax.swing.*;
@@ -45,8 +44,6 @@ public abstract class SquareComponent extends JComponent {
        this.clickController = clickController;
        this.isReversal = false;
    }
    // a void constructor
//   public SquareComponent(){}

    public boolean isReversal() {
        return isReversal;
@@ -115,8 +112,8 @@ public abstract class SquareComponent extends JComponent {
    //todo: add color detect
    //todo: move it to ChessComponent
    public boolean canMoveTo(SquareComponent[][] chessboard, ChessboardPoint destination) {
        //                                                              目的地
        SquareComponent destinationChess = chessboard[destination.getX()][destination.getY()];

        return destinationChess.isReversal|| destinationChess instanceof EmptySlotComponent;
        //todo: complete this method
    }
+66 −0
Original line number Diff line number Diff line
package controller;

import chessComponent.ChessComponent;
import chessComponent.EmptySlotComponent;
import chessComponent.SquareComponent;
import model.ChessType;
import view.Chessboard;

public class CheckChessLogic  {

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

    // todo: 1.change chess1x into chessLocation?
    //       2.override for CANNON
    // 对吃棋时的 path 判定
    // todo : optimize codes below
    public static boolean checkPath(ChessComponent handleChess, SquareComponent[][] chessboard, SquareComponent 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 EmptySlotComponent)) {
                        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 EmptySlotComponent)) {
                        count++;
                    }
                }
                check = count == 0;
            }
        }
        return check;
    }

    /* chess1 是第一个选中棋子
     * chess2 是第二个选中棋子,即被吃的棋子
     * 此处顺应 enum 索引,以 <= 判断吃棋是否合规*/
    // TODO: consider the situation when chess2 is not revealed
    public static boolean checkRank(ChessComponent chess1, ChessComponent chess2) {
        boolean check = false;
        //已翻转的情况
        //todo: check and delete '!chess1.isReversal()'
        if (!chess1.isReversal() && !chess2.isReversal()) {
            // chess1 == SOLDIER && chess2 == CANNON
            if (chess1.getChessType() == ChessType.SOLDIER && chess2.getChessType() == ChessType.CANNON) {
                check = true;
            } else {
                check = chess1.getChessType().ordinal() <= chess2.getChessType().ordinal() ? true : false;
            }
        } else if (chess2.isReversal() && chess1.getChessType() == ChessType.CANNON) {
            check = true;
        }
        return check;
    }

}
+1 −0
Original line number Diff line number Diff line
@@ -31,6 +31,7 @@ public class ClickController {
            } else if (first.canMoveTo(chessboard.getChessComponents(), squareComponent.getChessboardPoint())) {
                //repaint in swap chess method.
                chessboard.swapChessComponents(first, squareComponent);
                // first 是被移动的棋子,squareComponent 是 destination,
                chessboard.clickController.swapPlayer();

                first.setSelected(false);
+2 −1
Original line number Diff line number Diff line
package model;

public enum ChessType {
    GENERAL, ADVISOR, MINISTER, CHARIOT, HORSE, CANON, SOLDIER
    CANNON,GENERAL, ADVISOR, MINISTER, CHARIOT, HORSE, SOLDIER
//索引 0 ,    1   ,    2   ,    3   ,    4   ,   5  ,    6
}
+24 −10
Original line number Diff line number Diff line
@@ -2,8 +2,9 @@ package view;


import chessComponent.*;
import model.*;
import controller.ClickController;
import model.ChessColor;
import model.ChessboardPoint;

import javax.swing.*;
import java.awt.*;
@@ -21,20 +22,27 @@ public class Chessboard extends JComponent {
    private static final int COL_SIZE = 4;

    private final SquareComponent[][] squareComponents = new SquareComponent[ROW_SIZE][COL_SIZE];
    //todo: you can change the initial player
    private ChessColor currentColor = ChessColor.BLACK;

    private SideStack sideStacks[];

    private ChessColor currentColor = ChessColor.RED;

    //all chessComponents in this chessboard are shared only one model controller
    public final ClickController clickController = new ClickController(this);
    private final int CHESS_SIZE;


    public Chessboard(int width, int height) {
    public Chessboard(int mainWidth, int sideWidth, int height) {
        setLayout(null); // Use absolute layout.
        setSize(width + 2, height);
        setSize(mainWidth + sideWidth, height);
        CHESS_SIZE = (height - 6) / 8;
        SquareComponent.setSpacingLength(CHESS_SIZE / 12);
        System.out.printf("chessboard [%d * %d], chess size = %d\n", width, height, CHESS_SIZE);
        int sideHeight = sideWidth * SideStack.ROW_SIZE / SideStack.COL_SIZE;
        this.sideStacks = new SideStack[]{
                new SideStack(new Point(this.getX() + mainWidth, this.getY()), sideWidth, sideHeight),
                new SideStack(new Point(this.getX() + mainWidth, this.getY() + height - sideHeight), sideWidth, sideHeight)
        };
        System.out.printf("chessboard [(%d + %d) * %d], chess size = %d\n", mainWidth, sideWidth, height, CHESS_SIZE);

        initAllChessOnBoard();
    }
@@ -53,6 +61,7 @@ public class Chessboard extends JComponent {

    /**
     * 将SquareComponent 放置在 ChessBoard上。里面包含移除原有的component及放置新的component
     *
     * @param squareComponent
     */
    public void putChessOnBoard(SquareComponent squareComponent) {
@@ -65,13 +74,15 @@ public class Chessboard extends JComponent {

    /**
     * 交换chess1 chess2的位置
     *
     * @param chess1
     * @param chess2
     */
    public void swapChessComponents(SquareComponent chess1, SquareComponent chess2) {
    public void swapChessComponents(ChessComponent chess1, SquareComponent chess2) {
        // Note that chess1 has higher priority, 'destroys' chess2 if exists.
        if (!(chess2 instanceof EmptySlotComponent)) {
        if (chess2 instanceof ChessComponent chess20) {
            remove(chess2);
            sideStacks[chess20.getChessColor().ordinal()].addChess(chess20);
            add(chess2 = new EmptySlotComponent(chess2.getChessboardPoint(), chess2.getLocation(), clickController, CHESS_SIZE));
        }
        chess1.swapLocation(chess2);
@@ -94,9 +105,9 @@ public class Chessboard extends JComponent {
                ChessColor color = random.nextInt(2) == 0 ? ChessColor.RED : ChessColor.BLACK;
                SquareComponent squareComponent;
                if (random.nextInt(2) == 0) {
                    squareComponent = new AllChessComponents(new ChessboardPoint(i, j), calculatePoint(i, j), color, clickController, CHESS_SIZE);
                    squareComponent = new ChariotChessComponent(new ChessboardPoint(i, j), calculatePoint(i, j), color, clickController, CHESS_SIZE);
                } else {
                    squareComponent = new AllChessComponents(new ChessboardPoint(i, j), calculatePoint(i, j), color, clickController, CHESS_SIZE);
                    squareComponent = new SoldierChessComponent(new ChessboardPoint(i, j), calculatePoint(i, j), color, clickController, CHESS_SIZE);
                }
                squareComponent.setVisible(true);
                putChessOnBoard(squareComponent);
@@ -107,6 +118,7 @@ public class Chessboard extends JComponent {

    /**
     * 绘制棋盘格子
     *
     * @param g
     */
    @Override
@@ -118,6 +130,7 @@ public class Chessboard extends JComponent {

    /**
     * 将棋盘上行列坐标映射成Swing组件的Point
     *
     * @param row 棋盘上的行
     * @param col 棋盘上的列
     * @return
@@ -128,6 +141,7 @@ public class Chessboard extends JComponent {

    /**
     * 通过GameController调用该方法
     *
     * @param chessData
     */
    public void loadGame(List<String> chessData) {