简体   繁体   English

按特定顺序将对象放置在tablelayoutpanel单元格中

[英]Place objects in tablelayoutpanel cells in specific order

Im currently stuck on a part of my assignment that i can't seem to figure out. 我目前停留在我似乎无法弄清楚的作业中。 It involves me setting up a game board, using a tablelayoutpanel as the gameboard. 它涉及到我使用tablelayoutpanel作为游戏板来设置游戏板。

So far, I have displayed the tile "Squares" on the board, but the tiles (cells) are not in order as so: 到目前为止,我已经在板上显示了“ Squares”图块,但是这些图块(单元格)的顺序不一样:

在此处输入图片说明

I need the start tile to be displayed in the bottom left, and the finish to be in the top right, with the cells in the middle in order as so: 我需要在左下角显示开始图块,在右上角显示完成图,中间的单元格依次显示为:

在此处输入图片说明

The methods which pertain to this part are these: 与该部分有关的方法是:

/////This method sets up the game board using and array of "Squares", (still a work in progress, but mostly done) //////此方法使用“ Squares”和“ Squares”数组设置游戏板(仍在进行中,但大部分已完成)

private void SetupGameBoard() {

                    for (int i = 0; i <= 41; i++) {

            SquareControl squareCreate = new SquareControl(Board.Squares[i], null);

            int coloumn = 0;
            int row = 0;

            MapSquareNumToScreenRowAndColumn(i, out coloumn, out row);

            boardTableLayoutPanel.Controls.Add(squareCreate, coloumn, row);

        }


    }// SetupGameBaord

And this, 和这个,

 private static void MapSquareNumToScreenRowAndColumn(int squareNumber, out int rowNumber, out int columnNumber) {



            // ######################## Add more code to this method and replace the next two lines by something more sensible.  ###############################
            rowNumber = 0;      // Use 0 to make the compiler happy for now.
            columnNumber = 0;   // Use 0 to make the compiler happy for now.                  



        }//end MapSquareNumToScreenRowAndColumn

What I've grasped so far is that in MapSquareToScreenRowandcColumn is where my calculation of figuring out where the squares fall into place needs to happen. 到目前为止,我已经掌握了在MapSquareToScreenRowandcColumn中需要进行计算以找出正方形落在何处的地方。

Would really love some tips/advice/help/anything on this. 真的很喜欢这里的一些提示/建议/帮助/任何东西。

If you guys need more info, let me know. 如果你们需要更多信息,请告诉我。

Thanks! 谢谢!

EDIT: 编辑:

So I've figured out sort of how to do, just wondering about a better way to do it. 因此,我想出了一种方法,只是想知道一种更好的方法。 In MapSqauretoScreenRowandColumn I put this little if statement just to test: 在MapSqauretoScreenRowandColumn中,我将以下if语句用于测试:

if (squareNumber >= 0 && squareNumber <= 5) {

                rowNumber = 6;


            }

So if the sqaurenumber is more than 0 and less than 5, its placed in the right row. 因此,如果sqaurenumber大于0且小于5,则将其置于右行。 My question is, is there a better way to do this for the rest? 我的问题是,是否还有其他更好的方法? instead of writing a bunch of lines for this, is there are way to do it in a few? 除了为此写一堆线,还有几种方法可以做到吗?

Your parameters want the row, then the column 您的参数需要行,然后是列

private static void MapSquareNumToScreenRowAndColumn(int squareNumber,
                                    out int rowNumber, out int columnNumber) {

but you have it backwards: 但是你却倒退了:

MapSquareNumToScreenRowAndColumn(i, out coloumn, out row);

I can give you a hit, but I cant tell you everything cause im in the same class. 我可以给您带来成功,但我不能告诉您所有导致即时消息的原因。 haha. 哈哈。

Basically: 基本上:

the coordinates (x,y) is the same as (columnNumber,rowNumber) be careful not to confuse these. 坐标(x,y)与(columnNumber,rowNumber)相同,请注意不要混淆它们。 Where columnNumber = x and rowNumber = y. 其中columnNumber = x,rowNumber = y。

Now to work out what x and y we are in, we just do simple math. 现在计算出x和y是什么,我们只做简单的数学运算。

Table size: (6,7) (6 wide by 7 high),

we can calculate what x and y are according to index. 我们可以根据索引计算x和y是什么。

so lets start with x: 让我们以x开头:

to solve for what x is, all we need to do is find out how many across it is at any given time. 要求解x是什么,我们需要做的就是找出在任何给定时间内x的个数。 So simply: 如此简单:

x = index % table.width

now we solve for y this requires that we count back from max to min. 现在我们求解y,这要求我们从最大值倒数到最小值。 so: 所以:

 y = table.height - (index/table.width)

Also we divide in int for so that the decimals just fall of the end which is what we want in this situation. 同样,我们将int划分为int,以便小数点恰好落在末尾,这就是我们在这种情况下想要的。 we are dividing by the width, because we want to work out how many columns it has gone through to get to the current location. 我们用宽度除以,因为我们想算出到达当前位置需要经过多少列。

Now to get the reversing effect, we basically want this to happen every odd column so all we need to do is basically: 现在要获得反转效果,我们基本上希望在每个奇数列都发生这种情况,因此我们要做的基本上是:

 if (y % 2 != 0) { //if it is an odd number
    x = table.width - x;
 }

to create that inverse effect; 产生相反的效果;

ALSO, remember KEY POINT, table.width and table.height = keys, so if table.width = 6 then the key is 5 because of the array starting at 0... 还请记住KEY POINT,table.width和table.height =键,因此如果table.width = 6,则键为5,因为数组从0开始...

Hope that helps. 希望能有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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