Commit 30379a8b authored by YuFan Jia's avatar YuFan Jia 💤
Browse files

feat(merge): merge front with back

=-=
parents c42684f5 4faf766d
Loading
Loading
Loading
Loading
+82 −32
Original line number Diff line number Diff line

package Components;

import javax.swing.JPanel;
@@ -20,10 +21,8 @@ public class ChessBoardPanel extends JPanel {
    public ChessBoardPanel(int boardRows, int boardCols) {
        this.setVisible(true);
        this.setFocusable(true);
        this.setLayout(new GridLayout(this.boardRows, this.boardCols, 0, 0));
        this.setBackground(Color.yellow);

        this.setPreferredSize(new DimensionUIResource(boardRows * everyFromLength, boardCols * everyFromLength));
        this.setLayout(new GridLayout(this.boardRows, this.boardCols));
        this.setBackground(Color.PINK);// todo: choose a beautiful color
        this.boardRows = boardRows;
        this.boardCols = boardCols;
        chessBoard = new ChessPanel[boardRows][boardCols];
@@ -47,26 +46,43 @@ public class ChessBoardPanel extends JPanel {
        chessBoard[row][col].setChessPiece(chess);
    }

    public ChessPiece getChess(int y, int x) { return chessBoard[y][x].getChessPiece(); }
    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; }
        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; }
                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() == null) { lastChessGrids[i][j] = 0; }
                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;
                }
            }
        }

@@ -74,9 +90,15 @@ public class ChessBoardPanel extends JPanel {
        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); }
                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();
@@ -86,9 +108,15 @@ public class ChessBoardPanel extends JPanel {
    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); }
                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();
@@ -97,9 +125,15 @@ public class ChessBoardPanel extends JPanel {
    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; }
                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;
                }
            }
        }
    }
@@ -108,9 +142,15 @@ public class ChessBoardPanel extends JPanel {
        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);             }
                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();
@@ -126,4 +166,14 @@ public class ChessBoardPanel extends JPanel {
        }
        return String.valueOf(saveString);
    }

    @Override
    public Dimension getPreferredSize() {
        return chessBoard[0][0].getPreferredSize();
    }

    public void autoZoom(int width, int height) {// zoom
        this.setSize(chessBoard[0][0].getPreferredSize());
        System.out.println("board\t" + this.getWidth() + "\t" + this.getHeight());
    }
}
 No newline at end of file
