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

feat(QAQ): qwq

parents 2445fe91 85ae3f24
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
@@ -50,6 +50,13 @@ public class ChessBoardPanel extends JPanel {
        return chessBoard[y][x].getChessPiece();
    }

    public boolean isEmpty(int col, int row) {
        if (chessBoard[col][row].getChessPiece() == ChessPiece.EMPTY) {
            return true;
        }
        return false;
    }

    public boolean canClickGrid(int row, int col, ChessPiece currentPlayer) {// 判断该格子是否可以放棋
        int[][] digitBoard = new int[8][8];
        int color;
@@ -105,6 +112,42 @@ public class ChessBoardPanel extends JPanel {
        return d.getAns(row, col) == color;
    }

    public boolean isGameOver(ChessPiece currentPlayer) {
        int color;
        if (currentPlayer == ChessPiece.BLACK) {
            color = -1;
        } else {
            color = 1;
        }
        int[][] digitBoard = new int[8][8];
        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() == null) {
                    digitBoard[i][j] = 0;
                }
            }
        }

        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                while (digitBoard[i][j] != 0) {
                    DigitBoard db = new DigitBoard(i, j, color, digitBoard);
                    db.DoLogic();
                    if (db.outBoard != digitBoard) {
                        return false;
                    }
                }
            }
        }
        return true;
    }

    public void undoStep() { // 读取撤销棋盘
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
+6 −3
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package Components;
import javax.swing.ImageIcon;
import javax.swing.plaf.DimensionUIResource;

import Core.GameCore;
import GameUI.ChessGameUI;

import java.awt.*;
@@ -14,8 +15,8 @@ public class ChessPanel extends BasicComponent {
    private static int size = 8;
    private ChessPiece playersChess;
    ImageIcon emptyImg = new ImageIcon("Resources/empty.png");
    ImageIcon whiteImg = new ImageIcon("Resources/white.png");
    ImageIcon blackImg = new ImageIcon("Resources/black.png");
    ImageIcon whiteImg = new ImageIcon("Resources/white1.png");
    ImageIcon blackImg = new ImageIcon("Resources/black1.png");
    ImageIcon image;

    ChessPanel(int row, int col) {
@@ -50,8 +51,10 @@ public class ChessPanel extends BasicComponent {
            this.playersChess = ChessGameUI.thisGameCore.getCurrentPlayer();
            ChessGameUI.thisGameCore.swapPlayer();
            paintChess(playersChess);

            ChessGameUI.thisGameCore.havePlacedOnce = true;
        }
        ChessGameUI.undoCnt = 0;
        ChessGameUI.thisGameCore.checkGameOver();
    }

    @Override
+4 −3
Original line number Diff line number Diff line
@@ -2,7 +2,7 @@ package Core;

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

@@ -13,7 +13,6 @@ public class DigitBoard {
        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);
@@ -31,7 +30,9 @@ public class DigitBoard {
        }
    }

    public int getAns(int y, int x) { return this.outBoard[y][x]; }
    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;
+21 −4
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ public class GameCore {
    private String exportPath;
    private int panelWidth;
    private int panelHeight;
    public boolean havePlacedOnce = false;

    public GameCore(int rows, int cols) {
        this.gamePanel = new ChessBoardPanel(rows, cols);
@@ -51,6 +52,7 @@ public class GameCore {
                gamePanel.setChess(i, j, ChessPiece.EMPTY);
            }
        }
        havePlacedOnce = false;
        intializeChess();
        currentPlayer = ChessPiece.BLACK;
        ChessGameUI.thisStatus.setCurrentPlayer(currentPlayer);
@@ -123,16 +125,21 @@ public class GameCore {
    }

    public boolean canClick(int row, int col) {// 可以被点击
        if (!gamePanel.isEmpty(row, col)) {
            return false;
        }
        return gamePanel.canClickGrid(row, col, currentPlayer) || ChessGameUI.thisCheatModeStatus == CheatStatus.On;
    }

    public void undo() {// 撤销
        if (havePlacedOnce) {
            this.gamePanel.undoStep();
            swapPlayer();
            countScore();
        }
    }

    private ChessPiece checkWinner() {// 检测胜者
    private ChessPiece checkWinner() {// 检测胜者 下面那个满足就执行这个 其实返回值可以改void 然后补上谁赢了的弹窗
        if (this.blackScore > this.whiteScore) {
            return ChessPiece.BLACK;
        }
@@ -142,6 +149,16 @@ public class GameCore {
        return ChessPiece.EMPTY;
    }

    public boolean checkGameOver() {// 检测下完没 内部有调用checkWinner 这个每一次点击都会执行
        if (this.gamePanel.isGameOver(currentPlayer)) {
            System.out.println("gg");
            checkWinner();
        } else {
            System.out.println("continue");
        }
        return this.gamePanel.isGameOver(currentPlayer);
    }

    public void setImportpath(String importPath) throws FileNotFoundException {
        this.importPath = importPath;
        System.out.println(this.importPath);
+5 −1
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ public class ChessGameUI extends JPanel {
	public static TopBar thisTopBar;
	public static CheatStatus thisCheatModeStatus = CheatStatus.Off;// cheat模式的选择状态
	private PlayerName thisPlayerName;
	public static int undoCnt = 0;

	public CheatStatus getCheatStatus() {
		return thisCheatModeStatus;
@@ -58,8 +59,11 @@ public class ChessGameUI extends JPanel {
	}

	private void undoButtonActionPerformed() {// undo按钮触发事件
		if (undoCnt == 0) {
			thisGameCore.undo();
		}
		undoCnt++;
	}

	private void cheatOnOffButtonActionPerformed() {// cheat单选按钮被按下的触发事件
		if (cheatOnOff.isSelected()) {