public ChessGame Parse(IEnumerable<String> pgn, IChessMoveNotation moveNotation) { var lines = pgn.GetEnumerator(); var metainfo = ParseTags(lines); var movetext = ParseMovetext(lines); var game = new ChessGame(metainfo); foreach (ChessMove cm in ParseMoves(movetext, moveNotation, game.Players)) game.AddMove(cm); return game; }
public IEnumerable<ChessMove> ParseMoves(String movetext, IChessMoveNotation moveNotation, List<Player> players = null) { if (players == null) players = new List<Player> { new Player(1), new Player(2) }; foreach (Match m in RxFullMove.Matches(movetext)) { ChessMove move; move = TryGetMove(moveNotation, m.Groups["Move1"], players[0]); if (move != null) yield return move; move = TryGetMove(moveNotation, m.Groups["Move2"], players[1]); if (move != null) yield return move; } }
private static ChessMove TryGetMove(IChessMoveNotation moveNotation, Group g, Player p) { if (g.Captures.Count > 0 && g.Value != "") { ChessMove move = moveNotation.Parse(g.Value); move.Player = p; return move; } return null; }