public PgnMove(string pgnString) { Match m = r.Match(pgnString); if (m.Groups["Move"].Value == "") { ColorToPlay = PieceColor.Black; } else { ColorToPlay = PieceColor.White; } if (m.Groups["Castling"].Value == "") { PieceToPlay = StringToPiece(m.Groups["Piece"].Value, ColorToPlay); FromFile = StringToFile(m.Groups["File"].Value); FromRank = StringToRank(m.Groups["Rank"].Value); DestinationSquare = StringToSquare(m.Groups["Destination"].Value); PromotionPiece = StringToPromotion(m.Groups["Promotion"].Value); } else if (m.Groups["Castling"].Value == "O-O") { if (ColorToPlay == PieceColor.White) { DestinationSquare = Square.G1; PieceToPlay = Piece.WhiteKing; FromFile = 4; FromRank = 0; } if (ColorToPlay == PieceColor.Black) { DestinationSquare = Square.G8; PieceToPlay = Piece.BlackKing; FromFile = 4; FromRank = 7; } } else if (m.Groups["Castling"].Value == "O-O-O") { if (ColorToPlay == PieceColor.White) { DestinationSquare = Square.C1; PieceToPlay = Piece.WhiteKing; FromFile = 4; FromRank = 0; } if (ColorToPlay == PieceColor.Black) { DestinationSquare = Square.C8; PieceToPlay = Piece.BlackKing; FromFile = 4; FromRank = 7; } } }
/// <summary> /// Initializes a new instance of BoardChangedEventArgs. /// </summary> /// <param name="move">Move that has chenged the board. Can be null if the move has been changed in some other way.</param> internal BoardChangedEventArgs(Move move) { if (move == null) { m_from = Square.None; m_to = Square.None; } else { m_from = move.From; m_to = move.To; } }
public static Square[] GetDiagonals(Square CurrentPosition, PieceColor FriendlyColor) { GetColAndRowFromName(CurrentPosition.Name, out int column, out int row); var ValidSquares = new List <Square>(); bool FoundSW = false, FoundSE = false, FoundNW = false, FoundNE = false; for (int i = row - 1; i >= 0; --i) { if (column - Math.Abs(row - i) >= 0 && !FoundSW) { Square SW = GetSquareFromName(GetSquareNameFromColAndRow(column - Math.Abs(row - i), i)); if (SW.Piece == null) { ValidSquares.Add(SW); } else { FoundSW = true; if (SW.Piece.Color != FriendlyColor) { ValidSquares.Add(SW); } } } if (column + Math.Abs(row - i) < 8 && !FoundSE) { Square SE = GetSquareFromName(GetSquareNameFromColAndRow(column + Math.Abs(row - i), i)); if (SE.Piece == null) { ValidSquares.Add(SE); } else { FoundSE = true; if (SE.Piece.Color != FriendlyColor) { ValidSquares.Add(SE); } } } } for (int i = row + 1; i < 8; ++i) { if (column - Math.Abs(row - i) >= 0 && !FoundNW) { Square NW = GetSquareFromName(GetSquareNameFromColAndRow(column - Math.Abs(row - i), i)); if (NW.Piece == null) { ValidSquares.Add(NW); } else { FoundNW = true; if (NW.Piece.Color != FriendlyColor) { ValidSquares.Add(NW); } } } if (column + Math.Abs(row - i) < 8 && !FoundNE) { Square NE = GetSquareFromName(GetSquareNameFromColAndRow(column + Math.Abs(row - i), i)); if (NE.Piece == null) { ValidSquares.Add(NE); } else { FoundNE = true; if (NE.Piece.Color != FriendlyColor) { ValidSquares.Add(NE); } } } } return(ValidSquares.ToArray()); }
private void RaisePieceTaken(Square square, ChessPiece pieceTaken) { var handler = this.PieceTaken; if (handler != null) { var eventArgs = new PieceTakenEventArgs(square, pieceTaken); handler(this, eventArgs); } }
/// <summary> /// Creates a key that can be used to return an actual "Move" from the "MoveOrganizer" /// This method is also responsible for raising the Pawn promotion events. /// </summary> /// <param name="from">Where to move from.</param> /// <param name="to">Where to move to.</param> /// <returns>Key matching the requested move.</returns> private MoveIdentifier CreateMoveIdentifier(Square from, Square to) { MoveIdentifier result; result.From = from; result.To = to; result.PromotionPiece = Piece.None; if (Board.Rank(to) == 7 && m_currentGame.Board[from] == Piece.WhitePawn) { result.PromotionPiece = Piece.WhiteQueen; if (m_currentGame.Moves.Find(result) == null) { result.PromotionPiece = Piece.None; } else if (WhitePawnPromoted != null) { PromotionEventArgs promotionArgs = new PromotionEventArgs(PromotionPiece.Queen); WhitePawnPromoted(this, promotionArgs); switch (promotionArgs.PromotionPiece) { case PromotionPiece.Bishop: result.PromotionPiece = Piece.WhiteBishop; break; case PromotionPiece.Knight: result.PromotionPiece = Piece.WhiteKnight; break; case PromotionPiece.Rook: result.PromotionPiece = Piece.WhiteRook; break; } } } if (Board.Rank(to) == 0 && m_currentGame.Board[from] == Piece.BlackPawn) { result.PromotionPiece = Piece.BlackQueen; if (m_currentGame.Moves.Find(result) == null) { result.PromotionPiece = Piece.None; } else if (BlackPawnPromoted != null) { PromotionEventArgs promotionArgs = new PromotionEventArgs(PromotionPiece.Queen); BlackPawnPromoted(this, promotionArgs); switch (promotionArgs.PromotionPiece) { case PromotionPiece.Bishop: result.PromotionPiece = Piece.BlackBishop; break; case PromotionPiece.Knight: result.PromotionPiece = Piece.BlackKnight; break; case PromotionPiece.Rook: result.PromotionPiece = Piece.BlackRook; break; } } } return result; }
/// <summary> /// Returns the piece on a specefic square. /// </summary> /// <param name="square"></param> /// <returns></returns> public Piece PieceAt(Square square) { return m_boardCopy[square]; }
/// <summary> /// Performs a single move. /// </summary> /// <param name="from">Where to move from.</param> /// <param name="to">Where to move to.</param> /// <returns>True if the move was possible. False otherwise.</returns> /// <exception cref="Chess.Model.ChessModelException"></exception>" public bool PerformMove(Square from, Square to) { if (!m_engineManager.Thinking) { Move move = m_currentGame.Moves.Find(CreateMoveIdentifier(from, to)); if (move != null) { m_currentGame.MakeMove(move); HandleChangedGameState(move, m_engineConfiguration.EngineAutoPlay, true); return true; } } return false; }
public void DrawPiece(Square square, Piece piece) { if (square != Square.None && piece != Piece.None) { Image image = PieceImage(piece); switch (m_viewFromBlack) { case true: m_picturePainter.DrawImage(image, ((7 - ChessFacade.File(square)) * SquareSize) + EdgeSize, (ChessFacade.Rank(square) * SquareSize) + EdgeSize, SquareSize, SquareSize); break; case false: m_picturePainter.DrawImage(image, (ChessFacade.File(square) * SquareSize) + EdgeSize, ((7 - ChessFacade.Rank(square)) * SquareSize) + EdgeSize, SquareSize, SquareSize); break; } } }
/// <summary> /// Returns the rank (or row) the square is located on. /// </summary> /// <param name="square">Valid arguments are all squares except Square.NoSquare.</param> /// <returns>The rank [0..7], viewed from white player, where square is located on the board.</returns> public static int Rank(Square square) { return Board.Rank(square); }
/// <summary> /// Returns the file (or column) the square is located on. /// </summary> /// <param name="square">Valid arguments are all squares except Square.NoSquare.</param> /// <returns>The rank [0..7], left to right viewed from white player, where square is located on the board.</returns> public static int File(Square square) { return Board.File(square); }
public PieceTakenEventArgs(Square square, ChessPiece pieceTaken) { this.Square = square; this.PieceTaken = pieceTaken; }
/// <summary> /// Gets or sets the piece on a specific square. /// </summary> /// <param name="square">Location of ppiece to get or set.</param> /// <returns>Square at a specefic location.</returns> public Piece this[Square square] { get { return m_editBoard[square]; } set { m_editBoard.PlacePiece(square, value); if (BoardChanged != null) BoardChanged(this, new BoardChangedEventArgs(null)); } }
public void HighlightSquare(Square square) { if (square != Square.None) { switch (m_viewFromBlack) { case true: m_picturePainter.DrawRectangle(m_highlightPen, ((7 - ChessFacade.File(square)) * SquareSize) + EdgeSize, (ChessFacade.Rank(square) * SquareSize) + EdgeSize, SquareSize, SquareSize); break; case false: m_picturePainter.DrawRectangle(m_highlightPen, (ChessFacade.File(square) * SquareSize) + EdgeSize, ((7 - ChessFacade.Rank(square)) * SquareSize) + EdgeSize, SquareSize, SquareSize); break; } } }
public static Square[] GetStraights(Square CurrentPosition, PieceColor FriendlyColor) { var validSquares = new List <Square>(); var i = CurrentPosition.Row - 1; while (true) { if (i < 0) { break; } var square = GetSquareFromName(GetSquareNameFromColAndRow(CurrentPosition.Column, i)); if (!AddOnFriendlyCondition(square, validSquares, FriendlyColor)) { break; } --i; } i = CurrentPosition.Row + 1; while (true) { if (i > 7) { break; } var square = GetSquareFromName(GetSquareNameFromColAndRow(CurrentPosition.Column, i)); if (!AddOnFriendlyCondition(square, validSquares, FriendlyColor)) { break; } ++i; } i = CurrentPosition.Column - 1; while (true) { if (i < 0) { break; } var square = GetSquareFromName(GetSquareNameFromColAndRow(i, CurrentPosition.Row)); if (!AddOnFriendlyCondition(square, validSquares, FriendlyColor)) { break; } --i; } i = CurrentPosition.Column + 1; while (true) { if (i > 7) { break; } var square = GetSquareFromName(GetSquareNameFromColAndRow(i, CurrentPosition.Row)); if (!AddOnFriendlyCondition(square, validSquares, FriendlyColor)) { break; } ++i; } return(validSquares.ToArray()); }
/// <summary> /// Returns the valid squares a piece can move to. /// </summary> /// <param name="from">Location of the piece valid destination squares are returned for.</param> /// <returns>Array of valid destination squares.</returns> public Square[] GetValidSquaresForPiece(Square from) { List<Square> buffer = new List<Square>(); if (!m_engineManager.Thinking) foreach (Move move in m_currentGame.Moves) if (move.From == from) buffer.Add(move.To); return buffer.ToArray(); }
public static Square[] GetKnightSquares(Square CurrentPosition) { throw new NotImplementedException(); }
private void boardPicture_MouseDown(object sender, MouseEventArgs e) { m_movingFrom = m_graphicHandler.CoordinateToSquare(e.X, e.Y); m_graphicHandler.SwipeBoard(); for (Square square = Square.A1; square <= Square.H8; ++square) m_graphicHandler.DrawPiece(square, m_chessFacade.PieceAt(square)); Square[] squares = m_chessFacade.GetValidSquaresForPiece(m_movingFrom); foreach (Square square in squares) m_graphicHandler.HighlightSquare(square); m_graphicHandler.Refresh(); }