@Test public void testToPGN() { final Game partie1 = new Game(); partie1.moveFromCurrent(new Move(WHITE_PAWN, Square.valueOf("a2"), Square.valueOf("a4"))); partie1.moveFromCurrent(new Move(BLACK_PAWN, Square.valueOf("g7"), Square.valueOf("g6"))); partie1.moveFromCurrent(new Move(WHITE_KNIGHT, Square.valueOf("b1"), Square.valueOf("c3"))); Player joueur = partie1.getPlayer(true); joueur.setEngine(null); joueur.setName("Test"); joueur = partie1.getPlayer(false); joueur.setEngine(EngineFactory.newInstance("jChecs.NegaScout")); joueur.setName("NegaScout"); final StringBuffer attendu1 = new StringBuffer("[Event \"jChecs v"); attendu1.append(APPLICATION_VERSION).append(" chess game\"]\n"); attendu1.append("[Site \"" + _site + "\"]\n"); attendu1.append("[Date \"").append(new SimpleDateFormat("yyyy.MM.dd").format(new Date())) .append("\"]\n"); attendu1.append("[Round \"-\"]\n"); attendu1.append("[White \"Test\"]\n"); attendu1.append("[Black \"jChecs.NegaScout\"]\n"); attendu1.append("[Result \"*\"]\n"); attendu1.append("\n"); attendu1.append("1. a4 g6 2. Nc3 *\n"); assertEquals(attendu1.toString(), toPGN(partie1)); final Game partie2 = new Game(); final String fen = "rnbqkb1r/pp3ppp/4pn2/2pp4/3P4/2PBP3/PP3PPP/RNBQK1NR w KQkq c6 0 5"; Board plateau = null; try { plateau = toBoard(fen); } catch (final FENException e) { fail(e.toString()); } partie2.resetTo(BoardFactory.valueOf(FASTEST, STARTING).derive(plateau)); partie2.moveFromCurrent(new Move(WHITE_PAWN, Square.valueOf("e3"), Square.valueOf("e4"))); final StringBuffer attendu2 = new StringBuffer("[Event \"jChecs v"); attendu2.append(APPLICATION_VERSION).append(" chess game\"]\n"); attendu2.append("[Site \"" + _site + "\"]\n"); attendu2.append("[Date \"").append(new SimpleDateFormat("yyyy.MM.dd").format(new Date())) .append("\"]\n"); attendu2.append("[Round \"-\"]\n"); attendu2.append("[White \"\"]\n"); attendu2.append("[Black \"\"]\n"); attendu2.append("[Result \"*\"]\n"); attendu2.append("[SetUp \"1\"]\n"); attendu2.append("[FEN \"" + fen + "\"]\n"); attendu2.append("\n"); attendu2.append("5. e4 *\n"); assertEquals(attendu2.toString(), toPGN(partie2)); }
public static String toPGN(final Game pPartie) { if (pPartie == null) { throw new NullPointerException("Missing game description"); } final StringBuilder sb = new StringBuilder(); sb.append("[Event \"" + APPLICATION_NAME + " v" + APPLICATION_VERSION + " chess game\"]\n"); String site = "?"; try { final InetAddress lh = InetAddress.getLocalHost(); site = lh.getHostName(); } catch (final UnknownHostException e) { } sb.append("[Site \"").append(site).append("\"]\n"); sb.append("[Date \"").append(formatPGNDate(new Date())).append("\"]\n"); sb.append("[Round \"-\"]\n"); Player joueur = pPartie.getPlayer(true); String nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[White \"").append(nom).append("\"]\n"); joueur = pPartie.getPlayer(false); nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[Black \"").append(nom).append("\"]\n"); final String resultat; switch (pPartie.getState()) { case BLACK_MATES : resultat = "0-1"; break; case DRAWN_BY_50_MOVE_RULE : case DRAWN_BY_TRIPLE_REPETITION : case STALEMATE : resultat = "1/2-1/2"; break; case WHITE_MATES : resultat = "1-0"; break; case IN_PROGRESS : resultat = "*"; break; default : resultat = "*"; assert false; } sb.append("[Result \"").append(resultat).append("\"]\n"); final String depart = pPartie.getStartingPosition(); if (!depart.equals(STANDART_STARTING_FEN)) { sb.append("[SetUp \"1\"]\n"); sb.append("[FEN \"" + depart + "\"]\n"); } sb.append('\n'); int col = 0; for (final String san : pPartie.getSANStrings()) { String pgn = san.replace("0-0-0", "O-O-O"); pgn = pgn.replace("0-0", "O-O"); pgn = pgn.replace("++", "#"); final int l = pgn.length(); col += l; if (col >= 80) { sb.append('\n'); col = l; } sb.append(pgn); } col += resultat.length(); if (col >= 80) { sb.append('\n'); } sb.append(resultat).append('\n'); return sb.toString(); }
PGNUtils { public static String toPGN(final Game pPartie) { if (pPartie == null) { throw new NullPointerException("Missing game description"); } final StringBuilder sb = new StringBuilder(); sb.append("[Event \"" + APPLICATION_NAME + " v" + APPLICATION_VERSION + " chess game\"]\n"); String site = "?"; try { final InetAddress lh = InetAddress.getLocalHost(); site = lh.getHostName(); } catch (final UnknownHostException e) { } sb.append("[Site \"").append(site).append("\"]\n"); sb.append("[Date \"").append(formatPGNDate(new Date())).append("\"]\n"); sb.append("[Round \"-\"]\n"); Player joueur = pPartie.getPlayer(true); String nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[White \"").append(nom).append("\"]\n"); joueur = pPartie.getPlayer(false); nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[Black \"").append(nom).append("\"]\n"); final String resultat; switch (pPartie.getState()) { case BLACK_MATES : resultat = "0-1"; break; case DRAWN_BY_50_MOVE_RULE : case DRAWN_BY_TRIPLE_REPETITION : case STALEMATE : resultat = "1/2-1/2"; break; case WHITE_MATES : resultat = "1-0"; break; case IN_PROGRESS : resultat = "*"; break; default : resultat = "*"; assert false; } sb.append("[Result \"").append(resultat).append("\"]\n"); final String depart = pPartie.getStartingPosition(); if (!depart.equals(STANDART_STARTING_FEN)) { sb.append("[SetUp \"1\"]\n"); sb.append("[FEN \"" + depart + "\"]\n"); } sb.append('\n'); int col = 0; for (final String san : pPartie.getSANStrings()) { String pgn = san.replace("0-0-0", "O-O-O"); pgn = pgn.replace("0-0", "O-O"); pgn = pgn.replace("++", "#"); final int l = pgn.length(); col += l; if (col >= 80) { sb.append('\n'); col = l; } sb.append(pgn); } col += resultat.length(); if (col >= 80) { sb.append('\n'); } sb.append(resultat).append('\n'); return sb.toString(); } }
PGNUtils { public static String toPGN(final Game pPartie) { if (pPartie == null) { throw new NullPointerException("Missing game description"); } final StringBuilder sb = new StringBuilder(); sb.append("[Event \"" + APPLICATION_NAME + " v" + APPLICATION_VERSION + " chess game\"]\n"); String site = "?"; try { final InetAddress lh = InetAddress.getLocalHost(); site = lh.getHostName(); } catch (final UnknownHostException e) { } sb.append("[Site \"").append(site).append("\"]\n"); sb.append("[Date \"").append(formatPGNDate(new Date())).append("\"]\n"); sb.append("[Round \"-\"]\n"); Player joueur = pPartie.getPlayer(true); String nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[White \"").append(nom).append("\"]\n"); joueur = pPartie.getPlayer(false); nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[Black \"").append(nom).append("\"]\n"); final String resultat; switch (pPartie.getState()) { case BLACK_MATES : resultat = "0-1"; break; case DRAWN_BY_50_MOVE_RULE : case DRAWN_BY_TRIPLE_REPETITION : case STALEMATE : resultat = "1/2-1/2"; break; case WHITE_MATES : resultat = "1-0"; break; case IN_PROGRESS : resultat = "*"; break; default : resultat = "*"; assert false; } sb.append("[Result \"").append(resultat).append("\"]\n"); final String depart = pPartie.getStartingPosition(); if (!depart.equals(STANDART_STARTING_FEN)) { sb.append("[SetUp \"1\"]\n"); sb.append("[FEN \"" + depart + "\"]\n"); } sb.append('\n'); int col = 0; for (final String san : pPartie.getSANStrings()) { String pgn = san.replace("0-0-0", "O-O-O"); pgn = pgn.replace("0-0", "O-O"); pgn = pgn.replace("++", "#"); final int l = pgn.length(); col += l; if (col >= 80) { sb.append('\n'); col = l; } sb.append(pgn); } col += resultat.length(); if (col >= 80) { sb.append('\n'); } sb.append(resultat).append('\n'); return sb.toString(); } private  PGNUtils(); }
PGNUtils { public static String toPGN(final Game pPartie) { if (pPartie == null) { throw new NullPointerException("Missing game description"); } final StringBuilder sb = new StringBuilder(); sb.append("[Event \"" + APPLICATION_NAME + " v" + APPLICATION_VERSION + " chess game\"]\n"); String site = "?"; try { final InetAddress lh = InetAddress.getLocalHost(); site = lh.getHostName(); } catch (final UnknownHostException e) { } sb.append("[Site \"").append(site).append("\"]\n"); sb.append("[Date \"").append(formatPGNDate(new Date())).append("\"]\n"); sb.append("[Round \"-\"]\n"); Player joueur = pPartie.getPlayer(true); String nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[White \"").append(nom).append("\"]\n"); joueur = pPartie.getPlayer(false); nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[Black \"").append(nom).append("\"]\n"); final String resultat; switch (pPartie.getState()) { case BLACK_MATES : resultat = "0-1"; break; case DRAWN_BY_50_MOVE_RULE : case DRAWN_BY_TRIPLE_REPETITION : case STALEMATE : resultat = "1/2-1/2"; break; case WHITE_MATES : resultat = "1-0"; break; case IN_PROGRESS : resultat = "*"; break; default : resultat = "*"; assert false; } sb.append("[Result \"").append(resultat).append("\"]\n"); final String depart = pPartie.getStartingPosition(); if (!depart.equals(STANDART_STARTING_FEN)) { sb.append("[SetUp \"1\"]\n"); sb.append("[FEN \"" + depart + "\"]\n"); } sb.append('\n'); int col = 0; for (final String san : pPartie.getSANStrings()) { String pgn = san.replace("0-0-0", "O-O-O"); pgn = pgn.replace("0-0", "O-O"); pgn = pgn.replace("++", "#"); final int l = pgn.length(); col += l; if (col >= 80) { sb.append('\n'); col = l; } sb.append(pgn); } col += resultat.length(); if (col >= 80) { sb.append('\n'); } sb.append(resultat).append('\n'); return sb.toString(); } private  PGNUtils(); static Game toGame(final BufferedReader pFlux); static String toNormalizedSAN(final String pChaine); static String toPGN(final Game pPartie); }
PGNUtils { public static String toPGN(final Game pPartie) { if (pPartie == null) { throw new NullPointerException("Missing game description"); } final StringBuilder sb = new StringBuilder(); sb.append("[Event \"" + APPLICATION_NAME + " v" + APPLICATION_VERSION + " chess game\"]\n"); String site = "?"; try { final InetAddress lh = InetAddress.getLocalHost(); site = lh.getHostName(); } catch (final UnknownHostException e) { } sb.append("[Site \"").append(site).append("\"]\n"); sb.append("[Date \"").append(formatPGNDate(new Date())).append("\"]\n"); sb.append("[Round \"-\"]\n"); Player joueur = pPartie.getPlayer(true); String nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[White \"").append(nom).append("\"]\n"); joueur = pPartie.getPlayer(false); nom = joueur.getName(); if (joueur.getEngine() != null) { nom = APPLICATION_NAME + '.' + nom; } sb.append("[Black \"").append(nom).append("\"]\n"); final String resultat; switch (pPartie.getState()) { case BLACK_MATES : resultat = "0-1"; break; case DRAWN_BY_50_MOVE_RULE : case DRAWN_BY_TRIPLE_REPETITION : case STALEMATE : resultat = "1/2-1/2"; break; case WHITE_MATES : resultat = "1-0"; break; case IN_PROGRESS : resultat = "*"; break; default : resultat = "*"; assert false; } sb.append("[Result \"").append(resultat).append("\"]\n"); final String depart = pPartie.getStartingPosition(); if (!depart.equals(STANDART_STARTING_FEN)) { sb.append("[SetUp \"1\"]\n"); sb.append("[FEN \"" + depart + "\"]\n"); } sb.append('\n'); int col = 0; for (final String san : pPartie.getSANStrings()) { String pgn = san.replace("0-0-0", "O-O-O"); pgn = pgn.replace("0-0", "O-O"); pgn = pgn.replace("++", "#"); final int l = pgn.length(); col += l; if (col >= 80) { sb.append('\n'); col = l; } sb.append(pgn); } col += resultat.length(); if (col >= 80) { sb.append('\n'); } sb.append(resultat).append('\n'); return sb.toString(); } private  PGNUtils(); static Game toGame(final BufferedReader pFlux); static String toNormalizedSAN(final String pChaine); static String toPGN(final Game pPartie);  }