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

Merge remote-tracking branch 'origin/itr1-back'

parents 147e1c53 c6fae6dc
Loading
Loading
Loading
Loading
+67 −21
Original line number Diff line number Diff line
package controller;

import model.game.Chessboard;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONException;
import com.alibaba.fastjson2.JSONObject;
import model.History;
import model.game.Game;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;

/**
 * 负责全局工作,切换界面时引导相关Model初始化,也负责读写文存档
 */
public class AppController {
    
    AppStatus status = AppStatus.START;
    
    private History savedHistory;
    
    private Game savedGame;
    
    public AppController(Chessboard chessboard) {
        this.chessboard = chessboard;
    public static final String savePath = "/res/save/save.json";
    
    public AppController() {
        try {
            read(savePath);
        } catch (IOException | JSONException e) {
            savedGame = null;
            savedHistory = null;
        }
    }
    
    public History getSavedHistory() {
        return savedHistory;
    }
    
    public History getSavedGame(){
    public Game getSavedGame() {
        return savedGame;
    }
    
    public List<String> loadGameFromFile(String path) {
    public void switchStatus(AppStatus status1){
        switch(this.status){
            case START:
                switch (status1){
                    case START:
                        break;
                    case ROOM:
                        break;
                    case GAME:
                    
                }
                break;
            case ROOM:
                break;
            case GAME:
                switch (status1){
                    case START:
                        try {
            List<String> chessData = Files.readAllLines(Path.of(path));
            chessboard.loadGame(chessData);
            return chessData;
        } catch (IOException e) {
            e.printStackTrace();
                            write(savePath);
                        }catch (IOException | JSONException e){
                        
                        }
        return null;
                        //
                    case ROOM:
                        break;
                    case GAME:
                        break;
                }
                break;
        }
    }
    
    public void read(String path) throws IOException, JSONException {
        JSONObject jsonObject = JSONObject.parse(Files.readString(Path.of(path)));
        savedGame = jsonObject.getObject("Game", Game.class);
        savedHistory = jsonObject.getObject("History", History.class);
    }
    
    public void write(String path) throws IOException, JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("Game", Game.instance);
        jsonObject.put("History", History.instance);
        Files.writeString(Path.of(path), JSON.toJSONString(jsonObject));
    }
    
}
+5 −0
Original line number Diff line number Diff line
package controller;

public enum AppStatus {
    START, ROOM, GAME
}
+26 −43
Original line number Diff line number Diff line
package controller;


import model.History;
import model.Step;
import model.game.*;
import view.ChessGameFrame;
import view.ChessboardCom;
@@ -18,7 +20,7 @@ public enum ChessClickController {
    /**
     * 记忆两次点击中第一次点击选中的棋子
     */
    private Chess first = null;
    private int firstId = -1;

    /**
     * 响应click事件,棋盘的交互逻辑
@@ -26,53 +28,34 @@ public enum ChessClickController {
     */
    public void onClick(SquareCom squareCom) {
        Square square = squareCom.getBackSquare();

        if (first == null) {    //判断第一次点击
            if (square instanceof Chess chess && handleFirst(chess)) {
                chess.setSelected(true);
                first = chess;
        Step step = null;
        //处理一番,结果写到step,否则step为null即无结果
        if (firstId == -1) {    //判断第一次点击
            if (square instanceof Chess chess) {
                if (!chess.isReversal()) {  //未翻开,翻之
                    step = Game.instance.resolveFlip(chess);
                }else if(chess.getChessColor() == Game.instance.getCurrentColor()){
                    //选中该棋
                    firstId = chess.id;
                    squareCom.repaint();
                }
            }
        } else {    //第二次点击
            if (first == square) {  //点击同一个棋子取消选取
                first.setSelected(false);
            if (firstId == square.id) {  //点击同一个棋子取消选取
                squareCom.repaint();
                first = null;
            } else if (Chess.canMoveTo(first, square.getGridPoint())) {
                //取消选择
                first.setSelected(false);
                //走吃
                Chessboard.getInstance().moveAndEat(first, square);
                if (square instanceof Chess chess)
                    ChessboardCom.getInstance().getSideStackComs()[chess.getChessColor().ordinal()].repaint();
                ChessClickController.INSTANCE.swapPlayer();
                firstId = -1;
            } else {
                step = Game.instance.resolveMove(Chessboard.getInstance().getSquareById(firstId), square);
                if(step != null){
                    //擦除记忆
                first = null;
                    firstId = -1;
                }
            }
        }


    /**
     * @param chess 目标选取的棋子
     * @return 目标选取的棋子是否与棋盘记录的当前行棋方颜色相同
     */
    private boolean handleFirst(Chess chess) {
        if (!chess.isReversal()) {
            chess.setReversal(true);
            System.out.printf("onClick to reverse a chess at %s\n",
                    chess.getGridPoint());
            ChessboardCom.getInstance().getSquareComAtPoint(chess.getGridPoint()).repaint();
            ChessClickController.INSTANCE.swapPlayer();
            return false;
        }
        return chess.getChessColor() == Game.instance.getCurrentColor();
        if(step != null){
            History.instance.getSteps().add(step);
        }

    public void swapPlayer() {
        Chessboard.getInstance().setCurrentColor(
                Chessboard.getInstance().getCurrentColor() == ChessColor.BLACK ? ChessColor.RED : ChessColor.BLACK
        );
        ChessGameFrame.getStatusLabel().setText(String.format("%s's TURN", Chessboard.getInstance().getCurrentColor().getName()));
        ChessboardCom.getInstance().repaint();
        ChessGameFrame.getStatusLabel().setText(String.format("%s's TURN", Game.instance.getCurrentColor().getName()));
    }
}

src/model/EatStep.java

0 → 100644
+41 −0
Original line number Diff line number Diff line
package model;

import model.game.Chess;
import model.game.Chessboard;
import model.game.EmptySlot;
import model.game.Square;

/**
 * 记录一步行棋
 * src和dst互换
 */
public class EatStep extends Step {
    /**
     * 源棋子id
     */
    public final int srcChessId;
    
    /**
     * 目标空格/棋子id
     */
    public final int dstChessId;
    
    /**
     * 亡子堆原来那个Square的id
     */
    public final int burySquareId;
    
    public EatStep(int srcChessId, int dstChessId, int burySquareId) {
        if (Chessboard.getInstance().getSquareById(srcChessId) instanceof Chess
            && Chessboard.getInstance().getSquareById(dstChessId) instanceof Chess
            && !(Chessboard.getInstance().getSquareById(burySquareId) instanceof Chess)){
            this.srcChessId = srcChessId;
            this.dstChessId = dstChessId;
            this.burySquareId = burySquareId;
        }else {
            throw new IllegalArgumentException(String.format(
                "Illegal Step:{ srcChessId: %d, dstChessId: %d, burySquareId}",
                srcChessId, dstChessId, burySquareId));
        }
    }
}
 No newline at end of file
+26 −0
Original line number Diff line number Diff line
package model;

import model.game.Chess;
import model.game.Chessboard;
import model.game.EmptySlot;

/**
 * 记录一步行棋
 * src和dst互换
 */
public class FlipStep extends Step {
    /**
     * 棋子id
     */
    public final int srcChessId;
    
    public FlipStep(int srcChessId) {
        if (Chessboard.getInstance().getSquareById(srcChessId) instanceof Chess) {
            this.srcChessId = srcChessId;
        }else {
            throw new IllegalArgumentException(String.format(
                "Illegal Step:{ srcChessId: %d }",
                srcChessId));
        }
    }
}
 No newline at end of file
Loading