Example #1
0
        /// <summary>
        /// Gets a board in the starting position.
        /// </summary>
        /// <returns></returns>
        public static Board GetStartingBoard()
        {
            var squares = new Piece[SquareNo];
            var status  = new BoardStatus();

            squares[A8] = new BlackRook();
            squares[B8] = new BlackKnight();
            squares[C8] = new BlackBishop();
            squares[D8] = new BlackQueen();
            squares[E8] = new BlackKing();
            squares[F8] = new BlackBishop();
            squares[G8] = new BlackKnight();
            squares[H8] = new BlackRook();

            for (var sqIndex = SideSquareNo; sqIndex < SideSquareNo * 2; sqIndex++)
            {
                squares[sqIndex] = new BlackPawn();
            }

            for (var sqIndex = SideSquareNo * (SideSquareNo - 2); sqIndex < SideSquareNo * (SideSquareNo - 1); sqIndex++)
            {
                squares[sqIndex] = new WhitePawn();
            }

            squares[A1] = new WhiteRook();
            squares[B1] = new WhiteKnight();
            squares[C1] = new WhiteBishop();
            squares[D1] = new WhiteQueen();
            squares[E1] = new WhiteKing();
            squares[F1] = new WhiteBishop();
            squares[G1] = new WhiteKnight();
            squares[H1] = new WhiteRook();

            status.WhiteTurn            = true;
            status.WhiteCouldCastleLong = status.WhiteCouldCastleShort = status.BlackCouldCastleLong = status.BlackCouldCastleShort = true;
            status.EnPassantTarget      = null;
            status.Ply   = 0;
            status.Moves = 1;

            return(new Board(squares, status));
        }
