@Test public void testToMove() { MoveGenerator etat = BoardFactory.valueOf(ARRAY, STARTING); try { Move mvt = toMove(etat, "e3"); assertEquals(new Move(WHITE_PAWN, Square.valueOf("e2"), Square.valueOf("e3")), mvt); etat = etat.derive(mvt, true); mvt = toMove(etat, "Na6"); assertEquals(new Move(BLACK_KNIGHT, Square.valueOf("b8"), Square.valueOf("a6")), mvt); etat = etat.derive(mvt, true); mvt = toMove(etat, "Bc4"); assertEquals(new Move(WHITE_BISHOP, Square.valueOf("f1"), Square.valueOf("c4")), mvt); etat = etat.derive(mvt, true); mvt = toMove(etat, "c5"); assertEquals(new Move(BLACK_PAWN, Square.valueOf("c7"), Square.valueOf("c5")), mvt); etat = etat.derive(mvt, true); mvt = toMove(etat, "Qf3"); assertEquals(new Move(WHITE_QUEEN, Square.valueOf("d1"), Square.valueOf("f3")), mvt); etat = etat.derive(mvt, true); mvt = toMove(etat, "b6"); assertEquals(new Move(BLACK_PAWN, Square.valueOf("b7"), Square.valueOf("b6")), mvt); etat = etat.derive(mvt, true); mvt = toMove(etat, "Qxf7++"); assertEquals(new Move(WHITE_QUEEN, Square.valueOf("f3"), Square.valueOf("f7"), BLACK_PAWN), mvt); etat = etat.derive(mvt, true); } catch (final SANException e) { fail(e.toString()); } assertFalse(etat.isWhiteActive()); assertTrue(etat.isInCheck(false)); try { etat = BoardFactory.valueOf(ARRAY, EMPTY).derive(toBoard(AMBIGUOUS_PAWN_FEN)); Move mvt = toMove(etat, "cxb4"); assertEquals(new Move(WHITE_PAWN, Square.valueOf("c3"), Square.valueOf("b4"), BLACK_KNIGHT), mvt); etat = BoardFactory.valueOf(ARRAY, EMPTY).derive(toBoard(AMBIGUOUS_KNIGHT_FEN)); mvt = toMove(etat, "Nfxe5"); assertEquals(new Move(WHITE_KNIGHT, Square.valueOf("f3"), Square.valueOf("e5"), BLACK_PAWN), mvt); etat = BoardFactory.valueOf(ARRAY, EMPTY).derive(toBoard(AMBIGUOUS_QUEEN_FEN)); mvt = toMove(etat, "Qf5xd5(=)"); assertEquals(new Move(WHITE_QUEEN, Square.valueOf("f5"), Square.valueOf("d5"), BLACK_PAWN), mvt); } catch (final FENException e) { fail(e.toString()); } catch (final SANException e) { fail(e.toString()); } }
public static Move toMove(final MoveGenerator pEtat, final String pSAN) throws SANException { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pSAN == null) { throw new NullPointerException("Missing SAN string"); } if (!SAN_VALIDATOR.matcher(pSAN).matches()) { throw new SANException("Invalid SAN string [" + pSAN + ']', null); } final boolean trait = pEtat.isWhiteActive(); if ("0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(6)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(62)); } else if ("0-0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(2)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(58)); } final Piece piece; int posSrc = 0; char c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { if (trait) { piece = WHITE_PAWN; } else { piece = BLACK_PAWN; } } else { if (trait) { piece = Piece.valueOf(c); } else { piece = Piece.valueOf(Character.toLowerCase(c)); } posSrc++; } final boolean prise = pSAN.indexOf('x') >= 0; 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); final boolean capture = m.getCaptured() != null; if ((piece != m.getPiece()) || (prise != capture)) { mvts.remove(i); } } int posDst = pSAN.length() - 1; while ((posDst > 0) && (!Character.isDigit(pSAN.charAt(posDst)))) { posDst--; } final Square dst = Square.valueOf(pSAN.substring(posDst - 1, posDst + 1)); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (dst != m.getTo()) { mvts.remove(i); } } if (mvts.size() == 1) { return mvts.get(0); } c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { final int col = c - 'a'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (col != m.getFrom().getFile()) { mvts.remove(i); } } posSrc++; } c = pSAN.charAt(posSrc); if (Character.isDigit(c)) { final int lig = c - '1'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (lig != m.getFrom().getRank()) { mvts.remove(i); } } posSrc++; } final int l = mvts.size(); if (l > 1) { throw new SANException("Ambiguous SAN string [" + pSAN + ']', null); } else if (l < 1) { throw new SANException("Illegal SAN string context [" + pSAN + ']', null); } return mvts.get(0); }
SANUtils { public static Move toMove(final MoveGenerator pEtat, final String pSAN) throws SANException { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pSAN == null) { throw new NullPointerException("Missing SAN string"); } if (!SAN_VALIDATOR.matcher(pSAN).matches()) { throw new SANException("Invalid SAN string [" + pSAN + ']', null); } final boolean trait = pEtat.isWhiteActive(); if ("0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(6)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(62)); } else if ("0-0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(2)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(58)); } final Piece piece; int posSrc = 0; char c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { if (trait) { piece = WHITE_PAWN; } else { piece = BLACK_PAWN; } } else { if (trait) { piece = Piece.valueOf(c); } else { piece = Piece.valueOf(Character.toLowerCase(c)); } posSrc++; } final boolean prise = pSAN.indexOf('x') >= 0; 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); final boolean capture = m.getCaptured() != null; if ((piece != m.getPiece()) || (prise != capture)) { mvts.remove(i); } } int posDst = pSAN.length() - 1; while ((posDst > 0) && (!Character.isDigit(pSAN.charAt(posDst)))) { posDst--; } final Square dst = Square.valueOf(pSAN.substring(posDst - 1, posDst + 1)); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (dst != m.getTo()) { mvts.remove(i); } } if (mvts.size() == 1) { return mvts.get(0); } c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { final int col = c - 'a'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (col != m.getFrom().getFile()) { mvts.remove(i); } } posSrc++; } c = pSAN.charAt(posSrc); if (Character.isDigit(c)) { final int lig = c - '1'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (lig != m.getFrom().getRank()) { mvts.remove(i); } } posSrc++; } final int l = mvts.size(); if (l > 1) { throw new SANException("Ambiguous SAN string [" + pSAN + ']', null); } else if (l < 1) { throw new SANException("Illegal SAN string context [" + pSAN + ']', null); } return mvts.get(0); } }
SANUtils { public static Move toMove(final MoveGenerator pEtat, final String pSAN) throws SANException { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pSAN == null) { throw new NullPointerException("Missing SAN string"); } if (!SAN_VALIDATOR.matcher(pSAN).matches()) { throw new SANException("Invalid SAN string [" + pSAN + ']', null); } final boolean trait = pEtat.isWhiteActive(); if ("0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(6)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(62)); } else if ("0-0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(2)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(58)); } final Piece piece; int posSrc = 0; char c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { if (trait) { piece = WHITE_PAWN; } else { piece = BLACK_PAWN; } } else { if (trait) { piece = Piece.valueOf(c); } else { piece = Piece.valueOf(Character.toLowerCase(c)); } posSrc++; } final boolean prise = pSAN.indexOf('x') >= 0; 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); final boolean capture = m.getCaptured() != null; if ((piece != m.getPiece()) || (prise != capture)) { mvts.remove(i); } } int posDst = pSAN.length() - 1; while ((posDst > 0) && (!Character.isDigit(pSAN.charAt(posDst)))) { posDst--; } final Square dst = Square.valueOf(pSAN.substring(posDst - 1, posDst + 1)); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (dst != m.getTo()) { mvts.remove(i); } } if (mvts.size() == 1) { return mvts.get(0); } c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { final int col = c - 'a'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (col != m.getFrom().getFile()) { mvts.remove(i); } } posSrc++; } c = pSAN.charAt(posSrc); if (Character.isDigit(c)) { final int lig = c - '1'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (lig != m.getFrom().getRank()) { mvts.remove(i); } } posSrc++; } final int l = mvts.size(); if (l > 1) { throw new SANException("Ambiguous SAN string [" + pSAN + ']', null); } else if (l < 1) { throw new SANException("Illegal SAN string context [" + pSAN + ']', null); } return mvts.get(0); } private  SANUtils(); }
SANUtils { public static Move toMove(final MoveGenerator pEtat, final String pSAN) throws SANException { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pSAN == null) { throw new NullPointerException("Missing SAN string"); } if (!SAN_VALIDATOR.matcher(pSAN).matches()) { throw new SANException("Invalid SAN string [" + pSAN + ']', null); } final boolean trait = pEtat.isWhiteActive(); if ("0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(6)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(62)); } else if ("0-0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(2)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(58)); } final Piece piece; int posSrc = 0; char c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { if (trait) { piece = WHITE_PAWN; } else { piece = BLACK_PAWN; } } else { if (trait) { piece = Piece.valueOf(c); } else { piece = Piece.valueOf(Character.toLowerCase(c)); } posSrc++; } final boolean prise = pSAN.indexOf('x') >= 0; 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); final boolean capture = m.getCaptured() != null; if ((piece != m.getPiece()) || (prise != capture)) { mvts.remove(i); } } int posDst = pSAN.length() - 1; while ((posDst > 0) && (!Character.isDigit(pSAN.charAt(posDst)))) { posDst--; } final Square dst = Square.valueOf(pSAN.substring(posDst - 1, posDst + 1)); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (dst != m.getTo()) { mvts.remove(i); } } if (mvts.size() == 1) { return mvts.get(0); } c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { final int col = c - 'a'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (col != m.getFrom().getFile()) { mvts.remove(i); } } posSrc++; } c = pSAN.charAt(posSrc); if (Character.isDigit(c)) { final int lig = c - '1'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (lig != m.getFrom().getRank()) { mvts.remove(i); } } posSrc++; } final int l = mvts.size(); if (l > 1) { throw new SANException("Ambiguous SAN string [" + pSAN + ']', null); } else if (l < 1) { throw new SANException("Illegal SAN string context [" + pSAN + ']', null); } return mvts.get(0); } private  SANUtils(); static Move toMove(final MoveGenerator pEtat, final String pSAN); static String toSAN(final MoveGenerator pEtat, final Move pMouvement); }
SANUtils { public static Move toMove(final MoveGenerator pEtat, final String pSAN) throws SANException { if (pEtat == null) { throw new NullPointerException("Missing game state"); } if (pSAN == null) { throw new NullPointerException("Missing SAN string"); } if (!SAN_VALIDATOR.matcher(pSAN).matches()) { throw new SANException("Invalid SAN string [" + pSAN + ']', null); } final boolean trait = pEtat.isWhiteActive(); if ("0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(6)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(62)); } else if ("0-0-0".equals(pSAN)) { if (trait) { return new Move(WHITE_KING, Square.valueOf(4), Square.valueOf(2)); } return new Move(BLACK_KING, Square.valueOf(60), Square.valueOf(58)); } final Piece piece; int posSrc = 0; char c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { if (trait) { piece = WHITE_PAWN; } else { piece = BLACK_PAWN; } } else { if (trait) { piece = Piece.valueOf(c); } else { piece = Piece.valueOf(Character.toLowerCase(c)); } posSrc++; } final boolean prise = pSAN.indexOf('x') >= 0; 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); final boolean capture = m.getCaptured() != null; if ((piece != m.getPiece()) || (prise != capture)) { mvts.remove(i); } } int posDst = pSAN.length() - 1; while ((posDst > 0) && (!Character.isDigit(pSAN.charAt(posDst)))) { posDst--; } final Square dst = Square.valueOf(pSAN.substring(posDst - 1, posDst + 1)); for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (dst != m.getTo()) { mvts.remove(i); } } if (mvts.size() == 1) { return mvts.get(0); } c = pSAN.charAt(posSrc); if (Character.isLowerCase(c)) { final int col = c - 'a'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (col != m.getFrom().getFile()) { mvts.remove(i); } } posSrc++; } c = pSAN.charAt(posSrc); if (Character.isDigit(c)) { final int lig = c - '1'; for (int i = mvts.size() - 1; i >= 0; i--) { final Move m = mvts.get(i); if (lig != m.getFrom().getRank()) { mvts.remove(i); } } posSrc++; } final int l = mvts.size(); if (l > 1) { throw new SANException("Ambiguous SAN string [" + pSAN + ']', null); } else if (l < 1) { throw new SANException("Illegal SAN string context [" + pSAN + ']', null); } return mvts.get(0); } 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; }