From e768c9408a36b2b45ca4631b71e23f9b338f3692 Mon Sep 17 00:00:00 2001 From: Peter Osterlund Date: Sat, 10 Oct 2015 17:43:16 +0200 Subject: [PATCH] DroidFish: More efficient update of "thinking info" when the engine provides data faster than the GUI can handle. --- .../src/org/petero/droidfish/DroidFish.java | 13 ++++++------- .../org/petero/droidfish/GUIInterface.java | 12 ++++++++++-- .../gamelogic/DroidChessController.java | 19 ++++++++++++++----- 3 files changed, 30 insertions(+), 14 deletions(-) diff --git a/DroidFish/src/org/petero/droidfish/DroidFish.java b/DroidFish/src/org/petero/droidfish/DroidFish.java index 68eb584..9a1419f 100644 --- a/DroidFish/src/org/petero/droidfish/DroidFish.java +++ b/DroidFish/src/org/petero/droidfish/DroidFish.java @@ -1709,13 +1709,12 @@ public class DroidFish extends Activity implements GUIInterface { private ArrayList variantMoves = null; @Override - public void setThinkingInfo(String pvStr, String statStr, String bookInfo, - ArrayList> pvMoves, ArrayList bookMoves) { - thinkingStr1 = pvStr; - thinkingStr2 = statStr; - bookInfoStr = bookInfo; - this.pvMoves = pvMoves; - this.bookMoves = bookMoves; + public void setThinkingInfo(ThinkingInfo ti) { + thinkingStr1 = ti.pvStr; + thinkingStr2 = ti.statStr; + bookInfoStr = ti.bookInfo; + this.pvMoves = ti.pvMoves; + this.bookMoves = ti.bookMoves; updateThinkingInfo(); if (ctrl.computerBusy()) { diff --git a/DroidFish/src/org/petero/droidfish/GUIInterface.java b/DroidFish/src/org/petero/droidfish/GUIInterface.java index e8b39c8..93967e7 100644 --- a/DroidFish/src/org/petero/droidfish/GUIInterface.java +++ b/DroidFish/src/org/petero/droidfish/GUIInterface.java @@ -52,9 +52,17 @@ public interface GUIInterface { /** Update the list of moves. */ public void moveListUpdated(); + final public static class ThinkingInfo { + public int id; + public String pvStr; + public String statStr; + public String bookInfo; + public ArrayList> pvMoves; + public ArrayList bookMoves; + } + /** Update the computer thinking information. */ - public void setThinkingInfo(String pvStr, String statStr, String bookInfo, - ArrayList> pvMoves, ArrayList bookMoves); + public void setThinkingInfo(ThinkingInfo ti); /** Ask what to promote a pawn to. Should call reportPromotePiece() when done. */ public void requestPromotePiece(); diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java b/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java index 791aeb6..f07a0fc 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java @@ -30,6 +30,7 @@ import java.util.Map; import org.petero.droidfish.EngineOptions; import org.petero.droidfish.GUIInterface; +import org.petero.droidfish.GUIInterface.ThinkingInfo; import org.petero.droidfish.GameMode; import org.petero.droidfish.PGNOptions; import org.petero.droidfish.Util; @@ -67,6 +68,7 @@ public class DroidChessController { private Move promoteMove; private int searchId; + private volatile ThinkingInfo latestThinkingInfo = null; /** Constructor. */ public DroidChessController(GUIInterface gui, PgnToken.PgnTokenReceiver gameTextListener, PGNOptions options) { @@ -769,9 +771,17 @@ public class DroidChessController { pvMoves.add(pvInfoV.get(i).pv); } } + final ThinkingInfo ti = new ThinkingInfo(); + ti.id = id; + ti.pvStr = newPV; + ti.statStr = statStr; + ti.bookInfo = newBookInfo; + ti.pvMoves = pvMoves; + ti.bookMoves = bookMoves; + latestThinkingInfo = ti; gui.runOnUIThread(new Runnable() { public void run() { - setThinkingInfo(id, pvMoves, newPV, statStr, newBookInfo, bookMoves); + setThinkingInfo(ti); } }); } @@ -1122,10 +1132,9 @@ public class DroidChessController { gui.updateMaterialDifferenceTitle(Util.getMaterialDiff(game.currPos())); } - private final synchronized void setThinkingInfo(int id, ArrayList> pvMoves, String pvStr, - String statStr, String bookInfo, ArrayList bookMoves) { - if (id == searchId) - gui.setThinkingInfo(pvStr, statStr, bookInfo, pvMoves, bookMoves); + private final synchronized void setThinkingInfo(ThinkingInfo ti) { + if ((ti.id == searchId) && (ti == latestThinkingInfo)) + gui.setThinkingInfo(ti); } private final void updateMoveList() {