private static IMove ParseMove(string move)
        {
            if (move.Length < 4 || move.Length > 5)
            {
                throw new ArgumentException("The move {0} cannot be parsed as a chess move");
            }
            ChessFile startFile = (ChessFile)Enum.Parse(typeof(ChessFile), move[0].ToString());
            ChessRow  startRow  = (ChessRow)Enum.Parse(typeof(ChessRow), move[1].ToString());

            ChessFile endFile = (ChessFile)Enum.Parse(typeof(ChessFile), move[2].ToString());
            ChessRow  endRow  = (ChessRow)Enum.Parse(typeof(ChessRow), move[3].ToString());

            ChessPiece promotionPiece;

            if (move.Length == 5)
            {
                promotionPiece = (ChessPiece)Enum.Parse(typeof(ChessPiece), move[5].ToString());
            }
            else
            {
                promotionPiece = ChessPiece.noPromotion;
            }

            Square startSquare = new Square(startFile, startRow);
            Square endSquare   = new Square(endFile, endRow);

            return(new Move(startSquare, endSquare, promotionPiece));
        }
        public ChessBoardBuilder At(ChessFile file, int rank, char piece)
        {
            CheckValidPieces(piece.ToString());

            CheckValidRank(rank);

            _board[(int)file - 1, rank - 1] = piece;

            return(this);
        }
        public ChessBoardBuilder File(ChessFile chessFile, string pieces)
        {
            CheckValidPieces(pieces);

            var rank = 0;

            foreach (var piece in pieces)
            {
                if (ValidPieces.Contains(piece.ToString().ToUpper()))
                {
                    _board[(int)chessFile - 1, rank++] = piece;
                }
            }

            return(this);
        }
Exemple #4
0
        public BoardPosition(string position)
        {
            if (position == null)
            {
                throw new ArgumentNullException(nameof(position));
            }

            if (position.Length != 2)
            {
                throw new ArgumentException($"Length of '{nameof(position)}' is not 2.");
            }

            position = position.ToUpperInvariant();

            char file = position[0];
            char rank = position[1];

            switch (file)
            {
            case 'A':
                File = ChessFile.A;
                break;

            case 'B':
                File = ChessFile.B;
                break;

            case 'C':
                File = ChessFile.C;
                break;

            case 'D':
                File = ChessFile.D;
                break;

            case 'E':
                File = ChessFile.E;
                break;

            case 'F':
                File = ChessFile.F;
                break;

            case 'G':
                File = ChessFile.G;
                break;

            case 'H':
                File = ChessFile.H;
                break;

            default:
                throw new ArgumentException($"First char of '{nameof(position)}' not in range A-F.");
            }

            bool isNumber = int.TryParse(rank.ToString(), out int parsedRank);

            if (!(isNumber && parsedRank > 0 && parsedRank < 9))
            {
                throw new ArgumentException($"Second char of '{nameof(position)}' not in range 1-8.");
            }

            Rank = parsedRank;
        }
Exemple #5
0
 public BoardPosition(ChessFile file, int rank)
 {
     File = file;
     Rank = rank;
 }
Exemple #6
0
 protected virtual void SetPieceAt(ChessFile file, int rank, ChessPiece piece)
 {
     Board[8 - rank][(int)file] = piece;
 }
Exemple #7
0
 public ChessPiece GetPieceAt(ChessFile file, int rank)
 {
     return(Board[8 - rank][(int)file]);
 }
Exemple #8
0
 /// <summary>
 /// Returns an instance of this class initialized with the coordinates of the square.
 /// </summary>
 /// <param name="file"></param>
 /// <param name="rank"></param>
 public ChessSquare(ChessFile file, ChessRank rank)
 {
     this.File = file;
     this.Rank = rank;
 }
Exemple #9
0
 public Square(ChessFile startFile, ChessRow startRow)
 {
     _startFile = startFile;
     _startRow  = startRow;
 }