コード例 #1
0
        private void RedrawSquare(ChessSquare targetedSquare)
        {
            var g = this.CreateGraphics();
            var blackSquareBrush = new Pen(DarkSquaresColor).Brush;
            var whiteSquareBrush = new Pen(LightSquaresColor).Brush;
            var x             = (int)targetedSquare.File;
            var y             = (int)targetedSquare.Rank;
            var isWhiteSquare = (((int)targetedSquare.File + (int)targetedSquare.Rank) % 2) != 0;
            var square        = (BoardDirection == BoardDirection.BlackOnTop ?
                                 new RectangleF(DigitAreaWidth + x * SquareWidth, (7 - y) * SquareHeight, SquareWidth, SquareHeight) :
                                 new RectangleF(DigitAreaWidth + (7 - x) * SquareWidth, (y) * SquareHeight, SquareWidth, SquareHeight));

            //  Draw square
            g.FillRectangle(isWhiteSquare ? whiteSquareBrush : blackSquareBrush, square);
            var currentPiece = GetPieceAt(targetedSquare);

            if (currentPiece != null)
            {
                if (DragDropOperation.Origin == null || DragDropOperation.Origin != targetedSquare)
                {
                    g.DrawImage(GetPieceImage(currentPiece), square);
                }
            }

            //  Draw borders
            var borders = new Rectangle(0, 0, Width - 1, Height - 1);

            g.DrawRectangle(new Pen(Color.Black), borders);

            g.Flush();
        }
コード例 #2
0
 public IllegalMoveException(ChessSquare from, ChessSquare to)
 {
     Message = $"Unable to move from {from.AlgebraicNotation} to {to.AlgebraicNotation}.";
 }
コード例 #3
0
 internal ChessMove(ChessSquare from, ChessSquare to)
 {
     this.From = from;
     this.To   = to;
 }
コード例 #4
0
        protected override void OnPaint(PaintEventArgs pe)
        {
            var controlImage = new Bitmap(this.Width, this.Height);

            using (Graphics g = Graphics.FromImage(controlImage))
            {
                var coordinateAreaBrush = new Pen(CoordinateAreaBackColor).Brush;
                var darkSquaresBrush    = new Pen(DarkSquaresColor).Brush;
                var lightSquaresBrush   = new Pen(LightSquaresColor).Brush;

                //  Draw a filled rectangle for digits
                g.FillRectangle(coordinateAreaBrush, 0, 0, DigitAreaWidth, this.Height);

                //  Draw a filled rectangle for letters
                g.FillRectangle(coordinateAreaBrush, 0, this.Height - LetterAreaHeight, this.Width, LetterAreaHeight);

                //  Draw letters
                var coordinateFont = new Font(FontFamily.GenericMonospace, 11 + (SquareWidth - MINIMUM_SQUARE_WIDTH) / 4);
                for (int i = 0; i < 8; i++)
                {
                    var letter = (BoardDirection == BoardDirection.BlackOnTop ?
                                  ((ChessFile)i).ToString() :
                                  ((ChessFile)7 - i).ToString());
                    var characterSize = g.MeasureString(letter, coordinateFont);
                    var x             = DigitAreaWidth + i * SquareWidth + (SquareWidth - characterSize.Width) / 2;
                    var y             = this.Height - LetterAreaHeight + (LetterAreaHeight - characterSize.Height) / 2;
                    g.DrawString(letter, coordinateFont, DEFAULT_COORDINATE_FORGROUND_BRUSH, x, y);
                }

                //  Draw digits
                coordinateFont = new Font(FontFamily.GenericMonospace, 11 + (SquareHeight - MINIMUM_SQUARE_HEGHT) / 4);
                for (int i = 0; i < 8; i++)
                {
                    var digit = (BoardDirection == BoardDirection.BlackOnTop ?
                                 (8 - i).ToString() :
                                 (i + 1).ToString());
                    var characterSize = g.MeasureString(digit, coordinateFont);
                    var x             = (DigitAreaWidth - characterSize.Width) / 2;
                    var y             = i * SquareHeight + (SquareHeight - characterSize.Height) / 2;
                    g.DrawString(digit, coordinateFont, DEFAULT_COORDINATE_FORGROUND_BRUSH, x, y);
                }

                //  Draw Turn indicator
                var turnIndicatorBorder = new Rectangle(0, Height - LetterAreaHeight, DigitAreaWidth, LetterAreaHeight);
                var turnIndicatorSquare = new RectangleF(0, Height - LetterAreaHeight, DigitAreaWidth, LetterAreaHeight);
                g.FillRectangle(ChessEngine.Turn == ChessColor.White ? lightSquaresBrush : darkSquaresBrush, turnIndicatorSquare);
                g.DrawRectangle(new Pen(Color.Black), turnIndicatorBorder);

                //  Draw cells
                bool isLightSquare = true;
                for (int x = 0; x < 8; x++)
                {
                    for (int y = 0; y < 8; y++)
                    {
                        var square = new RectangleF(DigitAreaWidth + x * SquareWidth, y * SquareHeight, SquareWidth, SquareHeight);
                        g.FillRectangle(isLightSquare ? lightSquaresBrush : darkSquaresBrush, square);
                        ChessSquare currentSquare = BoardDirection == BoardDirection.BlackOnTop ?
                                                    new ChessSquare((ChessFile)x, (ChessRank)7 - y) :
                                                    new ChessSquare((ChessFile)7 - x, (ChessRank)y);
                        if (currentSquare != DragDropOperation.Origin)
                        {
                            ChessPiece currentPiece = ChessEngine.GetPieceAt(currentSquare);
                            if (currentPiece != null)
                            {
                                g.DrawImage(GetPieceImage(currentPiece), square);
                            }
                        }

                        isLightSquare = !isLightSquare;
                    }
                    isLightSquare = !isLightSquare;
                }

                //  Draw visual hints
                DrawVisualHints(g);

                //  Draw borders
                var borders = new Rectangle(0, 0, Width - 1, Height - 1);
                g.DrawRectangle(new Pen(Color.Black), borders);
                g.Flush();
            }

            pe.Graphics.DrawImage(this.Enabled ? controlImage : GrayOutImage(controlImage), new Point(0, 0));
        }
