// 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); } } }
////////////////////////////////////////////////////////////////////////// // 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); }
// 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); }
// 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); } } } }
// draw a checkered board private void Chessboard_DrawCheckerdBoard(PaintEventArgs e) { int square = 0; for (int r = 0; r < 8; ++r) { for (int c = 0; c < 8; ++c) { RectangleF rect = new RectangleF(c * m_SquareSize, (7 - r) * m_SquareSize, m_SquareSize, m_SquareSize); Etc.GetSquareFromRowCol(r, c, out square); // color the squares if ((c + r) % 2 == 0) { e.Graphics.FillRectangle(Brushes.LightGray, rect); } else { e.Graphics.FillRectangle(Brushes.White, rect); } // color square the mouse is over if (GetMouseSquarePosition() == square) { e.Graphics.FillRectangle(Brushes.LightBlue, rect); } // color square the mouse is over if (m_SelectedSquare >= 0 && m_SelectedSquare == square) { e.Graphics.FillRectangle(Brushes.MediumSeaGreen, rect); } // show attacked pieces if (m_bShowAttackedPieces) { if (GameData.g_SquaresAttackedByBlack.Contains(square) && GameData.g_CurrentGameState[square] != null && GameData.g_CurrentGameState[square].GetColor() == PColor.White) { rect = new RectangleF(c * m_SquareSize + 6, (7 - r) * m_SquareSize + 6, 6, 6); e.Graphics.FillRectangle(Brushes.IndianRed, rect); } else if (GameData.g_SquaresAttackedByWhite.Contains(square) && GameData.g_CurrentGameState[square] != null && GameData.g_CurrentGameState[square].GetColor() == PColor.Black) { rect = new RectangleF(c * m_SquareSize + 6, (7 - r) * m_SquareSize + 6, 6, 6); e.Graphics.FillRectangle(Brushes.IndianRed, rect); } } // show valid moves for current piece if (m_bShowValidMoves && m_bMouseDown && m_SelectedSquare >= 0 && GameData.g_CurrentGameState[m_SelectedSquare] != null && GameData.g_CurrentGameState[m_SelectedSquare].GetValidMoves().Contains(square)) { rect = new RectangleF(c * m_SquareSize + 3, (7 - r) * m_SquareSize + 3, 6, 6); e.Graphics.FillRectangle(Brushes.MediumSeaGreen, rect); } } } }
private bool DeconstructMoveString(String move, out Int32 From, out Int32 To) { if (move.Length == 4) { Int32 FromCol = -1; Int32 FromRow = -1; Int32 ToCol = -1; Int32 ToRow = -1; String FromColStr = move.Substring(0, 1); String FromRowStr = move.Substring(1, 1); String ToColStr = move.Substring(2, 1); String ToRowStr = move.Substring(3, 1); switch (FromColStr) { case "a": FromCol = 0; break; case "b": FromCol = 1; break; case "c": FromCol = 2; break; case "d": FromCol = 3; break; case "e": FromCol = 4; break; case "f": FromCol = 5; break; case "g": FromCol = 6; break; case "h": FromCol = 7; break; } FromRow = Int32.Parse(FromRowStr) - 1; switch (ToColStr) { case "a": ToCol = 0; break; case "b": ToCol = 1; break; case "c": ToCol = 2; break; case "d": ToCol = 3; break; case "e": ToCol = 4; break; case "f": ToCol = 5; break; case "g": ToCol = 6; break; case "h": ToCol = 7; break; } ToRow = Int32.Parse(ToRowStr) - 1; Etc.GetSquareFromRowCol(FromRow, FromCol, out From); Etc.GetSquareFromRowCol(ToRow, ToCol, out To); return(true); } else { From = -1; To = -1; return(false); } }
// 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); }
// 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); }