Commit c42684f5 authored by 林修诚's avatar 林修诚
Browse files

feat:

parent cbd660cf
Loading
Loading
Loading
Loading
+1 −2
Original line number Original line Diff line number Diff line
@@ -4,8 +4,7 @@ import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseEvent;


/* 这个你不用来看
/* 这个你不用来看 */
* IDEA git sync test */
public abstract class BasicComponent extends JComponent {
public abstract class BasicComponent extends JComponent {
    public BasicComponent() {
    public BasicComponent() {
        this.addMouseListener(new MouseAdapter() {
        this.addMouseListener(new MouseAdapter() {
+82 −2
Original line number Original line Diff line number Diff line
@@ -7,12 +7,15 @@ import javax.swing.plaf.DimensionUIResource;
import java.awt.*;
import java.awt.*;


import Model.ChessPiece;
import Model.ChessPiece;
import Core.*;


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


    public ChessBoardPanel(int boardRows, int boardCols) {
    public ChessBoardPanel(int boardRows, int boardCols) {
        this.setVisible(true);
        this.setVisible(true);
@@ -29,6 +32,8 @@ public class ChessBoardPanel extends JPanel {
    }
    }


    public void intitalChessPanel() {// 初始化每个格子
    public void intitalChessPanel() {// 初始化每个格子
        this.lastChessGrids = new int [8][8];
        this.saveGrids = new int [8][8];
        for (int i = 0; i != boardRows; ++i) {
        for (int i = 0; i != boardRows; ++i) {
            for (int j = 0; j != boardCols; ++j) {
            for (int j = 0; j != boardCols; ++j) {
                ChessPanel tempPanel = new ChessPanel(i, j);
                ChessPanel tempPanel = new ChessPanel(i, j);
@@ -42,8 +47,83 @@ public class ChessBoardPanel extends JPanel {
        chessBoard[row][col].setChessPiece(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) {// 判断该格子是否可以放棋
    public boolean canClickGrid(int row, int col, ChessPiece currentPlayer) {// 判断该格子是否可以放棋
        // todo check if can put chesspiece on current grind
        int[][] digitBoard = new int[8][8];
        return true;
        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() == null) { 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() == null) { 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(null); }
            }
        }
        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(null); }
            }
        }
        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() == null) { 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(null);             }
            }
        }
        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);
    }
    }
}
}
 No newline at end of file
+1 −1
Original line number Original line Diff line number Diff line
@@ -34,7 +34,7 @@ public class ChessPanel extends BasicComponent {


    @Override
    @Override
    public void onMouseClicked() {// todo 鼠标点击时的动作
    public void onMouseClicked() {// todo 鼠标点击时的动作
        System.out.printf("%s clicked (%d, %d)\n",
        System.out.printf("%s clicked (%d, %d)%n",
                ChessGameUI.thisGameCore.getCurrentPlayer(), row, col);
                ChessGameUI.thisGameCore.getCurrentPlayer(), row, col);
        // todo: complete mouse click method
        // todo: complete mouse click method
        if (ChessGameUI.thisGameCore.canClick(row, col)) {// todo
        if (ChessGameUI.thisGameCore.canClick(row, col)) {// todo

Core/DigitBoard.java

0 → 100644
+171 −0
Original line number Original line 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;
    }
}
+36 −14
Original line number Original line Diff line number Diff line
package Core;
package Core;


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


import Components.ChessBoardPanel;
import Components.ChessBoardPanel;
import GameUI.ChessGameUI;
import GameUI.ChessGameUI;
import GameUI.StatusUI;
import GameUI.TopBar;
import Model.CheatStatus;
import Model.ChessPiece;
import Model.ChessPiece;


public class GameCore {
public class GameCore {
@@ -17,6 +22,7 @@ public class GameCore {
    private ChessPiece currentPlayer;// 当前该谁下棋
    private ChessPiece currentPlayer;// 当前该谁下棋
    private String importPath;
    private String importPath;
    private String exportPath;
    private String exportPath;
    private CheatStatus cs;


    public GameCore(int rows, int cols) {
    public GameCore(int rows, int cols) {
        this.gamePanel = new ChessBoardPanel(rows, cols);
        this.gamePanel = new ChessBoardPanel(rows, cols);
@@ -25,6 +31,7 @@ public class GameCore {
        blackScore = 2;
        blackScore = 2;
        whiteScore = 2;
        whiteScore = 2;
        ChessGameUI.thisStatus.setScore(blackScore, whiteScore);
        ChessGameUI.thisStatus.setScore(blackScore, whiteScore);
        this.cs = CheatStatus.Off;
    }
    }


    private void intializeChess() {// 初始化放棋
    private void intializeChess() {// 初始化放棋
@@ -36,7 +43,8 @@ public class GameCore {


    public void swapPlayer() {// 交换玩家
    public void swapPlayer() {// 交换玩家
        countScore();
        countScore();
        currentPlayer = (currentPlayer == ChessPiece.BLACK) ? ChessPiece.WHITE : ChessPiece.BLACK;
        if (this.currentPlayer == ChessPiece.BLACK) { this.currentPlayer = ChessPiece.WHITE; }
        else { this.currentPlayer = ChessPiece.BLACK; }
        // todo:complete status panel
        // todo:complete status panel
        // statusPanel.setPlayerText(currentPlayer.name());
        // statusPanel.setPlayerText(currentPlayer.name());
        // statusPanel.setScoreText(blackScore, whiteScore);
        // statusPanel.setScoreText(blackScore, whiteScore);
@@ -47,7 +55,18 @@ public class GameCore {
    }
    }


    public void countScore() {// 计分
    public void countScore() {// 计分
        // todo: modify the countScore method
        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;
    
        //todo 在这里更新scorebar的分数
    }
    }


    public void readFileData(String fileName) {// 读存档
    public void readFileData(String fileName) {// 读存档
@@ -66,27 +85,30 @@ public class GameCore {
        }
        }
    }
    }


    public void writeDataToFile(String fileName) {// 写存档
    public void writeDataToFile(String filePath) throws IOException {
        // todo: write data into file
        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) {// 可以被点击
    public boolean canClick(int row, int col) {// 可以被点击
        // todo
        return gamePanel.canClickGrid(row, col, currentPlayer) || this.cs == CheatStatus.On;
        return gamePanel.canClickGrid(row, col, currentPlayer);
    }
    }


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


    private ChessPiece checkWinner() {// 检测胜者
    private ChessPiece checkWinner() {// 检测胜者
        // todo(用ChessPiece.EMPTY表示平局)
        if (this.blackScore > this.whiteScore) { return ChessPiece.BLACK; }
        return null;
        if (this.blackScore < this.whiteScore) { return ChessPiece.WHITE; }
    }
        return ChessPiece.EMPTY;

    private boolean canMove() {// 当前玩家有处可下
        // todo
        return true;
    }
    }


    public void setImportpath(String importPath) {
    public void setImportpath(String importPath) {
Loading