TextIO { public static Move uciStringToMove(String move) { if ((move.length() < 4) || (move.length() > 5)) return null; int fromSq = TextIO.getSquare(move.substring(0, 2)); int toSq = TextIO.getSquare(move.substring(2, 4)); if ((fromSq < 0) || (toSq < 0)) { return null; } char prom = ' '; boolean white = true; if (move.length() == 5) { prom = move.charAt(4); if (Position.getY(toSq) == 7) { white = true; } else if (Position.getY(toSq) == 0) { white = false; } else { return null; } } int promoteTo; switch (prom) { case ' ': promoteTo = Piece.EMPTY; break; case 'q': promoteTo = white ? Piece.WQUEEN : Piece.BQUEEN; break; case 'r': promoteTo = white ? Piece.WROOK : Piece.BROOK; break; case 'b': promoteTo = white ? Piece.WBISHOP : Piece.BBISHOP; break; case 'n': promoteTo = white ? Piece.WKNIGHT : Piece.BKNIGHT; break; default: return null; } return new Move(fromSq, toSq, promoteTo); }  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 testUciStringToMove() throws ChessParseError { System.out.println("stringToMove"); Position pos = TextIO.readFEN(TextIO.startPosFEN); Move m = TextIO.uciStringToMove("e2e4"); assertEquals(TextIO.stringToMove(pos, "e4"), m); m = TextIO.uciStringToMove("e2e5"); assertEquals(new Move(12, 12+8*3, Piece.EMPTY), m); m = TextIO.uciStringToMove("e2e5q"); assertEquals(null, m); m = TextIO.uciStringToMove("e7e8q"); assertEquals(Piece.WQUEEN, m.promoteTo); m = TextIO.uciStringToMove("e7e8r"); assertEquals(Piece.WROOK, m.promoteTo); m = TextIO.uciStringToMove("e7e8b"); assertEquals(Piece.WBISHOP, m.promoteTo); m = TextIO.uciStringToMove("e2e1n"); assertEquals(Piece.BKNIGHT, m.promoteTo); m = TextIO.uciStringToMove("e7e8x"); assertEquals(null, m); m = TextIO.uciStringToMove("i1i3"); assertEquals(null, m); }