Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

reversatile-88 Some evaluations are shown in Guess Best Move #89

Merged
merged 1 commit into from
Nov 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 18 additions & 5 deletions project/src/main/java/com/shurik/droidzebra/ZebraEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import android.util.Log;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONArray;
import org.json.JSONException;
Expand Down Expand Up @@ -186,9 +187,9 @@ private static void copyFile(InputStream in, OutputStream out) throws IOExceptio
}
}

private void waitForEngineState(ENGINE_STATE state, int milliseconds) {
private void waitForEngineState(int milliseconds, ENGINE_STATE... state) {
synchronized (engineStateEventLock) {
if (mEngineState != state)
if (!ArrayUtils.contains(state, mEngineState))
try {
engineStateEventLock.wait(milliseconds);
} catch (InterruptedException e) {
Expand All @@ -198,9 +199,9 @@ private void waitForEngineState(ENGINE_STATE state, int milliseconds) {
}
}

private void waitForEngineState(ENGINE_STATE state) {
private void waitForEngineState(ENGINE_STATE... state) {
synchronized (engineStateEventLock) {
while (mEngineState != state && isRunning)
while (!ArrayUtils.contains(state, mEngineState) && isRunning)
try {
engineStateEventLock.wait();
} catch (InterruptedException e) {
Expand Down Expand Up @@ -251,6 +252,18 @@ private void stopGame() {
}
}

public void forceStopGame() {
zeForceExit();
// if waiting for move - get back into the engine
mPendingEvent = new JSONObject();
try {
mPendingEvent.put("type", UI_EVENT_EXIT);
} catch (JSONException e) {
// Log.getStackTraceString(e);
}
waitForEngineState(ENGINE_STATE.ES_READY2PLAY, ENGINE_STATE.ES_USER_INPUT_WAIT);
}

public void makeMove(GameState gameState, Move move) throws InvalidMove {
if (gameState != currentGameState) {
//TODO switch context and play
Expand Down Expand Up @@ -284,7 +297,7 @@ public void makeMove(GameState gameState, Move move) throws InvalidMove {
private void stopIfThinkingOnHumanTime() {
if (isThinkingOnHumanTime()) {
stopMove();
waitForEngineState(ENGINE_STATE.ES_USER_INPUT_WAIT, 1000);
waitForEngineState(1000, ENGINE_STATE.ES_USER_INPUT_WAIT);
}
}

Expand Down
25 changes: 0 additions & 25 deletions project/src/main/java/de/earthlingz/oerszebra/DroidZebra.java
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,6 @@ public void onBoard(GameState gameState) {
}

if (!boardChanged) {
Log.v("Handler", "invalidate");
mBoardView.invalidate();
}
}
Expand Down Expand Up @@ -938,30 +937,6 @@ void undoAll() {
engine.undoAll(gameState);
}

//-------------------------------------------------------------------------
// Pass Dialog
public static class DialogQuit extends DialogFragment {

public static DialogQuit newInstance() {
return new DialogQuit();
}

public DroidZebra getDroidZebra() {
return (DroidZebra) getActivity();
}

@Nonnull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle(R.string.dialog_quit_title)
.setPositiveButton(R.string.dialog_quit_button_quit, (dialog, id) -> getDroidZebra().finish()
)
.setNegativeButton(R.string.dialog_quit_button_cancel, null)
.create();
}
}

//-------------------------------------------------------------------------
// Pass Dialog
public static class DialogBusy extends DialogFragment {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,27 @@ private static EngineConfig createGeneratorConfig(String opening) {
}

public void generate(int minIn, int max, GuessMoveListener guessMoveListener) {
gameState.setGameStateListener(new GameStateListener() {
@Override
public void onBoard(GameState board) {
return;
}
});
int min = Math.max(minIn, 4);
this.guessMoveListener = guessMoveListener;
final int movesPlayed = random.nextInt(max - min) + min;
this.candidateMoves = new CandidateMove[0];
listener.onBoardStateChanged();
engine.forceStopGame();

new GameGenerator(engine).generate(generatorConfig, guesserConfig, movesPlayed, gameState -> {
GuessMoveModeManager.this.gameState = gameState;
gameState.setGameStateListener(new GameStateListener() {
@Override
public void onBoard(GameState board) {
listener.onBoardStateChanged();
updateCandidateMoves(board.getCandidateMoves());
guessMoveListener.onSideToMoveChanged(board.getSideToMove());
listener.onBoardStateChanged();
}
});
guessMoveListener.onGenerated(gameState.getSideToMove());
Expand All @@ -97,7 +106,7 @@ private void updateCandidateMoves(CandidateMove[] newCandidates) {
if (this.candidateMoves.length == replacement.size()) {
this.candidateMoves = replacement.toArray(this.candidateMoves);
} else {
this.candidateMoves = replacement.toArray(new CandidateMove[replacement.size()]);
this.candidateMoves = replacement.toArray(new CandidateMove[0]);
}
}

Expand Down
Loading