Пример #1
0
        public BoardSpace(BoardSpace fromSpace)
        {
            position = fromSpace.position;
            piece    = null;

            if (fromSpace.piece != null)
            {
                if (fromSpace.piece.GetType() == typeof(Pawn))
                {
                    piece = new Pawn(fromSpace.piece, this);
                }
                else if (fromSpace.piece.GetType() == typeof(Knight))
                {
                    piece = new Knight(fromSpace.piece, this);
                }
                else if (fromSpace.piece.GetType() == typeof(Bishop))
                {
                    piece = new Bishop(fromSpace.piece, this);
                }
                else if (fromSpace.piece.GetType() == typeof(Rook))
                {
                    piece = new Rook(fromSpace.piece, this);
                }
                else if (fromSpace.piece.GetType() == typeof(Queen))
                {
                    piece = new Queen(fromSpace.piece, this);
                }
                else if (fromSpace.piece.GetType() == typeof(King))
                {
                    piece = new King(fromSpace.piece, this);
                }
            }
        }
Пример #2
0
 public Move(Chessman piece, BoardSpace fromSpace, BoardSpace toSpace, Board context, int score = 0)
 {
     contextBoard   = context;
     this.toSpace   = toSpace;
     this.fromSpace = fromSpace;
     this.score     = score; // AI Weight
 }
Пример #3
0
 public Move(Move other, Board newBoard) // copy Using New Reference Board
 {
     contextBoard   = newBoard;
     this.toSpace   = new BoardSpace(other.toSpace);
     this.fromSpace = new BoardSpace(other.fromSpace);
     this.score     = other.score; // AI Weight
     piece          = other.toSpace.piece;
 }
Пример #4
0
 public Move(Move other) // Copy using using Same Reference Board
 {
     contextBoard   = other.contextBoard;
     this.toSpace   = other.toSpace;
     this.fromSpace = other.fromSpace;
     this.score     = other.score; // AI Weight
     piece          = other.piece;
 }
Пример #5
0
 public BoardSpace(Char column, int row, Chessman piece)
 {
     if (column >= 'A' && column <= 'H' && row >= 1 && row <= 8)
     {
         // Valid Board Space
         position = new Tuple <char, int>(column, row);
     }
     else
     {
         throw new InvalidDataException($"{column}{row} is an invalid space designation.");
     }
     this.piece = piece;
 }
Пример #6
0
 public Move(String move, Board context, int score = 0, bool postMove = false)
 {
     if (Regex.IsMatch(move, "[A-Za-z][0-9][A-Za-z][0-9].*"))
     {
         contextBoard = context;
         toSpace      = contextBoard.getSpace(move.Substring(0, 1).ToString().ToUpper()[0], int.Parse(move.Substring(1, 1)));
         fromSpace    = contextBoard.getSpace(move.Substring(2, 1).ToString().ToUpper()[0], int.Parse(move.Substring(3, 1)));
         this.score   = score; // AI Weight
         piece        = (postMove) ? toSpace.piece : fromSpace.piece;
     }
     else
     {
         throw new Exception("Supplied Move is not UCI Formatted.");
     }
 }
Пример #7
0
 public Queen(Chessman fromPiece, BoardSpace initPos)
 {
     if (fromPiece.GetType() == typeof(Queen))
     {
         Queen from = (Queen)fromPiece;
         score      = from.score;
         position   = initPos;
         this.color = from.color;
         whiteImage = from.whiteImage;
         blackImage = from.blackImage;
     }
     else
     {
         throw new Exception($"Cant Create Copy of Piece becasue it is the wrong type");
     }
 }
Пример #8
0
 public Bishop(Chessman fromPiece)
 {
     if (fromPiece.GetType() == typeof(Bishop))
     {
         Bishop from = (Bishop)fromPiece;
         score      = from.score;
         position   = null;
         this.color = from.color;
         whiteImage = from.whiteImage;
         blackImage = from.blackImage;
     }
     else
     {
         throw new Exception($"Cant Create Copy of Piece becasue it is the wrong type");
     }
 }
