Commit 7043def5 authored by YuFan Jia's avatar YuFan Jia 💤
Browse files

chore(end):

parent 321c653e
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -22,7 +22,7 @@ public class ChessBoardPanel extends JPanel {
        this.setVisible(true);
        this.setFocusable(true);
        this.setLayout(new GridLayout(this.boardRows, this.boardCols));
        this.setBackground(Color.PINK);// todo: choose a beautiful color
        this.setBackground(Color.orange);// todo: choose a beautiful color
        this.boardRows = boardRows;
        this.boardCols = boardCols;
        chessBoard = new ChessPanel[boardRows][boardCols];
+107 −0
Original line number Diff line number Diff line
package Core;

import java.util.ArrayList;

import Model.ChessPiece;

public class CalculateCore {
    private static int[][] eightDirections = { { 1, 1 }, { -1, -1 }, { 1, -1 }, { -1, 1 }, { 1, 0 }, { 0, 1 },
            { -1, 0 }, { 0, -1 } };// 八个方向
    private ChessPiece[][] chessBoard = null;
    private int rows = 8, cols = 8;
    private ArrayList<ChessPiece>[] eightLineArrayLists = new ArrayList[8];
    private ArrayList<ChessPiece> LineList = new ArrayList<>();

    private boolean checkEmptyPoint(ChessPiece[][] mat, int row, int col) {
        if (mat[row][col] == ChessPiece.EMPTY)
            return true;
        return false;
    }

    CalculateCore() {
        initializeEightLineArraylists();
    }

    private int[] getCurrentDirection(int direction, int times) {
        int[] tem = { eightDirections[direction][0], eightDirections[direction][1] };
        for (int i = 0; i != 2; ++i) {
            if (tem[i] > 0) {
                tem[i] += times;
            } else if (tem[i] < 0) {
                tem[i] -= times;
            }
        }
        return tem;
    }

    private ChessPiece[][] checkAndMove(ChessPiece mat[][], int row, int col, ChessPiece selfColor) {
        int[] currentDirectionMove = {};// 当前方向的移动距离
        for (int direction = 0/* 切换八个方向 */, times = 0/* 该方向遍历的第几个元素 */; direction != 8; ++direction, times = 0) {
            currentDirectionMove = getCurrentDirection(direction, times);
            LineList.clear();
            while (row + currentDirectionMove[0] > -1 && row + currentDirectionMove[0] < 8
                    && col + currentDirectionMove[1] > -1
                    && col + currentDirectionMove[1] < 8) {
                if (mat[row + currentDirectionMove[0]][col + currentDirectionMove[1]] == ChessPiece.EMPTY) {
                    break;// 遇见空返回
                } else {
                    LineList.add(mat[row + currentDirectionMove[0]][col + currentDirectionMove[1]]);// 否则加入arraylist
                }
                ++times;// 下一个元素
                currentDirectionMove = getCurrentDirection(direction, times);// 刷新移动距离

            }

            if (!LineList.isEmpty() && LineList.get(0) != selfColor && LineList.indexOf(selfColor) != -1) {// 该方向不为空且第一个不为自己的颜色且其中含有自己的颜色
                for (int i = 0; i != LineList.indexOf(selfColor); ++i) {// 翻棋
                    currentDirectionMove = getCurrentDirection(direction, i);
                    mat[row + currentDirectionMove[0]][col + currentDirectionMove[1]] = selfColor;
                }
                mat[row][col] = selfColor;// 下棋的地方放自己的棋
            }
            // 如果list为空则直接进入下一个方向的检查
        }
        // 结束检查和翻棋
        return mat;
    }

    private void initializeEightLineArraylists() {
        for (int i = 0; i != 8; ++i) {
            eightLineArrayLists[i] = new ArrayList<ChessPiece>();
        }
    }

    private void clearEightLineArraylists() {
        for (int i = 0; i != 8; ++i) {
            eightLineArrayLists[i].clear();
        }
    }

    public boolean checkCanSet(ChessPiece mat[][], int row, int col, ChessPiece selfColor) {
        if (checkEmptyPoint(mat, row, col)) {
            int[] currentDirectionMove = {};// 当前方向的移动距离
            for (int direction = 0/* 切换八个方向 */, times = 0/* 该方向遍历的第几个元素 */; direction != 8; ++direction, times = 0) {
                currentDirectionMove = getCurrentDirection(direction, times);
                LineList.clear();
                while (row + currentDirectionMove[0] > -1 && row + currentDirectionMove[0] < 8
                        && col + currentDirectionMove[1] > -1
                        && col + currentDirectionMove[1] < 8) {
                    if (mat[row + currentDirectionMove[0]][col + currentDirectionMove[1]] == ChessPiece.EMPTY) {
                        break;// 遇见空返回
                    } else {
                        LineList.add(mat[row + currentDirectionMove[0]][col + currentDirectionMove[1]]);// 否则加入arraylist
                    }
                    ++times;// 下一个元素
                    currentDirectionMove = getCurrentDirection(direction, times);// 刷新移动距离

                }

                if (!LineList.isEmpty() && LineList.get(0) != selfColor && LineList.indexOf(selfColor) != -1) {// 该方向不为空且第一个不为自己的颜色且其中含有自己的颜色
                    return true;
                }

            }
        }
        return false;
    }
}
+13 −1
Original line number Diff line number Diff line
@@ -156,7 +156,7 @@ public class GameCore {
    }

    public boolean checkGameOver() {// 检测下完没 内部有调用checkWinner 这个每一次点击都会执行
        if (this.gamePanel.isGameOver(currentPlayer)) {
        if (this.gamePanel.isGameOver(currentPlayer) || isAllFull()) {
            System.out.println("gg");
            ChessPiece winner = checkWinner();
            String winPlayer = null;
@@ -199,4 +199,16 @@ public class GameCore {
    public ChessBoardPanel getGamePanel() {// 应该没啥用
        return gamePanel;
    }

    public boolean isAllFull() {
        int flag = 0;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (!gamePanel.isEmpty(i, j)) {
                    flag++;
                }
            }
        }
        return flag == 64;
    }
}
 No newline at end of file

Core/GameCore_new.java

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

import javax.swing.JOptionPane;
import javax.swing.plaf.OptionPaneUI;

import java.io.FileNotFoundException;

import Components.ChessBoardPanel;
import GameUI.BasicBackground;
import GameUI.ChessGameUI;
import GameUI.OpinionPanel;
import GameUI.StatusUI;
import GameUI.TopBar;
import Model.CheatStatus;
import Model.ChessPiece;
import Core.CalculateCore;

public class GameCore_new {
    private ChessBoardPanel gamePanel;// 棋盘
    private int blackScore;// 字面意思
    private int whiteScore;
    private ChessPiece currentPlayer;// 当前该谁下棋
    private String importPath;
    private String exportPath;
    private int panelWidth;
    private int panelHeight;
    public boolean havePlacedOnce = false;

    public GameCore_new(int rows, int cols) {
        this.gamePanel = new ChessBoardPanel(rows, cols);
        this.currentPlayer = ChessPiece.BLACK;
        intializeChess();
        blackScore = 2;
        whiteScore = 2;
        ChessGameUI.thisStatus.setScore(blackScore, whiteScore);

    }

    private void intializeChess() {// 初始化放棋

        gamePanel.setChess(3, 3, ChessPiece.BLACK);
        gamePanel.setChess(4, 4, ChessPiece.BLACK);
        gamePanel.setChess(3, 4, ChessPiece.WHITE);
        gamePanel.setChess(4, 3, ChessPiece.WHITE);
    }

    public void restart() {
        for (int i = 0; i != 8; ++i) {
            for (int j = 0; j != 8; ++j) {
                gamePanel.setChess(i, j, ChessPiece.EMPTY);
            }
        }
        havePlacedOnce = false;
        intializeChess();
        currentPlayer = ChessPiece.BLACK;
        ChessGameUI.thisStatus.setCurrentPlayer(currentPlayer);
        countScore();
    }

    public void swapPlayer() {// 交换玩家
        countScore();
        if (this.currentPlayer == ChessPiece.BLACK) {
            this.currentPlayer = ChessPiece.WHITE;
        } else {
            this.currentPlayer = ChessPiece.BLACK;
        }
        ChessGameUI.thisStatus.setCurrentPlayer(currentPlayer);
    }

    public void zoom(int outerWidth, int outHeight) {// 缩放
        panelWidth = outerWidth;
        panelHeight = outHeight;
        gamePanel.autoZoom(panelWidth, panelHeight);
        // todo complete zoom
    }

    public void countScore() {// 计分
        int dw = 0;
        int db = 0;
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (this.gamePanel.getChess(i, j) == ChessPiece.BLACK) {
                    db++;
                }
                if (this.gamePanel.getChess(i, j) == ChessPiece.WHITE) {
                    dw++;
                }
            }
        }
        this.whiteScore = dw;
        this.blackScore = db;
        ChessGameUI.thisStatus.setScore(blackScore, whiteScore);
        ChessGameUI.thisTopBar.setScoreBar(blackScore, whiteScore);
        // todo 在这里更新scorebar的分数
    }

    public void readFileData(String filePath) throws FileNotFoundException {
        // todo: read date from file
        BufferedReader br = new BufferedReader(new FileReader(filePath));
        Scanner in = new Scanner(br);
        String str = in.nextLine();
        in.close();
        this.gamePanel.readGame(str);
        countScore();
        if (str.substring(64).equals("b")) {
            currentPlayer = ChessPiece.BLACK;
        } else {
            currentPlayer = ChessPiece.WHITE;
        }
    }

    public void writeDataToFile(String filePath) throws IOException {
        File file = new File(filePath, "save.txt");
        FileWriter out = new FileWriter(file);
        gamePanel.saveGame();
        out.write(gamePanel.toString());
        if (currentPlayer == ChessPiece.BLACK) {
            out.append("b");
        } else {
            out.append("w");
        }
        out.close();
    }

    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() {// 检测胜者 下面那个满足就执行这个 其实返回值可以改void 然后补上谁赢了的弹窗
        if (this.blackScore > this.whiteScore) {
            return ChessPiece.BLACK;
        }
        if (this.blackScore < this.whiteScore) {
            return ChessPiece.WHITE;
        }
        return ChessPiece.EMPTY;
    }

    public boolean checkGameOver() {// 检测下完没 内部有调用checkWinner 这个每一次点击都会执行
        if (this.gamePanel.isGameOver(currentPlayer)) {
            System.out.println("gg");
            ChessPiece winner = checkWinner();
            String winPlayer = null;
            if (winner == ChessPiece.EMPTY) {
                winPlayer = "Nobody";
            } else {
                winPlayer = winner.toString();
            }
            OpinionPanel showWinner = new OpinionPanel(winPlayer + " win, press ok to restart game", "Game over",
                    JOptionPane.OK_CANCEL_OPTION);
            if (showWinner.clickYes()) {
                restart();
            } else {
                BasicBackground.backFrame.remove(BasicBackground.thisChessGameUI);
                BasicBackground.changePanel(BasicBackground.thisStartMenuUI);
            }

        } 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);
        readFileData(importPath);
    }

    public void setExportPath(String exportPath) throws IOException {
        this.exportPath = exportPath;
        System.out.println(this.exportPath);
        writeDataToFile(exportPath);
    }

    public ChessPiece getCurrentPlayer() {
        return currentPlayer;
    }

    public ChessBoardPanel getGamePanel() {// 应该没啥用
        return gamePanel;
    }
}
 No newline at end of file