Пример #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);
        }
Пример #2
0
        /// <summary>
        /// get the next position of this position with the move move
        /// return list of next positions if exist, null if:
        /// 1. no piece of mTurn in move._source.
        /// 2. there is piece of same color in move._dest
        ///
        /// This function not return positions that are check to the side that moved, cause it's not legal
        ///
        /// In most cases there is only one possible next move,
        /// the unusual case is when a pawn arrive to the last rank and it can be any piece
        /// </summary>
        public List <Position> GetNextPositionsByMove(Move move)
        {
            //create new Position, same as this (clone)
            Piece pieceMoveOldPos = this.getPieceBySquare(move._source);

            if (pieceMoveOldPos == null || !pieceMoveOldPos._color.Equals(mTurn))//there is no piece in the color that own the turn in this square
            {
                return(null);
            }
            Piece pieceCapturedOldPos = this.getPieceBySquare(move._dest);

            if (pieceCapturedOldPos != null && pieceCapturedOldPos._color.Equals(mTurn))    //there is piece of the same color in the dest square
            {
                return(null);
            }
            Position retPos0 = (Position)this.Clone();

            Piece pieceMoving = retPos0.getPieceBySquare(move._source);

            pieceMoving._lastMove  = new Move(move);
            retPos0.mLastMovePiece = pieceMoving;
            retPos0.mDepth++;
            retPos0.mPrevPos = this;

            if (pieceCapturedOldPos != null)
            {
                retPos0.removePieceFromBoard(retPos0.getPieceBySquare(move._dest));
            }

            List <Position> retAllPos = new List <Position>(1);

            //if the piece is a pawn that rich to last rank, return all options
            if (pieceMoving is Pawn && (move._dest._rank == 8 || move._dest._rank == 1))
            {
                List <Piece> ll;
                if (mTurn == Players.White)
                {
                    ll = retPos0.mWhitePieces;
                }
                else
                {
                    ll = retPos0.mBlackPieces;
                }
                ll.Remove(pieceMoving); //remove the pawn from the postion

                /****** Queen *********/
                Queen q = new Queen(move._dest, mTurn);
                q._lastMove = new Move(move);
                ll.Add(q);
                retPos0.mLastMovePiece = q;
                if (retPos0.isCheck() == null)//the new position is not check
                {
                    retPos0.mTurn = getOpsColor(mTurn);
                    retAllPos.Add((Position)retPos0.Clone());
                    retPos0.mTurn = getOpsColor(mTurn);
                }

                ll.Remove(q);

                /****** Rook *********/
                //turn back the color to the original, for checking the next pos
                //retPos0.mTurn = getOpsColor(retPos0.mTurn);
                Rook r = new Rook(move._dest, mTurn);
                r._lastMove = new Move(move);
                ll.Add(r);
                retPos0.mLastMovePiece = r;
                if (retPos0.isCheck() == null)//the new position is not check
                {
                    retPos0.mTurn = getOpsColor(mTurn);
                    retAllPos.Add((Position)retPos0.Clone());
                    retPos0.mTurn = getOpsColor(mTurn);
                }
                ll.Remove(r);

                /****** Bishop *********/
                //turn back the color to the original, for checking the next pos
                //retPos0.mTurn = getOpsColor(retPos0.mTurn);
                Bishop b = new Bishop(move._dest, mTurn);
                b._lastMove = new Move(move);
                ll.Add(b);
                retPos0.mLastMovePiece = b;
                if (retPos0.isCheck() == null)//the new position is not check
                {
                    retPos0.mTurn = getOpsColor(mTurn);
                    retAllPos.Add((Position)retPos0.Clone());
                    retPos0.mTurn = getOpsColor(mTurn);
                }
                ll.Remove(b);

                /****** Knight *********/
                //turn back the color to the original, for checking the next pos
                //retPos0.mTurn = getOpsColor(retPos0.mTurn);
                Knight k = new Knight(move._dest, mTurn);
                k._lastMove = new Move(move);
                ll.Add(k);
                retPos0.mLastMovePiece = k;
                if (retPos0.isCheck() == null)//the new position is not check
                {
                    retPos0.mTurn = getOpsColor(mTurn);
                    retAllPos.Add((Position)retPos0.Clone());
                    retPos0.mTurn = getOpsColor(mTurn);
                }
                ll.Remove(k);
            }
            else
            {
                pieceMoving._square = move._dest;
                if (retPos0.isCheck() == null)//the new position is not check
                {
                    retPos0.mTurn = getOpsColor(mTurn);
                    retAllPos.Add(retPos0);
                }
            }

            return(retAllPos);
        }
Пример #3
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);
        }