Exemple #1
0
        public void SetupBoard()
        {
            piece[0] = new Rook(false, "rbk");
            piece[1] = new Knight(false, "nbk");
            piece[2] = new Bishop(false, "bbk");
            piece[3] = new Queen(false, "qb");
            piece[4] = new King(false, "kb");
            piece[5] = new Bishop(false, "bbq");
            piece[6] = new Knight(false, "nbq");
            piece[7] = new Rook(false, "rbq");

            piece[8]  = new Pawn(false, "pba");
            piece[9]  = new Pawn(false, "pbb");
            piece[10] = new Pawn(false, "pbc");
            piece[11] = new Pawn(false, "pbd");
            piece[12] = new Pawn(false, "pbe");
            piece[13] = new Pawn(false, "pbf");
            piece[14] = new Pawn(false, "pbg");
            piece[15] = new Pawn(false, "pbh");

            piece[16] = new Rook(true, "rwk");
            piece[17] = new Knight(true, "nwk");
            piece[18] = new Bishop(true, "bwk");
            piece[19] = new Queen(true, "qw");
            piece[20] = new King(true, "kw");
            piece[21] = new Bishop(true, "bbw");
            piece[22] = new Knight(true, "nbw");
            piece[23] = new Rook(true, "rbw");

            piece[24] = new Pawn(true, "pba");
            piece[25] = new Pawn(true, "pbb");
            piece[26] = new Pawn(true, "pbc");
            piece[27] = new Pawn(true, "pbd");
            piece[28] = new Pawn(true, "pbe");
            piece[29] = new Pawn(true, "pbf");
            piece[30] = new Pawn(true, "pbg");
            piece[31] = new Pawn(true, "pbh");

            // set the first turn to white
            turn = true;

            // Adds the black home row to the board
            for (int i = 0; i < 8; i++)
            {
                piece[i].Location = new Point(i, 0);
            }

            // Adds the black pawns to the board
            for (int i = 8; i < 16; i++)
            {
                piece[i].Location = new Point(i - 8, 1);
            }

            // Adds the white home row to the board
            for (int i = 16; i < 24; i++)
            {
                piece[i].Location = new Point(i - 16, 7);
            }

            // Adds the White pawns to the board
            for (int i = 24; i < 32; i++)
            {
                piece[i].Location = new Point(i - 24, 6);
            }
        }
Exemple #2
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);
        }