From 2bb57cf48186edd59cf546d3f2d918702b74d190 Mon Sep 17 00:00:00 2001 From: Stefan Murawski Date: Mon, 25 Dec 2023 16:56:50 +0100 Subject: [PATCH] Move Number and Engine State --- .../java/com/shurik/droidzebra/GameState.java | 11 ++ .../com/shurik/droidzebra/ZebraEngine.java | 112 ++++++++++-------- .../de/earthlingz/oerszebra/DroidZebra.java | 38 ++++-- project/src/main/res/values-de/strings.xml | 2 +- project/src/main/res/values-fr/strings.xml | 2 +- project/src/main/res/values-ja/strings.xml | 2 +- project/src/main/res/values-ko/strings.xml | 2 +- project/src/main/res/values-ru/strings.xml | 2 +- project/src/main/res/values/strings.xml | 2 +- 9 files changed, 111 insertions(+), 62 deletions(-) diff --git a/project/src/main/java/com/shurik/droidzebra/GameState.java b/project/src/main/java/com/shurik/droidzebra/GameState.java index 66145cdf..7e948809 100644 --- a/project/src/main/java/com/shurik/droidzebra/GameState.java +++ b/project/src/main/java/com/shurik/droidzebra/GameState.java @@ -18,6 +18,8 @@ public class GameState { private String opening; private int lastMove; private int nextMove; + + private int reachedDepth; private ByteBoard byteBoard; private GameStateListener handler = new GameStateListener() { }; @@ -131,10 +133,19 @@ void setOpening(String opening) { handler.onBoard(this); } + void setReachedDepth(int reachedDepth) { + this.reachedDepth = reachedDepth; + handler.onBoard(this); + } + public String getOpening() { return opening; } + public int getReachedDepth() { + return reachedDepth; + } + void setLastMove(int lastMove) { this.lastMove = lastMove; handler.onBoard(this); diff --git a/project/src/main/java/com/shurik/droidzebra/ZebraEngine.java b/project/src/main/java/com/shurik/droidzebra/ZebraEngine.java index 0cfa6f7c..6ac01db1 100644 --- a/project/src/main/java/com/shurik/droidzebra/ZebraEngine.java +++ b/project/src/main/java/com/shurik/droidzebra/ZebraEngine.java @@ -73,12 +73,13 @@ public class ZebraEngine { MSG_DEBUG = 65535; // engine state - private static final int - ES_INITIAL = 0, - ES_READY2PLAY = 1, - ES_PLAY = 2, - ES_PLAY_IN_PROGRESS = 3, - ES_USER_INPUT_WAIT = 4; + public enum ENGINE_STATE { + ES_INITIAL, + ES_READY2PLAY, + ES_PLAY, + ES_PLAY_IN_PROGRESS, + ES_USER_INPUT_WAIT; + } private static final int UI_EVENT_EXIT = 0, @@ -118,7 +119,7 @@ public class ZebraEngine { private final transient Object engineStateEventLock = new Object(); - private int mEngineState = ES_INITIAL; + private ENGINE_STATE mEngineState = ENGINE_STATE.ES_INITIAL; private boolean isRunning = false; @@ -182,7 +183,7 @@ private static void copyFile(InputStream in, OutputStream out) throws IOExceptio } } - private void waitForEngineState(int state, int milliseconds) { + private void waitForEngineState(ENGINE_STATE state, int milliseconds) { synchronized (engineStateEventLock) { if (mEngineState != state) try { @@ -194,7 +195,7 @@ private void waitForEngineState(int state, int milliseconds) { } } - private void waitForEngineState(int state) { + private void waitForEngineState(ENGINE_STATE state) { synchronized (engineStateEventLock) { while (mEngineState != state && isRunning) try { @@ -206,15 +207,19 @@ private void waitForEngineState(int state) { } private void setEngineStatePlay() { - setEngineState(ZebraEngine.ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY); } - private void setEngineState(int state) { + private void setEngineState(ENGINE_STATE state) { + ZebraEngine.this.onDebugListener.onDebug("State: " + state); synchronized (engineStateEventLock) { mEngineState = state; engineStateEventLock.notifyAll(); } } + public ENGINE_STATE getState() { + return mEngineState; + } private void setRunning(boolean b) { boolean wasRunning = isRunning; @@ -232,14 +237,14 @@ private void stopGame() { zeForceExit(); // if waiting for move - get back into the engine // every other state should work itself out - if (mEngineState == ES_USER_INPUT_WAIT) { + if (mEngineState == ENGINE_STATE.ES_USER_INPUT_WAIT) { mPendingEvent = new JSONObject(); try { mPendingEvent.put("type", UI_EVENT_EXIT); } catch (JSONException e) { // Log.getStackTraceString(e); } - setEngineState(ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY); } } @@ -254,7 +259,7 @@ public void makeMove(GameState gameState, Move move) throws InvalidMove { // if thinking on human time - stop stopIfThinkingOnHumanTime(); - if (mEngineState != ES_USER_INPUT_WAIT) { + if (mEngineState != ENGINE_STATE.ES_USER_INPUT_WAIT) { // Log.d("ZebraEngine", "Invalid Engine State"); return; } @@ -267,7 +272,7 @@ public void makeMove(GameState gameState, Move move) throws InvalidMove { } catch (JSONException e) { // Log.getStackTraceString(e); } - setEngineState(ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY); } /** @@ -276,7 +281,7 @@ public void makeMove(GameState gameState, Move move) throws InvalidMove { private void stopIfThinkingOnHumanTime() { if (isThinkingOnHumanTime()) { stopMove(); - waitForEngineState(ES_USER_INPUT_WAIT, 1000); + waitForEngineState(ENGINE_STATE.ES_USER_INPUT_WAIT, 1000); } } @@ -289,7 +294,7 @@ public void undoMove(GameState gameState) { // if thinking on human time - stop stopIfThinkingOnHumanTime(); - if (mEngineState != ES_USER_INPUT_WAIT) { + if (mEngineState != ENGINE_STATE.ES_USER_INPUT_WAIT) { // Log.d("ZebraEngine", "Invalid Engine State"); return; } @@ -301,7 +306,7 @@ public void undoMove(GameState gameState) { } catch (JSONException e) { // Log.getStackTraceString(e); } - setEngineState(ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY); } @@ -314,7 +319,7 @@ public void undoAll(GameState gameState) { // if thinking on human time - stop stopIfThinkingOnHumanTime(); - if (mEngineState != ES_USER_INPUT_WAIT) { + if (mEngineState != ENGINE_STATE.ES_USER_INPUT_WAIT) { // Log.d("ZebraEngine", "Invalid Engine State"); return; } @@ -326,7 +331,7 @@ public void undoAll(GameState gameState) { } catch (JSONException e) { // Log.getStackTraceString(e); } - setEngineState(ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY); } public void redoMove(GameState gameState) { @@ -338,7 +343,7 @@ public void redoMove(GameState gameState) { // if thinking on human time - stop stopIfThinkingOnHumanTime(); - if (mEngineState != ES_USER_INPUT_WAIT) { + if (mEngineState != ENGINE_STATE.ES_USER_INPUT_WAIT) { // Log.d("ZebraEngine", "Invalid Engine State"); return; } @@ -350,7 +355,7 @@ public void redoMove(GameState gameState) { } catch (JSONException e) { // Log.getStackTraceString(e); } - setEngineState(ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY); } private boolean isThinkingOnHumanTime() { @@ -361,25 +366,25 @@ private boolean isThinkingOnHumanTime() { // notifications that some settings have changes - see if we care private void sendSettingsChanged() { // if we are waiting for input - restart the move (e.g. if sides switched) - if (mEngineState == ES_USER_INPUT_WAIT) { + if (mEngineState == ENGINE_STATE.ES_USER_INPUT_WAIT) { mPendingEvent = new JSONObject(); try { mPendingEvent.put("type", UI_EVENT_SETTINGS_CHANGE); } catch (JSONException e) { // Log.getStackTraceString(e); } - setEngineState(ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY); } } private void sendReplayMoves(List moves) { - if (mEngineState != ZebraEngine.ES_READY2PLAY) { + if (mEngineState != ZebraEngine.ENGINE_STATE.ES_READY2PLAY) { stopGame(); - waitForEngineState(ZebraEngine.ES_READY2PLAY); + waitForEngineState(ZebraEngine.ENGINE_STATE.ES_READY2PLAY); } initialGameState = new GameState(BOARD_SIZE, moves); - setEngineState(ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY); } // settings helpers @@ -535,7 +540,7 @@ private JSONObject Callback(int msgcode, JSONObject data) { bInCallback = true; switch (msgcode) { case MSG_ERROR: { - if (mEngineState == ES_INITIAL) { + if (mEngineState == ENGINE_STATE.ES_INITIAL) { // delete .bin files if initialization failed // will be recreated from resources new File(mFilesDir, PATTERNS_FILE).delete(); @@ -549,6 +554,18 @@ private JSONObject Callback(int msgcode, JSONObject data) { case MSG_DEBUG: { String message = data.getString("message"); + if(message.startsWith("status")) { + String[] split = message.split("\\s+"); + if(split.length > 3) { + ZebraEngine.this.onDebugListener.onDebug("Depth: " + split[2]); + try { + currentGameState.setReachedDepth(Integer.parseInt(split[2])); + } catch (NumberFormatException e) { + Log.e("Status", e.toString(), e); + } + } + + } ZebraEngine.this.onDebugListener.onDebug(message); } break; @@ -594,18 +611,18 @@ private JSONObject Callback(int msgcode, JSONObject data) { case MSG_GET_USER_INPUT: { - setEngineState(ES_USER_INPUT_WAIT); + setEngineState(ENGINE_STATE.ES_USER_INPUT_WAIT); - waitForEngineState(ES_PLAY); + waitForEngineState(ENGINE_STATE.ES_PLAY); while (mPendingEvent == null) { - setEngineState(ES_USER_INPUT_WAIT); - waitForEngineState(ES_PLAY); + setEngineState(ENGINE_STATE.ES_USER_INPUT_WAIT); + waitForEngineState(ENGINE_STATE.ES_PLAY); } retval = mPendingEvent; - setEngineState(ES_PLAY_IN_PROGRESS); + setEngineState(ENGINE_STATE.ES_PLAY_IN_PROGRESS); mValidMoves = null; mPendingEvent = null; @@ -613,17 +630,17 @@ private JSONObject Callback(int msgcode, JSONObject data) { break; case MSG_PASS: { - setEngineState(ES_USER_INPUT_WAIT); + setEngineState(ENGINE_STATE.ES_USER_INPUT_WAIT); currentGameState.sendPass(); - waitForEngineState(ES_PLAY); - setEngineState(ES_PLAY_IN_PROGRESS); + waitForEngineState(ENGINE_STATE.ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY_IN_PROGRESS); } break; case MSG_ANALYZE_GAME: { - setEngineState(ES_USER_INPUT_WAIT); + setEngineState(ENGINE_STATE.ES_USER_INPUT_WAIT); //currentGameState.sendMessage(msg); - waitForEngineState(ES_PLAY); - setEngineState(ES_PLAY_IN_PROGRESS); + waitForEngineState(ENGINE_STATE.ES_PLAY); + setEngineState(ENGINE_STATE.ES_PLAY_IN_PROGRESS); } break; @@ -683,6 +700,7 @@ private JSONObject Callback(int msgcode, JSONObject data) { android.os.SystemClock.sleep(computerMoveDelay - (moveEnd - mMoveStartTime)); } } + ZebraEngine.this.onDebugListener.onDebug("Move END"); currentGameState.sendMoveEnd(); } break; @@ -734,7 +752,7 @@ private JSONObject Callback(int msgcode, JSONObject data) { } private boolean isThinking() { - return mEngineState == ZebraEngine.ES_PLAY_IN_PROGRESS; + return mEngineState == ZebraEngine.ENGINE_STATE.ES_PLAY_IN_PROGRESS; } private boolean isValidMove(Move move) { @@ -827,11 +845,11 @@ private native void zeSetPlayerInfo( } void waitForReadyToPlay() { - waitForEngineState(ZebraEngine.ES_READY2PLAY); + waitForEngineState(ZebraEngine.ENGINE_STATE.ES_READY2PLAY); } private boolean isReadyToPlay() { - return mEngineState == ZebraEngine.ES_READY2PLAY; + return mEngineState == ZebraEngine.ENGINE_STATE.ES_READY2PLAY; } private void kill() { //TODO remove this, no one should be able to kill the engine @@ -991,7 +1009,7 @@ private class EngineThread extends Thread { public void run() { setRunning(true); - setEngineState(ES_INITIAL); + setEngineState(ENGINE_STATE.ES_INITIAL); // init data files if (!initFiles()) return; @@ -1002,14 +1020,14 @@ public void run() { zeSetPlayerInfo(PLAYER_WHITE, 0, 0, 0, INFINITE_TIME, 0); } - setEngineState(ES_READY2PLAY); + setEngineState(ENGINE_STATE.ES_READY2PLAY); while (isRunning) { - waitForEngineState(ES_PLAY); + waitForEngineState(ENGINE_STATE.ES_PLAY); if (!isRunning) break; // something may have happened while we were waiting - setEngineState(ES_PLAY_IN_PROGRESS); + setEngineState(ENGINE_STATE.ES_PLAY_IN_PROGRESS); synchronized (mJNILock) { setPlayerInfos(); @@ -1031,7 +1049,7 @@ public void run() { } - setEngineState(ES_READY2PLAY); + setEngineState(ENGINE_STATE.ES_READY2PLAY); //setEngineState(ES_PLAY); // test } diff --git a/project/src/main/java/de/earthlingz/oerszebra/DroidZebra.java b/project/src/main/java/de/earthlingz/oerszebra/DroidZebra.java index 9d4911af..d4b46afd 100644 --- a/project/src/main/java/de/earthlingz/oerszebra/DroidZebra.java +++ b/project/src/main/java/de/earthlingz/oerszebra/DroidZebra.java @@ -388,19 +388,37 @@ private void loadUISettings() { mBoardView.setDisplayEvals(evalsDisplayEnabled()); + setStatus(); + } + + } - TextView viewById = findViewById(R.id.status_settings); + private void setStatus() { + TextView viewById = findViewById(R.id.status_settings); - if(viewById != null) { - int depth = settingsProvider.getSettingZebraDepth(); - int depthExact = settingsProvider.getSettingZebraDepthExact(); - int depthWLD = settingsProvider.getSettingZebraDepthWLD(); - viewById.setText( - String.format(getString(R.string.display_depth), depth, depthExact, depthWLD) - ); + if(viewById != null) { + int depth = settingsProvider.getSettingZebraDepth(); + int depthExact = settingsProvider.getSettingZebraDepthExact(); + int reachedDepth = 0; + int moveNumber = 1; + if(gameState != null) { + reachedDepth = gameState.getReachedDepth(); + moveNumber = gameState.getDisksPlayed(); } + String state = "IDLE"; + if(engine != null ) { + switch (engine.getState()) { + case ES_PLAY_IN_PROGRESS: + state = "Thinking"; + break; + default: + state = "Idle"; + } + } + viewById.setText( + String.format(getString(R.string.display_depth), depth, depthExact, reachedDepth, moveNumber, state) + ); } - } @@ -678,6 +696,8 @@ public void onBoard(GameState gameState) { setStatusViewScores(sideToMove); + setStatus(); + if (gameState.getOpening() != null) { ((TextView)findViewById(R.id.status_opening)).setText(gameState.getOpening()); } diff --git a/project/src/main/res/values-de/strings.xml b/project/src/main/res/values-de/strings.xml index 092b929a..41d153ed 100644 --- a/project/src/main/res/values-de/strings.xml +++ b/project/src/main/res/values-de/strings.xml @@ -8,7 +8,7 @@ Du musst aussetzen Abbrechen Bist du sicher? - Mid: %1$d Exakt: %2$d G/V: %3$d + Move %4$s%nState: %5$s (%3$d)%nMid: %1$d Exact: %2$d Brett anzeigen e-Mail Neues Spiel diff --git a/project/src/main/res/values-fr/strings.xml b/project/src/main/res/values-fr/strings.xml index 5099c32a..d27f1154 100644 --- a/project/src/main/res/values-fr/strings.xml +++ b/project/src/main/res/values-fr/strings.xml @@ -60,7 +60,7 @@ OK e-Mail (ceci peut prendre du temps) - Milieu: %1$d Exact: %2$d G/P: %3$d + Move %4$s%nState: %5$s (%3$d)%nMid: %1$d Exact: %2$d Paramètres Généré par Reversatile (https://play.google.com/store/apps/details?id=de.earthlingz.oerszebra) Date: diff --git a/project/src/main/res/values-ja/strings.xml b/project/src/main/res/values-ja/strings.xml index c82a3e1b..25cdc5da 100644 --- a/project/src/main/res/values-ja/strings.xml +++ b/project/src/main/res/values-ja/strings.xml @@ -62,7 +62,7 @@ OK メール (しばらくお待ちください) - Mid: %1$d Exact: %2$d W/L: %3$d + Move %4$s%nState: %5$s (%3$d)%nMid: %1$d Exact: %2$d 設定 作成:DroidZebra (https://play.google.com/store/apps/details?id=de.earthlingz.oerszebra) 日付: diff --git a/project/src/main/res/values-ko/strings.xml b/project/src/main/res/values-ko/strings.xml index 4a78711e..cf0dcb18 100644 --- a/project/src/main/res/values-ko/strings.xml +++ b/project/src/main/res/values-ko/strings.xml @@ -62,7 +62,7 @@ 이메일 잠시만 기다려주십시오 - Mid: %1$d Exact: %2$d W/L: %3$d + Move %4$s%nState: %5$s (%3$d)%nMid: %1$d Exact: %2$d 환경설정 드로이드제브라에 의해 생성됨 (https://play.google.com/store/apps/details?id=de.earthlingz.oerszebra) 날짜: diff --git a/project/src/main/res/values-ru/strings.xml b/project/src/main/res/values-ru/strings.xml index 1c071975..c6199265 100644 --- a/project/src/main/res/values-ru/strings.xml +++ b/project/src/main/res/values-ru/strings.xml @@ -62,7 +62,7 @@ OK e-Mail (это займет немного времени) - Mid: %1$d Exact: %2$d W/L: %3$d + Move %4$s%nState: %5$s (%3$d)%nMid: %1$d Exact: %2$d Настройки Сгенерировано с помощью Reversatile (https://play.google.com/store/apps/details?id=de.earthlingz.oerszebra) Дата: diff --git a/project/src/main/res/values/strings.xml b/project/src/main/res/values/strings.xml index 240783e2..45038cbc 100644 --- a/project/src/main/res/values/strings.xml +++ b/project/src/main/res/values/strings.xml @@ -69,7 +69,7 @@ Cancel e-Mail (this may take a few moments) - Mid: %1$d Exact: %2$d W/L: %3$d + Move %4$s%nState: %5$s (%3$d)%nMid: %1$d Exact: %2$d Settings Generated by Reversatile (https://play.google.com/store/apps/details?id=de.earthlingz.oerszebra)