Ejemplo n.º 1
0
        /// <summary>
        /// ヘッダー部分をまとめてパースします。
        /// </summary>
        /// <remarks>
        /// 開始局面の設定も行います。
        /// </remarks>
        private void Parse()
        {
            var parser = new CsaBoardParser();
            while (ParseLine(ReadNextLine(), parser))
            {
            }

            if (parser.IsBoardParsing)
            {
                throw new FileFormatException(
                    "局面の読み取りを完了できませんでした。");
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// ヘッダー部分をまとめてパースします。
        /// </summary>
        /// <remarks>
        /// 開始局面の設定も行います。
        /// </remarks>
        private void Parse()
        {
            var parser = new CsaBoardParser();

            while (ParseLine(ReadNextLine(), parser))
            {
            }

            if (parser.IsBoardParsing)
            {
                throw new FileFormatException(
                          "局面の読み取りを完了できませんでした。");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// CSA形式の文字列から、局面を読み取ります。
        /// </summary>
        public static Board Parse(string csa)
        {
            if (string.IsNullOrEmpty(csa))
            {
                throw new ArgumentNullException("csa");
            }

            var parser = new CsaBoardParser();

            csa.Split(
                new char[] { '\n', '\r' },
                StringSplitOptions.RemoveEmptyEntries)
            .Where(_ => !CsaUtil.IsCommentLine(_))
            .ForEach(_ => parser.TryParse(_));

            return(parser.Board);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// CSA形式の文字列から、局面を読み取ります。
        /// </summary>
        public static Board Parse(string csa)
        {
            if (string.IsNullOrEmpty(csa))
            {
                throw new ArgumentNullException("csa");
            }

            var parser = new CsaBoardParser();

            csa.Split(
                    new char[] { '\n', '\r' },
                    StringSplitOptions.RemoveEmptyEntries)
                .Where(_ => !CsaUtil.IsCommentLine(_))
                .ForEach(_ => parser.TryParse(_));

            return parser.Board;
        }
Ejemplo n.º 5
0
        /// <summary>
        /// このReaderで与えられたファイルを処理できるか調べます。
        /// </summary>
        public bool CanHandle(TextReader reader)
        {
            try
            {
                this.reader           = reader;
                this.currentLines     = null;
                this.currentLineIndex = 0;
                this.lineNumber       = 0;

                this.header     = new KifuHeader();
                this.startBoard = null;
                this.board      = null;
                this.rootNode   = new MoveNode();
                this.lastNode   = this.rootNode;

                // ファイルを3行読めれば、CSA形式であると判断します。
                var parser = new CsaBoardParser();
                for (var i = 0; i < 3; ++i)
                {
                    var line = ReadNextLine();

                    if (line == null)
                    {
                        return(true);
                    }

                    if (!ParseLine(line, parser))
                    {
                        return(false);
                    }
                }

                return(true);
            }
            catch (Exception)
            {
                return(false);
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// ヘッダー行をパースします。
        /// </summary>
        private bool ParseLine(string line, CsaBoardParser parser)
        {
            if (line == null)
            {
                // ファイルの終了を意味します。
                return false;
            }

            if (CsaUtil.IsCommentLine(line))
            {
                return true;
            }

            // 局面の読み取りを試みます。
            if (parser.TryParse(line))
            {
                if (parser.HasBoard && !parser.IsBoardParsing)
                {
                    this.startBoard = parser.Board.Clone();
                    this.board = this.startBoard.Clone();
                }

                return true;
            }

            switch (line[0])
            {
                case 'V':
                    return true;

                case 'N':
                    ParseName(line);
                    return true;

                case '$':
                    ParseHeader(line);
                    return true;

                case '+': case '-':
                    var move = ParseMove(line);
                    var node = new MoveNode
                    {
                        Move = move,
                    };

                    this.lastNode.AddNext(node);
                    this.lastNode = node;
                    return true;

                case 'T':
                    var seconds = ParseTime(line);
                    if (this.lastNode != null)
                    {
                        this.lastNode.DurationSeconds = seconds;
                    }
                    return true;

                case '%':
                    return true;
            }

            return false;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// このReaderで与えられたファイルを処理できるか調べます。
        /// </summary>
        public bool CanHandle(TextReader reader)
        {
            try
            {
                this.reader = reader;
                this.currentLines = null;
                this.currentLineIndex = 0;
                this.lineNumber = 0;

                this.header = new KifuHeader();
                this.startBoard = null;
                this.board = null;
                this.rootNode = new MoveNode();
                this.lastNode = this.rootNode;

                // ファイルを3行読めれば、CSA形式であると判断します。
                var parser = new CsaBoardParser();
                for (var i = 0; i < 3; ++i)
                {
                    var line = ReadNextLine();

                    if (line == null)
                    {
                        return true;
                    }

                    if (!ParseLine(line, parser))
                    {
                        return false;
                    }
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }
Ejemplo n.º 8
0
        /// <summary>
        /// ヘッダー行をパースします。
        /// </summary>
        private bool ParseLine(string line, CsaBoardParser parser)
        {
            if (line == null)
            {
                // ファイルの終了を意味します。
                return false;
            }

            if (CsaUtil.IsCommentLine(line))
            {
                return true;
            }

            // 局面の読み取りを試みます。
            if (parser.TryParse(line))
            {
                if (parser.HasBoard && !parser.IsBoardParsing)
                {
                    Board = parser.Board.Clone();
                }

                return true;
            }

            switch (line[0])
            {
                case 'V':
                    return true;

                case 'N':
                    ParseName(line);
                    return true;

                case '$':
                    ParseHeader(line);
                    return true;

                case 'T':
                    return true;

                case '+': case '-':
                    ParseMove(line);
                    return true;

                case '%':
                    return true;
            }

            return false;
        }
Ejemplo n.º 9
0
        /// <summary>
        /// ヘッダー行をパースします。
        /// </summary>
        private bool ParseLine(string line, CsaBoardParser parser)
        {
            if (line == null)
            {
                // ファイルの終了を意味します。
                return(false);
            }

            if (CsaUtil.IsCommentLine(line))
            {
                return(true);
            }

            // 局面の読み取りを試みます。
            if (parser.TryParse(line))
            {
                if (parser.HasBoard && !parser.IsBoardParsing)
                {
                    this.startBoard = parser.Board.Clone();
                    this.board      = this.startBoard.Clone();
                }

                return(true);
            }

            switch (line[0])
            {
            case 'V':
                return(true);

            case 'N':
                ParseName(line);
                return(true);

            case '$':
                ParseHeader(line);
                return(true);

            case '+':
            case '-':
            case '%':
                var move = ParseMove(line);
                var node = new MoveNode
                {
                    Move = move,
                };

                this.lastNode.AddNextNode(node);
                this.lastNode = node;
                return(true);

            case 'T':
                var seconds = ParseTime(line);
                if (this.lastNode != null)
                {
                    this.lastNode.DurationSeconds = seconds;
                }
                return(true);
            }

            return(false);
        }