Wednesday, April 13, 2011

Revising the Fall Method

I feel like writing C# code today. Maybe the fall method is just bugging me too much.

When I started doing this, my first change was in the following line:


if (position[2] == location) return false;


I changed it to:


if (position[2] == location) return true;

Why? Well, my recursive fall method – the fall all the way method – needs to know whether to keep going. The I think the code will be trivial to make that work. But it needs to know not to stop when a stick moves to the lowest level in its node. For that, false can only mean that that no more movement is possible with respect to the board.

Making this change, though, wrecked havoc on my tests. After fighting with it for a while, I decided to change the tests first, then make the change in the code.

When I started to dig through the tests, I found places that should have worked whether the return value for the first sub-case was true or false:


*********************

start: 6, expected: 17, fail: 0

*********************

FAIL: Stick from position 6 did not fall to expected position 17.

Location 6 should fall to position 17.


Here's the flaw: it's in the calling code, down in dropStick in the Board class:


foreach (Node x in nodes)

{

if (x.fall(location)) return true;

}

Stick 6 is occupies position[2] of nodes[0], as well as position[0] of node[6]. When this code runs and gets a value of true for node[0], it leaves the method after making no changes.

Should I try to fix it? I don't know how I mean to implement this fall-all-the-way method, anyway. It might need to return the intermediate locations of stick so that it knows where to take the next fall.

So, I'm going to undo my change and re-run the tests.

OK, things are back to where they were.

Here's one of the things that ticks me off about my code for the fall method:

if ((!isFilled(position[1], board)) && (position[1] > -1))

Why do I have to check whether this position has a value of -1?

I spent some time digging down into the code to find out whether I needed to specify that a node position was not -1. The reason I found the following method is the one that deals with this method from the Board class:

private bool isValid(int candidate)

{

foreach (Node x in nodes)

{

if (x.currentNode(candidate)) return true;

}

return false;

}

The idea was that the board would poll the nodes to see whether a position existed. The problem with this-- a value of -1 will be a valid position. It's the value used to indicate a non-position within a node.

I I'm going to use -1 as the value of the “outside”, I can just include a test for -1.

Of course, I could re-organize the Node class to use a list of positions. The only problem with that is that the array also encodes information about the relationship of the positions to each other. I'd have to re-encode that, somehow, and I don't want to without a reason.

It's OK if it isn't elephants all the way down. At some point, you can put a foundation under it all and be done.

I also had to make this change:

public bool getPositionValue(int location)

{

if (!(isValid(location))) return true; // was false

return getStick(location).Value;

}


Now, any position outside the board is considered true, not false. If the validation rejects -1, then sticks from position -1 won't be able to move.

The tests still run.

I'm going to make the rest of the changes to the fall method and re-run the tests and call it a post.

No comments:

Post a Comment