Esempio n. 1
0
        public Position ReadPositionFromFile(string fileName)
        {
            List <Piece> whitePieces = new List <Piece>();
            List <Piece> blackPieces = new List <Piece>();
            StreamReader reader;

            try
            {
                reader = File.OpenText(fileName);
            }
            catch (Exception exc) { return(null); }

            string inputLine = null;

            inputLine = reader.ReadLine();
            Position.Players colTurn;
            if (inputLine.StartsWith("White"))
            {
                colTurn = Position.Players.White;
            }
            else if (inputLine.StartsWith("Black"))
            {
                colTurn = Position.Players.Black;
            }
            else
            {
                return(null);
            }
            while ((inputLine = reader.ReadLine()) != null)
            {
                if (inputLine.StartsWith("{"))
                {
                    continue;
                }
                string[]         s           = inputLine.Split(' ');
                string           pieceKind   = s[0];
                string           stringColor = s[1];
                Position.Players color;
                if (stringColor.Equals("White"))
                {
                    color = Position.Players.White;
                }
                else
                {
                    color = Position.Players.Black;
                }
                char   file   = Char.Parse(s[2]);
                int    rank   = Int32.Parse(s[3]);
                Piece  piece  = null;
                Square square = new Square(file, rank);
                switch (pieceKind)
                {
                case "King": piece = new King(square, color); break;

                case "Queen": piece = new Queen(square, color); break;

                case "Rook": piece = new Rook(square, color); break;

                case "Bishop": piece = new Bishop(square, color); break;

                case "Knight": piece = new Knight(square, color); break;

                case "Pawn": piece = new Pawn(square, color, null); break;
                }
                if (color.Equals(Position.Players.White))
                {
                    whitePieces.Add(piece);
                }
                else
                {
                    blackPieces.Add(piece);
                }
            }
            reader.Close();
            Position position = new Position(whitePieces, blackPieces, null, colTurn, 0, null);

            return(position);
        }
Esempio n. 2
0
        public Piece parseFenPiecePlacement(char piecePlacement, out int blankSquares)
        {
            Piece retPiece = null;

            blankSquares = 1;
            switch (piecePlacement)
            {
            case 'p':
                retPiece = new Pawn(null, Position.Players.Black, null);
                break;

            case 'n':
                retPiece = new Knight(null, Position.Players.Black, null);
                break;

            case 'b':
                retPiece = new Bishop(null, Position.Players.Black, null);
                break;

            case 'r':
                retPiece = new Rook(null, Position.Players.Black, null);
                break;

            case 'q':
                retPiece = new Queen(null, Position.Players.Black, null);
                break;

            case 'k':
                retPiece = new King(null, Position.Players.Black, null);
                break;

            case 'P':
                retPiece = new Pawn(null, Position.Players.White, null);
                break;

            case 'N':
                retPiece = new Knight(null, Position.Players.White, null);
                break;

            case 'B':
                retPiece = new Bishop(null, Position.Players.White, null);
                break;

            case 'R':
                retPiece = new Rook(null, Position.Players.White, null);
                break;

            case 'Q':
                retPiece = new Queen(null, Position.Players.White, null);
                break;

            case 'K':
                retPiece = new King(null, Position.Players.White, null);
                break;

            case '/':
                blankSquares = 9;
                break;

            case '1':
                blankSquares = 1;
                break;

            case '2':
                blankSquares = 2;
                break;

            case '3':
                blankSquares = 3;
                break;

            case '4':
                blankSquares = 4;
                break;

            case '5':
                blankSquares = 5;
                break;

            case '6':
                blankSquares = 6;
                break;

            case '7':
                blankSquares = 7;
                break;

            case '8':
                blankSquares = 8;
                break;
            }
            return(retPiece);
        }