ChessController { public void setPGN(String pgn) throws ChessParseError { { StringBuilder out = new StringBuilder(); Scanner sc = new Scanner(pgn); sc.useDelimiter(""); while (sc.hasNext()) { String c = sc.next(); if (c.equals("{")) { sc.skip("[^}]*}"); } else if (c.equals(";")) { sc.skip("[^\n]*\n"); } else { out.append(c); } } pgn = out.toString(); sc.close(); } Position pos = TextIO.readFEN(TextIO.startPosFEN); Scanner sc = new Scanner(pgn); sc.useDelimiter("\\s+"); while (sc.hasNext("\\[.*")) { String tagName = sc.next(); if (tagName.length() > 1) { tagName = tagName.substring(1); } else { tagName = sc.next(); } String tagValue = sc.findWithinHorizon(".*\\]", 0); tagValue = tagValue.trim(); if (tagValue.charAt(0) == '"') tagValue = tagValue.substring(1); if (tagValue.charAt(tagValue.length()-1) == ']') tagValue = tagValue.substring(0, tagValue.length() - 1); if (tagValue.charAt(tagValue.length()-1) == '"') tagValue = tagValue.substring(0, tagValue.length() - 1); if (tagName.equals("FEN")) { pos = TextIO.readFEN(tagValue); } } game.processString("new"); game.pos = pos; { StringBuilder out = new StringBuilder(); sc.useDelimiter(""); int level = 0; while (sc.hasNext()) { String c = sc.next(); if (c.equals("(")) { level++; } else if (c.equals(")")) { level--; } else if (level == 0) { out.append(c); } } pgn = out.toString(); } sc.close(); sc = new Scanner(pgn); sc.useDelimiter("\\s+"); while (sc.hasNext()) { String strMove = sc.next(); strMove = strMove.replaceFirst("\\$?[0-9]*\\.*([^?!]*)[?!]*", "$1"); if (strMove.length() == 0) continue; Move m = TextIO.stringToMove(game.pos, strMove); if (m == null) break; game.processString(strMove); } sc.close(); } ChessController(GUIInterface gui); void setThreadStackSize(int size); final void newGame(boolean humanIsWhite, int ttLogSize, boolean verbose); final void startGame(); final void setPosHistory(List<String> posHistStr); final List<String> getPosHistory(); String getFEN(); String getPGN(); void setPGN(String pgn); void setFENOrPGN(String fenPgn); final void setHumanWhite(final boolean humanIsWhite); final boolean humansTurn(); final boolean computerThinking(); final void takeBackMove(); final void redoMove(); final void humanMove(Move m); final void reportPromotePiece(int choice); final void setMoveList(); synchronized void stopComputerThinking(); synchronized void setTimeLimit();  }
@Test public final void testSetPGN() throws ChessParseError { ChessController ctrl = new ChessController(null); ctrl.newGame(true, 8, false); ctrl.setPGN("[FEN \"k/8/8/8/8/8/KP/8 w\"]\n"); assertEquals(TextIO.getSquare("a2"), ctrl.game.pos.getKingSq(true)); assertEquals(TextIO.getSquare("a8"), ctrl.game.pos.getKingSq(false)); ctrl.setPGN("1.e4 e5 2. Nf3!!! $6 (Nc3 (a3)) Nc6?? Bb5!!? a6?! * Ba4"); assertEquals(Piece.BPAWN, ctrl.game.pos.getPiece(TextIO.getSquare("a6"))); assertEquals(Piece.WBISHOP, ctrl.game.pos.getPiece(TextIO.getSquare("b5"))); assertEquals(Piece.EMPTY, ctrl.game.pos.getPiece(TextIO.getSquare("a4"))); ctrl.setPGN("[FEN \"r1bq1rk1/pp3ppp/2n1pn2/6B1/1bBP4/2N2N2/PPQ2PPP/R3K2R w KQ - 1 10\"]\n"); assertEquals(10, ctrl.game.pos.fullMoveCounter); }