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

fix(SideStack)

parent 36b89b8c
Loading
Loading
Loading
Loading
+38 −19
Original line number Diff line number Diff line
@@ -7,9 +7,7 @@ import model.chess.Square;
import io.vavr.Tuple2;
import view.ChessboardCom;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.*;

/**
 * 这个类表示棋盘组建,其包含:
@@ -21,20 +19,22 @@ public class Chessboard{
     */
    private static Chessboard instance;

    public static final int ROW_SIZE = 8;
    public static final int COL_SIZE = 4;
    public static final int ROW_SIZE = 8;   //行数
    public static final int COL_SIZE = 4;   //列数

    private final Square[][] squares = new Square[ROW_SIZE][COL_SIZE];
    private final Square[][] squares = new Square[ROW_SIZE][COL_SIZE];  //方格子列表

    private SideStack[] sideStacks = new SideStack[]{
    //两个亡子堆->亡子堆组
    private final SideStack[] sideStacks = new SideStack[]{
            new SideStack(ChessColor.BLACK),
            new SideStack(ChessColor.RED)
    };

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

    public Chessboard() {
        Random random = new Random();
        //此列表的元素是元组,每个元组存储的是对应棋子的部分参数(颜色和类别)
        ArrayList<Tuple2<ChessColor, ChessType>> paramsList = new ArrayList<>();
        for (ChessColor chessColor : new ChessColor[]{ChessColor.BLACK, ChessColor.RED}) {
            for (ChessType chessType : ChessType.values()) {
@@ -48,16 +48,18 @@ public class Chessboard{
                }
            }
        }
        paramsList.sort((a, b) -> random.nextInt());
        //打乱该列表
        Collections.shuffle(paramsList);

        //每个棋子初始化的时候,从末尾拿元组作为参数,并移除那个元组,如同出栈
        for (int i = 0; i < squares.length; i++) {
            squares[i] = new Square[COL_SIZE];
            for (int j = 0; j < squares[i].length; j++) {
                Chess chess;
                Tuple2<ChessColor, ChessType> params = paramsList.get(0);
                Tuple2<ChessColor, ChessType> params = paramsList.get(paramsList.size() - 1);
                chess = new Chess(new ChessboardPoint(i, j), params._1, params._2);
                putChessOnBoard(chess);
                paramsList.remove(0);
                paramsList.remove(paramsList.size() - 1);
            }
        }
    }
@@ -65,7 +67,6 @@ public class Chessboard{
    public static Chessboard getInstance() {
        return instance;
    }

    public static void setInstance(Chessboard instance1) {
        instance = instance1;
    }
@@ -74,16 +75,34 @@ public class Chessboard{
        return squares;
    }

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

    /**
     * 获取亡子堆组
     * @return
     */
    public SideStack[] getSideStacks() {
        return sideStacks;
    }

    /**
     * 获取某颜色的亡子堆
     * @param chessColor
     * @return
     */
    public SideStack getSideStack(ChessColor chessColor) {
        return sideStacks[chessColor.ordinal()];
        for (SideStack sideStack : sideStacks) {
            if (chessColor == sideStack.getChessColor())
                return sideStack;
        }
        throw new NoSuchElementException();
    }

    public ChessColor getCurrentColor() {
@@ -112,17 +131,17 @@ public class Chessboard{
     */
    //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);

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

        //Swap ChessBoardPoints members in squares
        ChessboardPoint chessboardPointTmp = chess1.getChessboardPoint();
        chess1.setChessboardPoint(square2.getChessboardPoint());
        square2.setChessboardPoint(chessboardPointTmp);

        //Set squares' references
        int row1 = chess1.getChessboardPoint().getY(), col1 = chess1.getChessboardPoint().getX();
        squares[row1][col1] = chess1;
+43 −2
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import model.chess.Chess;
import view.ChessboardCom;

import java.util.ArrayList;
import java.util.Collections;

public class SideStack {
    public static final int ROW_SIZE = 7;
@@ -32,10 +33,14 @@ public class SideStack {
        return null;
    }

    public void addChess(Chess chess) {
    public ChessColor getChessColor() {
        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.RED ?
        int coordY = chess.getChessColor() == ChessColor.BLACK ?
                6 - chess.getChessType().ordinal() : chess.getChessType().ordinal();
        chesses.get(coordY).add(chess);
        chess.kill();
@@ -45,4 +50,40 @@ public class SideStack {
                .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()));
        //黑色,翻转
        if(this.chessColor == ChessColor.RED)
            Collections.reverse(chesses);

        //找到适当位置插入
        int rowIndex = 0;
        int rank = chess.getChessType().ordinal();
        while (chesses.get(rowIndex).size() > 0 && chesses.get(rowIndex).get(0).getChessType().ordinal() > rank)
            rowIndex++;
        //没有同级的行,插入新行,删除尾空行
        if (!(chesses.get(rowIndex).size() > 0 && chesses.get(rowIndex).get(0).getChessType().ordinal() == rank)) {
            chesses.add(rowIndex, new ArrayList<>());
            chesses.remove(chesses.size() - 1);
        }
        //加入目标行的末尾
        chesses.get(rowIndex).add(chess);

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

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

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

}
+1 −1
Original line number Diff line number Diff line
@@ -43,7 +43,7 @@ public class ChessGameFrame extends JFrame {
     * 在游戏窗体中添加棋盘
     */
    private void addChessboard() {
        ChessboardCom.setInstance(new ChessboardCom(CHESSBOARD_SIZE / 2, CHESSBOARD_SIZE / 4, CHESSBOARD_SIZE));
        ChessboardCom.setInstance(new ChessboardCom(CHESSBOARD_SIZE / 2 + 6, CHESSBOARD_SIZE / 3, CHESSBOARD_SIZE + 6));
        gameController = new GameController(Chessboard.getInstance());
        ChessboardCom.getInstance().setLocation(HEIGHT / 10, HEIGHT / 10);
        add(ChessboardCom.getInstance());
+36 −15
Original line number Diff line number Diff line
@@ -24,12 +24,34 @@ public class ChessboardCom extends JComponent {

    private SideStackCom[] sideStacks;

    private int mainWidth;

    private int sideWidth;

    public static int SQUARE_SIZE;

    public ChessboardCom(int mainWidth, int sideWidth, int height) {
        this.mainWidth = mainWidth;
        this.sideWidth = sideWidth;
        setLayout(null); // Use absolute layout.
        setSize(mainWidth + sideWidth, height);
        SQUARE_SIZE = (height - 6) / 8;
        System.out.printf("chessboard [(%d + %d) * %d], chess size = %d\n", mainWidth, sideWidth, height, SQUARE_SIZE);

        //Main Chessboard Init
        int spacingLength = SQUARE_SIZE / 12;
        SquareCom.ALIVE_CHESS_FONT = new Font("Rockwell", Font.BOLD, (int)((SQUARE_SIZE - 2 * spacingLength) * 0.55));
        for (int i = 0; i < Chessboard.ROW_SIZE; i++) {
            for (int j = 0; j < Chessboard.COL_SIZE; j++) {
                ChessboardPoint chessboardPoint = new ChessboardPoint(i, j);
                putChessOnBoard(new SquareCom(
                        chessboardPoint, evalPoint(chessboardPoint), SQUARE_SIZE, spacingLength, true
                ));
            }
        }

        //SideStack Init
        SquareCom.DEAD_CHESS_FONT = new Font("Rockwell", Font.BOLD, (int)(sideWidth / SideStack.COL_SIZE * 0.55));
        int sideHeight = sideWidth * SideStack.ROW_SIZE / SideStack.COL_SIZE;
        this.sideStacks = new SideStackCom[]{
                new SideStackCom(
@@ -45,9 +67,6 @@ public class ChessboardCom extends JComponent {
            add(sideStackCom);
            sideStackCom.repaint();
        }
        System.out.printf("chessboard [(%d + %d) * %d], chess size = %d\n", mainWidth, sideWidth, height, SQUARE_SIZE);
        initAllChessOnBoard();

    }

    public static ChessboardCom getInstance() {
@@ -74,6 +93,19 @@ public class ChessboardCom extends JComponent {
        return sideStacks[chessColor.ordinal()];
    }

    public int getMainWidth() {
        return mainWidth;
    }
    public void setMainWidth(int mainWidth) {
        this.mainWidth = mainWidth;
    }
    public int getSideWidth() {
        return sideWidth;
    }
    public void setSideWidth(int sideWidth) {
        this.sideWidth = sideWidth;
    }

    /**
     * 将SquareComponent 放置在 ChessBoard上。里面包含移除原有的component及放置新的component
     *
@@ -88,17 +120,6 @@ public class ChessboardCom extends JComponent {
        squareCom.repaint();
    }

    private void initAllChessOnBoard() {
        for (int i = 0; i < Chessboard.ROW_SIZE; i++) {
            for (int j = 0; j < Chessboard.COL_SIZE; j++) {
                ChessboardPoint chessboardPoint = new ChessboardPoint(i, j);
                putChessOnBoard(new SquareCom(
                        chessboardPoint, evalPoint(chessboardPoint), SQUARE_SIZE, SQUARE_SIZE / 12, true
                ));
            }
        }
    }

    /**
     * 绘制棋盘格子
     *
@@ -108,7 +129,7 @@ public class ChessboardCom extends JComponent {
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.setColor(new Color(0,0,0));
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.fillRect(0, 0, this.getMainWidth(), this.getHeight());
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }

+10 −7
Original line number Diff line number Diff line
@@ -20,16 +20,19 @@ public class SideStackCom extends JComponent {
        setLayout(null);
        setLocation(point);
        setSize(width, height);
        setOpaque(true);
        for (int i = 0; i < squareComs.length; i++) {
            for (int j = 0; j < squareComs[i].length; j++) {
                int size = Math.min(this.getWidth() / SideStack.COL_SIZE, this.getHeight() / SideStack.ROW_SIZE);
                Point location = new Point(
                        this.getX() + this.getWidth() * j / SideStack.COL_SIZE,
                        this.getY() + this.getHeight() * i / SideStack.ROW_SIZE
                        this.getWidth() * j / SideStack.COL_SIZE,
                        this.getHeight() * i / SideStack.ROW_SIZE
                );
                squareComs[i][j] = new SquareCom(new ChessboardPoint(i, j), location, size,0,false);
                squareComs[i][j].setChessColor(this.chessColor);
                add(squareComs[i][j]);
                SquareCom squareCom = new SquareCom(
                        new ChessboardPoint(i, j), location, size,size / 12,false);
                squareCom.setSideStackColor(this.chessColor);
                add(squareComs[i][j] = squareCom);
                squareCom.repaint();
            }
        }
    }
@@ -40,9 +43,9 @@ public class SideStackCom extends JComponent {

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        super.paintComponent(g);/*
        g.setColor(new Color(0,0,200));
        g.fillRect(0, 0, this.getWidth(), this.getHeight());
        g.fillRect(0, 0, this.getWidth(), this.getHeight());*/
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    }

Loading