Commit 1388bd7a authored by 刘家荣's avatar 刘家荣 💬
Browse files

bugfix(Clone问题 & 弱化Game的单例模式) feat(hostColor)

parent 6efb70e5
Loading
Loading
Loading
Loading
+8 −0
Original line number Original line Diff line number Diff line
<component name="InspectionProjectProfileManager">
  <profile version="1.0">
    <option name="myName" value="Project Default" />
    <inspection_tool class="AutoCloseableResource" enabled="true" level="WARNING" enabled_by_default="true">
      <option name="METHOD_MATCHER_CONFIG" value="java.util.Formatter,format,java.io.Writer,append,com.google.common.base.Preconditions,checkNotNull,org.hibernate.Session,close,java.io.PrintWriter,printf,java.io.PrintStream,printf,network.Server,newServerSocket" />
    </inspection_tool>
  </profile>
</component>
 No newline at end of file
+1 −1
Original line number Original line Diff line number Diff line
@@ -34,7 +34,7 @@
      <property name="JTree.lineStyle" class="java.lang.String" />
      <property name="JTree.lineStyle" class="java.lang.String" />
    </properties>
    </properties>
  </component>
  </component>
  <component name="ProjectRootManager" version="2" languageLevel="JDK_17" project-jdk-name="17" project-jdk-type="JavaSDK">
  <component name="ProjectRootManager" version="2" languageLevel="JDK_17_PREVIEW" project-jdk-name="17" project-jdk-type="JavaSDK">
    <output url="file://$PROJECT_DIR$/out" />
    <output url="file://$PROJECT_DIR$/out" />
  </component>
  </component>
</project>
</project>
 No newline at end of file
+2 −2
Original line number Original line Diff line number Diff line
package controller;
package controller;


import model.game.Game;
import model.game.Game;
import model.game.Msg;
import model.dataType.Msg;