+105 −85
Original line number Diff line number Diff line
@@ -15,14 +15,14 @@ public class ChessPanel extends BasicComponent {
    private ChessPiece playersChess;
    ImageIcon emptyImg = new ImageIcon("Model/empty.png");
    ImageIcon whiteImg = new ImageIcon("Model/white1.png");
    ImageIcon blcakImg = new ImageIcon("Model/black1.png");
    ImageIcon blackImg = new ImageIcon("Model/black1.png");
    ImageIcon image;

    ChessPanel(int row, int col) {
        this.setPreferredSize(new DimensionUIResource(size, size));
        setVisible(true);
        this.row = row;
        this.col = col;
        // setPreferredSize(new Dimension(40, 40));
        image = emptyImg;
        playersChess = ChessPiece.EMPTY;

@@ -32,13 +32,21 @@ public class ChessPanel extends BasicComponent {
        ChessPanel.size = size;
    }

    @Override
    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);
        return new Dimension(length, length);
    }

    @Override
    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);
        // todo: complete mouse click method
        if (ChessGameUI.thisGameCore.canClick(row, col)) {// todo

            ChessGameUI.thisGameCore.countScore();
            this.playersChess = ChessGameUI.thisGameCore.getCurrentPlayer();
            ChessGameUI.thisGameCore.swapPlayer();
            paintChess(playersChess);
@@ -49,20 +57,32 @@ public class ChessPanel extends BasicComponent {
    @Override
    protected void paintComponent(Graphics g) {// 画棋子
        super.paintComponent(g);
        image.paintIcon(this, g, 0, 0);

        int width = this.getWidth();
        int height = this.getHeight();
        int drawWidth = Math.min(width, height);
        int drawHeight = drawWidth;

        Image imageTrans = image.getImage(); // transform it
        int size = drawWidth;
        Image newimg = imageTrans.getScaledInstance(size, size, java.awt.Image.SCALE_SMOOTH); // scale it the smooth way
        ImageIcon imgZoomed = new ImageIcon(newimg); // transform it back

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

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

    public void configSize(int grindPanelSize) {// 初始化长宽
        this.setPreferredSize(new DimensionUIResource(grindPanelSize / 8, grindPanelSize / 8));
    }

    public ChessPiece getChessPiece() {// 获取当前位置的棋子
+282 −129
Original line number Diff line number Diff line
@@ -22,6 +22,8 @@ public class GameCore {
    private ChessPiece currentPlayer;// 当前该谁下棋
    private String importPath;
    private String exportPath;
    private int panelWidth;
    private int panelHeight;
    private CheatStatus cs;

    public GameCore(int rows, int cols) {
@@ -43,14 +45,20 @@ public class GameCore {

    public void swapPlayer() {// 交换玩家
        countScore();
        if (this.currentPlayer == ChessPiece.BLACK) { this.currentPlayer = ChessPiece.WHITE; }
        else { this.currentPlayer = ChessPiece.BLACK; }
        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);
    }

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

@@ -59,13 +67,18 @@ public class GameCore {
        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++; }
                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的分数
    }

@@ -90,8 +103,11 @@ public class GameCore {
        FileWriter out = new FileWriter(file);
        gamePanel.saveGame();
        out.write(gamePanel.toString());
        if (currentPlayer == ChessPiece.BLACK) { out.append("b"); }
        else { out.append("w"); }
        if (currentPlayer == ChessPiece.BLACK) {
            out.append("b");
        } else {
            out.append("w");
        }
        out.close();
    }

@@ -106,17 +122,25 @@ public class GameCore {
    }

    private ChessPiece checkWinner() {// 检测胜者
        if (this.blackScore > this.whiteScore) { return ChessPiece.BLACK; }
        if (this.blackScore < this.whiteScore) { return ChessPiece.WHITE; }
        if (this.blackScore > this.whiteScore) {
            return ChessPiece.BLACK;
        }
        if (this.blackScore < this.whiteScore) {
            return ChessPiece.WHITE;
        }
        return ChessPiece.EMPTY;
    }

    public void setImportpath(String importPath) {
        this.importPath = importPath;
        System.out.println(this.importPath);
        readFileData(importPath);
    }

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

    public ChessPiece getCurrentPlayer() {
@@ -127,3 +151,132 @@ public class GameCore {
        return gamePanel;
    }
}
// =======
// package Core;

// import java.io.BufferedReader;
// import java.io.FileReader;
// import java.io.IOException;
// import java.util.ArrayList;
// import java.util.List;

// import Components.ChessBoardPanel;
// import GameUI.ChessGameUI;
// import Model.ChessPiece;

// public class GameCore {
// 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 GameCore(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 swapPlayer() {// 交换玩家
// countScore();
// currentPlayer = (currentPlayer == ChessPiece.BLACK) ? ChessPiece.WHITE :
// ChessPiece.BLACK;
// ChessGameUI.thisStatus.setCurrentPlayer(currentPlayer);
// ChessGameUI.thisStatus.setScore(blackScore, whiteScore);
// // todo:complete status panel
// // statusPanel.setPlayerText(currentPlayer.name());
// // statusPanel.setScoreText(blackScore, whiteScore);
// }

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

// public void countScore() {// 计分
// // todo: modify the countScore method
// }

// public void readFileData(String fileName) {// 读存档
// // todo: read date from file
// List<String> fileData = new ArrayList<>();
// try {
// FileReader fileReader = new FileReader(fileName);
// BufferedReader bufferedReader = new BufferedReader(fileReader);
// String line;
// while ((line = bufferedReader.readLine()) != null) {
// fileData.add(line);
// }
// fileData.forEach(System.out::println);
// } catch (IOException e) {
// e.printStackTrace();
// }
// }

// public void writeDataToFile(String fileName) {// 写存档
// // todo: write data into file
// }

// public boolean canClick(int row, int col) {// 可以被点击
// // todo
// return gamePanel.canClickGrid(row, col, currentPlayer);
// }

// public void undo() {// 撤销
// // todo
// }

// private ChessPiece checkWinner() {// 检测胜者
// // todo(用ChessPiece.EMPTY表示平局)
// return null;
// }

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

// public void setImportpath(String importPath) {
// this.importPath = importPath;
// System.out.println(this.importPath);
// readFileData(importPath);
// }

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

// public ChessPiece getCurrentPlayer() {
// return currentPlayer;
// }

// public ChessBoardPanel getGamePanel() {// 应该没啥用
// return gamePanel;
// }

// public int getRows() {
// return 8;
// }

// public int getCols() {
// return 8;
// }
// }
// >>>>>>> Front
+43 −0
Original line number Diff line number Diff line
package GameUI;

import java.awt.*;
import javax.swing.*;

/**
 * @author uint44t
 */
public class BasicBackground extends JFrame {
	public static ChessGameUI thisChessGameUI;
	public static StartMenuUI thisStartMenuUI;

	public BasicBackground() {
		initComponents();
		thisChessGameUI = new ChessGameUI();
		thisStartMenuUI = new StartMenuUI();
		changePanel(thisChessGameUI);
	}

	public void changePanel(JPanel o) {
		this.add(o);
		o.updateUI();
	}

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents

		// ======== basic ========
		{
			this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			this.setVisible(true);
			this.setTitle("Othello");
			this.setName("backFrame");
			Container basicContentPane = this.getContentPane();
			basicContentPane.setPreferredSize(new Dimension(700, 500));
			this.pack();
			this.setLocationRelativeTo(null);
		}
		// JFormDesigner - End of component initialization //GEN-END:initComponents
	}

}
+22 −0
Original line number Diff line number Diff line
JFDML JFormDesigner: "7.0.4.0.360" Java: "15.0.1" encoding: "UTF-8"

new FormModel {
	contentType: "form/swing"
	root: new FormRoot {
		"$setComponentNames": true
		add( new FormWindow( "javax.swing.JFrame", new FormLayoutManager( class java.awt.BorderLayout ) {
			"hgap": 500
			"vgap": 450
		} ) {
			name: "basic"
			"defaultCloseOperation": 3
			"$locationPolicy": 1
			"visible": true
			"title": "Othello"
			"name": "backFrame"
		}, new FormLayoutConstraints( null ) {
			"size": new java.awt.Dimension( 500, 500 )
			"location": new java.awt.Point( 180, 0 )
		} )
	}
}
Loading