예제 #1
0
        /// <summary>
        /// Writes en passant data.
        /// </summary>
        /// <param name="writer">The file writer.</param>
        /// <param name="enPassant">The en passant data.</param>
        private void WriteEnPassant(StreamWriter writer, FriendlyEnPassant enPassant)
        {
            writer.WriteLine(PersistenceConstants.EnPassantSection);

            WritePosition(writer, enPassant.WhiteEnPassant);
            WritePosition(writer, enPassant.BlackEnPassant);
        }
예제 #2
0
        /// <summary>
        /// Loads <see cref="FriendlyBoard"/> object from the specified file.
        /// </summary>
        /// <param name="path">The path to file.</param>
        /// <exception cref="InvalidSectionValueException">Thrown when loaded section name has invalid value.</exception>
        /// <returns><see cref="FriendlyBoard"/> object loaded from the file.</returns>
        public FriendlyBoard Read(string path)
        {
            FriendlyPiecesList pieces    = null;
            FriendlyCastling   castling  = null;
            FriendlyEnPassant  enPassant = null;

            using (var reader = new StreamReader(path))
            {
                while (!reader.EndOfStream)
                {
                    var line = reader.ReadLine();
                    if (line == null)
                    {
                        throw new InvalidSectionValueException();
                    }

                    var lineAfterTrim = line.Trim();
                    if (lineAfterTrim.Length == 0)
                    {
                        continue;
                    }

                    switch (lineAfterTrim)
                    {
                    case PersistenceConstants.BoardSection:
                    {
                        pieces = ReadBoard(reader);
                        break;
                    }

                    case PersistenceConstants.CastlingSection:
                    {
                        castling = ReadCastling(reader);
                        break;
                    }

                    case PersistenceConstants.EnPassantSection:
                    {
                        enPassant = ReadEnPassant(reader);
                        break;
                    }

                    default:
                    {
                        throw new InvalidSectionValueException();
                    }
                    }
                }
            }

            return(new FriendlyBoard(pieces, castling, enPassant));
        }