0

I need your advice.

Okay, so I am having a problem with this algorithm. I'm using the recursive method of adding to and maintaining the binary tree for this program, but when Split Once is done 3 times, the code breaks.

For the sake of example, lets say we're going to force the algorithm's methods to always perform vertical splits and only vertical splits, and thus it only calls the VertiSplit method I have. The method is as follows. I am doing this from memory, so there may be a thing or two wrong... I am going for the simplest way as possible for this.

public void VertiSplit(Node curNode) //The node that called the method is passed in.
{
 int vRatio = curNode.height / curNode.width;
 //if( vRatio <=2) // Commented out so it never does a Horizontal Split.
 //{
     //Calculate the Split Point Data for the Parent node that called the method.
     curNode.splitX = curNode.xCoor + (curNode.width / 2);
     curNode.splitY = curNode.yCoor;
     curNode.splitW = 1; // Is 1 because it is passed into the draw call for the line.
     curNode.splitH = curNode.height;
     // Calculate Data for Children...
     curNode.child1.xCoor = curNode.xCoor;
     curNode.child2.xCoor = curNode.splitX;
     curNode.child1.yCoor = curNode.yCoor;
     curNode.child2.yCoor = curNode.yCoor;
     curNode.child1.width = curNode.splitX;
     curNode.child2.width = curNode.splitX;
     curNode.child1.height = curNode.height;
     curNode.child2.height = curNode.height; //At this point, calculations and assignment of the children's variables is completed. So I'll stop writing the code here..

Assume that below this point is a Report method, a closing bracket, and an else statement for the false condition of the above if statement that calls the Horizontal Split (HoriSplit) method. Going through this logically. The calculations should go like this. The dungeon size is 512 x 512 square. Split X is the object of interest here.

1st iteration: Node ID: 0 Split X: 256

2nd iteration: Node ID: 1 Split X: 128 Node ID: 2 Split X: 384

3rd iteration: Node ID: 3 Split X: 64 Node ID: 4 Split X: 192 Node ID: 5 Split X: 320 Node ID: 6 Split X: 448

However, for iteration #3, I instead get this:

3rd iteration: Node ID: 3 Split X: 64 Node ID: 4 Split X: 192 Node ID: 5 Split X: 448 Node ID: 6 Split X: 448

The way the code is written, it should not be doing this. Yet why is it that Node #5 i broken, while the others are okay? I really don't know at this point, and I am at my wit's end about it. Could someone help me?

Also, there's another thing that isn't working. I am trying to convert a temp string variable into 32-bit integer for use with a method called SplitCreateAll that takes the value of numSplits (which is supposed to be an integer) which is assigned the value of temp, which is controlled by a textbox in the the GUI for Unity. For some reason, every time I try to have temp parsed so that numSplits can be assigned it's value, after changing it from 0 by typing in the textbox, an Error occurs where it says that temp is not in the correct format. Can someone tell me what went wrong there too?

EDIT:

public void GrowBranches(Node curNode)
{
  int randomval = randNum.Next();

  if(curNode.child1 != null)
     {
       findLeaves(curNode.child1);
       findLeaves(curNode.child2);
     }
  if(curNode.child1 == null)
    {
      curNode.child1 = new Node();
      curNode.child2 = new Node();
      curNode.child1.parent = curNode;
      curNode.child2.parent = curNode;
      curNode.child1.sister = curNode.child2;
      curNode.child2.sister = curNode.child1;
    }
    if(randomval % 2 == 0)
    {
     VertiSplit(curNode);
    }
    else
    {
      HoriSplit(curNode);
    }

There is my GrowBranches method. This is where the Split Methods are called in the recursion chain. GrowBranches is also called by the methods SplitCreateOnce and SplitCreateAll, but the latter is non-functional due to the above stated reason involving the parsing error.

4

1 回答 1

0

如果我不得不猜测,我会说是这两行:

 curNode.child1.width = curNode.splitX;
 curNode.child2.width = curNode.splitX;

查看 xCoord=256, width=256, splitX 为 386 (xCoord + 256/2) 的节点,但这不是子节点的宽度,宽度为 256/2。使用上面的代码,您将拥有一个 xCoord 386、宽度 386 的孩子。

至于关于文本错误的问题的第二部分,如果您使用的是 Unity 文本框,那么当您清除现有值以开始输入时,您最终可能会将一个空字符串传递给 Convert.ToInt32。只需使用 aTry/Catch并忽略错误。

于 2013-04-19T03:14:15.517 回答