示例#1
0
        // override setposition to handle enpassant
        public override void SetPosition(int Position)
        {
            // call the base
            base.SetPosition(Position);

            // check to see if this pawn can enpassant-ed
            if (Math.Abs(m_nPosition - m_nPrevPosition) == 16)
            {
                int Row    = -1;
                int Col    = -1;
                int Square = -1;

                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                // check left
                Col--;
                Etc.GetSquareFromRowCol(Row, Col, out Square);

                if (Square >= 0 && m_Type == PType.WhitePawn && GameData.g_CurrentGameState[Square] != null && GameData.g_CurrentGameState[Square].GetPieceType() == PType.BlackPawn)
                {
                    GameData.g_CurrentGameState[Square].AddEnPassantMove(m_nPosition - 8);
                }
                else if (Square >= 0 && m_Type == PType.BlackPawn && GameData.g_CurrentGameState[Square] != null && GameData.g_CurrentGameState[Square].GetPieceType() == PType.WhitePawn)
                {
                    GameData.g_CurrentGameState[Square].AddEnPassantMove(m_nPosition + 8);
                }

                // check right
                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
                Col++;
                Etc.GetSquareFromRowCol(Row, Col, out Square);

                if (Square >= 0 && m_Type == PType.WhitePawn && GameData.g_CurrentGameState[Square] != null && GameData.g_CurrentGameState[Square].GetPieceType() == PType.BlackPawn)
                {
                    GameData.g_CurrentGameState[Square].AddEnPassantMove(m_nPosition - 8);
                }
                else if (Square >= 0 && m_Type == PType.BlackPawn && GameData.g_CurrentGameState[Square] != null && GameData.g_CurrentGameState[Square].GetPieceType() == PType.WhitePawn)
                {
                    GameData.g_CurrentGameState[Square].AddEnPassantMove(m_nPosition + 8);
                }
            }
        }
示例#2
0
        //////////////////////////////////////////////////////////////////////////
        // Helpers
        //////////////////////////////////////////////////////////////////////////
        #region Helpers

        // updates the squares this piece is attacking
        protected override void UpdateAttackingSquares()
        {
            // initialize
            List <int> AttackedSquares = new List <int>();

            // clear our list
            m_lAttackingSquares.Clear();

            // we can use our current row and column number to make sure we only move inside the board
            // and can't cheat wraparound
            int Row           = 0;
            int Col           = 0;
            int CurrentSquare = -1;

            // gets row and column number from current square
            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

            // update when white
            if (m_Color == PColor.White)
            {
                #region NORMAL_TAKE
                // gets row and column number from current square
                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                // look to the NW to see if we can capture any pieces
                Row++;
                Col--;

                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                if (CurrentSquare >= 0)
                {
                    AttackedSquares.Add(CurrentSquare);
                }

                // gets row and column number from current square
                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                // look to the NW to see if we can capture any pieces
                Row++;
                Col++;

                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                if (CurrentSquare >= 0)
                {
                    AttackedSquares.Add(CurrentSquare);
                }
                #endregion

                #region ENPASSANT
                if (m_lEnPassantMoves.Count > 0)
                {
                    for (int i = 0; i < m_lEnPassantMoves.Count; i++)
                    {
                        AttackedSquares.Add(m_lEnPassantMoves[i] - 8);
                    }
                }
                #endregion
            }

            // update when black
            if (m_Color == PColor.Black)
            {
                #region NORMAL_TAKE
                // gets row and column number from current square
                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                // look to the SW to see if we can capture any pieces
                Row--;
                Col--;

                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                if (CurrentSquare >= 0)
                {
                    AttackedSquares.Add(CurrentSquare);
                }

                // gets row and column number from current square
                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                // look to the SE to see if we can capture any pieces
                Row--;
                Col++;

                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                if (CurrentSquare >= 0)
                {
                    AttackedSquares.Add(CurrentSquare);
                }
                #endregion

                #region ENPASSANT
                if (m_lEnPassantMoves.Count > 0)
                {
                    for (int i = 0; i < m_lEnPassantMoves.Count; i++)
                    {
                        AttackedSquares.Add(m_lEnPassantMoves[i] + 8);
                    }
                }
                #endregion
            }

            // add these square to the list
            m_lAttackingSquares.AddRange(AttackedSquares);
        }
