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

Merge branch 'Front' into 'Back'

Front

See merge request !6
parents e3c1b333 b1e1ee6a
Loading
Loading
Loading
Loading
+20 −19
Original line number Diff line number Diff line
@@ -4,6 +4,7 @@ import javax.swing.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

/* 这个你不用来看 */
public abstract class BasicComponent extends JComponent {
    public BasicComponent() {
        this.addMouseListener(new MouseAdapter() {
+48 −49
Original line number Diff line number Diff line
package Progressing;
package Components;

import javax.swing.JPanel;
import javax.swing.border.Border;
@@ -6,14 +6,13 @@ import javax.swing.plaf.DimensionUIResource;

import java.awt.*;

import GameUI.ChessGameUI;
import Model.ChessPiece;

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

    public ChessBoardPanel(int boardRows, int boardCols) {
        this.setVisible(true);
@@ -29,7 +28,7 @@ public class ChessBoardPanel extends JPanel {
        intitalChessPanel();
    }

    public void intitalChessPanel() {
    public void intitalChessPanel() {// 初始化每个格子
        for (int i = 0; i != boardRows; ++i) {
            for (int j = 0; j != boardCols; ++j) {
                ChessPanel tempPanel = new ChessPanel(i, j);
@@ -39,11 +38,11 @@ public class ChessBoardPanel extends JPanel {
        }
    }

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

    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
        return true;
    }
+0 −75
Original line number Diff line number Diff line
package Components;

import java.awt.*;

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

public class ChessGridComponent extends BasicComponent {
    public static int chessSize;
    public static int gridSize;
    public static Color gridColor = new Color(255, 150, 50);

    private ChessPiece chessPiece;
    private ChessPanel chess;
    private int row;
    private int col;

    public ChessGridComponent(int row, int col) {
        // this.setSize(gridSize, gridSize);

        this.row = row;
        this.col = col;
    }

    @Override
    public void onMouseClicked() {
        // System.out.printf("%s clicked (%d, %d)\n", GameCore.getCurrentPlayer(), row,
        // col);
        // todo: complete mouse click method
        if (ChessGameUI.thisGameCore.canClick(row, col)) {
            if (this.chessPiece == null) {
                this.chessPiece = ChessGameUI.thisGameCore.getCurrentPlayer();
                ChessGameUI.thisGameCore.swapPlayer();
            }
            repaint();
        }
    }

    public ChessPiece getChessPiece() {
        return chessPiece;
    }

    public void setChessPiece(ChessPiece playersChessPiece) {
        this.chessPiece = playersChessPiece;
    }

    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }

    public void drawPiece(Graphics g) {
        // g.setColor(gridColor);
        // g.fillRect(1, 1, this.getWidth() - 2, this.getHeight() - 2);// fill the rect
        // with chessboard color
        // if (this.chessPiece != null) {
        // g.setColor(chessPiece.getColor());
        // g.fillOval((gridSize - chessSize) / 2, (gridSize - chessSize) / 2, chessSize,
        // chessSize);// draw a point
        // }

        // todo:complete the grapg methords or use other ways to draw the line
    }

    @Override
    public void paintComponent(Graphics g) {
        super.printComponents(g);
        drawPiece(g);
    }

}
+85 −89
Original line number Diff line number Diff line
package Progressing;
package Components;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.plaf.DimensionUIResource;
import java.awt.*;
import java.lang.ref.Reference;

import Components.BasicComponent;
import GameUI.ChessGameUI;

import java.awt.*;
import Model.ChessPiece;

public class ChessPanel extends BasicComponent {
@@ -35,50 +33,48 @@ public class ChessPanel extends BasicComponent {
    }

    @Override
    public void onMouseClicked() {// todo
    public void onMouseClicked() {// todo 鼠标点击时的动作
        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
            if (this.playersChess == ChessPiece.EMPTY) {

            this.playersChess = ChessGameUI.thisGameCore.getCurrentPlayer();
            ChessGameUI.thisGameCore.swapPlayer();
            paintChess(playersChess);
            }

        }
    }

    @Override
    protected void paintComponent(Graphics g) {
    protected void paintComponent(Graphics g) {// 画棋子
        super.paintComponent(g);
        image.paintIcon(this, g, 0, 0);
    }

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

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

    public ChessPiece getChessPiece() {
    public ChessPiece getChessPiece() {// 获取当前位置的棋子
        return playersChess;
    }

    public void setChessPiece(ChessPiece player) {
    public void setChessPiece(ChessPiece player) {// 放棋
        this.playersChess = player;
        paintChess(this.playersChess);
    }

    /* 获取当前棋子的行列(好像没啥用) */
    public int getRow() {
        return row;
    }
+107 −82
Original line number Diff line number Diff line
package Progressing;
package Core;

import java.io.BufferedReader;
import java.io.FileReader;
@@ -6,14 +6,17 @@ 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 ChessBoardPanel gamePanel;// 棋盘
    private int blackScore;// 字面意思
    private int whiteScore;
    static ChessPiece currentPlayer;
    private ChessPiece currentPlayer;// 当前该谁下棋
    private String importPath;
    private String exportPath;

    public GameCore(int rows, int cols) {
        this.gamePanel = new ChessBoardPanel(rows, cols);
@@ -24,14 +27,14 @@ public class GameCore {
        ChessGameUI.thisStatus.setScore(blackScore, whiteScore);
    }

    private void intializeChess() {
    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() {
    public void swapPlayer() {// 交换玩家
        countScore();
        currentPlayer = (currentPlayer == ChessPiece.BLACK) ? ChessPiece.WHITE : ChessPiece.BLACK;
        // todo:complete status panel
@@ -39,23 +42,15 @@ public class GameCore {
        // statusPanel.setScoreText(blackScore, whiteScore);
    }

    public void zoom() {
    public void zoom() {// 缩放
        // todo complete zoom
    }

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

    }

    public ChessPiece getCurrentPlayer() {
        return currentPlayer;
    }

    public ChessBoardPanel getGamePanel() {
        return gamePanel;
    }

    public void readFileData(String fileName) {
    public void readFileData(String fileName) {// 读存档
        // todo: read date from file
        List<String> fileData = new ArrayList<>();
        try {
@@ -71,12 +66,42 @@ public class GameCore {
        }
    }

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

    public boolean canClick(int row, int col) {
    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;
    }

    public void setExportPath(String exportPath) {
        this.exportPath = exportPath;
    }

    public ChessPiece getCurrentPlayer() {
        return currentPlayer;
    }

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