Пример #9
0
 public King(Chessman fromPiece)
 {
     if (fromPiece.GetType() == typeof(King))
     {
         King from = (King)fromPiece;
         score      = from.score;
         position   = null;
         this.color = from.color;
         whiteImage = from.whiteImage;
         blackImage = from.blackImage;
         canCastle  = from.canCastle;
     }
     else
     {
         throw new Exception($"Cant Create Copy of Piece becasue it is the wrong type");
     }
 }
Пример #10
0
 public Pawn(Chessman fromPiece)
 {
     if (fromPiece.GetType() == typeof(Pawn))
     {
         Pawn from = (Pawn)fromPiece;
         score        = from.score;
         position     = null;
         this.color   = from.color;
         whiteImage   = from.whiteImage;
         blackImage   = from.blackImage;
         canMoveTwice = from.canMoveTwice;
     }
     else
     {
         throw new Exception($"Cant Create Copy of Piece becasue it is the wrong type");
     }
 }
Пример #11
0
        public Chessman generatePieceFromFen(String fen)
        {
            Chessman rv = null;

            List <String> fenBoard = fen.Split(new char[] { '/' }).ToList();

            fenBoard.Reverse();


            // replace Empty Squares with _
            if (Regex.IsMatch(fenBoard[position.Item2 - 1], "[0-9]"))
            {
                foreach (Match m in Regex.Matches(fenBoard[position.Item2 - 1], "[0-9]"))
                {
                    int emptySpaces = int.Parse(m.Value);

                    fenBoard[position.Item2 - 1] = Regex.Replace(fenBoard[position.Item2 - 1], $"{m.Value}", string.Concat(Enumerable.Repeat("_", emptySpaces)));
                }
            }

            try
            {
                for (int i = 0; i <= 7; i++)
                {
                    int  emptySpaces = 0;
                    Char nextFenRep  = fenBoard[position.Item2 - 1][i];

                    // Is a piece
                    if (position.Item1 == ('A' + i))
                    {
                        if (nextFenRep == '_')
                        {
                            piece = null;
                        }
                        else
                        {
                            // Setup White BackRow
                            switch (Char.ToUpper(nextFenRep))
                            {
                            case 'K':
                                piece = new King(this, ((Char.IsLower(nextFenRep)) ? ChessmanColor.black : ChessmanColor.white));
                                break;

                            case 'R':
                                piece = new Rook(this, ((Char.IsLower(nextFenRep)) ? ChessmanColor.black : ChessmanColor.white));
                                break;

                            case 'P':
                                piece = new Pawn(this, ((Char.IsLower(nextFenRep)) ? ChessmanColor.black : ChessmanColor.white));
                                break;

                            case 'B':
                                piece = new Bishop(this, ((Char.IsLower(nextFenRep)) ? ChessmanColor.black : ChessmanColor.white));
                                break;

                            case 'N':
                                piece = new Knight(this, ((Char.IsLower(nextFenRep)) ? ChessmanColor.black : ChessmanColor.white));
                                break;

                            case 'Q':
                                piece = new Queen(this, ((Char.IsLower(nextFenRep)) ? ChessmanColor.black : ChessmanColor.white));
                                break;
                            }
                        }
                        break;
                    }
                    else
                    {
                        continue;
                    }
                }
            }
            catch
            {
            }

            return(rv);
        }