示例#3
0
        // updates the squares this piece could potentially move to
        protected override void UpdateValidMoves()
        {
            // initialize
            List <int> ValidMoves = new List <int>();

            // clear our list
            m_lValidMoves.Clear();

            // we can use our current row and column number to make sure we only move inside the board
            // and can't cheat wraparound
            int Row           = 0;
            int Col           = 0;
            int CurrentSquare = -1;

            // gets row and column number from current square
            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

            // check for white
            if (m_Color == PColor.White)
            {
                #region STRAIGHT_AHEAD
                // move one square ahead
                Row++;
                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                // add square if not occupied
                if (CurrentSquare >= 0 && GameData.g_CurrentGameState[CurrentSquare] == null)
                {
                    m_lValidMoves.Add(CurrentSquare);

                    // move one square ahead again
                    Row++;
                    Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                    // get row col from current position
                    Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                    // add square if not occupied
                    if (Row == 1 && CurrentSquare >= 0 && GameData.g_CurrentGameState[CurrentSquare] == null)
                    {
                        m_lValidMoves.Add(CurrentSquare);
                    }
                }
                #endregion

                #region NORMAL_TAKE
                // gets row and column number from current square
                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                // look to the NW to see if we can capture any pieces
                Row++;
                Col--;

                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                if (CurrentSquare >= 0 &&
                    GameData.g_CurrentGameState[CurrentSquare] != null &&
                    GameData.g_CurrentGameState[CurrentSquare].GetColor() == PColor.Black)
                {
                    m_lValidMoves.Add(CurrentSquare);
                }

                // gets row and column number from current square
                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                // look to the NW to see if we can capture any pieces
                Row++;
                Col++;

                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                if (CurrentSquare >= 0 &&
                    GameData.g_CurrentGameState[CurrentSquare] != null &&
                    GameData.g_CurrentGameState[CurrentSquare].GetColor() == PColor.Black)
                {
                    m_lValidMoves.Add(CurrentSquare);
                }
                #endregion
            }

            // check for black
            if (m_Color == PColor.Black)
            {
                #region STRAIGHT_AHEAD
                // move one square ahead
                Row--;
                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                // add square if not occupied
                if (CurrentSquare >= 0 && GameData.g_CurrentGameState[CurrentSquare] == null)
                {
                    m_lValidMoves.Add(CurrentSquare);

                    // move one square ahead again
                    Row--;
                    Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                    // get row col from current position
                    Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                    // add square if not occupied
                    if (Row == 6 && CurrentSquare >= 0 && GameData.g_CurrentGameState[CurrentSquare] == null)
                    {
                        m_lValidMoves.Add(CurrentSquare);
                    }
                }
                #endregion

                #region NORMAL_TAKE
                // gets row and column number from current square
                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                // look to the NW to see if we can capture any pieces
                Row--;
                Col--;

                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                if (CurrentSquare >= 0 &&
                    GameData.g_CurrentGameState[CurrentSquare] != null &&
                    GameData.g_CurrentGameState[CurrentSquare].GetColor() == PColor.White)
                {
                    m_lValidMoves.Add(CurrentSquare);
                }

                // gets row and column number from current square
                Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);

                // look to the NW to see if we can capture any pieces
                Row--;
                Col++;

                Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare);

                if (CurrentSquare >= 0 &&
                    GameData.g_CurrentGameState[CurrentSquare] != null &&
                    GameData.g_CurrentGameState[CurrentSquare].GetColor() == PColor.White)
                {
                    m_lValidMoves.Add(CurrentSquare);
                }
                #endregion
            }

            // if there are moves, add them
            m_lValidMoves.AddRange(ValidMoves);

            // check to see if enpassant is still valid
            if (GameData.g_LastMove.FromSquare != GetPosition() && GameData.g_LastMove.ColorMoved == GetColor())
            {
                m_lEnPassantMoves.Clear();
            }

            // add enpassant moves
            m_lValidMoves.AddRange(m_lEnPassantMoves);
        }
