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

feat(new ):

parent e3c1b333
Loading
Loading
Loading
Loading
+1 −0
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() {
+61 −0
Original line number Diff line number Diff line
package Components;

import javax.swing.JPanel;
import javax.swing.plaf.DimensionUIResource;

import GameUI.ChessGameUI;

import java.awt.*;
import java.lang.reflect.WildcardType;

import Model.ChessPiece;

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

    public ChessBoardPanel(int boardRows, int boardCols) {
        this.setVisible(true);
        this.setFocusable(true);
        this.setLayout(new GridLayout(this.boardRows, this.boardCols));
        // this.setBackground(Color.yellow);// todo: choose a beautiful color
        this.boardRows = boardRows;
        this.boardCols = boardCols;
        chessBoard = new ChessPanel[boardRows][boardCols];
        ChessPanel.setSize(100);
        intitalChessPanel();
    }

    public void intitalChessPanel() {// 初始化每个格子
        for (int i = 0; i != boardRows; ++i) {
            for (int j = 0; j != boardCols; ++j) {
                ChessPanel tempPanel = new ChessPanel(i, j);
                this.add(tempPanel);
                chessBoard[i][j] = tempPanel;
            }
        }
    }

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

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

    public boolean canClickGrid(int row, int col, ChessPiece currentPlayer) {// 判断该格子是否可以放棋
        // todo check if can put chesspiece on current grind
        return true;
    }
}
 No newline at end of file
+104 −0
Original line number Diff line number Diff line
package Components;

import javax.swing.ImageIcon;
import javax.swing.plaf.DimensionUIResource;

import GameUI.ChessGameUI;

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

public class ChessPanel extends BasicComponent {
    private int row;
    private int col;
    private static int size = 8;
    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 image;

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

    }

    public static void setSize(int size) {
        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",
                ChessGameUI.thisGameCore.getCurrentPlayer(), row, col);
        // todo: complete mouse click method
        if (ChessGameUI.thisGameCore.canClick(row, col)) {// todo

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

        }
    }

    @Override
    protected void paintComponent(Graphics g) {// 画棋子
        super.paintComponent(g);

        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) {// 画棋子
        switch (playersChess) {
            case BLACK -> image = blcakImg;
            case WHITE -> image = whiteImg;
            default -> throw new IllegalArgumentException("Unexpected value: " + playersChess);
        }
        repaint();
    }

    public void configSize(int grindPanelSize) {// 初始化长宽
    }

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

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

    /* 获取当前棋子的行列(好像没啥用) */
    public int getRow() {
        return row;
    }

    public int getCol() {
        return col;
    }
}

Core/GameCore.java

0 → 100644
+126 −0
Original line number Diff line number Diff line
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;
    }
}
+32 −22
Original line number Diff line number Diff line
@@ -2,16 +2,15 @@ package GameUI;

import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

import javax.sound.midi.SysexMessage;
import javax.swing.*;
/*
 * Created by JFormDesigner on Tue Nov 30 22:41:46 CST 2021
 */

import Core.GameCore;
import Model.CheatStatus;
import Model.ChessPiece;
import Progressing.GameCore;