Пример #12
0
        private Chessman movePieceOnBoard(BoardSpace fromSpace, BoardSpace toSpace, String extraArgs = "")
        {
            // This is kust updating the data model. Validation Should occur prior to this
            // This function Assumes that the moves are valid.

            Chessman fromPiece   = fromSpace.piece;
            Chessman killedPiece = null;

            if (toSpace.piece != null)
            {
                // We killed piece remove it.
                killedPiece = toSpace.piece;
            }
            fromPiece.position = toSpace;
            toSpace.piece      = fromPiece;
            fromSpace.piece    = null;

            if (toSpace.piece.GetType() == typeof(King))
            {
                King kp = (King)toSpace.piece;
                if (kp.canCastle)
                {
                    kp.canCastle = false;
                }
                if (kp.color == ChessmanColor.white)
                {
                    whiteKingPos = toSpace;
                }
                else
                {
                    blackKingPos = toSpace;
                }
            }
            else if (toSpace.piece.GetType() == typeof(Rook))
            {
                Rook rp = (Rook)toSpace.piece;
                if (rp.canCastle)
                {
                    rp.canCastle = false;
                }
            }
            else if (toSpace.piece.GetType() == typeof(Pawn))
            {
                ((Pawn)toSpace.piece).canMoveTwice = false;  // Pawns Can only Move Twice on the first move
                if ((toSpace.piece.color == ChessmanColor.black && toSpace.position.Item2 == 1) || (toSpace.piece.color == ChessmanColor.white && toSpace.position.Item2 == 8))
                {
                    Char promotionType = 'q';
                    if (extraArgs.Length > 0)
                    {
                        promotionType = extraArgs.ToLower()[0];
                    }

                    switch (promotionType)
                    {
                    case 'q':
                        // Promote to Queen
                        Queen q = new Queen(toSpace, toSpace.piece.color);
                        toSpace.piece = q;
                        break;

                    case 'b':
                        // Promote to Queen
                        Bishop b = new Bishop(toSpace, toSpace.piece.color);
                        toSpace.piece = b;
                        break;

                    case 'n':
                        // Promote to Queen
                        Knight kn = new Knight(toSpace, toSpace.piece.color);
                        toSpace.piece = kn;
                        break;

                    case 'r':
                        // Promote to Queen
                        Rook r = new Rook(toSpace, toSpace.piece.color);
                        toSpace.piece = r;
                        break;
                    }
                }
            }

            updateAttacksOnKing(toSpace.piece.color);

            return(killedPiece);
        }