コード例 #5
0
 /// <summary>
 /// Defines the piece at the given position.
 /// </summary>
 /// <param name="square">Coordinates of the square where to set the piece.</param>
 /// <param name="piece">Piece to set.</param>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="piece"/> or <paramref name="square"/> are null.</exception>
 public void SetPieceAt(ChessPiece piece, ChessSquare square)
 {
     ChessEngine.PutPiece(piece, square);
 }
コード例 #6
0
 /// <summary>
 /// Removes the piece on the given square.
 /// </summary>
 /// <param name="square">Coordinates of the square where to remove the piece.</param>
 /// <returns>An instance of the removed piece or null if there was no piece on the square.</returns>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="square"/> is null.</exception>
 public ChessPiece RemovePieceAt(ChessSquare square)
 {
     return(ChessEngine.RemovePieceAt(square));
 }
コード例 #7
0
 /// <summary>
 /// Gets the piece at the given position.
 /// </summary>
 /// <param name="square">Coordinates of the square where to look at.</param>
 /// <returns></returns>
 public ChessPiece GetPieceAt(ChessSquare square)
 {
     return(ChessEngine.GetPieceAt(square));
 }
コード例 #8
0
 /// <summary>
 /// Returns all legal moves for the current position for the given square.
 /// </summary>
 /// <param name="square">Square for which to compute legal moves.</param>
 /// <returns></returns>
 /// <exception cref="ArgumentNullException">Thrown when the <paramref name="square"/> is null.</exception>
 /// <exception cref="ArgumentException">Thrown when the <paramref name="square"/> is empty.</exception>
 public List <ChessMove> GetLegalMoves(ChessSquare square)
 {
     return(ChessEngine.GetLegalMoves(square));
 }
コード例 #9
0
 internal DragOperation(ChessPiece selectedPiece, ChessSquare source)
 {
     DraggedPiece = selectedPiece;
     Origin       = source;
 }
コード例 #10
0
 /// <summary>
 /// Checks whether a move is valid and give the type of move (normal, capture, promotion).
 /// In case the move is a promotion, set the <see cref="ChessMove.PromotedTo"/> property accordingly before to pass the return object to the <see cref="MovePiece(ChessMove)"/> method.
 /// </summary>
 /// <param name="from">Square the piece move from.</param>
 /// <param name="to">Square the piece move to.</param>
 /// <returns></returns>
 /// <exception cref="ArgumentNullException">Thrown when <paramref name="from"/> or <paramref name="to"/> are null.</exception>
 public ChessMove CheckMoveValidity(ChessSquare from, ChessSquare to)
 {
     return(ChessEngine.GetMoveValidity(from, to));
 }