Loading src/model/game/Game.java +16 −4 Changes for src/model/game/Game.java: 16 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -70,6 +70,12 @@ public class Game implements Cloneable{ chessboard.getSquareById(eatStep.srcChessId), chessboard.getSquareById(eatStep.dstChessId)); } if(res != null){ if(this.gameType == GameType.LOCAL_AI){ AI ai = new AI(); ai.start(); } } return res; } Loading @@ -96,23 +102,29 @@ public class Game implements Cloneable{ return step; } public Step revertStep(Step step){ /** * 虽然正常走一步时History不归Game管,但是悔棋时History会被此方法减去一步 * 如果失败,返回值为null */ public Step revertStep(){ Step last = History.instance.steps.get(History.instance.steps.size() - 1); Step res = null; if(step instanceof FlipStep flipStep){ if(last instanceof FlipStep flipStep){ res = revertFlip(chessboard.getSquareById(flipStep.srcChessId)); } if (step instanceof MoveStep moveStep){ if (last instanceof MoveStep moveStep){ res = revertMove( chessboard.getSquareById(moveStep.srcChessId), chessboard.getSquareById(moveStep.dstSquareId), null); } if (step instanceof EatStep eatStep){ if (last instanceof EatStep eatStep){ res = revertMove( chessboard.getSquareById(eatStep.srcChessId), chessboard.getSquareById(eatStep.dstChessId), chessboard.getSquareById(eatStep.burySquareId)); } if(res != null) History.instance.steps.remove(History.instance.steps.size() - 1); return res; } Loading src/network/Client.java +18 −0 Changes for src/network/Client.java: 18 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -118,6 +118,9 @@ public class Client { * "step": FlipStep Object * }, * { * "type": "Revert" * } * { * "type": "Full" * } * </pre> Loading Loading @@ -153,6 +156,13 @@ public class Client { this.send(JSON.toJSONString(jsonObject)); } /** * 前端可以调用 */ public void sendRevert(){ this.send("{\"type\":\"Revert\"}"); } /** * 发送消息,但是不调用前端绘制 * <br/>只有服务器把消息发回来才会调用recvMsgCallback,它调用前端绘制 Loading Loading @@ -191,6 +201,9 @@ public class Client { * "step": EatStep Object, * }, * { * "type": "Revert" * }, * { * "type": "OnlineInfo", * "onlineStatus" : onlineStatus //仅代表游戏是否开始 * "serverAccount": Account, Loading @@ -217,6 +230,11 @@ public class Client { case "FlipStep" -> recvStepCallback(json.getObject("flipStep", FlipStep.class)); case "MoveStep" -> recvStepCallback(json.getObject("moveStep", MoveStep.class)); case "EatStep" -> recvStepCallback(json.getObject("eatStep", EatStep.class)); case "Revert" -> { if(Game.instance.revertStep() != null){ AppFrame.instance.repaint(); }else requestFull(); } case "OnlineInfo" -> recvOnlineInfoCallback( json.getObject("onlineStatus", OnlineStatus.class), json.getObject("serverAccount", Account.class), Loading src/network/Server.java +17 −5 Changes for src/network/Server.java: 17 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -27,7 +27,7 @@ import java.util.Scanner; public class Server { private static Server instance = null; public Server getInstance() { public static Server getInstance() { return instance; } Loading Loading @@ -175,6 +175,9 @@ public class Server { * "step": EatStep Object, * }, * { * "type": "Revert" * }, * { * "type": "OnlineInfo", * "onlineStatus" : onlineStatus //仅代表游戏是否开始 * "serverAccount": Account, Loading Loading @@ -215,21 +218,24 @@ public class Server { /** * History只伴随着一次Step广播而增加 * @param step */ public void broadcastStep(Step step) { History.instance.steps.add(step); JSONObject jsonObject = new JSONObject(); jsonObject.put("type", switch (step) { case FlipStep x -> "FlipStep"; case MoveStep x -> "MoveStep"; case EatStep x -> "EatStep"; case FlipStep ignored -> "FlipStep"; case MoveStep ignored -> "MoveStep"; case EatStep ignored -> "EatStep"; default -> throw new IllegalStateException("Unexpected value: " + step); }); jsonObject.put("step", step); this.broadcast(JSON.toJSONString(jsonObject)); } public void broadcastRevert(){ this.broadcast("{\"type\":\"Revert\"}"); } public void broadcastMsg(Msg msg) { JSONObject jsonObject = new JSONObject(); jsonObject.put("type", "Msg"); Loading Loading @@ -292,6 +298,9 @@ public class Server { * "step": FlipStep Object * }, * { * "type": "Revert" * }, * { * "type": "Full" * } * </pre> Loading @@ -305,6 +314,9 @@ public class Server { case "FlipStep" -> recvStepCallback(json.getObject("step", FlipStep.class)); case "MoveStep" -> recvStepCallback(json.getObject("step", MoveStep.class)); case "EatStep" -> recvStepCallback(json.getObject("step", EatStep.class)); case "Revert" -> { if(Game.instance.revertStep() != null) broadcastRevert(); } case "Full" -> sendFull(account); } } Loading Loading
src/model/game/Game.java +16 −4 Changes for src/model/game/Game.java: 16 added lines, 4 removed lines. Original line number Diff line number Diff line Loading @@ -70,6 +70,12 @@ public class Game implements Cloneable{ chessboard.getSquareById(eatStep.srcChessId), chessboard.getSquareById(eatStep.dstChessId)); } if(res != null){ if(this.gameType == GameType.LOCAL_AI){ AI ai = new AI(); ai.start(); } } return res; } Loading @@ -96,23 +102,29 @@ public class Game implements Cloneable{ return step; } public Step revertStep(Step step){ /** * 虽然正常走一步时History不归Game管,但是悔棋时History会被此方法减去一步 * 如果失败,返回值为null */ public Step revertStep(){ Step last = History.instance.steps.get(History.instance.steps.size() - 1); Step res = null; if(step instanceof FlipStep flipStep){ if(last instanceof FlipStep flipStep){ res = revertFlip(chessboard.getSquareById(flipStep.srcChessId)); } if (step instanceof MoveStep moveStep){ if (last instanceof MoveStep moveStep){ res = revertMove( chessboard.getSquareById(moveStep.srcChessId), chessboard.getSquareById(moveStep.dstSquareId), null); } if (step instanceof EatStep eatStep){ if (last instanceof EatStep eatStep){ res = revertMove( chessboard.getSquareById(eatStep.srcChessId), chessboard.getSquareById(eatStep.dstChessId), chessboard.getSquareById(eatStep.burySquareId)); } if(res != null) History.instance.steps.remove(History.instance.steps.size() - 1); return res; } Loading
src/network/Client.java +18 −0 Changes for src/network/Client.java: 18 added lines, 0 removed lines. Original line number Diff line number Diff line Loading @@ -118,6 +118,9 @@ public class Client { * "step": FlipStep Object * }, * { * "type": "Revert" * } * { * "type": "Full" * } * </pre> Loading Loading @@ -153,6 +156,13 @@ public class Client { this.send(JSON.toJSONString(jsonObject)); } /** * 前端可以调用 */ public void sendRevert(){ this.send("{\"type\":\"Revert\"}"); } /** * 发送消息,但是不调用前端绘制 * <br/>只有服务器把消息发回来才会调用recvMsgCallback,它调用前端绘制 Loading Loading @@ -191,6 +201,9 @@ public class Client { * "step": EatStep Object, * }, * { * "type": "Revert" * }, * { * "type": "OnlineInfo", * "onlineStatus" : onlineStatus //仅代表游戏是否开始 * "serverAccount": Account, Loading @@ -217,6 +230,11 @@ public class Client { case "FlipStep" -> recvStepCallback(json.getObject("flipStep", FlipStep.class)); case "MoveStep" -> recvStepCallback(json.getObject("moveStep", MoveStep.class)); case "EatStep" -> recvStepCallback(json.getObject("eatStep", EatStep.class)); case "Revert" -> { if(Game.instance.revertStep() != null){ AppFrame.instance.repaint(); }else requestFull(); } case "OnlineInfo" -> recvOnlineInfoCallback( json.getObject("onlineStatus", OnlineStatus.class), json.getObject("serverAccount", Account.class), Loading
src/network/Server.java +17 −5 Changes for src/network/Server.java: 17 added lines, 5 removed lines. Original line number Diff line number Diff line Loading @@ -27,7 +27,7 @@ import java.util.Scanner; public class Server { private static Server instance = null; public Server getInstance() { public static Server getInstance() { return instance; } Loading Loading @@ -175,6 +175,9 @@ public class Server { * "step": EatStep Object, * }, * { * "type": "Revert" * }, * { * "type": "OnlineInfo", * "onlineStatus" : onlineStatus //仅代表游戏是否开始 * "serverAccount": Account, Loading Loading @@ -215,21 +218,24 @@ public class Server { /** * History只伴随着一次Step广播而增加 * @param step */ public void broadcastStep(Step step) { History.instance.steps.add(step); JSONObject jsonObject = new JSONObject(); jsonObject.put("type", switch (step) { case FlipStep x -> "FlipStep"; case MoveStep x -> "MoveStep"; case EatStep x -> "EatStep"; case FlipStep ignored -> "FlipStep"; case MoveStep ignored -> "MoveStep"; case EatStep ignored -> "EatStep"; default -> throw new IllegalStateException("Unexpected value: " + step); }); jsonObject.put("step", step); this.broadcast(JSON.toJSONString(jsonObject)); } public void broadcastRevert(){ this.broadcast("{\"type\":\"Revert\"}"); } public void broadcastMsg(Msg msg) { JSONObject jsonObject = new JSONObject(); jsonObject.put("type", "Msg"); Loading Loading @@ -292,6 +298,9 @@ public class Server { * "step": FlipStep Object * }, * { * "type": "Revert" * }, * { * "type": "Full" * } * </pre> Loading @@ -305,6 +314,9 @@ public class Server { case "FlipStep" -> recvStepCallback(json.getObject("step", FlipStep.class)); case "MoveStep" -> recvStepCallback(json.getObject("step", MoveStep.class)); case "EatStep" -> recvStepCallback(json.getObject("step", EatStep.class)); case "Revert" -> { if(Game.instance.revertStep() != null) broadcastRevert(); } case "Full" -> sendFull(account); } } Loading