Commit 2380e420 authored by 刘家荣's avatar 刘家荣 💬
Browse files

bugfix: 修正一些Merge的bug

parent 9c1e55a2
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
import controller.AppController;
import view.ChessGameFrame;

import javax.swing.*;
@@ -5,8 +6,7 @@ import javax.swing.*;
public class Main {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(() -> {
            ChessGameFrame mainFrame = new ChessGameFrame(1080, 720);
            mainFrame.setVisible(true);
            AppController.instance = new AppController();
        });
    }
}
+5 −0
Original line number Diff line number Diff line
@@ -5,6 +5,7 @@ import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import model.History;
import model.game.Game;
import view.ChessGameFrame;

import java.io.IOException;
import java.nio.file.Files;
@@ -15,6 +16,8 @@ import java.nio.file.Path;
 */
public class AppController {
    
    public static AppController instance;
    
    AppStatus status = AppStatus.START;
    
    private History savedHistory;
@@ -30,6 +33,8 @@ public class AppController {
            savedGame = null;
            savedHistory = null;
        }
        ChessGameFrame mainFrame = new ChessGameFrame(1080, 720);
        mainFrame.setVisible(true);
    }
    
    public History getSavedHistory() {
+4 −10
Original line number Diff line number Diff line
package model.game;

import model.game.ChessType;
import model.game.Chessboard;
import model.game.GridPoint;
import model.game.Chess;
import model.game.Square;

public class CheckRule {
    
    /**
@@ -32,8 +26,8 @@ public class CheckRule {
     * @param dstSquare   第二个选中棋子,即被吃的棋子
     */
    public static boolean checkPath(Chess handleChess, Square dstSquare) {
        Location src = handleChess.getLocation();
        Location dst = dstSquare.getLocation();
        Site src = handleChess.getLocation();
        Site dst = dstSquare.getLocation();
        GridPoint move = dst.subtract(src);
        //是否都在主棋盘
        if(src.plate != ChessColor.NONE || dst.plate != ChessColor.NONE)
@@ -48,13 +42,13 @@ public class CheckRule {
            int count = 0;
            if (move.getY() == 0) {
                for (int i = Math.min(src.getX(), dst.getX()) + 1; i < Math.max(src.getX(), dst.getX()); i++) {
                    if (Chessboard.getInstance().getSquareAtPoint(new Location(ChessColor.NONE, src.getY(), i)) instanceof Chess) {
                    if (Chessboard.getInstance().getSquareAtPoint(new Site(ChessColor.NONE, src.getY(), i)) instanceof Chess) {
                        count++;
                    }
                }
            } else if (move.getX() == 0) {
                for (int i = Math.min(src.getY(), dst.getY()) + 1; i < Math.max(src.getY(), dst.getY()); i++) {
                    if (Chessboard.getInstance().getSquareAtPoint(new Location(ChessColor.NONE, i, src.getX())) instanceof Chess) {
                    if (Chessboard.getInstance().getSquareAtPoint(new Site(ChessColor.NONE, i, src.getX())) instanceof Chess) {
                        count++;
                    }
                }
+3 −3
Original line number Diff line number Diff line
@@ -13,10 +13,10 @@ public class Chess extends Square implements Serializable, Cloneable {
    private boolean selected;   //是否被选中
    private boolean isReversal = false; //是否被翻开
    
    public Chess(int id, Location location,
    public Chess(int id, Site site,
                 ChessColor chessColor, ChessType chessType) {
        super(location, id);
        this.location = location;
        super(id, site);
        this.site = site;
        this.chessColor = chessColor;
        this.chessType = chessType;
    }
+35 −35
Original line number Diff line number Diff line
@@ -39,15 +39,15 @@ public class Chessboard implements Serializable, Cloneable {
     */
    public Chessboard() {
        //遍历idGrid,全填-1,同时生成一个locationList包含每一个主棋盘的点位
        ArrayList<Location> locationList = new ArrayList<>();
        ArrayList<Site> siteList = new ArrayList<>();
        for (int i = 0; i < ROW_SIZE; i++) {
            for (int j = 0; j < COL_SIZE; j++) {
                idGrid[i][j] = -1;
                locationList.add(new Location(ChessColor.NONE, i, j));
                siteList.add(new Site(ChessColor.NONE, i, j));
            }
        }
        //打乱locationList
        Collections.shuffle(locationList);
        Collections.shuffle(siteList);
        //逐个生成Chess,同时弹掉locationList头部元素拿去初始化该Chess的位置,把Chess加入chessList尾部,再在idGrid对应位置写上id
        for (ChessColor chessColor : new ChessColor[]{ChessColor.BLACK, ChessColor.RED}) {
            for (ChessType chessType : ChessType.values()) {
@@ -57,10 +57,10 @@ public class Chessboard implements Serializable, Cloneable {
                    default -> 2;
                };
                for (int i = 0; i < cnt; i++) {
                    Location location = locationList.remove(0);
                    Chess chess = new Chess(squareList.size(), location, chessColor, chessType);
                    Site site = siteList.remove(0);
                    Chess chess = new Chess(squareList.size(), site, chessColor, chessType);
                    squareList.add(chess);
                    idGrid[location.y][location.x] = chess.id;
                    idGrid[site.y][site.x] = chess.id;
                }
            }
        }
@@ -96,38 +96,38 @@ public class Chessboard implements Serializable, Cloneable {
    
    /**
     * 获取棋盘中给定坐标的id
     * @param location 棋盘坐标
     * @param site 棋盘坐标
     */
    public int getIdAtPoint(Location location) {
        return location.plate == ChessColor.NONE ?
            idGrid[location.getY()][location.getX()] :
            this.getSideStack(location.plate).getIdAtPoint(location);
    public int getIdAtPoint(Site site) {
        return site.plate == ChessColor.NONE ?
            idGrid[site.getY()][site.getX()] :
            this.getSideStack(site.plate).getIdAtPoint(site);
    }
    
    /**
     * 获取棋盘中给定坐标的id,但是会从表面坐标映射回真实坐标
     * @param location 棋盘坐标
     * @param site 棋盘坐标
     */
    public int getIdAtVisualPoint(Location location) {
        return location.plate == ChessColor.NONE ?
            idGrid[location.getY()][location.getX()] :
            this.getSideStack(location.plate).getIdAtVisualPoint(location);
    public int getIdAtVisualPoint(Site site) {
        return site.plate == ChessColor.NONE ?
            idGrid[site.getY()][site.getX()] :
            this.getSideStack(site.plate).getIdAtVisualPoint(site);
    }
    
    /**
     * 获取棋盘中给定坐标的方格子
     * @param location 棋盘坐标
     * @param site 棋盘坐标
     */
    public Square getSquareAtPoint(Location location) {
        return getSquareById(getIdAtPoint(location));
    public Square getSquareAtPoint(Site site) {
        return getSquareById(getIdAtPoint(site));
    }
    
    /**
     * 获取棋盘中给定坐标的方格子,但是会从表面坐标映射回真实坐标
     * @param location 棋盘坐标
     * @param site 棋盘坐标
     */
    public Square getSquareAtVisualPoint(Location location) {
        return getSquareById(getIdAtVisualPoint(location));
    public Square getSquareAtVisualPoint(Site site) {
        return getSquareById(getIdAtVisualPoint(site));
    }

    /**
@@ -162,37 +162,37 @@ public class Chessboard implements Serializable, Cloneable {
        if(!CheckRule.canMoveTo(chess1, square2)) return null;
        
        Square square3;
        Location location1 = chess1.getLocation();
        Location location2 = square2.getLocation();
        Location location3;
        Site site1 = chess1.getLocation();
        Site site2 = square2.getLocation();
        Site site3;
        
        boolean mode;   //false, 行子; true, 吃子
        if(square2 instanceof Chess chess2){
            mode = true;
            location3 = this.getSideStack(chess2.chessColor).getBuryLocation(chess2);
            square3 = this.getSquareAtPoint(location3);
            site3 = this.getSideStack(chess2.chessColor).getBuryLocation(chess2);
            square3 = this.getSquareAtPoint(site3);
        }else {
            mode = false;
            location3 = null;
            site3 = null;
            square3 = null;
        }
    
        //交换Square的Location成员变量
        chess1.setLocation(location2);
        chess1.setLocation(site2);
        if(mode) {
            square2.setLocation(location3);
            square3.setLocation(location1);
            square2.setLocation(site3);
            square3.setLocation(site1);
        }else{
            square2.setLocation(location1);
            square2.setLocation(site1);
        }
        
        //依据Location成员变量,更新idGrid
        idGrid[location2.y][location2.x] = chess1.id;
        idGrid[location1.y][location1.x] = (mode ? square3 : square2).id;
        idGrid[site2.y][site2.x] = chess1.id;
        idGrid[site1.y][site1.x] = (mode ? square3 : square2).id;
        
        //Return
        if(mode){
            this.getSideStack(location3.plate).updateIdGrid(square2);
            this.getSideStack(site3.plate).updateIdGrid(square2);
            return new EatStep(chess1.id, square2.id, square3.id);
        }else {
            return new MoveStep(chess1.id, square2.id);
Loading