コード例 #1
0
        private void ChessBoard_Paint(object sender, PaintEventArgs e)
        {
            GraphicsBuffer graphicsBuffer = new GraphicsBuffer();

            graphicsBuffer.CreateGraphicsBuffer(e.Graphics, Width, Height);

            Graphics g = graphicsBuffer.Graphics;

            SolidBrush solidWhiteBrush = new SolidBrush(Color.White);
            SolidBrush solidBlackBrush = new SolidBrush(Color.Black);

            Pen penBlack = new Pen(Color.Black, 1);

            Pen penHightlight  = new Pen(Color.Black, 2);
            Pen penDestination = new Pen(Color.Yellow, 2);
            Pen penValidMove   = new Pen(Color.Black, 2);
            Pen penEnPassant   = new Pen(Color.DeepPink, 1);

            const int buffer = 10;

            if (Width < Height)
            {
                maxHeight = Width - 5 - buffer;
                boxHeight = maxHeight / 8;
            }
            else
            {
                maxHeight = Height - 5 - buffer;
                boxHeight = maxHeight / 8;
            }

            g.Clear(BackColor);

            try
            {
                int selectedX;
                int selectedY;

                //Draw Chess Board
                for (byte y = 0; y < 8; y++)
                {
                    for (byte x = 0; x < 8; x++)
                    {
                        if ((x + y) % 2 == 0)
                        {
                            g.FillRectangle(solidWhiteBrush, (x * boxHeight) + buffer, (y * boxHeight), boxHeight, boxHeight);
                        }
                        else
                        {
                            Rectangle           drawArea1   = new Rectangle((x * boxHeight) + buffer, (y * boxHeight), boxHeight, boxHeight);
                            LinearGradientBrush linearBrush = new LinearGradientBrush(
                                drawArea1, Color.Gainsboro, Color.Silver, LinearGradientMode.ForwardDiagonal);
                            g.FillRectangle(linearBrush, (x * boxHeight) + buffer, (y * boxHeight), boxHeight, boxHeight);
                        }

                        g.DrawRectangle(penBlack, (x * boxHeight) + buffer, (y * boxHeight), boxHeight, boxHeight);
                    }
                }
                for (byte i = 0; i < 8; i++)
                {
                    g.DrawString((8 - i).ToString(), new Font("Verdana", 8), solidBlackBrush, 0, (i * boxHeight) + buffer);
                    g.DrawString(GetColumnFromInt(i + 1).ToString(), new Font("Verdana", 8), solidBlackBrush, (i * boxHeight) + (boxHeight / 2) + 3, maxHeight - 1);
                }
                //Draw Pieces

                for (byte column = 0; column < 8; column++)
                {
                    for (byte row = 0; row < 8; row++)
                    {
                        ChessPieceType chessPieceType = engine.GetPieceTypeAt(column, row);

                        if (chessPieceType != ChessPieceType.None)
                        {
                            ChessPieceColor chessPieceColor = engine.GetPieceColorAt(column, row);
                            bool            selected        = engine.GetChessPieceSelected(column, row);


                            int x = (column) * boxHeight;
                            int y = (row) * boxHeight;

                            if (chessPieceColor == ChessPieceColor.White)
                            {
                                if (chessPieceType == ChessPieceType.Pawn)
                                {
                                    g.DrawImage(Resources.WPawn, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.Rook)
                                {
                                    g.DrawImage(Resources.WRook, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.Knight)
                                {
                                    g.DrawImage(Resources.WKnight, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.Bishop)
                                {
                                    g.DrawImage(Resources.WBishop, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.Queen)
                                {
                                    g.DrawImage(Resources.WQueen, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.King)
                                {
                                    g.DrawImage(Resources.WKing, x + buffer, y, boxHeight, boxHeight);
                                }
                            }
                            else
                            {
                                if (chessPieceType == ChessPieceType.Pawn)
                                {
                                    g.DrawImage(Resources.BPawn, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.Rook)
                                {
                                    g.DrawImage(Resources.BRook, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.Knight)
                                {
                                    g.DrawImage(Resources.BKnight, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.Bishop)
                                {
                                    g.DrawImage(Resources.BBishop, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.Queen)
                                {
                                    g.DrawImage(Resources.BQueen, x + buffer, y, boxHeight, boxHeight);
                                }
                                else if (chessPieceType == ChessPieceType.King)
                                {
                                    g.DrawImage(Resources.BKing, x + buffer, y, boxHeight, boxHeight);
                                }
                            }

                            if (selected)
                            {
                                selectedX = ((column) * boxHeight) + buffer;
                                //selectedY = (8 - Column - 1)*boxHeight;

                                //selectedX = ((8-column-1) * boxHeight) + buffer;
                                selectedY = (row) * boxHeight;

                                g.DrawRectangle(penHightlight, selectedX, selectedY, boxHeight - 1, boxHeight - 1);


                                //Draw Valid Moves
                                if (engine.GetValidMoves(column, row) != null)
                                {
                                    foreach (byte[] sqr in engine.GetValidMoves(column, row))
                                    {
                                        int moveY = (sqr[1]) * boxHeight;


                                        int moveX = (sqr[0] * boxHeight) + buffer;

                                        g.DrawRectangle(penValidMove, moveX, moveY, boxHeight - 1, boxHeight - 1);
                                    }
                                }
                            }
                            if (engine.GetEnPassantMoves()[0] > 0)
                            {
                                int moveY = (engine.GetEnPassantMoves()[1]) * boxHeight;

                                int moveX = (engine.GetEnPassantMoves()[0] * boxHeight) + buffer;

                                g.DrawRectangle(penEnPassant, moveX, moveY, boxHeight - 1, boxHeight - 1);
                            }
                        }
                    }
                }

                if (currentDestination.Selected)
                {
                    selectedY = (currentDestination.Row) * boxHeight;
                    selectedX = ((currentDestination.Column) * boxHeight) + buffer;

                    g.DrawRectangle(penDestination, selectedX, selectedY, boxHeight - 1, boxHeight - 1);
                }


                graphicsBuffer.Render(CreateGraphics());

                g.Dispose();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error Drawing Chess Board", MessageBoxButtons.OK);
            }
        }