private GameVariation GetVariation(List <PGNToken> tokens, ref int i, Board position) { var positions = new List <Board>() { position }; var variation = new GameVariation(); var token = tokens[i]; try { for (; i < tokens.Count; i++) { if (tokens[i].TokenType == PGNTokenType.End || tokens[i].TokenType == PGNTokenType.VariationEnd) { break; } var newPos = ParseToken(variation, tokens, ref i, positions, position); if (newPos != position) { positions.Add(newPos); position = newPos; } } } catch (Exception e) { throw new PGNException("Error parsing token", token, ParserSource.Parser, e); } return(variation); }
private Board ParseToken( GameVariation variation, List <PGNToken> tokens, ref int i, List <Board> positions, Board position) { var token = tokens[i]; switch (token.TokenType) { case (PGNTokenType.Annotation): variation.Elements.Add(new GameAnnotation(token.Value)); break; case (PGNTokenType.Comment): variation.Elements.Add(new GameComment(token.Value)); break; case (PGNTokenType.Ellipsis): // Assert its blacks turn if (position.PlayerTurn != Color.Black) { throw new PGNException("Encountered unexpected token. It should be blacks turn to play", token); } break; case (PGNTokenType.Move): position = position.Copy(); var move = MakeMove(token, position); variation.Elements.Add(move); break; case (PGNTokenType.MoveNumber): if (position.MoveCount > token.GetMoveNumber()) { throw new PGNException("Unexpected move number", token); } break; case (PGNTokenType.Results): variation.Elements.Add(new GameResults(token.Value)); break; case (PGNTokenType.VariationStart): var startpos = GetStartPosition(tokens, ref i, positions); var newVariation = GetVariation(tokens, ref i, startpos); variation.Elements.Add(newVariation); break; } return(position); }
public static PGNGame DecodeGame(string encodedString) { var s = encodedString; int i = 0; Color player = Color.White; int move = 1; var moves = new List <PGNMove>(); PGNMove lastmove = null; while (i < s.Length - 1) { // check if the last move wa actually a promotion if (Decode[s[i]] == SpecialInstruction) { lastmove.Promotion = (Piece)Decode[s[i + 1]]; continue; } var from = Decode[s[i]]; var to = Decode[s[i + 1]]; lastmove = new PGNMove() { From = from, To = to, MoveNumber = move, Color = player /*, Piece = ...*/ }; if (player == Color.Black) { move++; } player = (player == Color.White) ? Color.Black : Color.White; moves.Add(lastmove); i += 2; } var variation = new GameVariation(moves.Select(x => (IPGNElement)x).ToList()); var game = new PGNGame(new Dictionary <string, string>(), variation); return(game); }
private Board ParseToken( GameVariation variation, List<PGNToken> tokens, ref int i, List<Board> positions, Board position) { var token = tokens[i]; switch (token.TokenType) { case (PGNTokenType.Annotation): variation.Elements.Add(new GameAnnotation(token.Value)); break; case (PGNTokenType.Comment): variation.Elements.Add(new GameComment(token.Value)); break; case (PGNTokenType.Ellipsis): // Assert its blacks turn if (position.PlayerTurn != Color.Black) throw new PGNException("Encountered unexpected token. It should be blacks turn to play", token); break; case (PGNTokenType.Move): position = position.Copy(); var move = MakeMove(token, position); variation.Elements.Add(move); break; case (PGNTokenType.MoveNumber): if (position.MoveCount > token.GetMoveNumber()) throw new PGNException("Unexpected move number", token); break; case (PGNTokenType.Results): variation.Elements.Add(new GameResults(token.Value)); break; case (PGNTokenType.VariationStart): var startpos = GetStartPosition(tokens, ref i, positions); var newVariation = GetVariation(tokens, ref i, startpos); variation.Elements.Add(newVariation); break; } return position; }
private GameVariation GetVariation(List<PGNToken> tokens, ref int i, Board position) { var positions = new List<Board>() { position }; var variation = new GameVariation(); var token = tokens[i]; try { for (; i < tokens.Count; i++) { if (tokens[i].TokenType == PGNTokenType.End || tokens[i].TokenType == PGNTokenType.VariationEnd) break; var newPos = ParseToken(variation, tokens, ref i, positions, position); if(newPos != position) { positions.Add(newPos); position = newPos; } } } catch(Exception e) { throw new PGNException("Error parsing token", token, ParserSource.Parser, e); } return variation; }
public PGNGame(Dictionary <string, string> tags, GameVariation game) { Tags = tags; Game = game; Results = game.Elements.LastOrDefault(x => x is GameResults) as GameResults; }
public PGNGame(Dictionary<string, string> tags, GameVariation game) { Tags = tags; Game = game; Results = game.Elements.LastOrDefault(x => x is GameResults) as GameResults; }
public static PGNGame DecodeGame(string encodedString) { var s = encodedString; int i = 0; Color player = Color.White; int move = 1; var moves = new List<PGNMove>(); PGNMove lastmove = null; while(i < s.Length - 1) { // check if the last move wa actually a promotion if (Decode[s[i]] == SpecialInstruction) { lastmove.Promotion = (Piece)Decode[s[i + 1]]; continue; } var from = Decode[s[i]]; var to = Decode[s[i + 1]]; lastmove = new PGNMove() { From = from, To = to, MoveNumber = move, Color = player/*, Piece = ...*/ }; if(player == Color.Black) move++; player = (player == Color.White) ? Color.Black : Color.White; moves.Add(lastmove); i += 2; } var variation = new GameVariation(moves.Select(x => (IPGNElement)x).ToList()); var game = new PGNGame(new Dictionary<string, string>(), variation); return game; }