DroidFish: Don't crash if the engine wants to play an invalid move.

This commit is contained in:
Peter Osterlund 2017-06-11 22:37:01 +02:00
parent 97662f067e
commit e51c86f621
3 changed files with 33 additions and 31 deletions
DroidFish/src/org/petero/droidfish

@ -2805,27 +2805,20 @@ public class DroidFish extends Activity
builder.setItems(items, new DialogInterface.OnClickListener() { builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) { public void onClick(DialogInterface dialog, int item) {
int gameModeType = -1; int gameModeType = -1;
/* only flip site in case the player was specified resp. changed */ boolean matchPlayerNames = false;
boolean flipSite = false;
switch (item) { switch (item) {
case 0: gameModeType = GameMode.ANALYSIS; break; case 0: gameModeType = GameMode.ANALYSIS; break;
case 1: gameModeType = GameMode.EDIT_GAME; break; case 1: gameModeType = GameMode.EDIT_GAME; break;
case 2: gameModeType = GameMode.PLAYER_WHITE; flipSite = true; break; case 2: gameModeType = GameMode.PLAYER_WHITE; matchPlayerNames = true; break;
case 3: gameModeType = GameMode.PLAYER_BLACK; flipSite = true; break; case 3: gameModeType = GameMode.PLAYER_BLACK; matchPlayerNames = true; break;
case 4: gameModeType = GameMode.TWO_PLAYERS; break; case 4: gameModeType = GameMode.TWO_PLAYERS; break;
case 5: gameModeType = GameMode.TWO_COMPUTERS; break; case 5: gameModeType = GameMode.TWO_COMPUTERS; break;
default: break; default: break;
} }
dialog.dismiss(); dialog.dismiss();
if (gameModeType >= 0) { if (gameModeType >= 0) {
Editor editor = settings.edit(); newGameMode(gameModeType);
String gameModeStr = String.format(Locale.US, "%d", gameModeType); setBoardFlip(matchPlayerNames);
editor.putString("gameMode", gameModeStr);
editor.commit();
gameMode = new GameMode(gameModeType);
maybeAutoModeOff(gameMode);
ctrl.setGameMode(gameMode);
setBoardFlip(flipSite);
} }
} }
}); });
@ -3831,6 +3824,12 @@ public class DroidFish extends Activity
@Override @Override
public void movePlayed(Position pos, Move move, boolean computerMove) { public void movePlayed(Position pos, Move move, boolean computerMove) {
if (move == null) {
Toast.makeText(getApplicationContext(), R.string.engine_error,
Toast.LENGTH_SHORT).show();
newGameMode(GameMode.EDIT_GAME);
return;
}
if ("sound".equals(moveAnnounceType)) { if ("sound".equals(moveAnnounceType)) {
if (computerMove) { if (computerMove) {
if (moveSound != null) if (moveSound != null)
@ -3963,8 +3962,6 @@ public class DroidFish extends Activity
/** Set automatic move forward/backward mode. */ /** Set automatic move forward/backward mode. */
void setAutoMode(AutoMode am) { void setAutoMode(AutoMode am) {
// System.out.printf("%.3f DroidFish.setAutoMode(): %s\n",
// System.currentTimeMillis() * 1e-3, am.toString());
autoMode = am; autoMode = am;
switch (am) { switch (am) {
case BACKWARD: case BACKWARD:

@ -741,21 +741,24 @@ public class DroidComputerPlayer {
case PONDER: case PONDER:
case ANALYZE: { case ANALYZE: {
String[] tokens = tokenize(s); String[] tokens = tokenize(s);
if (tokens[0].equals("info")) { int nTok = tokens.length;
parseInfoCmd(tokens); if (nTok > 0) {
} else if (tokens[0].equals("bestmove")) { if (tokens[0].equals("info")) {
String bestMove = tokens[1]; parseInfoCmd(tokens);
String nextPonderMoveStr = ""; } else if (tokens[0].equals("bestmove")) {
if ((tokens.length >= 4) && (tokens[2].equals("ponder"))) String bestMove = nTok > 1 ? tokens[1] : "";
nextPonderMoveStr = tokens[3]; String nextPonderMoveStr = "";
Move nextPonderMove = TextIO.UCIstringToMove(nextPonderMoveStr); if ((nTok >= 4) && (tokens[2].equals("ponder")))
nextPonderMoveStr = tokens[3];
Move nextPonderMove = TextIO.UCIstringToMove(nextPonderMoveStr);
if (engineState.state == MainState.SEARCH) if (engineState.state == MainState.SEARCH)
reportMove(bestMove, nextPonderMove); reportMove(bestMove, nextPonderMove);
engineState.setState(MainState.IDLE); engineState.setState(MainState.IDLE);
searchRequest = null; searchRequest = null;
handleIdleState(); handleIdleState();
}
} }
break; break;
} }

@ -1027,10 +1027,12 @@ public class DroidChessController {
updateGameMode(); updateGameMode();
gui.movePlayed(game.prevPos(), res.second, true); gui.movePlayed(game.prevPos(), res.second, true);
listener.clearSearchInfo(searchId); listener.clearSearchInfo(searchId);
updateComputeThreads(); if (res.first) {
setSelection(); updateComputeThreads();
setAnimMove(oldPos, game.getLastMove(), true); setSelection();
updateGUI(); setAnimMove(oldPos, game.getLastMove(), true);
updateGUI();
}
} }
public final void repeatLastMove() { public final void repeatLastMove() {