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

bugfix(((((((

parent 7e4d505c
Loading
Loading
Loading
Loading
+1 −1

File changed.

Preview size limit exceeded, changes collapsed.

+29 −127
Original line number Diff line number Diff line
@@ -66,112 +66,6 @@ public class AppController {
        return savedGame;
    }
    
    /**
     * 实现不同状态的切换,START->GAME即为开始游戏,GAME->START即为返回主界面
     *
     * @param status1 要切换的目标状态
     * @param useSave 如果是进入游戏,那么是否调用存档
     */
    public void switchStatus(AppStatus status1, boolean useSave) {
        boolean success = true; //切换过程没有遇到任何问题阻止,哪里有问题就把它改成false
        switch (this.status) {
            case START: //从开始界面离开
                SwingUtilities.invokeLater(() -> {
                    AppFrame.instance.setVisible(false);
                });
                switch (status1) {
                    case START:
                        break;  //????????能进到这里的都不是一般人
                    case ROOM:  //进入房间界面
                        SwingUtilities.invokeLater(() -> {
                            RoomFrame.instance = new RoomFrame();
                            RoomFrame.instance.setVisible(true);
                        });
                        break;
                    case GAME:  //进入游戏界面
                        SwingUtilities.invokeLater(() -> {
                            ChessGameFrame.instance = new ChessGameFrame();
                            ChessGameFrame.instance.setVisible(true);
                        });
                        if (useSave) {
                            Game.instance = savedGame;
                            History.instance = savedHistory;
                        } else {
                            Game.instance = new Game(GameType.LOCAL_2P);
                            History.instance = new History(Game.instance.clone());
                        }
                        break;
                }
                break;
            case ROOM:
                SwingUtilities.invokeLater(() -> {
                    RoomFrame.instance.setVisible(false);
                });
                switch (status1) {
                    case START:
                        SwingUtilities.invokeLater(() -> {
                            AppFrame.instance = new AppFrame();
                            AppFrame.instance.setVisible(true);
                        });
                        break;
                    case ROOM:
                        break;
                    case GAME:  //进入游戏界面
                        SwingUtilities.invokeLater(() -> {
                            ChessGameFrame.instance = new ChessGameFrame();
                            ChessGameFrame.instance.setVisible(true);
                        });
                        if (useSave) {
                            Game.instance = savedGame;
                            History.instance = savedHistory;
                        } else {
                            Game.instance = new Game(GameType.LOCAL_2P);
                            History.instance = new History(Game.instance.clone());
                        }
                        //TODO 传ip port 开Client
                        break;
                }
                break;
            case GAME:
                SwingUtilities.invokeLater(() -> {
                    RoomFrame.instance.setVisible(false);
                });
                switch (status1) {
                    case START:
                        try {
                            write(savePath);
                        } catch (IOException | JSONException e) {
                            e.printStackTrace();
                        }
                        try {
                            read(savePath);
                        } catch (IOException | JSONException e) {
                            e.printStackTrace();
                            savedGame = null;
                            savedHistory = null;
                        }
                        break;
                    case ROOM:
                        break;
                    case GAME:
                        break;
                }
                break;
        }
//        AppFrame.instance.setContentPane(switch (status1){
//            case START -> new StartForm().rootPanel;
//            case ROOM -> new RoomsForm().rootPanel;
//            case GAME -> new GameForm().rootPanel;
//        });
        if (success) {
            this.status = status1;
        }
    }
    
    public void switchStatus(AppStatus status1) {
        this.switchStatus(status1, false);
    }
    
    public void goRooms() {
        SwingUtilities.invokeLater(() -> {
            if (status == AppStatus.START)
@@ -198,6 +92,7 @@ public class AppController {
            } else {
                Game.instance = new Game(type);
                History.instance = new History(Game.instance.clone());
                write(savePath);
            }
            status = AppStatus.GAME;
        }
@@ -242,19 +137,12 @@ public class AppController {
    public void backToStart() {
        if (status == AppStatus.ROOM) {
            RoomFrame.instance.dispose();
        }
        else if (status == AppStatus.GAME) {
        } else if (status == AppStatus.GAME) {
            ChessGameFrame.instance.dispose();
            if (Game.instance != null && Game.instance.gameType != GameType.ONLINE) {
                try {
                write(savePath);
                } catch (IOException e) {
                    e.printStackTrace();
                    JOptionPane.showMessageDialog(null, "阿哦,保存时发生错误了:" + e.getMessage(), "提示", JOptionPane.WARNING_MESSAGE);
                }
            }
        }
        else return;
        } else return;
        SwingUtilities.invokeLater(() -> {
            AppFrame.instance = new AppFrame();
            AppFrame.instance.setVisible(true);
@@ -264,17 +152,24 @@ public class AppController {
    
    /**
     * 年度大无语事件之JSON能做的事JSONObject不能做
     *
     * @param path
     * @throws IOException
     * @throws JSONException
     */
    public void read(String path) throws IOException, JSONException {
        try {
            JSONObject jsonObject = JSONObject.parse(Files.readString(Path.of(path)));
            savedGame = JSON.parseObject(jsonObject.getJSONObject("Game").toString(), Game.class);
            savedHistory = JSON.parseObject(jsonObject.getJSONObject("History").toString(), History.class);
        } catch (JSONException | IOException | IllegalArgumentException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "阿哦,自动读取存档失败:" + e.getMessage(), "提示", JOptionPane.WARNING_MESSAGE);
        }
    }
    
    public void write(String path) throws IOException, JSONException {
    public boolean write(String path) {
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Game", Game.instance);
            jsonObject.put("History", History.instance);
@@ -283,6 +178,13 @@ public class AppController {
                save.createNewFile();
            }
            Files.writeString(Path.of(path), JSON.toJSONString(jsonObject));
            read(path);
            return true;
        } catch (IOException | JSONException e) {
            e.printStackTrace();
            JOptionPane.showMessageDialog(null, "阿哦,保存时发生错误了:" + e.getMessage(), "提示", JOptionPane.WARNING_MESSAGE);
            return false;
        }
    }
    
}
+19 −2
Original line number Diff line number Diff line
@@ -10,6 +10,8 @@ import view.Frame.ChessGameFrame;
import view.Com.ChessboardCom;
import view.Com.SquareCom;

import javax.swing.*;

/**
 * 响应click事件,棋盘的交互逻辑
 */
@@ -24,11 +26,16 @@ public enum ChessClickController {
     */
    private int firstId = -1;
    
    public int getFirstId() {
        return firstId;
    }
    
    /**
     * 响应click事件,棋盘的交互逻辑
     * @param squareCom 点中的方格
     */
    public void onClick(SquareCom squareCom) {
        
        Square square = squareCom.getBackSquare();
        Step step = null;
        //处理一番,结果写到step,否则step为null即无结果
@@ -40,7 +47,6 @@ public enum ChessClickController {
                }else if(chess.getChessColor() == Game.instance.getCurrentColor()){
                    //选中该棋
                    firstId = chess.id;
                    squareCom.repaint();
                }
            }
        } else {    //第二次点击
@@ -69,5 +75,16 @@ public enum ChessClickController {
        //TODO: 这个咋搞
        ChessboardCom.getInstance().repaint();
        ChessGameFrame.getStatusLabel().setText(String.format("%s's TURN", Game.instance.getCurrentColor().getName()));
        ChessGameFrame.instance.refreshScore(0);
        ChessGameFrame.instance.refreshScore(1);
        if(Game.instance.getScoreboard()[0] >= 60){
            JOptionPane.showMessageDialog(null, "红方胜利", "提示", JOptionPane.WARNING_MESSAGE);
            AppController.instance.backToStart();
        }
        if(Game.instance.getScoreboard()[1] >= 60){
            JOptionPane.showMessageDialog(null, "黑方胜利", "提示", JOptionPane.WARNING_MESSAGE);
            AppController.instance.backToStart();
            
        }
    }
}
+6 −3
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@ import java.util.stream.Stream;
//TODO: 悔棋时要由按钮来停止它?
public class AI extends Thread {
    
    public static final int DEPTH = 10;
    public static final int DEPTH = 4;
    
    public static final double Q = 0.8;
    public static final double ALPHA = 2;
@@ -22,8 +22,8 @@ public class AI extends Thread {
    @Override
    public void run() {
        try {
            Thread.sleep(2000);
            Game.instance.resolveStep(eval0());
            Thread.sleep(100);
            Game.instance.resolveStep(eval());
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
@@ -42,6 +42,7 @@ public class AI extends Thread {
        double mark = -10000;
        Step bestStep = null;
        for (Step step: movableSteps(Game.instance.chessboard, Game.instance.hostColor.opponent())){
            System.out.println(step);
            double newMark = mark;
            game = Game.instance.clone();
            if(step instanceof FlipStep flipStep){
@@ -63,6 +64,8 @@ public class AI extends Thread {
        double this_ab = forMax ? -10000 : 10000;
        //Step bestStep = null;
        for (Step step: movableSteps(game.chessboard, player)){
            System.out.println(step);
            
            double newMark = this_ab;
            Game nextState = game.clone();
            if(step instanceof FlipStep flipStep){
+2 −2
Original line number Diff line number Diff line
@@ -77,7 +77,7 @@ public class History {
        this.flipSteps = flipSteps;
        this.moveSteps = moveSteps;
        this.eatSteps = eatSteps;
        this.initialState = initialState.clone();
        this.initialState = initialState;
    }
    
    public History(Game initialState) {
Loading