diff --git a/DroidFish/src/org/petero/droidfish/engine/DroidComputerPlayer.java b/DroidFish/src/org/petero/droidfish/engine/DroidComputerPlayer.java index 7924cc6..9f4e276 100644 --- a/DroidFish/src/org/petero/droidfish/engine/DroidComputerPlayer.java +++ b/DroidFish/src/org/petero/droidfish/engine/DroidComputerPlayer.java @@ -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); } diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java b/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java index 051be19..a8ce6b4 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/DroidChessController.java @@ -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); diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/Game.java b/DroidFish/src/org/petero/droidfish/gamelogic/Game.java index 22b8904..a7f0833 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/Game.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/Game.java @@ -124,25 +124,13 @@ public class Game { } Move m = TextIO.UCIstringToMove(str); - if (m != null) { - ArrayList 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; diff --git a/DroidFish/src/org/petero/droidfish/gamelogic/TextIO.java b/DroidFish/src/org/petero/droidfish/gamelogic/TextIO.java index 08540dc..e006fbe 100644 --- a/DroidFish/src/org/petero/droidfish/gamelogic/TextIO.java +++ b/DroidFish/src/org/petero/droidfish/gamelogic/TextIO.java @@ -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 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