Example #2
0
        public bool TryMove(ChessPlayer player, ChessboardTile tile)
        {
            int row, column;

            ChessboardTileHelper.TileToIndexes(tile, out row, out column);

            //Normal move
            if (m_board[m_selectedRow, m_selectedColumn].CanMove(row, column))
            {
                //Pick up the piece
                ChessPiece movingPiece = m_board[m_selectedRow, m_selectedColumn];
                m_board[m_selectedRow, m_selectedColumn] = null;
                m_selectedRow    = -1;
                m_selectedColumn = -1;

                //Update the piece
                movingPiece.Selected  = false;
                movingPiece.FirstMove = false;

                //Check for winning/losing
                if (row == m_whiteKing.CurrentRow && column == m_whiteKing.CurrentColumn)
                {
                    m_winningPlayer = ChessPlayer.Black;
                }
                if (row == m_blackKing.CurrentRow && column == m_blackKing.CurrentColumn)
                {
                    m_winningPlayer = ChessPlayer.White;
                }

                //Check for rochade (white, kingside)
                if (movingPiece == m_whiteKing && row == 0 && column == 6)
                {
                    WhiteRook rookSpot = m_board[0, 7] as WhiteRook;
                    if (rookSpot != null && rookSpot.FirstMove)
                    {
                        //We can do the rochade
                        m_board[0, 7]      = movingPiece;
                        m_board[0, 6]      = rookSpot;
                        rookSpot.FirstMove = false;
                    }
                }

                //Check for rochade (black, queenside)
                if (movingPiece == m_blackKing && row == 7 && column == 1)
                {
                    BlackRook rookSpot = m_board[7, 0] as BlackRook;
                    if (rookSpot != null && rookSpot.FirstMove)
                    {
                        //We can do the rochade
                        m_board[7, 0]      = movingPiece;
                        m_board[7, 1]      = rookSpot;
                        rookSpot.FirstMove = false;
                    }
                }

                //Check for rochade (black, kingside)
                if (movingPiece == m_blackKing && row == 7 && column == 6)
                {
                    BlackRook rookSpot = m_board[7, 7] as BlackRook;
                    if (rookSpot != null && rookSpot.FirstMove)
                    {
                        //We can do the rochade
                        m_board[7, 7]      = movingPiece;
                        m_board[7, 6]      = rookSpot;
                        rookSpot.FirstMove = false;
                    }
                }

                //Put down the piece
                m_board[row, column] = movingPiece;

                //Clear selection

                return(true);
            }

            //Rochade (white, queenside)
            if (m_board[m_selectedRow, m_selectedColumn] is WhiteKing &&
                m_whiteKing.FirstMove &&
                row == 0 &&
                column == 1 &&
                m_board[0, 1] == null &&
                m_board[0, 2] == null &&
                m_board[0, 3] == null)
            {
                WhiteRook rookSpot = m_board[0, 0] as WhiteRook;
                if (rookSpot != null && rookSpot.FirstMove)
                {
                    //Pickup the king
                    m_board[m_selectedRow, m_selectedColumn] = null;

                    //Deselect him
                    m_selectedRow        = -1;
                    m_selectedColumn     = -1;
                    m_whiteKing.Selected = false;

                    //Move
                    m_board[0, 1] = m_whiteKing;
                    m_board[0, 2] = rookSpot;

                    //Mark first moves
                    m_whiteKing.FirstMove = false;
                    rookSpot.FirstMove    = false;

                    //Teel the move was a success
                    return(true);
                }
            }

            //Rochade (white, kingside)
            if (m_board[m_selectedRow, m_selectedColumn] is WhiteKing &&
                m_whiteKing.FirstMove &&
                row == 0 &&
                column == 6 &&
                m_board[0, 5] == null &&
                m_board[0, 6] == null)
            {
                WhiteRook rookSpot = m_board[0, 7] as WhiteRook;
                if (rookSpot != null && rookSpot.FirstMove)
                {
                    //Pickup the king
                    m_board[m_selectedRow, m_selectedColumn] = null;

                    //Deselect him
                    m_selectedRow        = -1;
                    m_selectedColumn     = -1;
                    m_whiteKing.Selected = false;

                    //Move
                    m_board[0, 6] = m_whiteKing;
                    m_board[0, 5] = rookSpot;

                    //Mark first moves
                    m_whiteKing.FirstMove = false;
                    rookSpot.FirstMove    = false;

                    //Teel the move was a success
                    return(true);
                }
            }

            //Rochade (black, queenside)
            if (m_board[m_selectedRow, m_selectedColumn] is BlackKing &&
                m_blackKing.FirstMove &&
                row == 7 &&
                column == 1 &&
                m_board[7, 1] == null &&
                m_board[7, 2] == null &&
                m_board[7, 3] == null)
            {
                BlackRook rookSpot = m_board[7, 0] as BlackRook;
                if (rookSpot != null && rookSpot.FirstMove)
                {
                    //Pickup the king
                    m_board[m_selectedRow, m_selectedColumn] = null;

                    //Deselect him
                    m_selectedRow        = -1;
                    m_selectedColumn     = -1;
                    m_blackKing.Selected = false;

                    //Move
                    m_board[7, 1] = m_blackKing;
                    m_board[7, 2] = rookSpot;

                    //Mark first moves
                    m_blackKing.FirstMove = false;
                    rookSpot.FirstMove    = false;

                    //Teel the move was a success
                    return(true);
                }
            }

            //Rochade (black, kingside)
            if (m_board[m_selectedRow, m_selectedColumn] is BlackKing &&
                m_blackKing.FirstMove &&
                row == 7 &&
                column == 6 &&
                m_board[7, 5] == null &&
                m_board[7, 6] == null)
            {
                BlackRook rookSpot = m_board[7, 7] as BlackRook;
                if (rookSpot != null && rookSpot.FirstMove)
                {
                    //Pickup the king
                    m_board[m_selectedRow, m_selectedColumn] = null;

                    //Deselect him
                    m_selectedRow        = -1;
                    m_selectedColumn     = -1;
                    m_blackKing.Selected = false;

                    //Move
                    m_board[7, 6] = m_blackKing;
                    m_board[7, 5] = rookSpot;

                    //Mark first moves
                    m_blackKing.FirstMove = false;
                    rookSpot.FirstMove    = false;

                    //Teel the move was a success
                    return(true);
                }
            }

            return(false);
        }