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

fix( ):

BREAKING CHANGE:
parent 11e0dc1c
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -4,7 +4,6 @@ import java.awt.*;

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

public class ChessGridComponent extends BasicComponent {
    public static int chessSize;

Progressing/ChessBoardPanel.java

deleted100644 → 0
+0 −50
Original line number Diff line number Diff line
package Progressing;

import javax.swing.JPanel;
import javax.swing.border.Border;
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 static int everyFromLength = 50;
    private ChessPanel[][] chessBoard;

    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.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;
            }
        }
    }

    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

Progressing/ChessPanel.java

deleted100644 → 0
+0 −89
Original line number Diff line number Diff line
package Progressing;

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 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) {
        this.setPreferredSize(new DimensionUIResource(size, size));
        setVisible(true);
        this.row = row;
        this.col = col;
        image = emptyImg;
        playersChess = ChessPiece.EMPTY;

    }

    public static void setSize(int size) {
        ChessPanel.size = size;
    }

    @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
            if (this.playersChess == ChessPiece.EMPTY) {
                this.playersChess = ChessGameUI.thisGameCore.getCurrentPlayer();
                ChessGameUI.thisGameCore.swapPlayer();
                paintChess(playersChess);
            }
        }
    }

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

    private void paintChess(ChessPiece playersChess/* , Graphics g */) {
        switch (playersChess) {
            case BLACK -> {
                image = blcakImg;
            }
            case WHITE -> {
                image = whiteImg;
            }
        }
        repaint();
    }

    public void configSize(int grindPanelSize) {
        this.setPreferredSize(new DimensionUIResource(grindPanelSize / 8, grindPanelSize / 8));
    }

    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;
    }
}

Progressing/GameCore.java

deleted100644 → 0
+0 −82
Original line number Diff line number Diff line
package Progressing;

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

import GameUI.ChessGameUI;
import Model.ChessPiece;

public class GameCore {
    private ChessBoardPanel gamePanel;
    private int blackScore;
    private int whiteScore;
    static ChessPiece currentPlayer;

    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;
        // todo:complete status panel
        // statusPanel.setPlayerText(currentPlayer.name());
        // statusPanel.setScoreText(blackScore, whiteScore);
    }

    public void zoom() {
    }

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

    }

    public ChessPiece getCurrentPlayer() {
        return currentPlayer;
    }

    public ChessBoardPanel getGamePanel() {
        return gamePanel;
    }

    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) {
        return gamePanel.canClickGrid(row, col, currentPlayer);
    }

}