Example #1
0
 public PGNException(string message, PGNToken token, ParserSource source, Exception innerException)
     : base(message, innerException)
 {
     SourceComponent = source;
     Index = token.Index;
     PgnToken = token;
 }
Example #2
0
 public PGNException(string message, PGNToken token, ParserSource source, Exception innerException)
     : base(message, innerException)
 {
     SourceComponent = source;
     Index           = token.Index;
     PgnToken        = token;
 }
Example #3
0
 public PGNException(string message, PGNToken token, ParserSource source = ParserSource.Parser)
     : base(message)
 {
     SourceComponent = source;
     Index = token.Index;
     PgnToken = token;
 }
Example #4
0
 public PGNException(string message, PGNToken token, ParserSource source = ParserSource.Parser)
     : base(message)
 {
     SourceComponent = source;
     Index           = token.Index;
     PgnToken        = token;
 }
Example #5
0
        private List <PGNToken> Tokenize(string data, ref int i)
        {
            var      tokens  = new List <PGNToken>();
            var      iBefore = i;
            PGNToken token   = new PGNToken();

            while (i < data.Length)
            {
                try
                {
                    iBefore = i;
                    token   = GetToken(data, ref i);
                }
                catch (Exception e)
                {
                    throw new PGNException("Invalid token", i, GetWord(data, i), ParserSource.Tokenizer, e);
                }

                if (i == iBefore || token.TokenType == PGNTokenType.None)
                {
                    throw new PGNException("No token was detected", i, GetWord(data, i), ParserSource.Tokenizer);
                }

                tokens.Add(token);
                if (token.TokenType == PGNTokenType.End)
                {
                    break;
                }
            }

            return(tokens);
        }
Example #6
0
        private PGNMove MakeMove(PGNToken token, Board position)
        {
            var move = token.GetMove(position);

            if (move.Capture && position.State[move.To] == 0 && position.EnPassantTile == 0)
            {
                throw new PGNException("Move is not a capture", token);
            }

            if (move.Color != position.PlayerTurn)
            {
                throw new PGNException("It is not this players turn to move", token);
            }

            if (move.Color != position.GetColor(move.From))
            {
                throw new PGNException("Moving piece is not of the expected color", token);
            }

            if (move.Piece != position.GetPiece(move.From))
            {
                throw new PGNException("Moving piece is not of the expected type", token);
            }

            var ok = position.Move(move.From, move.To, true);

            if (!ok)
            {
                throw new PGNException("Illegal move", token);
            }

            if (move.Promotion != Piece.None)
            {
                ok = position.Promote(move.To, move.Promotion);
                if (!ok)
                {
                    throw new PGNException("Illegal promotion", token);
                }
            }

            if (move.Check && !position.IsChecked(position.PlayerTurn))
            {
                throw new PGNException("Move did not cause a check", token);
            }

            if (move.Mate && !position.IsCheckMate())
            {
                throw new PGNException("Move did not cause a mate", token);
            }

            return(move);
        }
Example #7
0
        private List<PGNToken> Tokenize(string data, ref int i)
        {
            var tokens = new List<PGNToken>();
            var iBefore = i;
            PGNToken token = new PGNToken();

            while(i < data.Length)
            {
                try
                {
                    iBefore = i;
                    token = GetToken(data, ref i);
                }
                catch(Exception e)
                {
                    throw new PGNException("Invalid token", i, GetWord(data, i), ParserSource.Tokenizer, e);
                }

                if(i == iBefore || token.TokenType == PGNTokenType.None)
                {
                    throw new PGNException("No token was detected", i, GetWord(data, i), ParserSource.Tokenizer);
                }

                tokens.Add(token);
                if (token.TokenType == PGNTokenType.End)
                    break;
            }

            return tokens;
        }
Example #8
0
        private PGNMove MakeMove(PGNToken token, Board position)
        {
            var move = token.GetMove(position);

            if (move.Capture && position.State[move.To] == 0 && position.EnPassantTile == 0)
                throw new PGNException("Move is not a capture", token);

            if (move.Color != position.PlayerTurn)
                throw new PGNException("It is not this players turn to move", token);

            if (move.Color != position.GetColor(move.From))
                throw new PGNException("Moving piece is not of the expected color", token);

            if (move.Piece != position.GetPiece(move.From))
                throw new PGNException("Moving piece is not of the expected type", token);

            var ok = position.Move(move.From, move.To, true);

            if (!ok)
                throw new PGNException("Illegal move", token);

            if (move.Promotion != Piece.None)
            {
                ok = position.Promote(move.To, move.Promotion);
                if(!ok)
                    throw new PGNException("Illegal promotion", token);
            }

            if (move.Check && !position.IsChecked(position.PlayerTurn))
                throw new PGNException("Move did not cause a check", token);

            if (move.Mate && !position.IsCheckMate())
                throw new PGNException("Move did not cause a mate", token);

            return move;
        }