@Test public void testToSAN() { MoveGenerator etat = BoardFactory.valueOf(ARRAY, STARTING); Move mvt = new Move(WHITE_PAWN, Square.valueOf("e2"), Square.valueOf("e3")); assertEquals("e3", toSAN(etat, mvt)); etat = etat.derive(mvt, true); mvt = new Move(BLACK_KNIGHT, Square.valueOf("b8"), Square.valueOf("a6")); assertEquals("Na6", toSAN(etat, mvt)); etat = etat.derive(mvt, true); mvt = new Move(WHITE_BISHOP, Square.valueOf("f1"), Square.valueOf("c4")); assertEquals("Bc4", toSAN(etat, mvt)); etat = etat.derive(mvt, true); mvt = new Move(BLACK_PAWN, Square.valueOf("c7"), Square.valueOf("c5")); assertEquals("c5", toSAN(etat, mvt)); etat = etat.derive(mvt, true); mvt = new Move(WHITE_QUEEN, Square.valueOf("d1"), Square.valueOf("f3")); assertEquals("Qf3", toSAN(etat, mvt)); etat = etat.derive(mvt, true); mvt = new Move(BLACK_PAWN, Square.valueOf("b7"), Square.valueOf("b6")); assertEquals("b6", toSAN(etat, mvt)); etat = etat.derive(mvt, true); mvt = new Move(WHITE_QUEEN, Square.valueOf("f3"), Square.valueOf("f7"), BLACK_PAWN); assertEquals("Qxf7++", toSAN(etat, mvt)); try { etat = BoardFactory.valueOf(ARRAY, EMPTY).derive(toBoard(AMBIGUOUS_KNIGHT_FEN)); mvt = new Move(WHITE_KNIGHT, Square.valueOf("f3"), Square.valueOf("e5"), BLACK_PAWN); assertEquals("Nfxe5", toSAN(etat, mvt)); etat = BoardFactory.valueOf(ARRAY, EMPTY).derive(toBoard(AMBIGUOUS_PAWN_FEN)); mvt = new Move(WHITE_PAWN, Square.valueOf("c3"), Square.valueOf("b4"), BLACK_KNIGHT); assertEquals("cxb4", toSAN(etat, mvt)); etat = BoardFactory.valueOf(ARRAY, EMPTY).derive(toBoard(AMBIGUOUS_QUEEN_FEN)); mvt = new Move(WHITE_QUEEN, Square.valueOf("f5"), Square.valueOf("d5"), BLACK_PAWN); assertEquals("Qf5xd5(=)", toSAN(etat, mvt)); } catch (final FENException e) { fail(e.toString()); } }
public static String toSAN(final MoveGenerator pEtat, final Move pMouvement) { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pMouvement == null) { throw new NullPointerException("Missing move"); } final boolean trait = pEtat.isWhiteActive(); final Piece piece = pMouvement.getPiece(); final PieceType t = piece.getType(); final StringBuilder sb = new StringBuilder(); final Square src = pMouvement.getFrom(); final Square dst = pMouvement.getTo(); final MoveGenerator apres = pEtat.derive(pMouvement, false); final int nbMvts = apres.getValidMoves(!trait).length; final int xSrc = src.getFile(); final int xDst = dst.getFile(); if ((t == KING) && (Math.abs(xSrc - xDst) > 1)) { sb.append("0-0"); if (xSrc > xDst) { sb.append("-0"); } } else { sb.append(t.getSANLetter()); if (t != PAWN) { final List<Move> mvts = new ArrayList<Move>(Arrays.asList(pEtat.getValidMoves(trait))); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if ((piece != m.getPiece()) || (dst != m.getTo()) || (m.equals(pMouvement))) { mvts.remove(i); } } boolean preciser = true; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (xSrc != m.getFrom().getFile()) { mvts.remove(i); if (preciser) { sb.append((char) ('a' + xSrc)); preciser = false; } } } final int ySrc = src.getRank(); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (ySrc != m.getFrom().getRank()) { sb.append((char) ('1' + ySrc)); break; } } } if ((pEtat.getPieceAt(dst) != null) || ((dst == pEtat.getEnPassant()) && (t == PAWN))) { if (t == PAWN) { sb.append((char) ('a' + xSrc)); } sb.append('x'); } sb.append(dst.getFENString()); if (t == PAWN) { if (dst == pEtat.getEnPassant()) { sb.append(" e.p."); } else { final int yDst = dst.getRank(); if ((trait && (yDst == RANK_COUNT - 1)) || ((!trait) && (yDst == 0))) { sb.append(QUEEN.getSANLetter()); } } } } if (apres.isInCheck(!trait)) { sb.append('+'); if (nbMvts == 0) { sb.append('+'); } } else if (nbMvts == 0) { sb.append("(=)"); } final String res = sb.toString(); assert SAN_VALIDATOR.matcher(res).matches(); return res; }
SANUtils { public static String toSAN(final MoveGenerator pEtat, final Move pMouvement) { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pMouvement == null) { throw new NullPointerException("Missing move"); } final boolean trait = pEtat.isWhiteActive(); final Piece piece = pMouvement.getPiece(); final PieceType t = piece.getType(); final StringBuilder sb = new StringBuilder(); final Square src = pMouvement.getFrom(); final Square dst = pMouvement.getTo(); final MoveGenerator apres = pEtat.derive(pMouvement, false); final int nbMvts = apres.getValidMoves(!trait).length; final int xSrc = src.getFile(); final int xDst = dst.getFile(); if ((t == KING) && (Math.abs(xSrc - xDst) > 1)) { sb.append("0-0"); if (xSrc > xDst) { sb.append("-0"); } } else { sb.append(t.getSANLetter()); if (t != PAWN) { final List<Move> mvts = new ArrayList<Move>(Arrays.asList(pEtat.getValidMoves(trait))); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if ((piece != m.getPiece()) || (dst != m.getTo()) || (m.equals(pMouvement))) { mvts.remove(i); } } boolean preciser = true; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (xSrc != m.getFrom().getFile()) { mvts.remove(i); if (preciser) { sb.append((char) ('a' + xSrc)); preciser = false; } } } final int ySrc = src.getRank(); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (ySrc != m.getFrom().getRank()) { sb.append((char) ('1' + ySrc)); break; } } } if ((pEtat.getPieceAt(dst) != null) || ((dst == pEtat.getEnPassant()) && (t == PAWN))) { if (t == PAWN) { sb.append((char) ('a' + xSrc)); } sb.append('x'); } sb.append(dst.getFENString()); if (t == PAWN) { if (dst == pEtat.getEnPassant()) { sb.append(" e.p."); } else { final int yDst = dst.getRank(); if ((trait && (yDst == RANK_COUNT - 1)) || ((!trait) && (yDst == 0))) { sb.append(QUEEN.getSANLetter()); } } } } if (apres.isInCheck(!trait)) { sb.append('+'); if (nbMvts == 0) { sb.append('+'); } } else if (nbMvts == 0) { sb.append("(=)"); } final String res = sb.toString(); assert SAN_VALIDATOR.matcher(res).matches(); return res; } }
SANUtils { public static String toSAN(final MoveGenerator pEtat, final Move pMouvement) { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pMouvement == null) { throw new NullPointerException("Missing move"); } final boolean trait = pEtat.isWhiteActive(); final Piece piece = pMouvement.getPiece(); final PieceType t = piece.getType(); final StringBuilder sb = new StringBuilder(); final Square src = pMouvement.getFrom(); final Square dst = pMouvement.getTo(); final MoveGenerator apres = pEtat.derive(pMouvement, false); final int nbMvts = apres.getValidMoves(!trait).length; final int xSrc = src.getFile(); final int xDst = dst.getFile(); if ((t == KING) && (Math.abs(xSrc - xDst) > 1)) { sb.append("0-0"); if (xSrc > xDst) { sb.append("-0"); } } else { sb.append(t.getSANLetter()); if (t != PAWN) { final List<Move> mvts = new ArrayList<Move>(Arrays.asList(pEtat.getValidMoves(trait))); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if ((piece != m.getPiece()) || (dst != m.getTo()) || (m.equals(pMouvement))) { mvts.remove(i); } } boolean preciser = true; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (xSrc != m.getFrom().getFile()) { mvts.remove(i); if (preciser) { sb.append((char) ('a' + xSrc)); preciser = false; } } } final int ySrc = src.getRank(); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (ySrc != m.getFrom().getRank()) { sb.append((char) ('1' + ySrc)); break; } } } if ((pEtat.getPieceAt(dst) != null) || ((dst == pEtat.getEnPassant()) && (t == PAWN))) { if (t == PAWN) { sb.append((char) ('a' + xSrc)); } sb.append('x'); } sb.append(dst.getFENString()); if (t == PAWN) { if (dst == pEtat.getEnPassant()) { sb.append(" e.p."); } else { final int yDst = dst.getRank(); if ((trait && (yDst == RANK_COUNT - 1)) || ((!trait) && (yDst == 0))) { sb.append(QUEEN.getSANLetter()); } } } } if (apres.isInCheck(!trait)) { sb.append('+'); if (nbMvts == 0) { sb.append('+'); } } else if (nbMvts == 0) { sb.append("(=)"); } final String res = sb.toString(); assert SAN_VALIDATOR.matcher(res).matches(); return res; } private  SANUtils(); }
SANUtils { public static String toSAN(final MoveGenerator pEtat, final Move pMouvement) { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pMouvement == null) { throw new NullPointerException("Missing move"); } final boolean trait = pEtat.isWhiteActive(); final Piece piece = pMouvement.getPiece(); final PieceType t = piece.getType(); final StringBuilder sb = new StringBuilder(); final Square src = pMouvement.getFrom(); final Square dst = pMouvement.getTo(); final MoveGenerator apres = pEtat.derive(pMouvement, false); final int nbMvts = apres.getValidMoves(!trait).length; final int xSrc = src.getFile(); final int xDst = dst.getFile(); if ((t == KING) && (Math.abs(xSrc - xDst) > 1)) { sb.append("0-0"); if (xSrc > xDst) { sb.append("-0"); } } else { sb.append(t.getSANLetter()); if (t != PAWN) { final List<Move> mvts = new ArrayList<Move>(Arrays.asList(pEtat.getValidMoves(trait))); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if ((piece != m.getPiece()) || (dst != m.getTo()) || (m.equals(pMouvement))) { mvts.remove(i); } } boolean preciser = true; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (xSrc != m.getFrom().getFile()) { mvts.remove(i); if (preciser) { sb.append((char) ('a' + xSrc)); preciser = false; } } } final int ySrc = src.getRank(); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (ySrc != m.getFrom().getRank()) { sb.append((char) ('1' + ySrc)); break; } } } if ((pEtat.getPieceAt(dst) != null) || ((dst == pEtat.getEnPassant()) && (t == PAWN))) { if (t == PAWN) { sb.append((char) ('a' + xSrc)); } sb.append('x'); } sb.append(dst.getFENString()); if (t == PAWN) { if (dst == pEtat.getEnPassant()) { sb.append(" e.p."); } else { final int yDst = dst.getRank(); if ((trait && (yDst == RANK_COUNT - 1)) || ((!trait) && (yDst == 0))) { sb.append(QUEEN.getSANLetter()); } } } } if (apres.isInCheck(!trait)) { sb.append('+'); if (nbMvts == 0) { sb.append('+'); } } else if (nbMvts == 0) { sb.append("(=)"); } final String res = sb.toString(); assert SAN_VALIDATOR.matcher(res).matches(); return res; } private  SANUtils(); static Move toMove(final MoveGenerator pEtat, final String pSAN); static String toSAN(final MoveGenerator pEtat, final Move pMouvement); }
SANUtils { public static String toSAN(final MoveGenerator pEtat, final Move pMouvement) { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pMouvement == null) { throw new NullPointerException("Missing move"); } final boolean trait = pEtat.isWhiteActive(); final Piece piece = pMouvement.getPiece(); final PieceType t = piece.getType(); final StringBuilder sb = new StringBuilder(); final Square src = pMouvement.getFrom(); final Square dst = pMouvement.getTo(); final MoveGenerator apres = pEtat.derive(pMouvement, false); final int nbMvts = apres.getValidMoves(!trait).length; final int xSrc = src.getFile(); final int xDst = dst.getFile(); if ((t == KING) && (Math.abs(xSrc - xDst) > 1)) { sb.append("0-0"); if (xSrc > xDst) { sb.append("-0"); } } else { sb.append(t.getSANLetter()); if (t != PAWN) { final List<Move> mvts = new ArrayList<Move>(Arrays.asList(pEtat.getValidMoves(trait))); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if ((piece != m.getPiece()) || (dst != m.getTo()) || (m.equals(pMouvement))) { mvts.remove(i); } } boolean preciser = true; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (xSrc != m.getFrom().getFile()) { mvts.remove(i); if (preciser) { sb.append((char) ('a' + xSrc)); preciser = false; } } } final int ySrc = src.getRank(); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (ySrc != m.getFrom().getRank()) { sb.append((char) ('1' + ySrc)); break; } } } if ((pEtat.getPieceAt(dst) != null) || ((dst == pEtat.getEnPassant()) && (t == PAWN))) { if (t == PAWN) { sb.append((char) ('a' + xSrc)); } sb.append('x'); } sb.append(dst.getFENString()); if (t == PAWN) { if (dst == pEtat.getEnPassant()) { sb.append(" e.p."); } else { final int yDst = dst.getRank(); if ((trait && (yDst == RANK_COUNT - 1)) || ((!trait) && (yDst == 0))) { sb.append(QUEEN.getSANLetter()); } } } } if (apres.isInCheck(!trait)) { sb.append('+'); if (nbMvts == 0) { sb.append('+'); } } else if (nbMvts == 0) { sb.append("(=)"); } final String res = sb.toString(); assert SAN_VALIDATOR.matcher(res).matches(); return res; } private  SANUtils(); static Move toMove(final MoveGenerator pEtat, final String pSAN); static String toSAN(final MoveGenerator pEtat, final Move pMouvement); static final Pattern SAN_VALIDATOR; }