Пример #13
0
        public bool updateBoardForMove(String move)
        {
            bool   moveValid = false;
            String fromPos   = move.Substring(0, 2).ToUpper();
            String toPos     = move.Substring(2, 2).ToUpper();
            String extraArgs = "";

            if (move.Length > 4)
            {
                extraArgs = move.Substring(4, move.Length - 4);
            }

            try
            {
                BoardSpace fromSpace = getSpace(fromPos[0], int.Parse(new String(new char[] { fromPos[1] })));
                BoardSpace toSpace   = getSpace(toPos[0], int.Parse(new String(new char[] { toPos[1] })));

                if (fromSpace.piece != null)
                {
                    Chessman fromPiece = fromSpace.piece;

                    if (true) // Do check if move is valid for piece type
                    {
                        if (fromPiece.GetType() == typeof(King) && ((King)fromPiece).canCastle && ((fromPiece.color == ChessmanColor.white && fromSpace.position.Item1 == 'E' && fromSpace.position.Item2 == 1 && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 1) || (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 1)) || (fromPiece.color == ChessmanColor.black && fromSpace.position.Item1 == 'E' && fromSpace.position.Item2 == 8 && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 8) || (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 8))))
                        {
                            King kingPiece = ((King)fromPiece);

                            //Attempting a castle VAlidate it
                            BoardSpace rookSpace = null;

                            if (kingPiece.color == ChessmanColor.white && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 1))
                            {
                                // White King Side Castle
                                rookSpace = getSpace('H', 1);

                                if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now
                                {
                                    if (getSpace('F', 1).piece == null && getSpace('G', 1).piece == null)           // Are all the spaces between the Rook and Knight Clear
                                    {
                                        if (true)                                                                   // Are any Spaces the King Must Move Under Attack. (E1(Check),F1, G1) (Skipping for now until we can check this)
                                        {
                                            movePieceOnBoard(fromSpace, toSpace, extraArgs);                        // Move King
                                            movePieceOnBoard(rookSpace, getSpace('F', 1), extraArgs);               // Move Rook
                                            moveValid = true;
                                        }
                                    }
                                }
                            }
                            else if (kingPiece.color == ChessmanColor.white && (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 1))
                            {
                                // White Queen Side Castle
                                rookSpace = getSpace('A', 1);

                                if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle)                         // Was the King Side Rook moved before now
                                {
                                    if (getSpace('B', 1).piece == null && getSpace('C', 1).piece == null && getSpace('D', 1).piece == null) // Are all the spaces between the Rook and Knight Clear
                                    {
                                        if (true)                                                                                           // Are any Spaces the King Must Move Under Attack. (E1(Check), D1, C1) (Skipping for now until we can check this)
                                        {
                                            movePieceOnBoard(fromSpace, toSpace, extraArgs);                                                // Move King
                                            movePieceOnBoard(rookSpace, getSpace('D', 1), extraArgs);                                       // Move Rook
                                            moveValid = true;
                                        }
                                    }
                                }
                            }
                            else if (kingPiece.color == ChessmanColor.black && (toSpace.position.Item1 == 'G' && toSpace.position.Item2 == 8))
                            {
                                // White King Side Castle
                                rookSpace = getSpace('H', 8);

                                if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle) // Was the King Side Rook moved before now
                                {
                                    if (getSpace('F', 8).piece == null && getSpace('G', 8).piece == null)           // Are all the spaces between the Rook and Knight Clear
                                    {
                                        if (true)                                                                   // Are any Spaces the King Must Move Under Attack. (E8(Check),F8, G8) (Skipping for now until we can check this)
                                        {
                                            movePieceOnBoard(fromSpace, toSpace, extraArgs);                        // Move King
                                            movePieceOnBoard(rookSpace, getSpace('F', 8), extraArgs);               // Move Rook
                                            moveValid = true;
                                        }
                                    }
                                }
                            }
                            else if (kingPiece.color == ChessmanColor.black && (toSpace.position.Item1 == 'C' && toSpace.position.Item2 == 8))
                            {
                                // White Queen Side Castle
                                rookSpace = getSpace('A', 8);

                                if (rookSpace.piece.GetType() == typeof(Rook) && ((Rook)rookSpace.piece).canCastle)                         // Was the King Side Rook moved before now
                                {
                                    if (getSpace('B', 8).piece == null && getSpace('C', 8).piece == null && getSpace('D', 8).piece == null) // Are all the spaces between the Rook and Knight Clear
                                    {
                                        if (true)                                                                                           // Are any Spaces the King Must Move Under Attack. (E1(Check), D1, C1) (Skipping for now until we can check this)
                                        {
                                            movePieceOnBoard(fromSpace, toSpace, extraArgs);                                                // Move King
                                            movePieceOnBoard(rookSpace, getSpace('D', 8), extraArgs);                                       // Move Rook
                                            moveValid = true;
                                        }
                                    }
                                }
                            }

                            if (moveValid)
                            {
                                // Grab All King and Rooks for the color and disable castling
                                foreach (BoardSpace s in boardState)
                                {
                                    if (s.piece != null && s.piece.color == fromPiece.color)
                                    {
                                        if (s.piece.GetType() == typeof(Rook))
                                        {
                                            ((Rook)s.piece).canCastle = false;
                                        }
                                        else if (s.piece.GetType() == typeof(King))
                                        {
                                            ((King)s.piece).canCastle = false;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            movePieceOnBoard(fromSpace, toSpace, extraArgs);
                            moveValid = true;
                        }
                    }
                }
                else
                {
                    // There should always be a piece in the from Space
                }
            }
            catch
            {
            }

            return(moveValid);
        }