public class BulletAnim extends Thread{
public class BulletAnim extends Thread{
    @Override
    @Override
    public void run() {
    public void run() {
        super.run();
        super.run();
        while (true){
        while (true){
            for (Msg msg: Game.instance.getMsgList()){
            for (Msg msg: Game.instance.msgList){
                drawBullet(msg);
                drawBullet(msg);
            }
            }
            try {
            try {
+32 −16
Original line number Original line Diff line number Diff line
@@ -9,23 +9,27 @@ import model.dataType.ChessColor;
import model.dataType.GameType;
import model.dataType.GameType;
import model.dataType.Msg;
import model.dataType.Msg;


import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
import java.util.Random;


public class Game implements Cloneable{
public class Game implements Cloneable{


    public static Game instance;
    public static Game instance;


    private Chessboard chessboard;
    public final Chessboard chessboard;


    private List<Msg> msgList = new ArrayList<>();
    public final List<Msg> msgList = new ArrayList<>();


    private int[] scoreboard = {0, 0};  //计分板
    private final int[] scoreboard = {0, 0};  //计分板


    private ChessColor currentColor = ChessColor.RED;   //执棋颜色
    private ChessColor currentColor = ChessColor.RED;   //执棋颜色
    
    
    public final GameType gameType;
    public final GameType gameType;
    
    
    public final ChessColor hostColor;  //服务器颜色,或者人机对战中人类颜色

    public Chessboard getChessboard() {
    public Chessboard getChessboard() {
        return chessboard;
        return chessboard;
    }
    }
@@ -43,29 +47,28 @@ public class Game implements Cloneable{
    }
    }
    
    
    public Game(GameType gameType){
    public Game(GameType gameType){
        this(gameType, false);
    }
    
    public Game(GameType gameType, boolean setInstance){
        if(setInstance) Game.instance = this;
        this.gameType = gameType;
        this.gameType = gameType;
        this.chessboard = new Chessboard(setInstance);
        this.hostColor = new Random().nextInt(2) == 1 ? ChessColor.RED : ChessColor.BLACK;
        this.chessboard = new Chessboard();
    }
    }
    
    
    /**
     * 不知道吃子第三个参数是啥的也可以送一个MoveStep
     */
    public Step resolveStep(Step step){
    public Step resolveStep(Step step){
        Step res = null;
        Step res = null;
        if(step instanceof FlipStep flipStep){
        if(step instanceof FlipStep flipStep){
            res = resolveFlip(Chessboard.instance.getSquareById(flipStep.srcChessId));
            res = resolveFlip(chessboard.getSquareById(flipStep.srcChessId));
        }
        }
        if (step instanceof MoveStep moveStep){
        if (step instanceof MoveStep moveStep){
            res = resolveMove(
            res = resolveMove(
                Chessboard.instance.getSquareById(moveStep.srcChessId),
                chessboard.getSquareById(moveStep.srcChessId),
                Chessboard.instance.getSquareById(moveStep.dstSquareId));
                chessboard.getSquareById(moveStep.dstSquareId));
        }
        }
        if (step instanceof EatStep eatStep){
        if (step instanceof EatStep eatStep){
            res = resolveMove(
            res = resolveMove(
                Chessboard.instance.getSquareById(eatStep.srcChessId),
                chessboard.getSquareById(eatStep.srcChessId),
                Chessboard.instance.getSquareById(eatStep.dstChessId));
                chessboard.getSquareById(eatStep.dstChessId));
        }
        }
        if(res != null){
        if(res != null){
            if(this.gameType == GameType.LOCAL_AI){
            if(this.gameType == GameType.LOCAL_AI){
@@ -102,9 +105,22 @@ public class Game implements Cloneable{
    @Override
    @Override
    public Game clone() {
    public Game clone() {
        try {
        try {
            return (Game) super.clone();
            Game clone = (Game) super.clone();
            //就这?直接反射
            Field f = this.getClass().getDeclaredField("chessboard");
            f.setAccessible(true);
            f.set(clone, this.chessboard.clone());
            f.setAccessible(false);
            //就这?
            f = this.getClass().getDeclaredField("msgList");
            f.setAccessible(true);
            f.set(clone, new ArrayList<>(this.msgList));
            f.setAccessible(false);
            return clone;
        } catch (CloneNotSupportedException e) {
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
            throw new AssertionError();
        } catch (NoSuchFieldException | IllegalAccessException e) {
            throw new RuntimeException(e);
        }
        }
    }
    }
}
}
 No newline at end of file
+6 −5
Original line number Original line Diff line number Diff line
@@ -17,14 +17,15 @@ public class SideStack implements Serializable, Cloneable {
    
    
    private final ChessColor chessColor;
    private final ChessColor chessColor;
    
    
    public SideStack(ChessColor chessColor) {
    public SideStack(ChessColor chessColor, Chessboard parent) {
        this.chessColor = chessColor;
        this.chessColor = chessColor;
        ArrayList<Square> SquareList = Chessboard.getInstance().squareList;
        ArrayList<Square> squareList = parent.squareList;
        //初始化所有Square
        //初始化所有Square
        for (int i = 0; i < ROW_SIZE; i++) {
        for (int i = 0; i < ROW_SIZE; i++) {
            for (int j = 0; j < COL_SIZE; j++) {
            for (int j = 0; j < COL_SIZE; j++) {
                Square square = new EmptySlot(SquareList.size(), new Site(this.chessColor, i, j));
                Square square = new EmptySlot(
                SquareList.add(square);
                    Chessboard.ID_DIVISION + squareList.size(), new Site(this.chessColor, i, j));
                squareList.add(square);
                idGrid[i][j] = square.id;
                idGrid[i][j] = square.id;
            }
            }
        }
        }
@@ -101,7 +102,7 @@ public class SideStack implements Serializable, Cloneable {
    public SideStack clone() {
    public SideStack clone() {
        try {
        try {
            SideStack clone = (SideStack) super.clone();
            SideStack clone = (SideStack) super.clone();
            // TODO: copy mutable state here, so the clone can't change the internals of the original
            clone.idGrid = this.idGrid.clone();
            return clone;
            return clone;
        } catch (CloneNotSupportedException e) {
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
            throw new AssertionError();
Loading