@Test public void testToGame() { final String pgn1 = "[Event \"jChecs vX.X.X chess game\"]\n" + "[Site \"" + _site + "\"]\n" + "[Date \"2006.12.31\"]\n" + "[Round \"-\"]\n" + "[White \"jChecs.AlphaBeta\"]\n" + "[Black \"Test\"]\n" + "[Result \"*\"]\n" + "\n" + "1. Pa4 h6? 2. N@c3 *\n"; try { final Game partie = toGame(new BufferedReader(new StringReader(pgn1))); Player joueur = partie.getPlayer(true); assertEquals("AlphaBeta", joueur.getName()); assertEquals(EngineFactory.newInstance("jChecs.AlphaBeta").getClass(), joueur.getEngine() .getClass()); joueur = partie.getPlayer(false); assertEquals("Test", joueur.getName()); assertNull(joueur.getEngine()); assertTrue(partie.getState() == Game.State.IN_PROGRESS); assertEquals("rnbqkbnr/ppppppp1/7p/8/P7/2N5/1PPPPPPP/R1BQKBNR b KQkq - 1 2", toFEN(partie .getBoard())); } catch (final PGNException e) { fail(e.toString()); } final String pgn2 = "[Event \"jChecs vX.X.X chess game\"]\n" + "[Site \"" + _site + "\"]\n" + "[Date \"2006.12.31\"]\n" + "[Round \"-\"]\n" + "[White \"\"]\n" + "[Black \"\"]\n" + "[Result \"*\"]\n" + "[SetUp \"1\"]\n" + "[FEN \"rnbqkb1r/pp3ppp/4pn2/2pp4/3P4/2PBP3/PP3PPP/RNBQK1NR w KQkq c6 0 5\"]\n" + "\n" + "5. e4 *\n"; try { final Game partie = toGame(new BufferedReader(new StringReader(pgn2))); Player joueur = partie.getPlayer(true); assertEquals("", joueur.getName()); assertNull(joueur.getEngine()); joueur = partie.getPlayer(false); assertEquals("", joueur.getName()); assertNull(joueur.getEngine()); assertTrue(partie.getState() == Game.State.IN_PROGRESS); assertEquals("rnbqkb1r/pp3ppp/4pn2/2pp4/3P4/2PBP3/PP3PPP/RNBQK1NR w KQkq c6 0 5", partie .getStartingPosition()); assertEquals("rnbqkb1r/pp3ppp/4pn2/2pp4/3PP3/2PB4/PP3PPP/RNBQK1NR b KQkq - 0 5", toFEN(partie .getBoard())); } catch (final PGNException e) { fail(e.toString()); } }
public static Game toGame(final BufferedReader pFlux) throws PGNException { assert pFlux != null; final Game res = new Game(); try { String ligneLue = pFlux.readLine(); while (ligneLue != null) { String ligne = ligneLue.trim(); if (ligne.startsWith("[")) { final int debTag = ligne.indexOf(" \""); if ((debTag >= 0) && ligne.endsWith("\"]")) { final String contenu = ligne.substring(debTag + 2, ligne.indexOf("\"]")).trim(); if (ligne.startsWith("[Black \"")) { final Player joueur = res.getPlayer(false); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[White \"")) { final Player joueur = res.getPlayer(true); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[FEN \"")) { try { final Board depart = toBoard(contenu); res.resetTo(BoardFactory.valueOf(FASTEST, STARTING).derive(depart)); } catch (final FENException e) { throw new PGNException("Invalid FEN tag", e); } } } } else { final StringBuilder sb = new StringBuilder(); while (ligneLue != null) { ligne = ligneLue.trim(); if (ligne.startsWith("[Event")) { break; } sb.append(' ').append(ligne); ligneLue = pFlux.readLine(); } int p = 0; int prof = 0; while (p < sb.length()) { final char c = sb.charAt(p); if ((c == '(') || (c == '{')) { prof++; } if ((prof != 0) || (c == '+') || (c == '#')) { sb.deleteCharAt(p); } else { if (c == 'O') { sb.setCharAt(p, '0'); } p++; } if ((c == '}') || (c == ')')) { prof--; } } p = 0; while (p < sb.length()) { final char c = sb.charAt(p); if (c == '.') { int deb = p - 1; while ((deb >= 0) && Character.isDigit(sb.charAt(deb))) { deb--; } int fin = p + 1; while ((fin < sb.length()) && ((" .".indexOf(sb.charAt(fin))) >= 0)) { fin++; } sb.delete(deb + 1, fin); } else if (c == '$') { int fin = p + 1; while ((fin < sb.length()) && Character.isDigit(sb.charAt(fin))) { fin++; } sb.delete(p, fin + 1); } else { p++; } } for (final String mvt : SPLITTER.split(sb.toString())) { if ("*".equals(mvt) || "1-0".equals(mvt) || "0-1".equals(mvt) || "1/2-1/2".equals(mvt)) { break; } if (mvt.length() > 0) { try { res.moveFromCurrent(toMove(res.getBoard(), toNormalizedSAN(mvt))); } catch (final SANException e) { throw new PGNException("Invalid PGN stream", e); } } } break; } ligneLue = pFlux.readLine(); } } catch (final IOException e) { throw new PGNException("PGN stream reading error", e); } return res; }
PGNUtils { public static Game toGame(final BufferedReader pFlux) throws PGNException { assert pFlux != null; final Game res = new Game(); try { String ligneLue = pFlux.readLine(); while (ligneLue != null) { String ligne = ligneLue.trim(); if (ligne.startsWith("[")) { final int debTag = ligne.indexOf(" \""); if ((debTag >= 0) && ligne.endsWith("\"]")) { final String contenu = ligne.substring(debTag + 2, ligne.indexOf("\"]")).trim(); if (ligne.startsWith("[Black \"")) { final Player joueur = res.getPlayer(false); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[White \"")) { final Player joueur = res.getPlayer(true); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[FEN \"")) { try { final Board depart = toBoard(contenu); res.resetTo(BoardFactory.valueOf(FASTEST, STARTING).derive(depart)); } catch (final FENException e) { throw new PGNException("Invalid FEN tag", e); } } } } else { final StringBuilder sb = new StringBuilder(); while (ligneLue != null) { ligne = ligneLue.trim(); if (ligne.startsWith("[Event")) { break; } sb.append(' ').append(ligne); ligneLue = pFlux.readLine(); } int p = 0; int prof = 0; while (p < sb.length()) { final char c = sb.charAt(p); if ((c == '(') || (c == '{')) { prof++; } if ((prof != 0) || (c == '+') || (c == '#')) { sb.deleteCharAt(p); } else { if (c == 'O') { sb.setCharAt(p, '0'); } p++; } if ((c == '}') || (c == ')')) { prof--; } } p = 0; while (p < sb.length()) { final char c = sb.charAt(p); if (c == '.') { int deb = p - 1; while ((deb >= 0) && Character.isDigit(sb.charAt(deb))) { deb--; } int fin = p + 1; while ((fin < sb.length()) && ((" .".indexOf(sb.charAt(fin))) >= 0)) { fin++; } sb.delete(deb + 1, fin); } else if (c == '$') { int fin = p + 1; while ((fin < sb.length()) && Character.isDigit(sb.charAt(fin))) { fin++; } sb.delete(p, fin + 1); } else { p++; } } for (final String mvt : SPLITTER.split(sb.toString())) { if ("*".equals(mvt) || "1-0".equals(mvt) || "0-1".equals(mvt) || "1/2-1/2".equals(mvt)) { break; } if (mvt.length() > 0) { try { res.moveFromCurrent(toMove(res.getBoard(), toNormalizedSAN(mvt))); } catch (final SANException e) { throw new PGNException("Invalid PGN stream", e); } } } break; } ligneLue = pFlux.readLine(); } } catch (final IOException e) { throw new PGNException("PGN stream reading error", e); } return res; } }
PGNUtils { public static Game toGame(final BufferedReader pFlux) throws PGNException { assert pFlux != null; final Game res = new Game(); try { String ligneLue = pFlux.readLine(); while (ligneLue != null) { String ligne = ligneLue.trim(); if (ligne.startsWith("[")) { final int debTag = ligne.indexOf(" \""); if ((debTag >= 0) && ligne.endsWith("\"]")) { final String contenu = ligne.substring(debTag + 2, ligne.indexOf("\"]")).trim(); if (ligne.startsWith("[Black \"")) { final Player joueur = res.getPlayer(false); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[White \"")) { final Player joueur = res.getPlayer(true); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[FEN \"")) { try { final Board depart = toBoard(contenu); res.resetTo(BoardFactory.valueOf(FASTEST, STARTING).derive(depart)); } catch (final FENException e) { throw new PGNException("Invalid FEN tag", e); } } } } else { final StringBuilder sb = new StringBuilder(); while (ligneLue != null) { ligne = ligneLue.trim(); if (ligne.startsWith("[Event")) { break; } sb.append(' ').append(ligne); ligneLue = pFlux.readLine(); } int p = 0; int prof = 0; while (p < sb.length()) { final char c = sb.charAt(p); if ((c == '(') || (c == '{')) { prof++; } if ((prof != 0) || (c == '+') || (c == '#')) { sb.deleteCharAt(p); } else { if (c == 'O') { sb.setCharAt(p, '0'); } p++; } if ((c == '}') || (c == ')')) { prof--; } } p = 0; while (p < sb.length()) { final char c = sb.charAt(p); if (c == '.') { int deb = p - 1; while ((deb >= 0) && Character.isDigit(sb.charAt(deb))) { deb--; } int fin = p + 1; while ((fin < sb.length()) && ((" .".indexOf(sb.charAt(fin))) >= 0)) { fin++; } sb.delete(deb + 1, fin); } else if (c == '$') { int fin = p + 1; while ((fin < sb.length()) && Character.isDigit(sb.charAt(fin))) { fin++; } sb.delete(p, fin + 1); } else { p++; } } for (final String mvt : SPLITTER.split(sb.toString())) { if ("*".equals(mvt) || "1-0".equals(mvt) || "0-1".equals(mvt) || "1/2-1/2".equals(mvt)) { break; } if (mvt.length() > 0) { try { res.moveFromCurrent(toMove(res.getBoard(), toNormalizedSAN(mvt))); } catch (final SANException e) { throw new PGNException("Invalid PGN stream", e); } } } break; } ligneLue = pFlux.readLine(); } } catch (final IOException e) { throw new PGNException("PGN stream reading error", e); } return res; } private  PGNUtils(); }
PGNUtils { public static Game toGame(final BufferedReader pFlux) throws PGNException { assert pFlux != null; final Game res = new Game(); try { String ligneLue = pFlux.readLine(); while (ligneLue != null) { String ligne = ligneLue.trim(); if (ligne.startsWith("[")) { final int debTag = ligne.indexOf(" \""); if ((debTag >= 0) && ligne.endsWith("\"]")) { final String contenu = ligne.substring(debTag + 2, ligne.indexOf("\"]")).trim(); if (ligne.startsWith("[Black \"")) { final Player joueur = res.getPlayer(false); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[White \"")) { final Player joueur = res.getPlayer(true); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[FEN \"")) { try { final Board depart = toBoard(contenu); res.resetTo(BoardFactory.valueOf(FASTEST, STARTING).derive(depart)); } catch (final FENException e) { throw new PGNException("Invalid FEN tag", e); } } } } else { final StringBuilder sb = new StringBuilder(); while (ligneLue != null) { ligne = ligneLue.trim(); if (ligne.startsWith("[Event")) { break; } sb.append(' ').append(ligne); ligneLue = pFlux.readLine(); } int p = 0; int prof = 0; while (p < sb.length()) { final char c = sb.charAt(p); if ((c == '(') || (c == '{')) { prof++; } if ((prof != 0) || (c == '+') || (c == '#')) { sb.deleteCharAt(p); } else { if (c == 'O') { sb.setCharAt(p, '0'); } p++; } if ((c == '}') || (c == ')')) { prof--; } } p = 0; while (p < sb.length()) { final char c = sb.charAt(p); if (c == '.') { int deb = p - 1; while ((deb >= 0) && Character.isDigit(sb.charAt(deb))) { deb--; } int fin = p + 1; while ((fin < sb.length()) && ((" .".indexOf(sb.charAt(fin))) >= 0)) { fin++; } sb.delete(deb + 1, fin); } else if (c == '$') { int fin = p + 1; while ((fin < sb.length()) && Character.isDigit(sb.charAt(fin))) { fin++; } sb.delete(p, fin + 1); } else { p++; } } for (final String mvt : SPLITTER.split(sb.toString())) { if ("*".equals(mvt) || "1-0".equals(mvt) || "0-1".equals(mvt) || "1/2-1/2".equals(mvt)) { break; } if (mvt.length() > 0) { try { res.moveFromCurrent(toMove(res.getBoard(), toNormalizedSAN(mvt))); } catch (final SANException e) { throw new PGNException("Invalid PGN stream", e); } } } break; } ligneLue = pFlux.readLine(); } } catch (final IOException e) { throw new PGNException("PGN stream reading error", e); } return res; } private  PGNUtils(); static Game toGame(final BufferedReader pFlux); static String toNormalizedSAN(final String pChaine); static String toPGN(final Game pPartie); }
PGNUtils { public static Game toGame(final BufferedReader pFlux) throws PGNException { assert pFlux != null; final Game res = new Game(); try { String ligneLue = pFlux.readLine(); while (ligneLue != null) { String ligne = ligneLue.trim(); if (ligne.startsWith("[")) { final int debTag = ligne.indexOf(" \""); if ((debTag >= 0) && ligne.endsWith("\"]")) { final String contenu = ligne.substring(debTag + 2, ligne.indexOf("\"]")).trim(); if (ligne.startsWith("[Black \"")) { final Player joueur = res.getPlayer(false); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[White \"")) { final Player joueur = res.getPlayer(true); if (contenu.startsWith(APPLICATION_NAME + '.')) { joueur.setEngine(EngineFactory.newInstance(contenu)); joueur.setName(contenu.substring(APPLICATION_NAME.length() + 1)); } else { joueur.setEngine(null); joueur.setName(contenu); } } else if (ligne.startsWith("[FEN \"")) { try { final Board depart = toBoard(contenu); res.resetTo(BoardFactory.valueOf(FASTEST, STARTING).derive(depart)); } catch (final FENException e) { throw new PGNException("Invalid FEN tag", e); } } } } else { final StringBuilder sb = new StringBuilder(); while (ligneLue != null) { ligne = ligneLue.trim(); if (ligne.startsWith("[Event")) { break; } sb.append(' ').append(ligne); ligneLue = pFlux.readLine(); } int p = 0; int prof = 0; while (p < sb.length()) { final char c = sb.charAt(p); if ((c == '(') || (c == '{')) { prof++; } if ((prof != 0) || (c == '+') || (c == '#')) { sb.deleteCharAt(p); } else { if (c == 'O') { sb.setCharAt(p, '0'); } p++; } if ((c == '}') || (c == ')')) { prof--; } } p = 0; while (p < sb.length()) { final char c = sb.charAt(p); if (c == '.') { int deb = p - 1; while ((deb >= 0) && Character.isDigit(sb.charAt(deb))) { deb--; } int fin = p + 1; while ((fin < sb.length()) && ((" .".indexOf(sb.charAt(fin))) >= 0)) { fin++; } sb.delete(deb + 1, fin); } else if (c == '$') { int fin = p + 1; while ((fin < sb.length()) && Character.isDigit(sb.charAt(fin))) { fin++; } sb.delete(p, fin + 1); } else { p++; } } for (final String mvt : SPLITTER.split(sb.toString())) { if ("*".equals(mvt) || "1-0".equals(mvt) || "0-1".equals(mvt) || "1/2-1/2".equals(mvt)) { break; } if (mvt.length() > 0) { try { res.moveFromCurrent(toMove(res.getBoard(), toNormalizedSAN(mvt))); } catch (final SANException e) { throw new PGNException("Invalid PGN stream", e); } } } break; } ligneLue = pFlux.readLine(); } } catch (final IOException e) { throw new PGNException("PGN stream reading error", e); } return res; } private  PGNUtils(); static Game toGame(final BufferedReader pFlux); static String toNormalizedSAN(final String pChaine); static String toPGN(final Game pPartie);  }