示例#4
0
        // draws the pieces
        private void Chessboard_DrawPieces(PaintEventArgs e)
        {
            // make sure we have our images
            if (!m_bImageFilesLoaded)
            {
                return;
            }

            // locals
            int        square = 0;
            RectangleF RectImage;
            Image      currImage = null;

            // iterate through board
            for (int r = 0; r < 8; ++r)
            {
                for (int c = 0; c < 8; ++c)
                {
                    // create new rectangle
                    RectImage = new RectangleF(c * m_SquareSize, (7 - r) * m_SquareSize, m_SquareSize, m_SquareSize);

                    // calculate current square we are on
                    Etc.GetSquareFromRowCol(r, c, out square);

                    // get piece for this square
                    if (GameData.g_CurrentGameState[square] != null)
                    {
                        PType currVal = GameData.g_CurrentGameState[square].GetPieceType();

                        // if there is a piece on this square
                        if (currVal != PType.None)
                        {
                            // get appropriate image for this piece
                            currImage = GetImageForValue(currVal);

                            // draw image
                            if (currImage != null)
                            {
                                // draw images not currently dragged by player
                                if (m_bMouseDown && m_SelectedSquare == square)
                                {
                                    // do not draw this image, we want to draw it later so it overdraws the whole board
                                }
                                else
                                {
                                    // draw image and tag
                                    e.Graphics.DrawImage(currImage, RectImage);
                                }
                            }
                        }
                    }
                }
            }

            // add offset to current image for mouse dragging
            if (m_bMouseDown && m_SelectedSquare >= 0)
            {
                // get row and coloumn for current square
                int r = 0;
                int c = 0;
                Etc.GetRowColFromSquare(m_SelectedSquare, out r, out c);

                // get image on current square
                if (GameData.g_CurrentGameState[m_SelectedSquare] != null)
                {
                    PType currVal = GameData.g_CurrentGameState[m_SelectedSquare].GetPieceType();
                    currImage = GetImageForValue(currVal);

                    // calculate drawing rectangle
                    RectImage = new RectangleF((c * m_SquareSize) + m_PieceOffsetX, ((7 - r) * m_SquareSize) + m_PieceOffsetY, m_SquareSize, m_SquareSize);

                    // draw image
                    if (currImage != null)
                    {
                        e.Graphics.DrawImage(currImage, RectImage);
                    }
                }
            }
        }
示例#5
0
        // returns all diagonal moves a piece can make
        protected override List <int> CalculateVerticalMoves(int MaxRange)
        {
            // we can use our current row and column number to make sure we only move inside the board
            // and can't cheat wraparound
            int        Row            = 0;
            int        Col            = 0;
            int        CurrentSquare  = -1;
            int        CurrentRange   = 0;
            List <int> PotentialMoves = new List <int>();

            // if Maxrange = 0, then Maxrange is infinite
            if (MaxRange == 0)
            {
                MaxRange = int.MaxValue;
            }

            // iterate through all possible move and add them to a prevalidated moves list
            // moving W
            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            do
            {
                Row--;

                if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
                {
                    // calculate range from current position
                    CurrentRange = Math.Abs((CurrentSquare - m_nPosition) - 7);

                    // add this square
                    PotentialMoves.Add(CurrentSquare);

                    // do not pursue this line further when another piece is in the way
                    if (GameData.g_CurrentGameState[CurrentSquare] != null || CurrentRange >= MaxRange)
                    {
                        break;
                    }
                }
            } while (Col >= 0 && Col <= 7 && Row >= 0 && Row <= 7);

            // moving E
            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            do
            {
                Row++;

                if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
                {
                    // calculate range from current position
                    CurrentRange = Math.Abs((CurrentSquare - m_nPosition) - 7);

                    // add this square
                    PotentialMoves.Add(CurrentSquare);

                    // do not pursue this line further when another piece is in the way
                    if (GameData.g_CurrentGameState[CurrentSquare] != null || CurrentRange >= MaxRange)
                    {
                        break;
                    }
                }
            } while (Col >= 0 && Col <= 7 && Row >= 0 && Row <= 7);

            // return our list
            return(PotentialMoves);
        }
示例#6
0
        // returns all potential moves regardless of other pieces
        private List <int> CalculateKnightMoves()
        {
            List <int> PotentialMoves = new List <int>();

            int Row           = 0;
            int Col           = 0;
            int CurrentSquare = -1;

            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            Row += 2;
            Col += 1;
            if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
            {
                PotentialMoves.Add(CurrentSquare);
            }

            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            Row += 2;
            Col -= 1;
            if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
            {
                PotentialMoves.Add(CurrentSquare);
            }

            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            Row += 1;
            Col += 2;
            if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
            {
                PotentialMoves.Add(CurrentSquare);
            }

            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            Row += 1;
            Col -= 2;
            if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
            {
                PotentialMoves.Add(CurrentSquare);
            }

            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            Row -= 2;
            Col += 1;
            if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
            {
                PotentialMoves.Add(CurrentSquare);
            }

            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            Row -= 2;
            Col -= 1;
            if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
            {
                PotentialMoves.Add(CurrentSquare);
            }

            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            Row -= 1;
            Col += 2;
            if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
            {
                PotentialMoves.Add(CurrentSquare);
            }

            Etc.GetRowColFromSquare(m_nPosition, out Row, out Col);
            Row -= 1;
            Col -= 2;
            if (Etc.GetSquareFromRowCol(Row, Col, out CurrentSquare))
            {
                PotentialMoves.Add(CurrentSquare);
            }

            return(PotentialMoves);
        }