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

fix random init, fix SideStack, complete comments, rename GridPoint, Clean up codes

parent f38553be
Loading
Loading
Loading
Loading
+19 −8
Original line number Diff line number Diff line
@@ -3,18 +3,31 @@ package controller;

import model.ChessColor;
import model.Chessboard;
import model.ChessboardPoint;
import model.GridPoint;
import model.chess.Chess;
import model.chess.Square;
import view.ChessGameFrame;
import view.ChessboardCom;
import view.SquareCom;

/**
 * 响应click事件,棋盘的交互逻辑
 */
public enum ClickController {
    /**
     * 单例
     */
    INSTANCE;

    /**
     * 记忆两次点击中第一次点击选中的棋子
     */
    private Chess first = null;

    //TODO: Send to frontend
    /**
     * 响应click事件,棋盘的交互逻辑
     * @param squareCom 点中的方格
     */
    public void onClick(SquareCom squareCom) {
        Square square = squareCom.getBackSquare();

@@ -29,15 +42,13 @@ public enum ClickController {
                first.setSelected(false);
                squareCom.repaint();
                first = null;
            } else if (Chess.canMoveTo(first, square.getChessboardPoint())) {
                ChessboardPoint firstPoint = first.getChessboardPoint();
                ChessboardPoint secondPoint = square.getChessboardPoint();
            } else if (Chess.canMoveTo(first, square.getGridPoint())) {
                //取消选择
                first.setSelected(false);
                //走吃
                Chessboard.getInstance().moveAndEat(first, square);
                if (square instanceof Chess chess)
                    ChessboardCom.getInstance().getSideStacks()[chess.getChessColor().ordinal()].repaint();
                    ChessboardCom.getInstance().getSideStackComs()[chess.getChessColor().ordinal()].repaint();
                ClickController.INSTANCE.swapPlayer();
                //擦除记忆
                first = null;
@@ -54,8 +65,8 @@ public enum ClickController {
        if (!chess.isReversal()) {
            chess.setReversal(true);
            System.out.printf("onClick to reverse a chess at %s\n",
                    chess.getChessboardPoint());
            ChessboardCom.getInstance().getSquareComAtPoint(chess.getChessboardPoint()).repaint();
                    chess.getGridPoint());
            ChessboardCom.getInstance().getSquareComAtPoint(chess.getGridPoint()).repaint();
            ClickController.INSTANCE.swapPlayer();
            return false;
        }
+1 −2
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ package model;
import java.awt.*;

/**
 * 这个类主要用于包装Color对象,用于Chess游戏使用。
 * 颜色,但是是特指阵营颜色
 */
public enum ChessColor {
     BLACK("Black", Color.BLACK), RED("Red", Color.RED), NONE("No Player", Color.WHITE);
@@ -19,7 +19,6 @@ public enum ChessColor {
    public String getName() {
        return name;
    }

    public Color getColor() {
        return color;
    }
+23 −28
Original line number Diff line number Diff line
@@ -7,13 +7,14 @@ import model.chess.Square;
import io.vavr.Tuple2;
import view.ChessboardCom;

import java.io.Serializable;
import java.util.*;

/**
 * 这个类表示棋盘组建,其包含:
 * Square[][]: 4*8个方块格子组件
 */
public class Chessboard{
public class Chessboard implements Serializable {
    /**
     * 不彻底的单例
     */
@@ -32,6 +33,9 @@ public class Chessboard{

    private ChessColor currentColor = ChessColor.RED;   //执棋颜色

    /**
     * 初始化整个棋盘,包括所有棋子和亡子堆
     */
    public Chessboard() {
        Random random = new Random();
        //此列表的元素是元组,每个元组存储的是对应棋子的部分参数(颜色和类别)
@@ -57,7 +61,7 @@ public class Chessboard{
            for (int j = 0; j < squares[i].length; j++) {
                Chess chess;
                Tuple2<ChessColor, ChessType> params = paramsList.get(paramsList.size() - 1);
                chess = new Chess(new ChessboardPoint(i, j), params._1, params._2);
                chess = new Chess(new GridPoint(i, j), params._1, params._2);
                putChessOnBoard(chess);
                paramsList.remove(paramsList.size() - 1);
            }
@@ -77,16 +81,14 @@ public class Chessboard{

    /**
     * 获取棋盘中给定坐标的方格子
     * @param chessboardPoint
     * @return Square 方格子
     * @param gridPoint 棋盘坐标
     */
    public Square getSquareAtPoint(ChessboardPoint chessboardPoint) {
        return squares[chessboardPoint.getY()][chessboardPoint.getX()];
    public Square getSquareAtPoint(GridPoint gridPoint) {
        return squares[gridPoint.getY()][gridPoint.getX()];
    }

    /**
     * 获取亡子堆组
     * @return
     */
    public SideStack[] getSideStacks() {
        return sideStacks;
@@ -94,8 +96,7 @@ public class Chessboard{

    /**
     * 获取某颜色的亡子堆
     * @param chessColor
     * @return
     * @param chessColor 某颜色
     */
    public SideStack getSideStack(ChessColor chessColor) {
        for (SideStack sideStack : sideStacks) {
@@ -108,54 +109,48 @@ public class Chessboard{
    public ChessColor getCurrentColor() {
        return currentColor;
    }

    public void setCurrentColor(ChessColor currentColor) {
        this.currentColor = currentColor;
    }

    /**
     * 将SquareComponent 放置在 ChessBoard上。里面包含移除原有的component及放置新的component
     *
     * @param square
     * 根据Square存储的位置数据,将Square放置在棋盘的数组上
     */
    public void putChessOnBoard(Square square) {
        int row = square.getChessboardPoint().getY(), col = square.getChessboardPoint().getX();
        int row = square.getGridPoint().getY(), col = square.getGridPoint().getX();
        squares[row][col] = square;
    }

    /**
     * 移动选中棋子到目标位置上,而使目标位置的棋子(如果有)被吃
     *
     * @param chess1
     * @param square2
     * 交换高亮的棋子和目的地方格,使棋子移过去,同时吃掉目的地的棋子
     * @param chess1 移动的棋子
     * @param square2 目的地方格
     */
    //TODO: Send to FrontEnd
    public void moveAndEat(Chess chess1, Square square2) {
        //Swap ChessBoardPoint members in squares
        ChessboardPoint chessboardPointTmp = chess1.getChessboardPoint();
        chess1.setChessboardPoint(square2.getChessboardPoint());
        square2.setChessboardPoint(chessboardPointTmp);
        GridPoint gridPointTmp = chess1.getGridPoint();
        chess1.setGridPoint(square2.getGridPoint());
        square2.setGridPoint(gridPointTmp);

        // Note that chess1 has higher priority, 'destroys' chess2 if exists.
        if (square2 instanceof Chess chess2) {
            this.getSideStack(chess2.getChessColor()).addChess(chess2);
            square2 = new EmptySlot(chessboardPointTmp);
            square2 = new EmptySlot(gridPointTmp);
        }

        //Set squares' references
        int row1 = chess1.getChessboardPoint().getY(), col1 = chess1.getChessboardPoint().getX();
        int row1 = chess1.getGridPoint().getY(), col1 = chess1.getGridPoint().getX();
        squares[row1][col1] = chess1;
        int row2 = square2.getChessboardPoint().getY(), col2 = square2.getChessboardPoint().getX();
        int row2 = square2.getGridPoint().getY(), col2 = square2.getGridPoint().getX();
        squares[row2][col2] = square2;

        //重绘
        ChessboardCom.getInstance().getSquareComAtPoint(chess1.getChessboardPoint()).repaint();
        ChessboardCom.getInstance().getSquareComAtPoint(square2.getChessboardPoint()).repaint();
        ChessboardCom.getInstance().getSquareComAtPoint(chess1.getGridPoint()).repaint();
        ChessboardCom.getInstance().getSquareComAtPoint(square2.getGridPoint()).repaint();
    }

    /**
     * 通过GameController调用该方法
     *
     * @param chessData
     */
    public void loadGame(List<String> chessData) {
+31 −0
Original line number Diff line number Diff line
package model;

/**
 * 这个类表示棋盘的位置,如(0, 0), (0, 3)等等
 * 其中,左上角是(0, 0),左下角是(7, 0),右上角是(0, 3),右下角是(7, 3)
 * 这个类表示网格中的整数坐标,可以表示棋盘的位置,也可以表示亡子堆的位置
 * 注意一般表示网格坐标时,按照二维数组的习惯是先行后列
 */
//TODO: gridPoint
public class ChessboardPoint {
public class GridPoint {
    private int x, y;

    /**
     *
     * @param x: row
     * @param y: col
     * @param y: row
     * @param x: col
     */
    public ChessboardPoint(int y, int x) {
    public GridPoint(int y, int x) {
        this.y = y;
        this.x = x;
    }

    public int getX() {
        return x;
    }

    public int getY() {
        return y;
    }

    public int getX() {
        return x;
    }

    @Override
    public String toString() {
        return String.format("ChessboardPoint{y: %d, x: %d}", y, x);
        return String.format("GridPoint{y: %d, x: %d}", y, x);
    }
}
+21 −23
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ public class SideStack {
    public SideStack(ChessColor chessColor) {
        this.chessColor = chessColor;
        this.chesses = new ArrayList<>();
        //初始化所有行,都是空行
        for (int i = 0; i < ROW_SIZE; i++) {
            chesses.add(new ArrayList<>());
        }
@@ -26,10 +27,14 @@ public class SideStack {
        return chesses;
    }

    public Chess getChessAtPoint(ChessboardPoint chessboardPoint) {
        if (this.chesses.size() > chessboardPoint.getY())
            if (this.chesses.get(chessboardPoint.getY()).size() > chessboardPoint.getX())
                return this.chesses.get(chessboardPoint.getY()).get(chessboardPoint.getX());
    /**
     * 获取某个位置的棋子
     * @param gridPoint 位置
     */
    public Chess getChessAtPoint(GridPoint gridPoint) {
        if (this.chesses.size() > gridPoint.getY())
            if (this.chesses.get(gridPoint.getY()).size() > gridPoint.getX())
                return this.chesses.get(gridPoint.getY()).get(gridPoint.getX());
        return null;
    }

@@ -37,23 +42,16 @@ public class SideStack {
        return chessColor;
    }

    public void addChess2(Chess chess) {
        if (chess.getChessColor() != this.chessColor)
            throw new IllegalArgumentException(String.format("this:%s chess:%s", this.chessColor, chess.getChessColor()));
        int coordY = chess.getChessColor() == ChessColor.BLACK ?
                6 - chess.getChessType().ordinal() : chess.getChessType().ordinal();
        chesses.get(coordY).add(chess);
        chess.kill();
        chess.setReversal(true);
        chess.setChessboardPoint(new ChessboardPoint(coordY, chesses.get(coordY).size() - 1));
        ChessboardCom.getInstance().getSideStack(chess.getChessColor())
                .getSquareComAtPoint(chess.getChessboardPoint()).repaint();
    }

    /**
     * 把棋盘中卸下的棋子加到亡子堆里面
     */
    public void addChess(Chess chess) {
        if (chess.getChessColor() != this.chessColor)
            throw new IllegalArgumentException(String.format("this:%s chess:%s", this.chessColor, chess.getChessColor()));
        //黑色,翻转
            throw new IllegalArgumentException(
                    String.format("this:%s chess:%s", this.chessColor, chess.getChessColor())
            );

        //红色的亡子堆要先把行的顺序翻转
        if(this.chessColor == ChessColor.RED)
            Collections.reverse(chesses);

@@ -70,20 +68,20 @@ public class SideStack {
        //加入目标行的末尾
        chesses.get(rowIndex).add(chess);

        //色,再翻转回去
        //色,再翻转回去
        if(this.chessColor == ChessColor.RED) {
            Collections.reverse(chesses);
            rowIndex = chesses.size() - rowIndex - 1;
        }

        //操作chess的属性使之归位
        //操作chess的数据
        chess.kill();
        chess.setReversal(true);
        chess.setChessboardPoint(new ChessboardPoint(rowIndex, chesses.get(rowIndex).size() - 1));
        chess.setGridPoint(new GridPoint(rowIndex, chesses.get(rowIndex).size() - 1));

        //重绘
        ChessboardCom.getInstance().getSideStack(chess.getChessColor())
                .getSquareComAtPoint(chess.getChessboardPoint()).repaint();
                .getSquareComAtPoint(chess.getGridPoint()).repaint();
    }

}
Loading