/**
 * @author uint44t
@@ -20,39 +19,41 @@ public class ChessGameUI {

	public ChessGameUI() {
		initComponents();

	}

	private int rows = 8;
	private int cols = 8;

	public static GameCore thisGameCore;
	private ChessPiece player;
	public static GameCore contorl;
	public static StatusUI thisStatus;
	private CheatStatus thisCheatModeStatus = CheatStatus.Off;
	public static CheatStatus thisCheatModeStatus = CheatStatus.Off;// cheat模式的选择状态
	private PlayerName thisPlayerName;

	public CheatStatus getCheatStatus() {
		return thisCheatModeStatus;
	}

	private void importButtonActionPerformed(ActionEvent e) {
		// TODO add your code here
	private void importButtonActionPerformed() {// import按钮触发事件
		String tem = new FileChooser(true).Path();
		if (tem != null)
			thisGameCore.setImportpath(tem);
	}

	private void exportButtonActionPerformed(ActionEvent e) {
		// TODO add your code here
	private void exportButtonActionPerformed() {// export按钮触发事件
		String tem = new FileChooser(false).Path();
		if (tem != null)
			thisGameCore.setImportpath(tem);
	}

	private void restartButtonActionPerformed(ActionEvent e) {
	private void restartButtonActionPerformed() {// restart按钮触发事件
		// TODO add your code here
	}

	private void undoButtonActionPerformed() {
	private void undoButtonActionPerformed() {// undo按钮触发事件
		// TODO add your code here
	}

	private void cheatOnOffButtonActionPerformed() {
	private void cheatOnOffButtonActionPerformed() {// cheat单选按钮被按下的触发事件
		if (cheatOnOff.isSelected()) {
			cheat.setVisible(true);
			thisCheatModeStatus = CheatStatus.Black;
@@ -63,7 +64,7 @@ public class ChessGameUI {
		}
	}

	private void cheatListener(ItemEvent e) {
	private void cheatListener(ItemEvent e) {// 上面出发后弹出的选项卡的触发事件
		String select = e.getItem().toString();
		if (cheatOnOff.isSelected()) {
			switch (select) {
@@ -84,11 +85,13 @@ public class ChessGameUI {
	JButton undoButton = new JButton();
	JComboBox cheat = new JComboBox();
	JRadioButton cheatOnOff = new JRadioButton();
	JPanel center;

	private void initComponents() {
		// JFormDesigner - Component initialization - DO NOT MODIFY
		// //GEN-BEGIN:initComponents
		thisStatus = new StatusUI("blackPlayer", "whitePlayer");
		thisPlayerName = new PlayerName("blackName", "whiteName");
		thisGameCore = new GameCore(rows, cols);

		// ======== background ========
@@ -99,16 +102,23 @@ public class ChessGameUI {
			background.setName("backFrame");
			Container bcakgroundContentPane = background.getContentPane();
			bcakgroundContentPane.setLayout(new BorderLayout(10, 10));
			background.setPreferredSize(new Dimension(700, 500));
			background.addComponentListener(new ComponentAdapter() {
				@Override
				public void componentResized(ComponentEvent e) {
					thisGameCore.zoom();
					System.out.println("zooming");
					thisGameCore.zoom(bcakgroundContentPane.getWidth(),
							bcakgroundContentPane.getHeight());
					thisGameCore.getGamePanel().updateUI();
					// System.out.println(background.getWidth() + "\t" + background.getHeight());
				}
			});

			bcakgroundContentPane.add(thisGameCore.getGamePanel(), BorderLayout.CENTER);
			center = new JPanel();
			center.add(thisGameCore.getGamePanel());
			// background.add(new JPanel().add(thisGameCore.getGamePanel()),
			// BorderLayout.CENTER);
			bcakgroundContentPane.add(center, BorderLayout.CENTER);
			bcakgroundContentPane.add(thisStatus, BorderLayout.WEST);
			bcakgroundContentPane.add(thisPlayerName, BorderLayout.EAST);

			// ======== bottomButtons ========
			{
@@ -117,17 +127,17 @@ public class ChessGameUI {

				// ---- importButton ----
				importButton.setText("Import");
				importButton.addActionListener(e -> importButtonActionPerformed(e));
				importButton.addActionListener(e -> importButtonActionPerformed());
				bottomButtons.add(importButton);

				// ---- exportButton ----
				exportButton.setText("Export");
				exportButton.addActionListener(e -> exportButtonActionPerformed(e));
				exportButton.addActionListener(e -> exportButtonActionPerformed());
				bottomButtons.add(exportButton);

				// ---- restartButton ----
				restartButton.setText("Restart");
				restartButton.addActionListener(e -> restartButtonActionPerformed(e));
				restartButton.addActionListener(e -> restartButtonActionPerformed());
				bottomButtons.add(restartButton);

				// ---- undoButton ----
Loading