Commit b2eaecc8 authored by YuFan Jia's avatar YuFan Jia 💤
Browse files

Merge branch 'Front' into 'main'

fix( ):

See merge request !8
parents d224f155 76721286
Loading
Loading
Loading
Loading
+178 −60
Original line number Diff line number Diff line

package Components;

import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.plaf.DimensionUIResource;

import GameUI.ChessGameUI;

import java.awt.*;
import java.lang.reflect.WildcardType;

import Model.ChessPiece;
import Core.*;

public class ChessBoardPanel extends JPanel {
    private int boardRows = 8;// 行数
    private int boardCols = 8;// 列数
    private static int length;
    private int width = 100;
    private int height = 100;
    private static int everyFromLength = 50;
    private ChessPanel[][] chessBoard;// 棋盘
    private int[][] lastChessGrids;
    private int[][] saveGrids;

    public ChessBoardPanel(int boardRows, int boardCols) {
        this.setVisible(true);
        this.setFocusable(true);
        this.setLayout(new GridLayout(this.boardRows, this.boardCols));
        // this.setBackground(Color.yellow);// todo: choose a beautiful color
        this.setBackground(Color.PINK);// todo: choose a beautiful color
        this.boardRows = boardRows;
        this.boardCols = boardCols;
        chessBoard = new ChessPanel[boardRows][boardCols];
@@ -31,6 +31,8 @@ public class ChessBoardPanel extends JPanel {
    }

    public void intitalChessPanel() {// 初始化每个格子
        this.lastChessGrids = new int[8][8];
        this.saveGrids = new int[8][8];
        for (int i = 0; i != boardRows; ++i) {
            for (int j = 0; j != boardCols; ++j) {
                ChessPanel tempPanel = new ChessPanel(i, j);
@@ -40,6 +42,131 @@ public class ChessBoardPanel extends JPanel {
        }
    }

    public void setChess(int row, int col, ChessPiece chess) {// 在指定格子放棋
        chessBoard[row][col].setChessPiece(chess);
    }

    public ChessPiece getChess(int y, int x) {
        return chessBoard[y][x].getChessPiece();
    }

    public boolean canClickGrid(int row, int col, ChessPiece currentPlayer) {// 判断该格子是否可以放棋
        int[][] digitBoard = new int[8][8];
        int color;
        if (currentPlayer == ChessPiece.BLACK) {
            color = -1;
        } else {
            color = 1;
        }
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (chessBoard[i][j].getChessPiece() == ChessPiece.BLACK) {
                    digitBoard[i][j] = -1;
                }
                if (chessBoard[i][j].getChessPiece() == ChessPiece.WHITE) {
                    digitBoard[i][j] = 1;
                }
                if (chessBoard[i][j].getChessPiece() == ChessPiece.EMPTY) {
                    digitBoard[i][j] = 0;
                }
            }
        }

        for (int i = 0; i < 8; i++) { // 刷新棋盘之前把上一步拷到undogrids里
            for (int j = 0; j < 8; j++) {
                if (chessBoard[i][j].getChessPiece() == ChessPiece.BLACK) {
                    lastChessGrids[i][j] = -1;
                }
                if (chessBoard[i][j].getChessPiece() == ChessPiece.WHITE) {
                    lastChessGrids[i][j] = 1;
                }
                if (chessBoard[i][j].getChessPiece() == ChessPiece.EMPTY) {
                    lastChessGrids[i][j] = 0;
                }
            }
        }

        DigitBoard d = new DigitBoard(row, col, color, digitBoard);
        d.DoLogic();
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (d.getAns(i, j) == -1) {
                    chessBoard[i][j].setChessPiece(ChessPiece.BLACK);
                }
                if (d.getAns(i, j) == 1) {
                    chessBoard[i][j].setChessPiece(ChessPiece.WHITE);
                }
                if (d.getAns(i, j) == 0) {
                    chessBoard[i][j].setChessPiece(ChessPiece.EMPTY);
                }
            }
        }
        repaint();
        return d.getAns(row, col) == color;
    }

    public void undoStep() { // 读取撤销棋盘
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (lastChessGrids[i][j] == -1) {
                    chessBoard[i][j].setChessPiece(ChessPiece.BLACK);
                }
                if (lastChessGrids[i][j] == 1) {
                    chessBoard[i][j].setChessPiece(ChessPiece.WHITE);
                }
                if (lastChessGrids[i][j] == 0) {
                    chessBoard[i][j].setChessPiece(ChessPiece.EMPTY);
                }
            }
        }
        repaint();
    }

    public void saveGame() { // 存档 存一个txt 由210和最后一步玩家(玩家在gamecore写入)组成 简陋但有效
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (chessBoard[i][j].getChessPiece() == ChessPiece.BLACK) {
                    saveGrids[i][j] = 2;
                }
                if (chessBoard[i][j].getChessPiece() == ChessPiece.WHITE) {
                    saveGrids[i][j] = 1;
                }
                if (chessBoard[i][j].getChessPiece() == ChessPiece.EMPTY) {
                    saveGrids[i][j] = 0;
                }
            }
        }
    }

    public void readGame(String readGame) { // 读取 数字代表棋子颜色
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                String substring = readGame.substring(i * 8 + j, i * 8 + j + 1);
                if (substring.equals("2")) {
                    chessBoard[i][j].setChessPiece(ChessPiece.BLACK);
                }
                if (substring.equals("1")) {
                    chessBoard[i][j].setChessPiece(ChessPiece.WHITE);
                }
                if (substring.equals("0")) {
                    chessBoard[i][j].setChessPiece(ChessPiece.EMPTY);
                }
            }
        }
        repaint();
    }

    @Override
    public String toString() { // 存档用
        StringBuilder saveString = new StringBuilder();
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                saveString.append(saveGrids[i][j]);
            }
        }
        return String.valueOf(saveString);
    }

    @Override
    public Dimension getPreferredSize() {
        return chessBoard[0][0].getPreferredSize();
@@ -47,15 +174,6 @@ public class ChessBoardPanel extends JPanel {

    public void autoZoom(int width, int height) {// zoom
        this.setSize(chessBoard[0][0].getPreferredSize());
        System.out.println("board\t" + this.getWidth() + "\t" + this.getHeight());
    }

    public void setChess(int row, int col, ChessPiece chess) {// 在指定格子放棋
        chessBoard[row][col].setChessPiece(chess);
    }

    public boolean canClickGrid(int row, int col, ChessPiece currentPlayer) {// 判断该格子是否可以放棋
        // todo check if can put chesspiece on current grind
        return true;
        // System.out.println("board\t" + this.getWidth() + "\t" + this.getHeight());
    }
}
 No newline at end of file
+1 −2
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ import java.awt.*;

import GameUI.ChessGameUI;
import Model.ChessPiece;
import Progressing.ChessPanel;

public class ChessGridComponent extends BasicComponent {
    public static int chessSize;
@@ -29,7 +28,7 @@ public class ChessGridComponent extends BasicComponent {
        // col);
        // todo: complete mouse click method
        if (ChessGameUI.thisGameCore.canClick(row, col)) {
            if (this.chessPiece == null) {
            if (this.chessPiece == ChessPiece.EMPTY) {
                this.chessPiece = ChessGameUI.thisGameCore.getCurrentPlayer();
                ChessGameUI.thisGameCore.swapPlayer();
            }
+3 −4
Original line number Diff line number Diff line
package Components;

import javax.swing.ImageIcon;
import javax.swing.plaf.DimensionUIResource;

import GameUI.ChessGameUI;

import java.awt.*;
@@ -36,7 +34,7 @@ public class ChessPanel extends BasicComponent {
    public Dimension getPreferredSize() {
        Dimension space = getParent().getParent().getSize();
        int length = 8 * (Math.min(space.width / 8, space.height / 8));
        System.out.println(space.width + "\t" + space.height + "\t" + length);
        // System.out.println(space.width + "\t" + space.height + "\t" + length);
        return new Dimension(length, length);
    }

@@ -69,13 +67,14 @@ public class ChessPanel extends BasicComponent {
        ImageIcon imgZoomed = new ImageIcon(newimg); // transform it back

        imgZoomed.paintIcon(this, g, 0, 0);
        System.out.println(width);
        // System.out.println(width);
    }

    private void paintChess(ChessPiece playersChess) {// 画棋子
        switch (playersChess) {
            case BLACK -> image = blcakImg;
            case WHITE -> image = whiteImg;
            case EMPTY -> image = emptyImg;
            default -> throw new IllegalArgumentException("Unexpected value: " + playersChess);
        }
        repaint();

Core/DigitBoard.java

0 → 100644
+171 −0
Original line number Diff line number Diff line
package Core;

public class DigitBoard {
    int[][] inBoard = new int[8][8];
    int[][] outBoard = new int[8][8];
    int[][] operate = new int[1][2];
    int color = 0;

    public DigitBoard(int row, int col, int color, int[][] digitBoard) {
        this.inBoard = digitBoard;
        this.color = color;
        this.operate[0][0] = row;
        this.operate[0][1] = col;
    }


    public void DoLogic() {
        for (int i = 0, flag = 0; i != 1; this.color *= -1, flag = 0, ++i) {
            boolean[][][] direction = CheckAvailable(this.inBoard, this.operate[i], this.color);
            for (int j = 0; j != 8; ++j) {
                if (direction[this.operate[i][0]][this.operate[i][1]][j]) {
                    ++flag;
                    this.inBoard = OperateMat(this.inBoard, this.operate[i], direction, this.color);
                }
            }
            if (flag == 0)
                break;
        }
        for (int i = 0; i < 8; i++) {
            System.arraycopy(this.inBoard[i], 0, this.outBoard[i], 0, 8);
        }
    }

    public int getAns(int y, int x) { return this.outBoard[y][x]; }

    public static boolean CheckEmptyPoint(int[][] mat, int[] pos, int color) {
        return mat[pos[0]][pos[1]] == 0;
    }

    public static boolean CheckRivalColorLine(int[][] mat, int row, int col, int pointColor, int rowFlag, int colFlag) {
        for (; row + rowFlag > -1 && row + rowFlag < 8 && col + colFlag > -1
                && col + colFlag < 8; rowFlag = FlagChange(rowFlag), colFlag = FlagChange(colFlag)) {
            if (mat[row + rowFlag][col + colFlag] == 0)
                return false;
            if (mat[row + rowFlag][col + colFlag] == pointColor)
                return false;
            if (mat[row + rowFlag][col + colFlag] == -pointColor)
                return true;
        }
        return false;
    }

    public static boolean CheckSelfColorLine(int[][] mat, int row, int col, int pointColor, int rowFlag, int colFlag) {
        for (int count = 0; row + rowFlag > -1 && row + rowFlag < 8 && col + colFlag > -1
                && col + colFlag < 8; rowFlag = FlagChange(rowFlag), colFlag = FlagChange(colFlag), ++count) {
            if (mat[row + rowFlag][col + colFlag] == 0)
                return false;
            if (mat[row + rowFlag][col + colFlag] == pointColor) {
                return count != 0;
            }
        }
        return false;
    }

    public static int[][] ChangeRivalColor(int[][] mat, int[] pos, int rowFlag, int colFlag, int selfcolor) {
        int[][] result = mat;
        for (; pos[0] + rowFlag > -1 && pos[0] + rowFlag < 8 && pos[1] + colFlag > -1
                && pos[1] + colFlag < 8; rowFlag = FlagChange(rowFlag), colFlag = FlagChange(colFlag)) {
            if (mat[pos[0] + rowFlag][pos[1] + colFlag] == selfcolor)
                break;
            if (mat[pos[0] + rowFlag][pos[1] + colFlag] == -selfcolor) {
                result[pos[0] + rowFlag][pos[1] + colFlag] = selfcolor;
            }
        }
        return result;
    }

    public static int FlagChange(int flag) {
        if (flag == 0)
            ;
        else if (flag < 0)
            --flag;
        else
            ++flag;
        return flag;
    }

    public static boolean[][][] CheckAvailable(int[][] mat, int[] pos, int color) {
        boolean[][][] direction = new boolean[8][8][8];
        if (CheckEmptyPoint(mat, pos, color)) {
            if (CheckRivalColorLine(mat, pos[0], pos[1], color, 1, 0)
                    && CheckSelfColorLine(mat, pos[0], pos[1], color, 1, 0))
                direction[pos[0]][pos[1]][4] = true;
            if (CheckRivalColorLine(mat, pos[0], pos[1], color, -1, 0)
                    && CheckSelfColorLine(mat, pos[0], pos[1], color, -1, 0))
                direction[pos[0]][pos[1]][0] = true;
            if (CheckRivalColorLine(mat, pos[0], pos[1], color, 0, -1)
                    && CheckSelfColorLine(mat, pos[0], pos[1], color, 0, -1))
                direction[pos[0]][pos[1]][6] = true;
            if (CheckRivalColorLine(mat, pos[0], pos[1], color, 0, 1)
                    && CheckSelfColorLine(mat, pos[0], pos[1], color, 0, 1))
                direction[pos[0]][pos[1]][2] = true;
            if (CheckRivalColorLine(mat, pos[0], pos[1], color, -1, -1)
                    && CheckSelfColorLine(mat, pos[0], pos[1], color, -1, -1))
                direction[pos[0]][pos[1]][7] = true;
            if (CheckRivalColorLine(mat, pos[0], pos[1], color, 1, 1)
                    && CheckSelfColorLine(mat, pos[0], pos[1], color, 1, 1))
                direction[pos[0]][pos[1]][3] = true;
            if (CheckRivalColorLine(mat, pos[0], pos[1], color, -1, 1)
                    && CheckSelfColorLine(mat, pos[0], pos[1], color, -1, 1))
                direction[pos[0]][pos[1]][1] = true;
            if (CheckRivalColorLine(mat, pos[0], pos[1], color, 1, -1)
                    && CheckSelfColorLine(mat, pos[0], pos[1], color, 1, -1))
                direction[pos[0]][pos[1]][5] = true;
        }
        return direction;
    }

    public static int[][] OperateMat(int[][] mat, int[] position, boolean[][][] direction, int color) {
        int[][] result = new int[8][8];
        for (int k = 0; k != 8; ++k) {
            if (direction[position[0]][position[1]][k]) {
                int rowFlag, colFlag;
                switch (k) {
                    case 0:
                        rowFlag = -1;
                        colFlag = 0;
                        result = ChangeRivalColor(mat, position, rowFlag, colFlag, color);
                        break;
                    case 1:
                        rowFlag = -1;
                        colFlag = 1;
                        result = ChangeRivalColor(mat, position, rowFlag, colFlag, color);
                        break;
                    case 2:
                        rowFlag = 0;
                        colFlag = 1;
                        result = ChangeRivalColor(mat, position, rowFlag, colFlag, color);
                        break;
                    case 3:
                        rowFlag = 1;
                        colFlag = 1;
                        result = ChangeRivalColor(mat, position, rowFlag, colFlag, color);
                        break;
                    case 4:
                        rowFlag = 1;
                        colFlag = 0;
                        result = ChangeRivalColor(mat, position, rowFlag, colFlag, color);
                        break;
                    case 5:
                        rowFlag = 1;
                        colFlag = -1;
                        result = ChangeRivalColor(mat, position, rowFlag, colFlag, color);
                        break;
                    case 6:
                        rowFlag = 0;
                        colFlag = -1;
                        result = ChangeRivalColor(mat, position, rowFlag, colFlag, color);
                        break;
                    case 7:
                        rowFlag = -1;
                        colFlag = -1;
                        result = ChangeRivalColor(mat, position, rowFlag, colFlag, color);
                        break;
                }
            }
        }
        result[position[0]][position[1]] = color;
        return result;
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -41,6 +41,11 @@ public class GameCore {
        currentPlayer = (currentPlayer == ChessPiece.BLACK) ? ChessPiece.WHITE : ChessPiece.BLACK;
        ChessGameUI.thisStatus.setCurrentPlayer(currentPlayer);
        ChessGameUI.thisStatus.setScore(blackScore, whiteScore);
        if (this.currentPlayer == ChessPiece.BLACK) {
            this.currentPlayer = ChessPiece.WHITE;
        } else {
            this.currentPlayer = ChessPiece.BLACK;
        }
        // todo:complete status panel
        // statusPanel.setPlayerText(currentPlayer.name());
        // statusPanel.setScoreText(blackScore, whiteScore);
Loading