DroidFish: Fixed crash if engine reported invalid moves in the PV or an invalid ponder move.

This commit is contained in:
Peter Osterlund 2012-07-14 01:14:55 +00:00
parent ef9871c2e0
commit ce5600db80
4 changed files with 44 additions and 17 deletions

View File

@ -745,18 +745,37 @@ public class DroidComputerPlayer {
private final void reportMove(String bestMove, Move nextPonderMove) {
SearchRequest sr = searchRequest;
boolean canPonder = true;
// Claim draw if appropriate
if (statScore <= 0) {
String drawClaim = canClaimDraw(sr.currPos, sr.posHashList, sr.posHashListSize,
TextIO.UCIstringToMove(bestMove));
if (drawClaim != "")
if (drawClaim != "") {
bestMove = drawClaim;
canPonder = false;
}
}
// Accept draw offer if engine is losing
if (sr.drawOffer && !statIsMate && (statScore <= -300)) {
bestMove = "draw accept";
canPonder = false;
}
if (canPonder) {
Move bestM = TextIO.stringToMove(sr.currPos, bestMove);
if ((bestM == null) || !TextIO.isValid(sr.currPos, bestM, null))
canPonder = false;
if (canPonder) {
Position tmpPos = new Position(sr.currPos);
UndoInfo ui = new UndoInfo();
tmpPos.makeMove(bestM, ui);
if (!TextIO.isValid(tmpPos, nextPonderMove, null))
canPonder = false;
}
}
if (!canPonder)
nextPonderMove = null;
listener.notifySearchResult(sr.searchId, bestMove, nextPonderMove);
}

View File

@ -673,6 +673,8 @@ public class DroidChessController {
for (Move m : pv.pv) {
if (m == null)
break;
if (!TextIO.isValid(tmpPos, m, null))
break;
String moveStr = TextIO.moveToString(tmpPos, m, false);
buf.append(String.format(" %s", moveStr));
tmpPos.makeMove(m, ui);

View File

@ -124,25 +124,13 @@ public class Game {
}
Move m = TextIO.UCIstringToMove(str);
if (m != null) {
ArrayList<Move> moves = new MoveGen().pseudoLegalMoves(currPos());
moves = MoveGen.removeIllegal(currPos(), moves);
boolean legal = false;
for (int i = 0; i < moves.size(); i++) {
if (m.equals(moves.get(i))) {
legal = true;
break;
}
}
if (!legal)
if (m != null)
if (!TextIO.isValid(currPos(), m, null))
m = null;
}
if (m == null) {
if (m == null)
m = TextIO.stringToMove(currPos(), str);
}
if (m == null) {
if (m == null)
return false;
}
addToGameTree(m, pendingDrawOffer ? "draw offer" : "");
return true;

View File

@ -422,6 +422,24 @@ public class TextIO {
}
}
/**
* Decide if move is valid in position pos.
* @param pos Position for which to test move.
* @param move The move to check for validity.
* @param moves If non-null, list of valid moves in position pos.
* @return True if move is valid in position pos, false otherwise.
*/
public static final boolean isValid(Position pos, Move move, ArrayList<Move> moves) {
if (moves == null) {
moves = new MoveGen().pseudoLegalMoves(pos);
moves = MoveGen.removeIllegal(pos, moves);
}
for (int i = 0; i < moves.size(); i++)
if (move.equals(moves.get(i)))
return true;
return false;
}
private final static class MoveInfo {
int piece; // -1 for unspecified
int fromX, fromY, toX, toY; // -1 for unspecified