动态生成n(行)× m(列)棋盘

本文关键字:棋盘 动态 | 更新日期: 2023-09-27 18:12:06

想象以下场景(Silverlight 4):我有两个矩形。一个是黑色的,一个是白色的。它们都有固定的尺寸,比如50x50。我还有一个面积(500x500),我想用这些矩形填充它,但是用交替的方式填充——白色,黑色,白色,黑色等等。为了更好地说明,请看下面的链接:

http://screencast.com/t/BwsPSbtg2eaM

http://screencast.com/t/gTuexSSyW

这个视频(链接#2)正好展示了我想要达到的目标。

任何帮助都将非常感激!

琼斯

动态生成n(行)× m(列)棋盘

    int totalRectsInaRow = TotalWidth/ WidthOfOneRect;
    int totalRectsInaColumn = TotalHeight/ HeightOfOneRect;
    //Create a Grid of Width = TotalWidth and Height = Total Height;
    //Add columns equal to totalRectsInaColumn and rows equal to totalRectsInaRow in Grid
    //Set wdith of each column equal to width of one rectangle
    //set height of each row equal to height of one rectangle
    bool drawWhite = true;
    for (int i = 0; i < totalRectsInaColumn; i++)
    {
        for (int j = 0; j < totalRectsInaRow; j++)
        {
            if (drawWhite)
            { 
                //draw white rectanlge at i column and j row
                //basically you create a rectangle and place it in grid on particular location
                DrawWhileRectangle(i, j);
                drawWhite = false;
            }
            else
            {
                //draw black rectanlge at i column and j row
                //basically you create a rectangle and place it in grid on particular location
                DrawBlackRectangle(i, j);
                drawWhite = true;
            }
        }
        drawWhite = !drawWhite;
    }