TextIO { public static Move stringToMove(Position pos, String strMove) { strMove = strMove.replaceAll("=", ""); if (strMove.length() == 0) return null; MoveGen.MoveList moves = MoveGen.instance.pseudoLegalMoves(pos); MoveGen.removeIllegal(pos, moves); { char lastChar = strMove.charAt(strMove.length() - 1); if ((lastChar == '#') || (lastChar == '+')) { MoveGen.MoveList subMoves = new MoveGen.MoveList(); int len = 0; for (int mi = 0; mi < moves.size; mi++) { Move m = moves.m[mi]; String str1 = TextIO.moveToString(pos, m, true, moves); if (str1.charAt(str1.length() - 1) == lastChar) { subMoves.m[len++] = m; } } subMoves.size = len; moves = subMoves; strMove = normalizeMoveString(strMove); } } for (int i = 0; i < 2; i++) { for (int mi = 0; mi < moves.size; mi++) { Move m = moves.m[mi]; String str1 = normalizeMoveString(TextIO.moveToString(pos, m, true, moves)); String str2 = normalizeMoveString(TextIO.moveToString(pos, m, false, moves)); if (i == 0) { if (strMove.equals(str1) || strMove.equals(str2)) { return m; } } else { if (strMove.toLowerCase().equals(str1.toLowerCase()) || strMove.toLowerCase().equals(str2.toLowerCase())) { return m; } } } } Move move = null; for (int i = 0; i < 2; i++) { for (int mi = 0; mi < moves.size; mi++) { Move m = moves.m[mi]; String str1 = normalizeMoveString(TextIO.moveToString(pos, m, true)); String str2 = normalizeMoveString(TextIO.moveToString(pos, m, false)); boolean match; if (i == 0) { match = (str1.startsWith(strMove) || str2.startsWith(strMove)); } else { match = (str1.toLowerCase().startsWith(strMove.toLowerCase()) || str2.toLowerCase().startsWith(strMove.toLowerCase())); } if (match) { if (move != null) { return null; } else { move = m; } } } if (move != null) return move; } return null; }  static Position readFEN(String fen); static void fixupEPSquare(Position pos); static String toFEN(Position pos); static String moveToString(Position pos, Move move, boolean longForm); static String moveToUCIString(Move m); static Move uciStringToMove(String move); static Move stringToMove(Position pos, String strMove); static int getSquare(String s); static String squareToString(int square); static String asciiBoard(Position pos); static final String startPosFEN; }
@Test public void testStringToMove() throws ChessParseError { System.out.println("stringToMove"); Position pos = TextIO.readFEN("r4rk1/2pn3p/2q1q1n1/8/2q2p2/6R1/p4PPP/1R4K1 b - - 0 1"); Move mNe5 = new Move(Position.getSquare(3, 6), Position.getSquare(4, 4), Piece.EMPTY); Move m = TextIO.stringToMove(pos, "Ne5"); assertEquals(mNe5, m); m = TextIO.stringToMove(pos, "ne"); assertEquals(mNe5, m); m = TextIO.stringToMove(pos, "N"); assertEquals(null, m); Move mQc6e4 = new Move(Position.getSquare(2, 5), Position.getSquare(4, 3), Piece.EMPTY); m = TextIO.stringToMove(pos, "Qc6-e4"); assertEquals(mQc6e4, m); m = TextIO.stringToMove(pos, "Qc6e4"); assertEquals(mQc6e4, m); m = TextIO.stringToMove(pos, "Qce4"); assertEquals(null, m); m = TextIO.stringToMove(pos, "Q6e4"); assertEquals(null, m); Move maxb1Q = new Move(Position.getSquare(0, 1), Position.getSquare(1, 0), Piece.BQUEEN); m = TextIO.stringToMove(pos, "axb1Q"); assertEquals(maxb1Q, m); m = TextIO.stringToMove(pos, "axb1Q#"); assertEquals(maxb1Q, m); m = TextIO.stringToMove(pos, "axb1Q+"); assertEquals(null, m); Move mh5= new Move(Position.getSquare(7, 6), Position.getSquare(7, 4), Piece.EMPTY); m = TextIO.stringToMove(pos, "h5"); assertEquals(mh5, m); m = TextIO.stringToMove(pos, "h7-h5"); assertEquals(mh5, m); m = TextIO.stringToMove(pos, "h"); assertEquals(null, m); pos = TextIO.readFEN("r1b1k2r/1pqpppbp/p5pn/3BP3/8/2pP4/PPPBQPPP/R3K2R w KQkq - 0 12"); m = TextIO.stringToMove(pos, "bxc3"); assertEquals(TextIO.getSquare("b2"), m.from); m = TextIO.stringToMove(pos, "Bxc3"); assertEquals(TextIO.getSquare("d2"), m.from); m = TextIO.stringToMove(pos, "bxc"); assertEquals(TextIO.getSquare("b2"), m.from); m = TextIO.stringToMove(pos, "Bxc"); assertEquals(TextIO.getSquare("d2"), m.from); pos = TextIO.readFEN("5k2/p1pQn3/1p2Bp1r/8/4P1pN/2N5/PPP2PPP/R3K2R w KQ - 0 16"); Move kCastle = new Move(Position.getSquare(4,0), Position.getSquare(6,0), Piece.EMPTY); Move qCastle = new Move(Position.getSquare(4,0), Position.getSquare(2,0), Piece.EMPTY); m = TextIO.stringToMove(pos, "o"); assertEquals(null, m); m = TextIO.stringToMove(pos, "o-o"); assertEquals(kCastle, m); m = TextIO.stringToMove(pos, "O-O"); assertEquals(kCastle, m); m = TextIO.stringToMove(pos, "o-o-o"); assertEquals(qCastle, m); pos.setPiece(Position.getSquare(5,1), Piece.EMPTY); pos.setPiece(Position.getSquare(5,5), Piece.EMPTY); m = TextIO.stringToMove(pos, "o"); assertEquals(null, m); m = TextIO.stringToMove(pos, "o-o"); assertEquals(kCastle, m); m = TextIO.stringToMove(pos, "o-o-o"); assertEquals(qCastle, m); m = TextIO.stringToMove(pos, "o-o+"); assertEquals(kCastle, m); pos = TextIO.readFEN("1r3r2/2kP2Rp/p1bN1p2/2p5/5P2/2P5/P5PP/3R2K1 w - -"); m = TextIO.stringToMove(pos, "d8=Q+"); Move m2 = TextIO.stringToMove(pos, "d8Q"); assertEquals(m2, m); }