简体   繁体   中英

C# Drawing Chess Board

I'm trying to draw a 8x8 chess board using C#. Here's my first attempt to draw it. It won't draw the board and I haven't found what I'm missing.

     public void Form1_Load(object sender, EventArgs e)
     {
         Bitmap bm = new Bitmap(8 * 100, 8 * 100);
         Graphics g = Graphics.FromImage(bm);
         Color color1, color2;
         for (int i = 0; i < 8; i++)
         {
             if (i % 2 == 0)
             {
                 color1 = Color.Black;
                 color2 = Color.White;
             }
             else
             {
                 color1 = Color.White;
                 color2 = Color.Black;
             }
             SolidBrush blackBrush = new SolidBrush(color1);
             SolidBrush whiteBrush = new SolidBrush(color2);

             for (int j = 0; j < 8; j++)
             {
                 if (j % 2 == 0)
                     g.FillRectangle(blackBrush, i * 100, j * 100, 100, 100);
                 else
                     g.FillRectangle(whiteBrush, i * 100, j * 100, 100, 100);
             }
         }

         g.DrawImage(bm, 150, 200);
     }  

add BackgroundImage = bm; to the bottom of your code.

You are drawing the board fine, just not displaying the bitmap...

Edit: im not sure if you're interested, but i rewrote this code.

Bitmap bm = new Bitmap(800, 800);
using (Graphics g = Graphics.FromImage(bm))
using (SolidBrush blackBrush = new SolidBrush(Color.Black))
using (SolidBrush whiteBrush = new SolidBrush(Color.White))
{
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 8; j++)
        {
            if ((j % 2 == 0 && i % 2 == 0) || (j % 2 != 0 && i % 2 != 0))
                g.FillRectangle(blackBrush, i * 100, j * 100, 100, 100);
            else if ((j % 2 == 0 && i % 2 != 0) || (j % 2 != 0 && i % 2 == 0))
                g.FillRectangle(whiteBrush, i * 100, j * 100, 100, 100);
        }
    }
    BackgroundImage = bm;
}

also this project could help if you want to make a chess game: http://www.codeproject.com/Articles/20736/CC-CLI-Micro-Chess-Huo-Chess

        //Variables
        int rowSize = 8;
        int colSize = 8;
        //Calculation
        for (int row = 0; row < rowSize; row++) //Loop throw ROW's
        {
            for (int col = 0; col < colSize; col++) //Loop throw COL's
            {
                if ((row + col) % 2 == 0) //Check if cells is EVEN
                {
                    Console.Write("X"); //White square
                }
                else
                {
                    Console.Write("O"); //Black square
                }
            }
            Console.WriteLine(); //Switch to a new line

Consider a chessboard decomposed into rows and columns (horizontal and vertical). Apply indexing of rows 0 through 8 through a for loop. The same for the columns. For each square check if the sum of the indexing of the line with the column gives "par" (the rest of the division by 2 gives 0), then equals to white square. Otherwise, it assigns a black square. The trick is ((row + col)% 2). It gives values of 0 or 1, alternately. At the end of each iteration of the columns "for loop", the line changes.

For performance reasons, you could also skip drawing the other colored tiles, and just fill the background first with the secondary color, then iterate the primary color tiles. Example:

    public static class GridGenerator
    {
        public static void CreateGridBackground()
        {

            DrawingVisual visual = new DrawingVisual();
            DrawingContext context = visual.RenderOpen();
            context.DrawRectangle(Brushes.Red, null, new Rect(0, 0, 256, 512));
            for (int xRow = 0; xRow < 256 / 16; xRow++)
            {
                for (int yColumn = 0; yColumn < 512 / 16; yColumn++)
                {
                    if ((yColumn % 2 == 0 && xRow % 2 == 0) || (yColumn % 2 != 0 && xRow % 2 != 0))
                    {
                        context.DrawRectangle(Brushes.White, null, new Rect(xRow * 16, yColumn * 16, 16, 16));
                    }
                }
            }
            context.Close();

            RenderTargetBitmap bmp = new RenderTargetBitmap(256, 512, 96, 96, PixelFormats.Pbgra32);
            bmp.Render(visual);

            PngBitmapEncoder encoder = new PngBitmapEncoder();
            encoder.Frames.Add(BitmapFrame.Create(bmp));
            encoder.Save(new FileStream("C:/Users/Krythic/Desktop/Test.png", FileMode.Create));
        }
    }

This is theoretically 50% faster (haven't actually benchmarked so feel free to treat this as an imaginary number), because you're cutting the drawing in half by filling one of the colors instead of actually drawing it as individual tiles. Just saying; take it or leave it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM